FormHelperTest.php 305 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  319. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  320. );
  321. /**
  322. * beforeValidate method
  323. *
  324. * @return void
  325. */
  326. public function beforeValidate($options = array()) {
  327. $this->invalidate('email');
  328. return false;
  329. }
  330. }
  331. /**
  332. * ValidateProfile class
  333. *
  334. * @package Cake.Test.Case.View.Helper
  335. */
  336. class ValidateProfile extends CakeTestModel {
  337. /**
  338. * useTable property
  339. *
  340. * @var boolean
  341. */
  342. public $useTable = false;
  343. /**
  344. * schema property
  345. *
  346. * @var array
  347. */
  348. protected $_schema = array(
  349. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  350. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  351. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  352. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  353. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  354. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  355. );
  356. /**
  357. * hasOne property
  358. *
  359. * @var array
  360. */
  361. public $hasOne = array('ValidateItem' => array(
  362. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  363. ));
  364. /**
  365. * belongsTo property
  366. *
  367. * @var array
  368. */
  369. public $belongsTo = array('ValidateUser' => array(
  370. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  371. ));
  372. /**
  373. * beforeValidate method
  374. *
  375. * @return void
  376. */
  377. public function beforeValidate($options = array()) {
  378. $this->invalidate('full_name');
  379. $this->invalidate('city');
  380. return false;
  381. }
  382. }
  383. /**
  384. * ValidateItem class
  385. *
  386. * @package Cake.Test.Case.View.Helper
  387. */
  388. class ValidateItem extends CakeTestModel {
  389. /**
  390. * useTable property
  391. *
  392. * @var boolean
  393. */
  394. public $useTable = false;
  395. /**
  396. * schema property
  397. *
  398. * @var array
  399. */
  400. protected $_schema = array(
  401. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  402. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  403. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  404. 'description' => array(
  405. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  406. ),
  407. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  408. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  409. );
  410. /**
  411. * belongsTo property
  412. *
  413. * @var array
  414. */
  415. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  416. /**
  417. * beforeValidate method
  418. *
  419. * @return void
  420. */
  421. public function beforeValidate($options = array()) {
  422. $this->invalidate('description');
  423. return false;
  424. }
  425. }
  426. /**
  427. * TestMail class
  428. *
  429. * @package Cake.Test.Case.View.Helper
  430. */
  431. class TestMail extends CakeTestModel {
  432. /**
  433. * useTable property
  434. *
  435. * @var boolean
  436. */
  437. public $useTable = false;
  438. }
  439. /**
  440. * FormHelperTest class
  441. *
  442. * @package Cake.Test.Case.View.Helper
  443. * @property FormHelper $Form
  444. */
  445. class FormHelperTest extends CakeTestCase {
  446. /**
  447. * Fixtures to be used
  448. *
  449. * @var array
  450. */
  451. public $fixtures = array('core.post');
  452. /**
  453. * Do not load the fixtures by default
  454. *
  455. * @var boolean
  456. */
  457. public $autoFixtures = false;
  458. /**
  459. * setUp method
  460. *
  461. * @return void
  462. */
  463. public function setUp() {
  464. parent::setUp();
  465. Configure::write('Config.language', 'eng');
  466. Configure::write('App.base', '');
  467. Configure::delete('Asset');
  468. $this->Controller = new ContactTestController();
  469. $this->View = new View($this->Controller);
  470. $this->Form = new FormHelper($this->View);
  471. $this->Form->Html = new HtmlHelper($this->View);
  472. $this->Form->request = new CakeRequest('contacts/add', false);
  473. $this->Form->request->here = '/contacts/add';
  474. $this->Form->request['action'] = 'add';
  475. $this->Form->request->webroot = '';
  476. $this->Form->request->base = '';
  477. ClassRegistry::addObject('Contact', new Contact());
  478. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  479. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  480. ClassRegistry::addObject('User', new UserForm());
  481. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  482. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  483. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  484. $this->oldSalt = Configure::read('Security.salt');
  485. $this->dateRegex = array(
  486. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  487. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  488. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  491. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  492. );
  493. Configure::write('Security.salt', 'foo!');
  494. }
  495. /**
  496. * tearDown method
  497. *
  498. * @return void
  499. */
  500. public function tearDown() {
  501. parent::tearDown();
  502. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  503. Configure::write('Security.salt', $this->oldSalt);
  504. }
  505. /**
  506. * testFormCreateWithSecurity method
  507. *
  508. * Test form->create() with security key.
  509. *
  510. * @return void
  511. */
  512. public function testCreateWithSecurity() {
  513. $this->Form->request['_Token'] = array('key' => 'testKey');
  514. $encoding = strtolower(Configure::read('App.encoding'));
  515. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  516. $expected = array(
  517. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  518. 'div' => array('style' => 'display:none;'),
  519. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  520. array('input' => array(
  521. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  522. )),
  523. '/div'
  524. );
  525. $this->assertTags($result, $expected);
  526. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  527. $expected['form']['id'] = 'MyForm';
  528. $this->assertTags($result, $expected);
  529. }
  530. /**
  531. * testFormCreateGetNoSecurity method
  532. *
  533. * Test form->create() with no security key as its a get form
  534. *
  535. * @return void
  536. */
  537. public function testCreateEndGetNoSecurity() {
  538. $this->Form->request['_Token'] = array('key' => 'testKey');
  539. $encoding = strtolower(Configure::read('App.encoding'));
  540. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  541. $this->assertNotContains('Token', $result);
  542. $result = $this->Form->end('Save');
  543. $this->assertNotContains('Token', $result);
  544. }
  545. /**
  546. * test that create() clears the fields property so it starts fresh
  547. *
  548. * @return void
  549. */
  550. public function testCreateClearingFields() {
  551. $this->Form->fields = array('model_id');
  552. $this->Form->create('Contact');
  553. $this->assertEquals(array(), $this->Form->fields);
  554. }
  555. /**
  556. * Tests form hash generation with model-less data
  557. *
  558. * @return void
  559. */
  560. public function testValidateHashNoModel() {
  561. $this->Form->request['_Token'] = array('key' => 'foo');
  562. $result = $this->Form->secure(array('anything'));
  563. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  564. }
  565. /**
  566. * Tests that models with identical field names get resolved properly
  567. *
  568. * @return void
  569. */
  570. public function testDuplicateFieldNameResolution() {
  571. $result = $this->Form->create('ValidateUser');
  572. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  573. $result = $this->Form->input('ValidateItem.name');
  574. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateUser.name');
  576. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  577. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  578. $this->assertRegExp('/type="text"/', $result);
  579. $result = $this->Form->input('ValidateItem.name');
  580. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  581. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  582. $this->assertRegExp('/<textarea/', $result);
  583. $result = $this->Form->input('name');
  584. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  585. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  586. $this->assertRegExp('/type="text"/', $result);
  587. }
  588. /**
  589. * Tests that hidden fields generated for checkboxes don't get locked
  590. *
  591. * @return void
  592. */
  593. public function testNoCheckboxLocking() {
  594. $this->Form->request['_Token'] = array('key' => 'foo');
  595. $this->assertSame(array(), $this->Form->fields);
  596. $this->Form->checkbox('check', array('value' => '1'));
  597. $this->assertSame($this->Form->fields, array('check'));
  598. }
  599. /**
  600. * testFormSecurityFields method
  601. *
  602. * Test generation of secure form hash generation.
  603. *
  604. * @return void
  605. */
  606. public function testFormSecurityFields() {
  607. $key = 'testKey';
  608. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  609. $this->Form->request['_Token'] = array('key' => $key);
  610. $result = $this->Form->secure($fields);
  611. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  612. $hash .= ':' . 'Model.valid';
  613. $hash = urlencode($hash);
  614. $expected = array(
  615. 'div' => array('style' => 'display:none;'),
  616. array('input' => array(
  617. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  618. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  619. )),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  622. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  623. )),
  624. '/div'
  625. );
  626. $this->assertTags($result, $expected);
  627. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  628. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  629. $result = $this->Form->secure($fields);
  630. $expected = array(
  631. 'div' => array('class' => 'hidden'),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  634. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  635. )),
  636. array('input' => array(
  637. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  638. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  639. )),
  640. '/div'
  641. );
  642. $this->assertTags($result, $expected);
  643. }
  644. /**
  645. * Tests correct generation of number fields for double and float fields
  646. *
  647. * @return void
  648. */
  649. public function testTextFieldGenerationForFloats() {
  650. $model = ClassRegistry::getObject('Contact');
  651. $model->setSchema(array('foo' => array(
  652. 'type' => 'float',
  653. 'null' => false,
  654. 'default' => null,
  655. 'length' => 10
  656. )));
  657. $this->Form->create('Contact');
  658. $result = $this->Form->input('foo');
  659. $expected = array(
  660. 'div' => array('class' => 'input number'),
  661. 'label' => array('for' => 'ContactFoo'),
  662. 'Foo',
  663. '/label',
  664. array('input' => array(
  665. 'type' => 'number',
  666. 'name' => 'data[Contact][foo]',
  667. 'id' => 'ContactFoo',
  668. 'step' => 'any'
  669. )),
  670. '/div'
  671. );
  672. $this->assertTags($result, $expected);
  673. $result = $this->Form->input('foo', array('step' => 0.5));
  674. $expected = array(
  675. 'div' => array('class' => 'input number'),
  676. 'label' => array('for' => 'ContactFoo'),
  677. 'Foo',
  678. '/label',
  679. array('input' => array(
  680. 'type' => 'number',
  681. 'name' => 'data[Contact][foo]',
  682. 'id' => 'ContactFoo',
  683. 'step' => '0.5'
  684. )),
  685. '/div'
  686. );
  687. $this->assertTags($result, $expected);
  688. }
  689. /**
  690. * Tests correct generation of number fields for integer fields
  691. *
  692. * @return void
  693. */
  694. public function testTextFieldTypeNumberGenerationForIntegers() {
  695. $model = ClassRegistry::getObject('Contact');
  696. $model->setSchema(array('foo' => array(
  697. 'type' => 'integer',
  698. 'null' => false,
  699. 'default' => null,
  700. 'length' => null
  701. )));
  702. $this->Form->create('Contact');
  703. $result = $this->Form->input('foo');
  704. $expected = array(
  705. 'div' => array('class' => 'input number'),
  706. 'label' => array('for' => 'ContactFoo'),
  707. 'Foo',
  708. '/label',
  709. array('input' => array(
  710. 'type' => 'number', 'name' => 'data[Contact][foo]',
  711. 'id' => 'ContactFoo'
  712. )),
  713. '/div'
  714. );
  715. $this->assertTags($result, $expected);
  716. }
  717. /**
  718. * testFormSecurityMultipleFields method
  719. *
  720. * Test secure() with multiple row form. Ensure hash is correct.
  721. *
  722. * @return void
  723. */
  724. public function testFormSecurityMultipleFields() {
  725. $key = 'testKey';
  726. $fields = array(
  727. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  728. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  729. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  730. );
  731. $this->Form->request['_Token'] = array('key' => $key);
  732. $result = $this->Form->secure($fields);
  733. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  734. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  735. $expected = array(
  736. 'div' => array('style' => 'display:none;'),
  737. array('input' => array(
  738. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  739. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  740. )),
  741. array('input' => array(
  742. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  743. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  744. )),
  745. '/div'
  746. );
  747. $this->assertTags($result, $expected);
  748. }
  749. /**
  750. * testFormSecurityMultipleSubmitButtons
  751. *
  752. * test form submit generation and ensure that _Token is only created on end()
  753. *
  754. * @return void
  755. */
  756. public function testFormSecurityMultipleSubmitButtons() {
  757. $key = 'testKey';
  758. $this->Form->request['_Token'] = array('key' => $key);
  759. $this->Form->create('Addresses');
  760. $this->Form->input('Address.title');
  761. $this->Form->input('Address.first_name');
  762. $result = $this->Form->submit('Save', array('name' => 'save'));
  763. $expected = array(
  764. 'div' => array('class' => 'submit'),
  765. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  766. '/div',
  767. );
  768. $this->assertTags($result, $expected);
  769. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  770. $expected = array(
  771. 'div' => array('class' => 'submit'),
  772. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  773. '/div',
  774. );
  775. $this->assertTags($result, $expected);
  776. $result = $this->Form->end(null);
  777. $expected = array(
  778. 'div' => array('style' => 'display:none;'),
  779. array('input' => array(
  780. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  781. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  782. )),
  783. array('input' => array(
  784. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  785. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  786. )),
  787. '/div'
  788. );
  789. $this->assertTags($result, $expected);
  790. }
  791. /**
  792. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  793. *
  794. * @return void
  795. */
  796. public function testSecurityButtonNestedNamed() {
  797. $key = 'testKey';
  798. $this->Form->request['_Token'] = array('key' => $key);
  799. $this->Form->create('Addresses');
  800. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  801. $result = $this->Form->unlockField();
  802. $this->assertEquals(array('Address.button'), $result);
  803. }
  804. /**
  805. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecuritySubmitNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that the correct fields are unlocked for image submits with no names.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitImageNoName() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('User');
  826. $result = $this->Form->submit('save.png');
  827. $expected = array(
  828. 'div' => array('class' => 'submit'),
  829. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  834. }
  835. /**
  836. * Test that the correct fields are unlocked for image submits with names.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitImageName() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('User');
  844. $result = $this->Form->submit('save.png', array('name' => 'test'));
  845. $expected = array(
  846. 'div' => array('class' => 'submit'),
  847. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  848. '/div'
  849. );
  850. $this->assertTags($result, $expected);
  851. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  852. }
  853. /**
  854. * testFormSecurityMultipleInputFields method
  855. *
  856. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  857. *
  858. * @return void
  859. */
  860. public function testFormSecurityMultipleInputFields() {
  861. $key = 'testKey';
  862. $this->Form->request['_Token'] = array('key' => $key);
  863. $this->Form->create('Addresses');
  864. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  865. $this->Form->input('Addresses.0.title');
  866. $this->Form->input('Addresses.0.first_name');
  867. $this->Form->input('Addresses.0.last_name');
  868. $this->Form->input('Addresses.0.address');
  869. $this->Form->input('Addresses.0.city');
  870. $this->Form->input('Addresses.0.phone');
  871. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  872. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  873. $this->Form->input('Addresses.1.title');
  874. $this->Form->input('Addresses.1.first_name');
  875. $this->Form->input('Addresses.1.last_name');
  876. $this->Form->input('Addresses.1.address');
  877. $this->Form->input('Addresses.1.city');
  878. $this->Form->input('Addresses.1.phone');
  879. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  880. $result = $this->Form->secure($this->Form->fields);
  881. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  882. $expected = array(
  883. 'div' => array('style' => 'display:none;'),
  884. array('input' => array(
  885. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  886. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  887. )),
  888. array('input' => array(
  889. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  890. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  891. )),
  892. '/div'
  893. );
  894. $this->assertTags($result, $expected);
  895. }
  896. /**
  897. * Test form security with Model.field.0 style inputs
  898. *
  899. * @return void
  900. */
  901. public function testFormSecurityArrayFields() {
  902. $key = 'testKey';
  903. $this->Form->request->params['_Token']['key'] = $key;
  904. $this->Form->create('Address');
  905. $this->Form->input('Address.primary.1');
  906. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  907. $this->Form->input('Address.secondary.1.0');
  908. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  909. }
  910. /**
  911. * testFormSecurityMultipleInputDisabledFields method
  912. *
  913. * test secure form generation with multiple records and disabled fields.
  914. *
  915. * @return void
  916. */
  917. public function testFormSecurityMultipleInputDisabledFields() {
  918. $key = 'testKey';
  919. $this->Form->request->params['_Token'] = array(
  920. 'key' => $key,
  921. 'unlockedFields' => array('first_name', 'address')
  922. );
  923. $this->Form->create();
  924. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  925. $this->Form->input('Addresses.0.title');
  926. $this->Form->input('Addresses.0.first_name');
  927. $this->Form->input('Addresses.0.last_name');
  928. $this->Form->input('Addresses.0.address');
  929. $this->Form->input('Addresses.0.city');
  930. $this->Form->input('Addresses.0.phone');
  931. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  932. $this->Form->input('Addresses.1.title');
  933. $this->Form->input('Addresses.1.first_name');
  934. $this->Form->input('Addresses.1.last_name');
  935. $this->Form->input('Addresses.1.address');
  936. $this->Form->input('Addresses.1.city');
  937. $this->Form->input('Addresses.1.phone');
  938. $result = $this->Form->secure($this->Form->fields);
  939. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  940. $expected = array(
  941. 'div' => array('style' => 'display:none;'),
  942. array('input' => array(
  943. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  944. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  945. )),
  946. array('input' => array(
  947. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  948. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  949. )),
  950. '/div'
  951. );
  952. $this->assertTags($result, $expected);
  953. }
  954. /**
  955. * testFormSecurityInputDisabledFields method
  956. *
  957. * Test single record form with disabled fields.
  958. *
  959. * @return void
  960. */
  961. public function testFormSecurityInputUnlockedFields() {
  962. $key = 'testKey';
  963. $this->Form->request['_Token'] = array(
  964. 'key' => $key,
  965. 'unlockedFields' => array('first_name', 'address')
  966. );
  967. $this->Form->create();
  968. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  969. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  970. $this->Form->input('Addresses.title');
  971. $this->Form->input('Addresses.first_name');
  972. $this->Form->input('Addresses.last_name');
  973. $this->Form->input('Addresses.address');
  974. $this->Form->input('Addresses.city');
  975. $this->Form->input('Addresses.phone');
  976. $result = $this->Form->fields;
  977. $expected = array(
  978. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  979. 'Addresses.city', 'Addresses.phone'
  980. );
  981. $this->assertEquals($expected, $result);
  982. $result = $this->Form->secure($expected);
  983. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  984. $expected = array(
  985. 'div' => array('style' => 'display:none;'),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  988. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  989. )),
  990. array('input' => array(
  991. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  992. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  993. )),
  994. '/div'
  995. );
  996. $this->assertTags($result, $expected);
  997. }
  998. /**
  999. * test securing inputs with custom name attributes.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testFormSecureWithCustomNameAttribute() {
  1004. $this->Form->request->params['_Token']['key'] = 'testKey';
  1005. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1006. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1008. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1009. }
  1010. /**
  1011. * testFormSecuredInput method
  1012. *
  1013. * Test generation of entire secure form, assertions made on input() output.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testFormSecuredInput() {
  1018. $this->Form->request['_Token'] = array('key' => 'testKey');
  1019. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1020. $encoding = strtolower(Configure::read('App.encoding'));
  1021. $expected = array(
  1022. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1023. 'div' => array('style' => 'display:none;'),
  1024. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1025. array('input' => array(
  1026. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1027. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1028. )),
  1029. '/div'
  1030. );
  1031. $this->assertTags($result, $expected);
  1032. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1033. $expected = array(
  1034. 'div' => array('class' => 'input text'),
  1035. 'label' => array('for' => 'UserFormPublished'),
  1036. 'Published',
  1037. '/label',
  1038. array('input' => array(
  1039. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1040. 'id' => 'UserFormPublished'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormOther'),
  1049. 'Other',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1053. 'id' => 'UserFormOther'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->hidden('UserForm.stuff');
  1059. $expected = array(
  1060. 'input' => array(
  1061. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1062. 'id' => 'UserFormStuff'
  1063. ));
  1064. $this->assertTags($result, $expected);
  1065. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1066. $expected = array('input' => array(
  1067. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1068. 'value' => '0', 'id' => 'UserFormHidden'
  1069. ));
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1072. $expected = array(
  1073. 'div' => array('class' => 'input checkbox'),
  1074. array('input' => array(
  1075. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1076. 'value' => '0', 'id' => 'UserFormSomething_'
  1077. )),
  1078. array('input' => array(
  1079. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1080. 'value' => '1', 'id' => 'UserFormSomething'
  1081. )),
  1082. 'label' => array('for' => 'UserFormSomething'),
  1083. 'Something',
  1084. '/label',
  1085. '/div'
  1086. );
  1087. $this->assertTags($result, $expected);
  1088. $result = $this->Form->fields;
  1089. $expected = array(
  1090. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1091. 'UserForm.hidden' => '0', 'UserForm.something'
  1092. );
  1093. $this->assertEquals($expected, $result);
  1094. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1095. $result = $this->Form->secure($this->Form->fields);
  1096. $expected = array(
  1097. 'div' => array('style' => 'display:none;'),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1100. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1101. )),
  1102. array('input' => array(
  1103. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1104. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1105. )),
  1106. '/div'
  1107. );
  1108. $this->assertTags($result, $expected);
  1109. }
  1110. /**
  1111. * Test secured inputs with custom names.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testSecuredInputCustomName() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $this->assertEquals(array(), $this->Form->fields);
  1118. $this->Form->input('text_input', array(
  1119. 'name' => 'data[Option][General.default_role]',
  1120. ));
  1121. $expected = array('Option.General.default_role');
  1122. $this->assertEquals($expected, $this->Form->fields);
  1123. $this->Form->input('select_box', array(
  1124. 'name' => 'data[Option][General.select_role]',
  1125. 'type' => 'select',
  1126. 'options' => array(1, 2),
  1127. ));
  1128. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1129. $this->assertEquals($expected, $this->Form->fields);
  1130. }
  1131. /**
  1132. * Tests that the correct keys are added to the field hash index
  1133. *
  1134. * @return void
  1135. */
  1136. public function testFormSecuredFileInput() {
  1137. $this->Form->request['_Token'] = array('key' => 'testKey');
  1138. $this->assertEquals(array(), $this->Form->fields);
  1139. $this->Form->file('Attachment.file');
  1140. $expected = array(
  1141. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1142. 'Attachment.file.error', 'Attachment.file.size'
  1143. );
  1144. $this->assertEquals($expected, $this->Form->fields);
  1145. }
  1146. /**
  1147. * test that multiple selects keys are added to field hash
  1148. *
  1149. * @return void
  1150. */
  1151. public function testFormSecuredMultipleSelect() {
  1152. $this->Form->request['_Token'] = array('key' => 'testKey');
  1153. $this->assertEquals(array(), $this->Form->fields);
  1154. $options = array('1' => 'one', '2' => 'two');
  1155. $this->Form->select('Model.select', $options);
  1156. $expected = array('Model.select');
  1157. $this->assertEquals($expected, $this->Form->fields);
  1158. $this->Form->fields = array();
  1159. $this->Form->select('Model.select', $options, array('multiple' => true));
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. }
  1162. /**
  1163. * testFormSecuredRadio method
  1164. *
  1165. * @return void
  1166. */
  1167. public function testFormSecuredRadio() {
  1168. $this->Form->request['_Token'] = array('key' => 'testKey');
  1169. $this->assertEquals(array(), $this->Form->fields);
  1170. $options = array('1' => 'option1', '2' => 'option2');
  1171. $this->Form->radio('Test.test', $options);
  1172. $expected = array('Test.test');
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * Test that when disabled is in a list based attribute array it works.
  1177. *
  1178. * @return void
  1179. */
  1180. public function testFormSecuredAndDisabledNotAssoc() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1183. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1184. $this->Form->text('Model.text', array('disabled'));
  1185. $this->Form->textarea('Model.textarea', array('disabled'));
  1186. $this->Form->password('Model.password', array('disabled'));
  1187. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1188. $expected = array(
  1189. 'Model.radio' => ''
  1190. );
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1195. * hashing.
  1196. *
  1197. * @return void
  1198. */
  1199. public function testFormSecuredAndDisabled() {
  1200. $this->Form->request['_Token'] = array('key' => 'testKey');
  1201. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1202. $this->Form->text('Model.text', array('disabled' => true));
  1203. $this->Form->password('Model.text', array('disabled' => true));
  1204. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1205. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1206. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1207. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1208. $this->Form->month('Model.month', array('disabled' => true));
  1209. $this->Form->day('Model.day', array('disabled' => true));
  1210. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1211. $this->Form->minute('Model.minute', array('disabled' => true));
  1212. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1213. $expected = array(
  1214. 'Model.radio' => ''
  1215. );
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. }
  1218. /**
  1219. * testDisableSecurityUsingForm method
  1220. *
  1221. * @return void
  1222. */
  1223. public function testDisableSecurityUsingForm() {
  1224. $this->Form->request['_Token'] = array(
  1225. 'key' => 'testKey',
  1226. 'disabledFields' => array()
  1227. );
  1228. $this->Form->create();
  1229. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1230. $this->Form->input('Addresses.title');
  1231. $this->Form->input('Addresses.first_name', array('secure' => false));
  1232. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1233. $this->Form->input('Addresses.zip', array(
  1234. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1235. ));
  1236. $result = $this->Form->fields;
  1237. $expected = array(
  1238. 'Addresses.id' => '123456', 'Addresses.title',
  1239. );
  1240. $this->assertEquals($expected, $result);
  1241. }
  1242. /**
  1243. * test disableField
  1244. *
  1245. * @return void
  1246. */
  1247. public function testUnlockFieldAddsToList() {
  1248. $this->Form->request['_Token'] = array(
  1249. 'key' => 'testKey',
  1250. 'unlockedFields' => array()
  1251. );
  1252. $this->Form->create('Contact');
  1253. $this->Form->unlockField('Contact.name');
  1254. $this->Form->text('Contact.name');
  1255. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1256. $this->assertEquals(array(), $this->Form->fields);
  1257. }
  1258. /**
  1259. * test unlockField removing from fields array.
  1260. *
  1261. * @return void
  1262. */
  1263. public function testUnlockFieldRemovingFromFields() {
  1264. $this->Form->request['_Token'] = array(
  1265. 'key' => 'testKey',
  1266. 'unlockedFields' => array()
  1267. );
  1268. $this->Form->create('Contact');
  1269. $this->Form->hidden('Contact.id', array('value' => 1));
  1270. $this->Form->text('Contact.name');
  1271. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1272. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1273. $this->Form->unlockField('Contact.name');
  1274. $this->Form->unlockField('Contact.id');
  1275. $this->assertEquals(array(), $this->Form->fields);
  1276. }
  1277. /**
  1278. * testTagIsInvalid method
  1279. *
  1280. * @return void
  1281. */
  1282. public function testTagIsInvalid() {
  1283. $Contact = ClassRegistry::getObject('Contact');
  1284. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1285. $this->Form->setEntity('Contact.0.email');
  1286. $result = $this->Form->tagIsInvalid();
  1287. $this->assertEquals($expected, $result);
  1288. $this->Form->setEntity('Contact.1.email');
  1289. $result = $this->Form->tagIsInvalid();
  1290. $this->assertFalse($result);
  1291. $this->Form->setEntity('Contact.0.name');
  1292. $result = $this->Form->tagIsInvalid();
  1293. $this->assertFalse($result);
  1294. }
  1295. /**
  1296. * Test tagIsInvalid with validation errors from a saveMany
  1297. *
  1298. * @return void
  1299. */
  1300. public function testTagIsInvalidSaveMany() {
  1301. $Contact = ClassRegistry::getObject('Contact');
  1302. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1303. $this->Form->create('Contact');
  1304. $this->Form->setEntity('0.email');
  1305. $result = $this->Form->tagIsInvalid();
  1306. $this->assertEquals($expected, $result);
  1307. $this->Form->setEntity('0.Contact.email');
  1308. $result = $this->Form->tagIsInvalid();
  1309. $this->assertEquals($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. * @return void
  1353. */
  1354. public function testEmptyErrorValidation() {
  1355. $this->Form->validationErrors['Contact']['password'] = '';
  1356. $result = $this->Form->input('Contact.password');
  1357. $expected = array(
  1358. 'div' => array('class' => 'input password error'),
  1359. 'label' => array('for' => 'ContactPassword'),
  1360. 'Password',
  1361. '/label',
  1362. 'input' => array(
  1363. 'type' => 'password', 'name' => 'data[Contact][password]',
  1364. 'id' => 'ContactPassword', 'class' => 'form-error'
  1365. ),
  1366. array('div' => array('class' => 'error-message')),
  1367. array(),
  1368. '/div',
  1369. '/div'
  1370. );
  1371. $this->assertTags($result, $expected);
  1372. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1373. $expected = array(
  1374. 'div' => array('class' => 'input password error'),
  1375. 'label' => array('for' => 'ContactPassword'),
  1376. 'Password',
  1377. '/label',
  1378. 'input' => array(
  1379. 'type' => 'password', 'name' => 'data[Contact][password]',
  1380. 'id' => 'ContactPassword', 'class' => 'form-error'
  1381. ),
  1382. '/div'
  1383. );
  1384. $this->assertTags($result, $expected);
  1385. }
  1386. /**
  1387. * Test validation errors, when calling input() overriding validation message by an empty string.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testEmptyInputErrorValidation() {
  1392. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1393. $result = $this->Form->input('Contact.password', array('error' => ''));
  1394. $expected = array(
  1395. 'div' => array('class' => 'input password error'),
  1396. 'label' => array('for' => 'ContactPassword'),
  1397. 'Password',
  1398. '/label',
  1399. 'input' => array(
  1400. 'type' => 'password', 'name' => 'data[Contact][password]',
  1401. 'id' => 'ContactPassword', 'class' => 'form-error'
  1402. ),
  1403. array('div' => array('class' => 'error-message')),
  1404. array(),
  1405. '/div',
  1406. '/div'
  1407. );
  1408. $this->assertTags($result, $expected);
  1409. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1410. $expected = array(
  1411. 'div' => array('class' => 'input password error'),
  1412. 'label' => array('for' => 'ContactPassword'),
  1413. 'Password',
  1414. '/label',
  1415. 'input' => array(
  1416. 'type' => 'password', 'name' => 'data[Contact][password]',
  1417. 'id' => 'ContactPassword', 'class' => 'form-error'
  1418. ),
  1419. '/div'
  1420. );
  1421. $this->assertTags($result, $expected);
  1422. }
  1423. /**
  1424. * testFormValidationAssociated method
  1425. *
  1426. * test display of form errors in conjunction with model::validates.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testFormValidationAssociated() {
  1431. $this->UserForm = ClassRegistry::getObject('UserForm');
  1432. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1433. $data = array(
  1434. 'UserForm' => array('name' => 'user'),
  1435. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1436. );
  1437. $result = $this->UserForm->OpenidUrl->create($data);
  1438. $this->assertFalse(empty($result));
  1439. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1440. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1441. $encoding = strtolower(Configure::read('App.encoding'));
  1442. $expected = array(
  1443. 'form' => array(
  1444. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1445. 'accept-charset' => $encoding
  1446. ),
  1447. 'div' => array('style' => 'display:none;'),
  1448. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1449. '/div'
  1450. );
  1451. $this->assertTags($result, $expected);
  1452. $result = $this->Form->error(
  1453. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1454. );
  1455. $this->assertEquals('Error, not registered', $result);
  1456. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1457. }
  1458. /**
  1459. * testFormValidationAssociatedFirstLevel method
  1460. *
  1461. * test form error display with associated model.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormValidationAssociatedFirstLevel() {
  1466. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1467. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1468. $data = array(
  1469. 'ValidateUser' => array('name' => 'mariano'),
  1470. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1471. );
  1472. $result = $this->ValidateUser->create($data);
  1473. $this->assertFalse(empty($result));
  1474. $this->assertFalse($this->ValidateUser->validates());
  1475. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1476. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1477. $encoding = strtolower(Configure::read('App.encoding'));
  1478. $expected = array(
  1479. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1480. 'div' => array('style' => 'display:none;'),
  1481. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1482. '/div'
  1483. );
  1484. $this->assertTags($result, $expected);
  1485. $result = $this->Form->error(
  1486. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1487. );
  1488. $this->assertEquals('Invalid email', $result);
  1489. $result = $this->Form->error(
  1490. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1491. );
  1492. $this->assertEquals('Invalid name', $result);
  1493. $result = $this->Form->error(
  1494. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1495. );
  1496. $this->assertEquals('Invalid city', $result);
  1497. unset($this->ValidateUser->ValidateProfile);
  1498. unset($this->ValidateUser);
  1499. }
  1500. /**
  1501. * testFormValidationAssociatedSecondLevel method
  1502. *
  1503. * test form error display with associated model.
  1504. *
  1505. * @return void
  1506. */
  1507. public function testFormValidationAssociatedSecondLevel() {
  1508. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1509. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1510. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1511. $data = array(
  1512. 'ValidateUser' => array('name' => 'mariano'),
  1513. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1514. 'ValidateItem' => array('name' => 'Item')
  1515. );
  1516. $result = $this->ValidateUser->create($data);
  1517. $this->assertFalse(empty($result));
  1518. $this->assertFalse($this->ValidateUser->validates());
  1519. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1520. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1521. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1522. $encoding = strtolower(Configure::read('App.encoding'));
  1523. $expected = array(
  1524. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1525. 'div' => array('style' => 'display:none;'),
  1526. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1527. '/div'
  1528. );
  1529. $this->assertTags($result, $expected);
  1530. $result = $this->Form->error(
  1531. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1532. );
  1533. $this->assertEquals('Invalid email', $result);
  1534. $result = $this->Form->error(
  1535. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid name', $result);
  1538. $result = $this->Form->error(
  1539. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1540. );
  1541. $result = $this->Form->error(
  1542. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1543. );
  1544. $this->assertEquals('Invalid description', $result);
  1545. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1546. unset($this->ValidateUser->ValidateProfile);
  1547. unset($this->ValidateUser);
  1548. }
  1549. /**
  1550. * testFormValidationMultiRecord method
  1551. *
  1552. * test form error display with multiple records.
  1553. *
  1554. * @return void
  1555. */
  1556. public function testFormValidationMultiRecord() {
  1557. $Contact = ClassRegistry::getObject('Contact');
  1558. $Contact->validationErrors[2] = array(
  1559. 'name' => array('This field cannot be left blank')
  1560. );
  1561. $result = $this->Form->input('Contact.2.name');
  1562. $expected = array(
  1563. 'div' => array('class' => 'input text error'),
  1564. 'label' => array('for' => 'Contact2Name'),
  1565. 'Name',
  1566. '/label',
  1567. 'input' => array(
  1568. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1569. 'class' => 'form-error', 'maxlength' => 255
  1570. ),
  1571. array('div' => array('class' => 'error-message')),
  1572. 'This field cannot be left blank',
  1573. '/div',
  1574. '/div'
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. }
  1578. /**
  1579. * testMultipleInputValidation method
  1580. *
  1581. * test multiple record form validation error display.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testMultipleInputValidation() {
  1586. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1587. $Address->validationErrors[0] = array(
  1588. 'title' => array('This field cannot be empty'),
  1589. 'first_name' => array('This field cannot be empty')
  1590. );
  1591. $Address->validationErrors[1] = array(
  1592. 'last_name' => array('You must have a last name')
  1593. );
  1594. $this->Form->create();
  1595. $result = $this->Form->input('Address.0.title');
  1596. $expected = array(
  1597. 'div' => array('class'),
  1598. 'label' => array('for'),
  1599. 'preg:/[^<]+/',
  1600. '/label',
  1601. 'input' => array(
  1602. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1603. ),
  1604. array('div' => array('class' => 'error-message')),
  1605. 'This field cannot be empty',
  1606. '/div',
  1607. '/div'
  1608. );
  1609. $this->assertTags($result, $expected);
  1610. $result = $this->Form->input('Address.0.first_name');
  1611. $expected = array(
  1612. 'div' => array('class'),
  1613. 'label' => array('for'),
  1614. 'preg:/[^<]+/',
  1615. '/label',
  1616. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1617. array('div' => array('class' => 'error-message')),
  1618. 'This field cannot be empty',
  1619. '/div',
  1620. '/div'
  1621. );
  1622. $this->assertTags($result, $expected);
  1623. $result = $this->Form->input('Address.0.last_name');
  1624. $expected = array(
  1625. 'div' => array('class'),
  1626. 'label' => array('for'),
  1627. 'preg:/[^<]+/',
  1628. '/label',
  1629. 'input' => array('type' => 'text', 'name', 'id'),
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->input('Address.1.last_name');
  1634. $expected = array(
  1635. 'div' => array('class'),
  1636. 'label' => array('for'),
  1637. 'preg:/[^<]+/',
  1638. '/label',
  1639. 'input' => array(
  1640. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1641. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1642. ),
  1643. array('div' => array('class' => 'error-message')),
  1644. 'You must have a last name',
  1645. '/div',
  1646. '/div'
  1647. );
  1648. $this->assertTags($result, $expected);
  1649. }
  1650. /**
  1651. * testInput method
  1652. *
  1653. * Test various incarnations of input().
  1654. *
  1655. * @return void
  1656. */
  1657. public function testInput() {
  1658. $result = $this->Form->input('ValidateUser.balance');
  1659. $expected = array(
  1660. 'div' => array('class'),
  1661. 'label' => array('for'),
  1662. 'Balance',
  1663. '/label',
  1664. 'input' => array('name', 'type' => 'number', 'id'),
  1665. '/div',
  1666. );
  1667. $this->assertTags($result, $expected);
  1668. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1669. $expected = array(
  1670. 'div' => array('class' => 'input email'),
  1671. 'label' => array('for' => 'custom'),
  1672. 'Email',
  1673. '/label',
  1674. array('input' => array(
  1675. 'type' => 'email', 'name' => 'data[Contact][email]',
  1676. 'id' => 'custom', 'maxlength' => 255
  1677. )),
  1678. '/div'
  1679. );
  1680. $this->assertTags($result, $expected);
  1681. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1682. $expected = array(
  1683. '<div',
  1684. 'label' => array('for' => 'ContactEmail'),
  1685. 'Email',
  1686. '/label',
  1687. array('input' => array(
  1688. 'type' => 'email', 'name' => 'data[Contact][email]',
  1689. 'id' => 'ContactEmail', 'maxlength' => 255
  1690. )),
  1691. '/div'
  1692. );
  1693. $this->assertTags($result, $expected);
  1694. $result = $this->Form->hidden('Contact.idontexist');
  1695. $expected = array('input' => array(
  1696. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1697. 'id' => 'ContactIdontexist'
  1698. ));
  1699. $this->assertTags($result, $expected);
  1700. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1701. $expected = array(
  1702. 'div' => array('class' => 'input text'),
  1703. 'label' => array('for' => 'ContactEmail'),
  1704. 'Email',
  1705. '/label',
  1706. array('input' => array(
  1707. 'type' => 'text', 'name' => 'data[Contact][email]',
  1708. 'id' => 'ContactEmail'
  1709. )),
  1710. '/div'
  1711. );
  1712. $this->assertTags($result, $expected);
  1713. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1714. $expected = array(
  1715. 'div' => array('class' => 'input text'),
  1716. 'label' => array('for' => 'Contact5Email'),
  1717. 'Email',
  1718. '/label',
  1719. array('input' => array(
  1720. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1721. 'id' => 'Contact5Email'
  1722. )),
  1723. '/div'
  1724. );
  1725. $this->assertTags($result, $expected);
  1726. $result = $this->Form->input('Contact.password');
  1727. $expected = array(
  1728. 'div' => array('class' => 'input password'),
  1729. 'label' => array('for' => 'ContactPassword'),
  1730. 'Password',
  1731. '/label',
  1732. array('input' => array(
  1733. 'type' => 'password', 'name' => 'data[Contact][password]',
  1734. 'id' => 'ContactPassword'
  1735. )),
  1736. '/div'
  1737. );
  1738. $this->assertTags($result, $expected);
  1739. $result = $this->Form->input('Contact.email', array(
  1740. 'type' => 'file', 'class' => 'textbox'
  1741. ));
  1742. $expected = array(
  1743. 'div' => array('class' => 'input file'),
  1744. 'label' => array('for' => 'ContactEmail'),
  1745. 'Email',
  1746. '/label',
  1747. array('input' => array(
  1748. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1749. 'id' => 'ContactEmail'
  1750. )),
  1751. '/div'
  1752. );
  1753. $this->assertTags($result, $expected);
  1754. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1755. $result = $this->Form->input('Contact.phone');
  1756. $expected = array(
  1757. 'div' => array('class' => 'input tel'),
  1758. 'label' => array('for' => 'ContactPhone'),
  1759. 'Phone',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1763. 'value' => 'Hello &amp; World &gt; weird chars',
  1764. 'id' => 'ContactPhone', 'maxlength' => 255
  1765. )),
  1766. '/div'
  1767. );
  1768. $this->assertTags($result, $expected);
  1769. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1770. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1771. $expected = array(
  1772. 'div' => array('class' => 'input text'),
  1773. 'label' => array('for' => 'myId'),
  1774. 'Field',
  1775. '/label',
  1776. 'input' => array(
  1777. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1778. 'value' => 'My value', 'id' => 'myId'
  1779. ),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. unset($this->Form->request->data);
  1784. $Contact = ClassRegistry::getObject('Contact');
  1785. $Contact->validationErrors['field'] = array('Badness!');
  1786. $result = $this->Form->input('Contact.field');
  1787. $expected = array(
  1788. 'div' => array('class' => 'input text error'),
  1789. 'label' => array('for' => 'ContactField'),
  1790. 'Field',
  1791. '/label',
  1792. 'input' => array(
  1793. 'type' => 'text', 'name' => 'data[Contact][field]',
  1794. 'id' => 'ContactField', 'class' => 'form-error'
  1795. ),
  1796. array('div' => array('class' => 'error-message')),
  1797. 'Badness!',
  1798. '/div',
  1799. '/div'
  1800. );
  1801. $this->assertTags($result, $expected);
  1802. $result = $this->Form->input('Contact.field', array(
  1803. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1804. ));
  1805. $expected = array(
  1806. 'label' => array('for' => 'ContactField'),
  1807. 'Field',
  1808. '/label',
  1809. 'input' => array(
  1810. 'type' => 'text', 'name' => 'data[Contact][field]',
  1811. 'id' => 'ContactField', 'class' => 'form-error'
  1812. ),
  1813. array('span' => array('class' => 'error-message')),
  1814. 'Badness!',
  1815. '/span'
  1816. );
  1817. $this->assertTags($result, $expected);
  1818. $result = $this->Form->input('Contact.field', array(
  1819. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1820. ));
  1821. $expected = array(
  1822. 'div' => array('class' => 'input text error'),
  1823. 'label' => array('for' => 'ContactField'),
  1824. 'Field',
  1825. '/label',
  1826. 'input' => array(
  1827. 'type' => 'text', 'name' => 'data[Contact][field]',
  1828. 'id' => 'ContactField', 'class' => 'form-error'
  1829. ),
  1830. array('div' => array('class' => 'error')),
  1831. 'Badness!',
  1832. '/div'
  1833. );
  1834. $this->assertTags($result, $expected);
  1835. $result = $this->Form->input('Contact.field', array(
  1836. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1837. ));
  1838. $expected = array(
  1839. 'span' => array('class' => 'input text error'),
  1840. 'label' => array('for' => 'ContactField'),
  1841. 'Field',
  1842. '/label',
  1843. 'input' => array(
  1844. 'type' => 'text', 'name' => 'data[Contact][field]',
  1845. 'id' => 'ContactField', 'class' => 'form-error'
  1846. ),
  1847. 'Badness!',
  1848. '/span'
  1849. );
  1850. $this->assertTags($result, $expected);
  1851. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1852. $expected = array(
  1853. 'div' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'A message to you, Rudy',
  1862. array('div' => array('class' => 'error-message')),
  1863. 'Badness!',
  1864. '/div',
  1865. '/div'
  1866. );
  1867. $this->assertTags($result, $expected);
  1868. $this->Form->setEntity(null);
  1869. $this->Form->setEntity('Contact.field');
  1870. $result = $this->Form->input('Contact.field', array(
  1871. 'after' => 'A message to you, Rudy', 'error' => false
  1872. ));
  1873. $expected = array(
  1874. 'div' => array('class' => 'input text'),
  1875. 'label' => array('for' => 'ContactField'),
  1876. 'Field',
  1877. '/label',
  1878. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1879. 'A message to you, Rudy',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1884. $expected = array(
  1885. 'div' => array('class' => 'input text'),
  1886. 'label' => array('for' => 'ObjectField'),
  1887. 'Field',
  1888. '/label',
  1889. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1890. 'A message to you, Rudy',
  1891. '/div'
  1892. );
  1893. $this->assertTags($result, $expected);
  1894. $Contact->validationErrors['field'] = array('minLength');
  1895. $result = $this->Form->input('Contact.field', array(
  1896. 'error' => array(
  1897. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1898. 'maxLength' => 'login too large'
  1899. )
  1900. ));
  1901. $expected = array(
  1902. 'div' => array('class' => 'input text error'),
  1903. 'label' => array('for' => 'ContactField'),
  1904. 'Field',
  1905. '/label',
  1906. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1907. array('div' => array('class' => 'error-message')),
  1908. 'Le login doit contenir au moins 2 caractères',
  1909. '/div',
  1910. '/div'
  1911. );
  1912. $this->assertTags($result, $expected);
  1913. $Contact->validationErrors['field'] = array('maxLength');
  1914. $result = $this->Form->input('Contact.field', array(
  1915. 'error' => array(
  1916. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1917. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1918. 'maxLength' => 'login too large',
  1919. )
  1920. ));
  1921. $expected = array(
  1922. 'div' => array('class' => 'input text error'),
  1923. 'label' => array('for' => 'ContactField'),
  1924. 'Field',
  1925. '/label',
  1926. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1927. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1928. 'login too large',
  1929. '/span',
  1930. '/div'
  1931. );
  1932. $this->assertTags($result, $expected);
  1933. }
  1934. /**
  1935. * Test that inputs with 0 can be created.
  1936. *
  1937. * @return void
  1938. */
  1939. public function testInputZero() {
  1940. $this->Form->create('User');
  1941. $result = $this->Form->input('0');
  1942. $expected = array(
  1943. 'div' => array('class' => 'input text'),
  1944. 'label' => array('for' => 'User0'), '/label',
  1945. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. }
  1950. /**
  1951. * test input() with checkbox creation
  1952. *
  1953. * @return void
  1954. */
  1955. public function testInputCheckbox() {
  1956. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1957. $expected = array(
  1958. 'div' => array('class' => 'input checkbox'),
  1959. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1960. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1961. '/div'
  1962. );
  1963. $this->assertTags($result, $expected);
  1964. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1965. $expected = array(
  1966. 'div' => array('class' => 'input checkbox'),
  1967. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1968. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1969. '/div'
  1970. );
  1971. $this->assertTags($result, $expected);
  1972. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1973. $expected = array(
  1974. 'div' => array('class' => 'input checkbox'),
  1975. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1976. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. $result = $this->Form->input('User.disabled', array(
  1981. 'label' => 'Disabled',
  1982. 'type' => 'checkbox',
  1983. 'data-foo' => 'disabled'
  1984. ));
  1985. $expected = array(
  1986. 'div' => array('class' => 'input checkbox'),
  1987. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1988. array('input' => array(
  1989. 'type' => 'checkbox',
  1990. 'name' => 'data[User][disabled]',
  1991. 'value' => '1',
  1992. 'id' => 'UserDisabled',
  1993. 'data-foo' => 'disabled'
  1994. )),
  1995. 'label' => array('for' => 'UserDisabled'),
  1996. 'Disabled',
  1997. '/label',
  1998. '/div'
  1999. );
  2000. $this->assertTags($result, $expected);
  2001. }
  2002. /**
  2003. * test form->input() with time types.
  2004. *
  2005. */
  2006. public function testInputTime() {
  2007. extract($this->dateRegex);
  2008. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2009. $result = explode(':', $result);
  2010. $this->assertRegExp('/option value="23"/', $result[0]);
  2011. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2012. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2013. $result = explode(':', $result);
  2014. $this->assertRegExp('/option value="23"/', $result[0]);
  2015. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2016. $result = $this->Form->input('Model.field', array(
  2017. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2018. ));
  2019. $result = explode(':', $result);
  2020. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2021. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2022. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2023. $result = $this->Form->input('Model.field', array(
  2024. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2025. ));
  2026. $result = explode(':', $result);
  2027. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2028. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2029. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2030. $result = $this->Form->input('prueba', array(
  2031. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2032. 'maxYear' => date('Y') + 1, 'interval' => 15
  2033. ));
  2034. $result = explode(':', $result);
  2035. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2036. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2037. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2038. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2039. $result = $this->Form->input('Random.start_time', array(
  2040. 'type' => 'time',
  2041. 'selected' => '18:15'
  2042. ));
  2043. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2044. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2045. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2046. $result = $this->Form->input('published', array('type' => 'time'));
  2047. $now = strtotime('now');
  2048. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2049. $now = strtotime('2013-03-09 00:42:21');
  2050. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2051. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2052. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2053. }
  2054. /**
  2055. * Test interval + selected near the hour roll over.
  2056. *
  2057. * @return void
  2058. */
  2059. public function testTimeSelectedWithInterval() {
  2060. $result = $this->Form->input('Model.start_time', array(
  2061. 'type' => 'time',
  2062. 'interval' => 15,
  2063. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2064. ));
  2065. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2066. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2067. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2068. $result = $this->Form->input('Model.start_time', array(
  2069. 'type' => 'time',
  2070. 'interval' => 15,
  2071. 'selected' => '2012-10-23 15:57:00'
  2072. ));
  2073. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2074. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2075. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2076. $result = $this->Form->input('Model.start_time', array(
  2077. 'timeFormat' => 24,
  2078. 'type' => 'time',
  2079. 'interval' => 15,
  2080. 'selected' => '15:57'
  2081. ));
  2082. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2083. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2084. $result = $this->Form->input('Model.start_time', array(
  2085. 'timeFormat' => 24,
  2086. 'type' => 'time',
  2087. 'interval' => 15,
  2088. 'selected' => '23:57'
  2089. ));
  2090. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2091. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2092. $result = $this->Form->input('Model.created', array(
  2093. 'timeFormat' => 24,
  2094. 'type' => 'datetime',
  2095. 'interval' => 15,
  2096. 'selected' => '2012-09-30 23:56'
  2097. ));
  2098. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2099. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2100. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2101. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2102. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2103. }
  2104. /**
  2105. * Test time with selected values around 12:xx:xx
  2106. *
  2107. * @return void
  2108. */
  2109. public function testTimeSelectedWithIntervalTwelve() {
  2110. $result = $this->Form->input('Model.start_time', array(
  2111. 'type' => 'time',
  2112. 'timeFormat' => 12,
  2113. 'interval' => 15,
  2114. 'selected' => '00:00:00'
  2115. ));
  2116. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2117. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2118. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2119. $result = $this->Form->input('Model.start_time', array(
  2120. 'type' => 'time',
  2121. 'timeFormat' => 12,
  2122. 'interval' => 15,
  2123. 'selected' => '12:00:00'
  2124. ));
  2125. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2126. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2127. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2128. $result = $this->Form->input('Model.start_time', array(
  2129. 'type' => 'time',
  2130. 'timeFormat' => 12,
  2131. 'interval' => 15,
  2132. 'selected' => '12:15:00'
  2133. ));
  2134. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2135. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2136. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2137. }
  2138. /**
  2139. * Test interval & timeFormat = 12
  2140. *
  2141. * @return void
  2142. */
  2143. public function testInputTimeWithIntervalAnd12HourFormat() {
  2144. $result = $this->Form->input('Model.start_time', array(
  2145. 'type' => 'time',
  2146. 'timeFormat' => 12,
  2147. 'interval' => 5,
  2148. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2149. ));
  2150. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2151. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2152. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2153. $result = $this->Form->input('Model.start_time', array(
  2154. 'type' => 'time',
  2155. 'timeFormat' => '12',
  2156. 'interval' => 5,
  2157. 'selected' => '2013-04-19 16:30:00'
  2158. ));
  2159. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2160. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2161. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2162. $result = $this->Form->input('Model.start_time', array(
  2163. 'type' => 'time',
  2164. 'timeFormat' => '12',
  2165. 'interval' => 10,
  2166. 'selected' => '2013-05-19 00:33:00'
  2167. ));
  2168. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2169. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2170. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2171. $result = $this->Form->input('Model.start_time', array(
  2172. 'type' => 'time',
  2173. 'timeFormat' => '12',
  2174. 'interval' => 10,
  2175. 'selected' => '2013-05-19 13:33:00'
  2176. ));
  2177. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2178. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2179. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2180. $result = $this->Form->input('Model.start_time', array(
  2181. 'type' => 'time',
  2182. 'timeFormat' => '12',
  2183. 'interval' => 10,
  2184. 'selected' => '2013-05-19 01:33:00'
  2185. ));
  2186. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2187. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2188. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2189. }
  2190. /**
  2191. * test form->input() with datetime, date and time types
  2192. *
  2193. * @return void
  2194. */
  2195. public function testInputDatetime() {
  2196. extract($this->dateRegex);
  2197. $result = $this->Form->input('prueba', array(
  2198. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2199. 'maxYear' => date('Y') + 1, 'interval' => 15
  2200. ));
  2201. $result = explode(':', $result);
  2202. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2203. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2204. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2205. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2206. //related to ticket #5013
  2207. $result = $this->Form->input('Contact.date', array(
  2208. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2209. ));
  2210. $this->assertRegExp('/class="customClass"/', $result);
  2211. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2212. $result = $this->Form->input('Contact.date', array(
  2213. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2214. ));
  2215. $this->assertRegExp('/id="customIdDay"/', $result);
  2216. $this->assertRegExp('/id="customIdMonth"/', $result);
  2217. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2218. $result = $this->Form->input('Model.field', array(
  2219. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2220. ));
  2221. $this->assertRegExp('/id="customIDDay"/', $result);
  2222. $this->assertRegExp('/id="customIDHour"/', $result);
  2223. $result = explode('</select><select', $result);
  2224. $result = explode(':', $result[1]);
  2225. $this->assertRegExp('/option value="23"/', $result[0]);
  2226. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2227. $result = $this->Form->input('Model.field', array(
  2228. 'type' => 'datetime', 'timeFormat' => 12
  2229. ));
  2230. $result = explode('</select><select', $result);
  2231. $result = explode(':', $result[1]);
  2232. $this->assertRegExp('/option value="12"/', $result[0]);
  2233. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2234. $this->Form->request->data = array('Contact' => array('created' => null));
  2235. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2236. $expected = array(
  2237. 'div' => array('class' => 'input date'),
  2238. 'label' => array('for' => 'ContactCreatedMonth'),
  2239. 'Created',
  2240. '/label',
  2241. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2242. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2243. $monthsRegex,
  2244. '/select', '-',
  2245. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2246. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2247. $daysRegex,
  2248. '/select', '-',
  2249. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2250. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2251. $yearsRegex,
  2252. '/select',
  2253. '/div'
  2254. );
  2255. $this->assertTags($result, $expected);
  2256. $this->Form->request->data = array('Contact' => array('created' => null));
  2257. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2258. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2259. $this->Form->request->data = array('Contact' => array('created' => null));
  2260. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2261. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2262. $result = $this->Form->input('Contact.created', array(
  2263. 'type' => 'date',
  2264. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2265. 'timeFormat' => 'NONE'
  2266. ));
  2267. $this->assertRegExp('/for\="created-month"/', $result);
  2268. }
  2269. /**
  2270. * Test generating checkboxes in a loop.
  2271. *
  2272. * @return void
  2273. */
  2274. public function testInputCheckboxesInLoop() {
  2275. for ($i = 1; $i < 5; $i++) {
  2276. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2277. $expected = array(
  2278. 'div' => array('class' => 'input checkbox'),
  2279. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2280. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2281. 'label' => array('for' => "Contact{$i}Email"),
  2282. 'Email',
  2283. '/label',
  2284. '/div'
  2285. );
  2286. $this->assertTags($result, $expected);
  2287. }
  2288. }
  2289. /**
  2290. * Test generating checkboxes with disabled elements.
  2291. *
  2292. * @return void
  2293. */
  2294. public function testInputCheckboxWithDisabledElements() {
  2295. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2296. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2297. $expected = array(
  2298. array('div' => array('class' => 'input select')),
  2299. array('label' => array('for' => "ContactMultiple")),
  2300. 'Multiple',
  2301. '/label',
  2302. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2303. array('div' => array('class' => 'checkbox')),
  2304. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2305. array('label' => array('for' => "ContactMultiple1")),
  2306. 'One',
  2307. '/label',
  2308. '/div',
  2309. array('div' => array('class' => 'checkbox')),
  2310. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2311. array('label' => array('for' => "ContactMultiple2")),
  2312. 'Two',
  2313. '/label',
  2314. '/div',
  2315. array('div' => array('class' => 'checkbox')),
  2316. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2317. array('label' => array('for' => "ContactMultiple3")),
  2318. 'Three',
  2319. '/label',
  2320. '/div',
  2321. '/div'
  2322. );
  2323. $this->assertTags($result, $expected);
  2324. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2325. $this->assertTags($result, $expected);
  2326. $disabled = array('2', 3);
  2327. $expected = array(
  2328. array('div' => array('class' => 'input select')),
  2329. array('label' => array('for' => "ContactMultiple")),
  2330. 'Multiple',
  2331. '/label',
  2332. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2333. array('div' => array('class' => 'checkbox')),
  2334. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2335. array('label' => array('for' => "ContactMultiple1")),
  2336. 'One',
  2337. '/label',
  2338. '/div',
  2339. array('div' => array('class' => 'checkbox')),
  2340. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2341. array('label' => array('for' => "ContactMultiple2")),
  2342. 'Two',
  2343. '/label',
  2344. '/div',
  2345. array('div' => array('class' => 'checkbox')),
  2346. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2347. array('label' => array('for' => "ContactMultiple3")),
  2348. 'Three',
  2349. '/label',
  2350. '/div',
  2351. '/div'
  2352. );
  2353. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2354. $this->assertTags($result, $expected);
  2355. // make sure 50 does only disable 50, and not 50f5c0cf
  2356. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2357. $disabled = array(50);
  2358. $expected = array(
  2359. array('div' => array('class' => 'input select')),
  2360. array('label' => array('for' => "ContactMultiple")),
  2361. 'Multiple',
  2362. '/label',
  2363. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2364. array('div' => array('class' => 'checkbox')),
  2365. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2366. array('label' => array('for' => "ContactMultiple50")),
  2367. 'Fifty',
  2368. '/label',
  2369. '/div',
  2370. array('div' => array('class' => 'checkbox')),
  2371. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2372. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2373. 'Stringy',
  2374. '/label',
  2375. '/div',
  2376. '/div'
  2377. );
  2378. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2379. $this->assertTags($result, $expected);
  2380. }
  2381. /**
  2382. * test input name with leading integer, ensure attributes are generated correctly.
  2383. *
  2384. * @return void
  2385. */
  2386. public function testInputWithLeadingInteger() {
  2387. $result = $this->Form->text('0.Node.title');
  2388. $expected = array(
  2389. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2390. );
  2391. $this->assertTags($result, $expected);
  2392. }
  2393. /**
  2394. * test form->input() with select type inputs.
  2395. *
  2396. * @return void
  2397. */
  2398. public function testInputSelectType() {
  2399. $result = $this->Form->input('email', array(
  2400. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2401. );
  2402. $expected = array(
  2403. 'div' => array('class' => 'input select'),
  2404. 'label' => array('for' => 'email'),
  2405. 'Email',
  2406. '/label',
  2407. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2408. array('option' => array('value' => '')),
  2409. '/option',
  2410. array('option' => array('value' => 'è')),
  2411. 'Firést',
  2412. '/option',
  2413. array('option' => array('value' => 'é')),
  2414. 'Secoènd',
  2415. '/option',
  2416. '/select',
  2417. '/div'
  2418. );
  2419. $this->assertTags($result, $expected);
  2420. $result = $this->Form->input('email', array(
  2421. 'options' => array('First', 'Second'), 'empty' => true)
  2422. );
  2423. $expected = array(
  2424. 'div' => array('class' => 'input select'),
  2425. 'label' => array('for' => 'email'),
  2426. 'Email',
  2427. '/label',
  2428. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2429. array('option' => array('value' => '')),
  2430. '/option',
  2431. array('option' => array('value' => '0')),
  2432. 'First',
  2433. '/option',
  2434. array('option' => array('value' => '1')),
  2435. 'Second',
  2436. '/option',
  2437. '/select',
  2438. '/div'
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2442. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2443. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2444. $expected = array(
  2445. 'div' => array('class' => 'input select'),
  2446. 'label' => array('for' => 'ModelUserId'),
  2447. 'User',
  2448. '/label',
  2449. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2450. array('option' => array('value' => '')),
  2451. '/option',
  2452. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2453. 'good',
  2454. '/option',
  2455. array('option' => array('value' => 'other')),
  2456. 'bad',
  2457. '/option',
  2458. '/select',
  2459. '/div'
  2460. );
  2461. $this->assertTags($result, $expected);
  2462. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2463. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2464. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2465. $expected = array(
  2466. 'div' => array('class' => 'input select'),
  2467. 'label' => array('for' => 'ThingUserId'),
  2468. 'User',
  2469. '/label',
  2470. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2471. array('option' => array('value' => '')),
  2472. 'Some Empty',
  2473. '/option',
  2474. array('option' => array('value' => 'value')),
  2475. 'good',
  2476. '/option',
  2477. array('option' => array('value' => 'other')),
  2478. 'bad',
  2479. '/option',
  2480. '/select',
  2481. '/div'
  2482. );
  2483. $this->assertTags($result, $expected);
  2484. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2485. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2486. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2487. $expected = array(
  2488. 'div' => array('class' => 'input select'),
  2489. 'label' => array('for' => 'ThingUserId'),
  2490. 'User',
  2491. '/label',
  2492. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2493. array('option' => array('value' => '')),
  2494. 'Some Empty',
  2495. '/option',
  2496. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2497. 'good',
  2498. '/option',
  2499. array('option' => array('value' => 'other')),
  2500. 'bad',
  2501. '/option',
  2502. '/select',
  2503. '/div'
  2504. );
  2505. $this->assertTags($result, $expected);
  2506. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2507. $this->Form->request->data = array('User' => array('User' => array('value')));
  2508. $result = $this->Form->input('User.User', array('empty' => true));
  2509. $expected = array(
  2510. 'div' => array('class' => 'input select'),
  2511. 'label' => array('for' => 'UserUser'),
  2512. 'User',
  2513. '/label',
  2514. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2515. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2516. array('option' => array('value' => '')),
  2517. '/option',
  2518. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2519. 'good',
  2520. '/option',
  2521. array('option' => array('value' => 'other')),
  2522. 'bad',
  2523. '/option',
  2524. '/select',
  2525. '/div'
  2526. );
  2527. $this->assertTags($result, $expected);
  2528. $this->Form->data = array();
  2529. $result = $this->Form->input('Publisher.id', array(
  2530. 'label' => 'Publisher',
  2531. 'type' => 'select',
  2532. 'multiple' => 'checkbox',
  2533. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2534. ));
  2535. $expected = array(
  2536. array('div' => array('class' => 'input select')),
  2537. array('label' => array('for' => 'PublisherId')),
  2538. 'Publisher',
  2539. '/label',
  2540. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2541. array('div' => array('class' => 'checkbox')),
  2542. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2543. array('label' => array('for' => 'PublisherIdValue1')),
  2544. 'Label 1',
  2545. '/label',
  2546. '/div',
  2547. array('div' => array('class' => 'checkbox')),
  2548. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2549. array('label' => array('for' => 'PublisherIdValue2')),
  2550. 'Label 2',
  2551. '/label',
  2552. '/div',
  2553. '/div'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. }
  2557. /**
  2558. * test that input() and a non standard primary key makes a hidden input by default.
  2559. *
  2560. * @return void
  2561. */
  2562. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2563. $this->Form->create('User');
  2564. $this->Form->fieldset = array(
  2565. 'User' => array(
  2566. 'fields' => array(
  2567. 'model_id' => array('type' => 'integer')
  2568. ),
  2569. 'validates' => array(),
  2570. 'key' => 'model_id'
  2571. )
  2572. );
  2573. $result = $this->Form->input('model_id');
  2574. $expected = array(
  2575. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2576. );
  2577. $this->assertTags($result, $expected);
  2578. }
  2579. /**
  2580. * test that overriding the magic select type widget is possible
  2581. *
  2582. * @return void
  2583. */
  2584. public function testInputOverridingMagicSelectType() {
  2585. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2586. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2587. $expected = array(
  2588. 'div' => array('class' => 'input text'),
  2589. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2590. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2591. '/div'
  2592. );
  2593. $this->assertTags($result, $expected);
  2594. //Check that magic types still work for plural/singular vars
  2595. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2596. $result = $this->Form->input('Model.type');
  2597. $expected = array(
  2598. 'div' => array('class' => 'input select'),
  2599. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2600. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2601. array('option' => array('value' => 'value')), 'good', '/option',
  2602. array('option' => array('value' => 'other')), 'bad', '/option',
  2603. '/select',
  2604. '/div'
  2605. );
  2606. $this->assertTags($result, $expected);
  2607. }
  2608. /**
  2609. * Test that inferred types do not override developer input
  2610. *
  2611. * @return void
  2612. */
  2613. public function testInputMagicTypeDoesNotOverride() {
  2614. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2615. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2616. $expected = array(
  2617. 'div' => array('class' => 'input checkbox'),
  2618. array('input' => array(
  2619. 'type' => 'hidden',
  2620. 'name' => 'data[Model][user]',
  2621. 'id' => 'ModelUser_',
  2622. 'value' => 0,
  2623. )),
  2624. array('input' => array(
  2625. 'name' => 'data[Model][user]',
  2626. 'type' => 'checkbox',
  2627. 'id' => 'ModelUser',
  2628. 'value' => 1
  2629. )),
  2630. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2631. '/div'
  2632. );
  2633. $this->assertTags($result, $expected);
  2634. }
  2635. /**
  2636. * Test that magic input() selects are created for type=number
  2637. *
  2638. * @return void
  2639. */
  2640. public function testInputMagicSelectForTypeNumber() {
  2641. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2642. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2643. $result = $this->Form->input('ValidateUser.balance');
  2644. $expected = array(
  2645. 'div' => array('class' => 'input select'),
  2646. 'label' => array('for' => 'ValidateUserBalance'),
  2647. 'Balance',
  2648. '/label',
  2649. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2650. array('option' => array('value' => '0')),
  2651. 'nothing',
  2652. '/option',
  2653. array('option' => array('value' => '1', 'selected' => 'selected')),
  2654. 'some',
  2655. '/option',
  2656. array('option' => array('value' => '100')),
  2657. 'a lot',
  2658. '/option',
  2659. '/select',
  2660. '/div'
  2661. );
  2662. $this->assertTags($result, $expected);
  2663. }
  2664. /**
  2665. * Test that magic input() selects can easily be converted into radio types without error.
  2666. *
  2667. * @return void
  2668. */
  2669. public function testInputMagicSelectChangeToRadio() {
  2670. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2671. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2672. $this->assertRegExp('/input type="radio"/', $result);
  2673. }
  2674. /**
  2675. * fields with the same name as the model should work.
  2676. *
  2677. * @return void
  2678. */
  2679. public function testInputWithMatchingFieldAndModelName() {
  2680. $this->Form->create('User');
  2681. $this->Form->fieldset = array(
  2682. 'User' => array(
  2683. 'fields' => array(
  2684. 'User' => array('type' => 'text')
  2685. ),
  2686. 'validates' => array(),
  2687. 'key' => 'id'
  2688. )
  2689. );
  2690. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2691. $result = $this->Form->input('User', array('type' => 'text'));
  2692. $expected = array(
  2693. 'div' => array('class' => 'input text'),
  2694. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2695. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2696. '/div'
  2697. );
  2698. $this->assertTags($result, $expected);
  2699. }
  2700. /**
  2701. * testFormInputs method
  2702. *
  2703. * test correct results from form::inputs().
  2704. *
  2705. * @return void
  2706. */
  2707. public function testFormInputs() {
  2708. $this->Form->create('Contact');
  2709. $result = $this->Form->inputs('The Legend');
  2710. $expected = array(
  2711. '<fieldset',
  2712. '<legend',
  2713. 'The Legend',
  2714. '/legend',
  2715. '*/fieldset',
  2716. );
  2717. $this->assertTags($result, $expected);
  2718. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2719. $expected = array(
  2720. 'fieldset' => array('class' => 'classy-stuff'),
  2721. '<legend',
  2722. 'Field of Dreams',
  2723. '/legend',
  2724. '*/fieldset'
  2725. );
  2726. $this->assertTags($result, $expected);
  2727. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2728. $this->assertTags($result, $expected);
  2729. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2730. $this->assertTags($result, $expected);
  2731. $this->Form->create('Contact');
  2732. $this->Form->request['prefix'] = 'admin';
  2733. $this->Form->request['action'] = 'admin_edit';
  2734. $result = $this->Form->inputs();
  2735. $expected = array(
  2736. '<fieldset',
  2737. '<legend',
  2738. 'Edit Contact',
  2739. '/legend',
  2740. '*/fieldset',
  2741. );
  2742. $this->assertTags($result, $expected);
  2743. $this->Form->create('Contact');
  2744. $result = $this->Form->inputs(false);
  2745. $expected = array(
  2746. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2747. array('div' => array('class' => 'input text')),
  2748. '*/div',
  2749. array('div' => array('class' => 'input email')),
  2750. '*/div',
  2751. array('div' => array('class' => 'input tel')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input password')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input date')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input date')),
  2758. '*/div',
  2759. array('div' => array('class' => 'input datetime')),
  2760. '*/div',
  2761. array('div' => array('class' => 'input number')),
  2762. '*/div',
  2763. array('div' => array('class' => 'input select')),
  2764. '*/div',
  2765. );
  2766. $this->assertTags($result, $expected);
  2767. $this->Form->create('Contact');
  2768. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2769. $expected = array(
  2770. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2771. array('div' => array('class' => 'input text')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input email')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input tel')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input password')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input date')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input date')),
  2782. '*/div',
  2783. array('div' => array('class' => 'input datetime')),
  2784. '*/div',
  2785. array('div' => array('class' => 'input number')),
  2786. '*/div',
  2787. array('div' => array('class' => 'input select')),
  2788. '*/div',
  2789. );
  2790. $this->assertTags($result, $expected);
  2791. $this->Form->create('Contact');
  2792. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2793. $this->assertTags($result, $expected);
  2794. $this->Form->create('Contact');
  2795. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2796. $expected = array(
  2797. 'fieldset' => array(),
  2798. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2799. array('div' => array('class' => 'input text')),
  2800. '*/div',
  2801. array('div' => array('class' => 'input email')),
  2802. '*/div',
  2803. array('div' => array('class' => 'input tel')),
  2804. '*/div',
  2805. array('div' => array('class' => 'input password')),
  2806. '*/div',
  2807. array('div' => array('class' => 'input date')),
  2808. '*/div',
  2809. array('div' => array('class' => 'input date')),
  2810. '*/div',
  2811. array('div' => array('class' => 'input datetime')),
  2812. '*/div',
  2813. array('div' => array('class' => 'input number')),
  2814. '*/div',
  2815. array('div' => array('class' => 'input select')),
  2816. '*/div',
  2817. '/fieldset'
  2818. );
  2819. $this->assertTags($result, $expected);
  2820. $this->Form->create('Contact');
  2821. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2824. array('div' => array('class' => 'input text')),
  2825. '*/div',
  2826. array('div' => array('class' => 'input email')),
  2827. '*/div',
  2828. array('div' => array('class' => 'input tel')),
  2829. '*/div',
  2830. array('div' => array('class' => 'input password')),
  2831. '*/div',
  2832. array('div' => array('class' => 'input date')),
  2833. '*/div',
  2834. array('div' => array('class' => 'input date')),
  2835. '*/div',
  2836. array('div' => array('class' => 'input datetime')),
  2837. '*/div',
  2838. array('div' => array('class' => 'input number')),
  2839. '*/div',
  2840. array('div' => array('class' => 'input select')),
  2841. '*/div',
  2842. );
  2843. $this->assertTags($result, $expected);
  2844. $this->Form->create('Contact');
  2845. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2846. $this->assertTags($result, $expected);
  2847. $this->Form->create('Contact');
  2848. $result = $this->Form->inputs('Hello');
  2849. $expected = array(
  2850. 'fieldset' => array(),
  2851. 'legend' => array(),
  2852. 'Hello',
  2853. '/legend',
  2854. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2855. array('div' => array('class' => 'input text')),
  2856. '*/div',
  2857. array('div' => array('class' => 'input email')),
  2858. '*/div',
  2859. array('div' => array('class' => 'input tel')),
  2860. '*/div',
  2861. array('div' => array('class' => 'input password')),
  2862. '*/div',
  2863. array('div' => array('class' => 'input date')),
  2864. '*/div',
  2865. array('div' => array('class' => 'input date')),
  2866. '*/div',
  2867. array('div' => array('class' => 'input datetime')),
  2868. '*/div',
  2869. array('div' => array('class' => 'input number')),
  2870. '*/div',
  2871. array('div' => array('class' => 'input select')),
  2872. '*/div',
  2873. '/fieldset'
  2874. );
  2875. $this->assertTags($result, $expected);
  2876. $this->Form->create('Contact');
  2877. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2878. $expected = array(
  2879. 'fieldset' => array(),
  2880. 'legend' => array(),
  2881. 'Hello',
  2882. '/legend',
  2883. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2884. array('div' => array('class' => 'input text')),
  2885. '*/div',
  2886. array('div' => array('class' => 'input email')),
  2887. '*/div',
  2888. array('div' => array('class' => 'input tel')),
  2889. '*/div',
  2890. array('div' => array('class' => 'input password')),
  2891. '*/div',
  2892. array('div' => array('class' => 'input date')),
  2893. '*/div',
  2894. array('div' => array('class' => 'input date')),
  2895. '*/div',
  2896. array('div' => array('class' => 'input datetime')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input number')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input select')),
  2901. '*/div',
  2902. '/fieldset'
  2903. );
  2904. $this->assertTags($result, $expected);
  2905. $this->Form->create('Contact');
  2906. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2907. $this->assertTags($result, $expected);
  2908. $this->Form->end();
  2909. $this->Form->create(false);
  2910. $expected = array(
  2911. 'fieldset' => array(),
  2912. array('div' => array('class' => 'input text')),
  2913. 'label' => array('for' => 'foo'),
  2914. 'Foo',
  2915. '/label',
  2916. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2917. '*/div',
  2918. '/fieldset'
  2919. );
  2920. $result = $this->Form->inputs(
  2921. array('foo' => array('type' => 'text')),
  2922. array(),
  2923. array('legend' => false)
  2924. );
  2925. $this->assertTags($result, $expected);
  2926. }
  2927. /**
  2928. * Tests inputs() works with plugin models
  2929. *
  2930. * @return void
  2931. */
  2932. public function testInputsPluginModel() {
  2933. $this->loadFixtures('Post');
  2934. App::build(array(
  2935. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2936. ));
  2937. CakePlugin::load('TestPlugin');
  2938. $this->Form->request['models'] = array(
  2939. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2940. );
  2941. $this->Form->create('TestPlugin.TestPluginPost');
  2942. $result = $this->Form->inputs();
  2943. $this->assertContains('data[TestPluginPost][id]', $result);
  2944. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2945. $this->assertContains('data[TestPluginPost][title]', $result);
  2946. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2947. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2948. $this->assertEquals('TestPluginPost', $this->Form->model());
  2949. }
  2950. /**
  2951. * testSelectAsCheckbox method
  2952. *
  2953. * test multi-select widget with checkbox formatting.
  2954. *
  2955. * @return void
  2956. */
  2957. public function testSelectAsCheckbox() {
  2958. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2959. $expected = array(
  2960. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2961. array('div' => array('class' => 'checkbox')),
  2962. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2963. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2964. 'first',
  2965. '/label',
  2966. '/div',
  2967. array('div' => array('class' => 'checkbox')),
  2968. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2969. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2970. 'second',
  2971. '/label',
  2972. '/div',
  2973. array('div' => array('class' => 'checkbox')),
  2974. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2975. array('label' => array('for' => 'ModelMultiField2')),
  2976. 'third',
  2977. '/label',
  2978. '/div',
  2979. );
  2980. $this->assertTags($result, $expected);
  2981. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2982. $expected = array(
  2983. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2984. array('div' => array('class' => 'checkbox')),
  2985. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/2')),
  2986. array('label' => array('for' => 'ModelMultiField1/2')),
  2987. 'half',
  2988. '/label',
  2989. '/div',
  2990. );
  2991. $this->assertTags($result, $expected);
  2992. }
  2993. /**
  2994. * testLabel method
  2995. *
  2996. * test label generation.
  2997. *
  2998. * @return void
  2999. */
  3000. public function testLabel() {
  3001. $this->Form->text('Person.name');
  3002. $result = $this->Form->label();
  3003. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3004. $this->Form->text('Person.name');
  3005. $result = $this->Form->label();
  3006. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3007. $result = $this->Form->label('Person.first_name');
  3008. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3009. $result = $this->Form->label('Person.first_name', 'Your first name');
  3010. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3011. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3012. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3013. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3014. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3015. $result = $this->Form->label('Person.first_name', '');
  3016. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3017. $result = $this->Form->label('Person.2.name', '');
  3018. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3019. }
  3020. /**
  3021. * testTextbox method
  3022. *
  3023. * test textbox element generation
  3024. *
  3025. * @return void
  3026. */
  3027. public function testTextbox() {
  3028. $result = $this->Form->text('Model.field');
  3029. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3030. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3031. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3032. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3033. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3034. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3035. $result = $this->Form->text('Model.text');
  3036. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3037. $Contact = ClassRegistry::getObject('Contact');
  3038. $Contact->validationErrors['text'] = array(true);
  3039. $this->Form->request->data['Contact']['text'] = 'test';
  3040. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3041. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3042. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3043. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3044. $expected = array(
  3045. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3046. );
  3047. $this->assertTags($result, $expected);
  3048. }
  3049. /**
  3050. * testDefaultValue method
  3051. *
  3052. * Test default value setting
  3053. *
  3054. * @return void
  3055. */
  3056. public function testDefaultValue() {
  3057. $this->Form->request->data['Model']['field'] = 'test';
  3058. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3059. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3060. unset($this->Form->request->data['Model']['field']);
  3061. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3062. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3063. }
  3064. /**
  3065. * testCheckboxDefaultValue method
  3066. *
  3067. * Test default value setting on checkbox() method
  3068. *
  3069. * @return void
  3070. */
  3071. public function testCheckboxDefaultValue() {
  3072. $this->Form->request->data['Model']['field'] = false;
  3073. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3074. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3075. unset($this->Form->request->data['Model']['field']);
  3076. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3077. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3078. $this->Form->request->data['Model']['field'] = true;
  3079. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3080. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3081. unset($this->Form->request->data['Model']['field']);
  3082. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3083. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3084. }
  3085. /**
  3086. * testError method
  3087. *
  3088. * Test field error generation
  3089. *
  3090. * @return void
  3091. */
  3092. public function testError() {
  3093. $Contact = ClassRegistry::getObject('Contact');
  3094. $Contact->validationErrors['field'] = array(1);
  3095. $result = $this->Form->error('Contact.field');
  3096. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3097. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3098. $this->assertEquals('Error in field Field', $result);
  3099. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3100. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3101. $this->assertEquals('This field contains invalid input', $result);
  3102. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3103. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3104. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3105. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3106. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3107. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3108. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3109. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3110. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3111. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3112. $this->assertEquals('<strong>Badness!</strong>', $result);
  3113. $Contact->validationErrors['field'] = array("email");
  3114. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3115. $expected = array(
  3116. 'div' => array('class' => 'field-error'),
  3117. 'No good!',
  3118. '/div'
  3119. );
  3120. $this->assertTags($result, $expected);
  3121. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3122. $result = $this->Form->error('Contact.field', array(
  3123. 'notEmpty' => 'Cannot be empty',
  3124. 'email' => 'No good!'
  3125. ));
  3126. $expected = array(
  3127. 'div' => array('class' => 'error-message'),
  3128. 'ul' => array(),
  3129. '<li', 'Cannot be empty', '/li',
  3130. '<li', 'No good!', '/li',
  3131. '<li', 'Something else', '/li',
  3132. '/ul',
  3133. '/div'
  3134. );
  3135. $this->assertTags($result, $expected);
  3136. // Testing error messages list options
  3137. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3138. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3139. $expected = array(
  3140. 'div' => array('class' => 'error-message'),
  3141. 'ol' => array(),
  3142. '<li', 'notEmpty', '/li',
  3143. '<li', 'email', '/li',
  3144. '/ol',
  3145. '/div'
  3146. );
  3147. $this->assertTags($result, $expected);
  3148. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3149. $expected = array(
  3150. 'div' => array('class' => 'error-message'),
  3151. 'ol' => array(),
  3152. '<li', 'notEmpty', '/li',
  3153. '<li', 'email', '/li',
  3154. '/ol',
  3155. '/div'
  3156. );
  3157. $this->assertTags($result, $expected);
  3158. $result = $this->Form->error('Contact.field', null, array(
  3159. 'listOptions' => array(
  3160. 'class' => 'ul-class',
  3161. 'itemOptions' => array(
  3162. 'class' => 'li-class'
  3163. )
  3164. )
  3165. ));
  3166. $expected = array(
  3167. 'div' => array('class' => 'error-message'),
  3168. 'ul' => array('class' => 'ul-class'),
  3169. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3170. array('li' => array('class' => 'li-class')), 'email', '/li',
  3171. '/ul',
  3172. '/div'
  3173. );
  3174. $this->assertTags($result, $expected);
  3175. }
  3176. /**
  3177. * test error options when using form->input();
  3178. *
  3179. * @return void
  3180. */
  3181. public function testInputErrorEscape() {
  3182. $this->Form->create('ValidateProfile');
  3183. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3184. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3185. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3186. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3187. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3188. $this->assertRegExp('/required<br>/', $result);
  3189. }
  3190. /**
  3191. * testPassword method
  3192. *
  3193. * Test password element generation
  3194. *
  3195. * @return void
  3196. */
  3197. public function testPassword() {
  3198. $Contact = ClassRegistry::getObject('Contact');
  3199. $result = $this->Form->password('Contact.field');
  3200. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3201. $Contact->validationErrors['passwd'] = 1;
  3202. $this->Form->request->data['Contact']['passwd'] = 'test';
  3203. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3204. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3205. }
  3206. /**
  3207. * testRadio method
  3208. *
  3209. * Test radio element set generation
  3210. *
  3211. * @return void
  3212. */
  3213. public function testRadio() {
  3214. $result = $this->Form->radio('Model.field', array('option A'));
  3215. $expected = array(
  3216. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3217. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3218. 'label' => array('for' => 'ModelField0'),
  3219. 'option A',
  3220. '/label'
  3221. );
  3222. $this->assertTags($result, $expected);
  3223. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3224. $expected = array(
  3225. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3226. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField1/2')),
  3227. 'label' => array('for' => 'ModelField1/2'),
  3228. 'half',
  3229. '/label'
  3230. );
  3231. $this->assertTags($result, $expected);
  3232. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3233. $expected = array(
  3234. 'fieldset' => array(),
  3235. 'legend' => array(),
  3236. 'Field',
  3237. '/legend',
  3238. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3240. array('label' => array('for' => 'ModelField0')),
  3241. 'option A',
  3242. '/label',
  3243. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3244. array('label' => array('for' => 'ModelField1')),
  3245. 'option B',
  3246. '/label',
  3247. '/fieldset'
  3248. );
  3249. $this->assertTags($result, $expected);
  3250. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3251. $expected = array(
  3252. 'fieldset' => array(),
  3253. 'legend' => array(),
  3254. 'Field',
  3255. '/legend',
  3256. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3257. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3258. array('label' => array('for' => 'ModelField0')),
  3259. 'option A',
  3260. '/label',
  3261. 'br' => array(),
  3262. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3263. array('label' => array('for' => 'ModelField1')),
  3264. 'option B',
  3265. '/label',
  3266. '/fieldset'
  3267. );
  3268. $this->assertTags($result, $expected);
  3269. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3270. $expected = array(
  3271. 'fieldset' => array(),
  3272. 'legend' => array(),
  3273. 'Gender',
  3274. '/legend',
  3275. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3276. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3277. array('label' => array('for' => 'EmployeeGenderMale')),
  3278. 'Male',
  3279. '/label',
  3280. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3281. array('label' => array('for' => 'EmployeeGenderFemale')),
  3282. 'Female',
  3283. '/label',
  3284. '/fieldset',
  3285. );
  3286. $this->assertTags($result, $expected);
  3287. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3288. $expected = array(
  3289. 'fieldset' => array(),
  3290. 'legend' => array(),
  3291. 'Gender',
  3292. '/legend',
  3293. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3294. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3295. array('label' => array('for' => 'OfficerGenderMale')),
  3296. 'Male',
  3297. '/label',
  3298. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3299. array('label' => array('for' => 'OfficerGenderFemale')),
  3300. 'Female',
  3301. '/label',
  3302. '/fieldset',
  3303. );
  3304. $this->assertTags($result, $expected);
  3305. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3306. $expected = array(
  3307. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3308. array('input' => array(
  3309. 'type' => 'radio',
  3310. 'name' => 'data[Contact][1][imrequired]',
  3311. 'value' => '0',
  3312. 'id' => 'Contact1Imrequired0',
  3313. 'required' => 'required'
  3314. )),
  3315. 'label' => array('for' => 'Contact1Imrequired0'),
  3316. 'option A',
  3317. '/label'
  3318. );
  3319. $this->assertTags($result, $expected);
  3320. $result = $this->Form->radio('Model.1.field', array('option A'));
  3321. $expected = array(
  3322. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3323. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3324. 'label' => array('for' => 'Model1Field0'),
  3325. 'option A',
  3326. '/label'
  3327. );
  3328. $this->assertTags($result, $expected);
  3329. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3330. $expected = array(
  3331. 'fieldset' => array(),
  3332. 'legend' => array(),
  3333. 'Field',
  3334. '/legend',
  3335. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3336. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3337. array('label' => array('for' => 'ModelField0')),
  3338. 'option A',
  3339. '/label',
  3340. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3341. array('label' => array('for' => 'ModelField1')),
  3342. 'option B',
  3343. '/label',
  3344. '/fieldset'
  3345. );
  3346. $this->assertTags($result, $expected);
  3347. $result = $this->Form->radio(
  3348. 'Model.field',
  3349. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3350. );
  3351. $expected = array(
  3352. 'fieldset' => array(),
  3353. 'legend' => array(),
  3354. 'Field',
  3355. '/legend',
  3356. 'input' => array(
  3357. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3358. 'id' => 'ModelField_', 'value' => '',
  3359. ),
  3360. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3361. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3362. array('label' => array('for' => 'ModelFieldAB')),
  3363. 'first',
  3364. '/label',
  3365. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3366. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3367. array('label' => array('for' => 'ModelFieldAB1')),
  3368. 'second',
  3369. '/label',
  3370. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3371. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3372. array('label' => array('for' => 'ModelFieldAB2')),
  3373. 'third',
  3374. '/label',
  3375. '/fieldset'
  3376. );
  3377. $this->assertTags($result, $expected);
  3378. }
  3379. /**
  3380. * Test radio inputs with between as string or array. Also ensure
  3381. * that an array with less between elements works.
  3382. *
  3383. * @return void
  3384. */
  3385. public function testRadioBetween() {
  3386. $result = $this->Form->radio(
  3387. 'Model.field',
  3388. array('option A', 'option B'),
  3389. array('between' => 'I am between')
  3390. );
  3391. $expected = array(
  3392. 'fieldset' => array(),
  3393. 'legend' => array(),
  3394. 'Field',
  3395. '/legend',
  3396. 'I am between',
  3397. 'input' => array(
  3398. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3399. 'value' => '', 'id' => 'ModelField_'
  3400. ),
  3401. array('input' => array(
  3402. 'type' => 'radio', 'name' => 'data[Model][field]',
  3403. 'value' => '0', 'id' => 'ModelField0'
  3404. )),
  3405. array('label' => array('for' => 'ModelField0')),
  3406. 'option A',
  3407. '/label',
  3408. array('input' => array(
  3409. 'type' => 'radio', 'name' => 'data[Model][field]',
  3410. 'value' => '1', 'id' => 'ModelField1'
  3411. )),
  3412. array('label' => array('for' => 'ModelField1')),
  3413. 'option B',
  3414. '/label',
  3415. '/fieldset'
  3416. );
  3417. $this->assertTags($result, $expected);
  3418. $result = $this->Form->radio(
  3419. 'Model.field',
  3420. array('option A', 'option B', 'option C'),
  3421. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3422. );
  3423. $expected = array(
  3424. 'fieldset' => array(),
  3425. 'legend' => array(),
  3426. 'Field',
  3427. '/legend',
  3428. 'input' => array(
  3429. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3430. 'value' => '', 'id' => 'ModelField_'
  3431. ),
  3432. array('input' => array(
  3433. 'type' => 'radio', 'name' => 'data[Model][field]',
  3434. 'value' => '0', 'id' => 'ModelField0'
  3435. )),
  3436. array('label' => array('for' => 'ModelField0')),
  3437. 'option A',
  3438. '/label',
  3439. 'between A',
  3440. '--separator--',
  3441. array('input' => array(
  3442. 'type' => 'radio', 'name' => 'data[Model][field]',
  3443. 'value' => '1', 'id' => 'ModelField1'
  3444. )),
  3445. array('label' => array('for' => 'ModelField1')),
  3446. 'option B',
  3447. '/label',
  3448. 'between B',
  3449. '--separator--',
  3450. array('input' => array(
  3451. 'type' => 'radio', 'name' => 'data[Model][field]',
  3452. 'value' => '2', 'id' => 'ModelField2'
  3453. )),
  3454. array('label' => array('for' => 'ModelField2')),
  3455. 'option C',
  3456. '/label',
  3457. 'between C',
  3458. '/fieldset'
  3459. );
  3460. $this->assertTags($result, $expected);
  3461. $result = $this->Form->input('Model.field', array(
  3462. 'options' => array('1' => 'first', '2' => 'second'),
  3463. 'type' => 'radio',
  3464. 'before' => '--before--',
  3465. 'after' => '--after--',
  3466. 'separator' => '--separator--',
  3467. 'between' => array('--between first--', '--between second--')
  3468. ));
  3469. $expected = array(
  3470. 'div' => array('class' => 'input radio'),
  3471. '--before--',
  3472. 'fieldset' => array(),
  3473. 'legend' => array(),
  3474. 'Field',
  3475. '/legend',
  3476. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3477. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3478. array('label' => array('for' => 'ModelField1')),
  3479. 'first',
  3480. '/label',
  3481. '--between first--',
  3482. '--separator--',
  3483. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3484. array('label' => array('for' => 'ModelField2')),
  3485. 'second',
  3486. '/label',
  3487. '--between second--',
  3488. '/fieldset',
  3489. '--after--',
  3490. '/div'
  3491. );
  3492. $this->assertTags($result, $expected);
  3493. $result = $this->Form->input('Model.field', array(
  3494. 'options' => array('1' => 'first', '2' => 'second'),
  3495. 'type' => 'radio',
  3496. 'before' => '--before--',
  3497. 'after' => '--after--',
  3498. 'separator' => '--separator--',
  3499. 'between' => array('--between first--')
  3500. ));
  3501. $expected = array(
  3502. 'div' => array('class' => 'input radio'),
  3503. '--before--',
  3504. 'fieldset' => array(),
  3505. 'legend' => array(),
  3506. 'Field',
  3507. '/legend',
  3508. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3509. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3510. array('label' => array('for' => 'ModelField1')),
  3511. 'first',
  3512. '/label',
  3513. '--between first--',
  3514. '--separator--',
  3515. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3516. array('label' => array('for' => 'ModelField2')),
  3517. 'second',
  3518. '/label',
  3519. '/fieldset',
  3520. '--after--',
  3521. '/div'
  3522. );
  3523. $this->assertTags($result, $expected);
  3524. }
  3525. /**
  3526. * Test that radios with a 0 value are selected under the correct conditions.
  3527. * Also ensure that values that are booleanish are handled correctly.
  3528. *
  3529. * @return void
  3530. */
  3531. public function testRadioOptionWithBooleanishValues() {
  3532. $expected = array(
  3533. 'fieldset' => array(),
  3534. 'legend' => array(),
  3535. 'Field',
  3536. '/legend',
  3537. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3538. array('label' => array('for' => 'ModelField1')),
  3539. 'Yes',
  3540. '/label',
  3541. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3542. array('label' => array('for' => 'ModelField0')),
  3543. 'No',
  3544. '/label',
  3545. '/fieldset'
  3546. );
  3547. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3548. $this->assertTags($result, $expected);
  3549. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3550. $this->assertTags($result, $expected);
  3551. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3552. $this->assertTags($result, $expected);
  3553. $expected = array(
  3554. 'fieldset' => array(),
  3555. 'legend' => array(),
  3556. 'Field',
  3557. '/legend',
  3558. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3559. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3560. array('label' => array('for' => 'ModelField1')),
  3561. 'Yes',
  3562. '/label',
  3563. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3564. array('label' => array('for' => 'ModelField0')),
  3565. 'No',
  3566. '/label',
  3567. '/fieldset'
  3568. );
  3569. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3570. $this->assertTags($result, $expected);
  3571. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3572. $this->assertTags($result, $expected);
  3573. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3574. $this->assertTags($result, $expected);
  3575. $expected = array(
  3576. 'fieldset' => array(),
  3577. 'legend' => array(),
  3578. 'Field',
  3579. '/legend',
  3580. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3581. array('label' => array('for' => 'ModelField1')),
  3582. 'Yes',
  3583. '/label',
  3584. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3585. array('label' => array('for' => 'ModelField0')),
  3586. 'No',
  3587. '/label',
  3588. '/fieldset'
  3589. );
  3590. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3591. $this->assertTags($result, $expected);
  3592. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3593. $this->assertTags($result, $expected);
  3594. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3595. $this->assertTags($result, $expected);
  3596. }
  3597. /**
  3598. * test disabled radio options
  3599. *
  3600. * @return void
  3601. */
  3602. public function testRadioDisabled() {
  3603. $result = $this->Form->radio(
  3604. 'Model.field',
  3605. array('option A', 'option B'),
  3606. array('disabled' => array(0), 'value' => '0')
  3607. );
  3608. $expected = array(
  3609. 'fieldset' => array(),
  3610. 'legend' => array(),
  3611. 'Field',
  3612. '/legend',
  3613. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3614. array('label' => array('for' => 'ModelField0')),
  3615. 'option A',
  3616. '/label',
  3617. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3618. array('label' => array('for' => 'ModelField1')),
  3619. 'option B',
  3620. '/label',
  3621. '/fieldset'
  3622. );
  3623. $this->assertTags($result, $expected);
  3624. $result = $this->Form->radio(
  3625. 'Model.field',
  3626. array('option A', 'option B'),
  3627. array('disabled' => true, 'value' => '0')
  3628. );
  3629. $expected = array(
  3630. 'fieldset' => array(),
  3631. 'legend' => array(),
  3632. 'Field',
  3633. '/legend',
  3634. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3635. array('label' => array('for' => 'ModelField0')),
  3636. 'option A',
  3637. '/label',
  3638. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3639. array('label' => array('for' => 'ModelField1')),
  3640. 'option B',
  3641. '/label',
  3642. '/fieldset'
  3643. );
  3644. $this->assertTags($result, $expected);
  3645. $result = $this->Form->radio(
  3646. 'Model.field',
  3647. array('option A', 'option B'),
  3648. array('disabled' => 'disabled', 'value' => '0')
  3649. );
  3650. $expected = array(
  3651. 'fieldset' => array(),
  3652. 'legend' => array(),
  3653. 'Field',
  3654. '/legend',
  3655. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3656. array('label' => array('for' => 'ModelField0')),
  3657. 'option A',
  3658. '/label',
  3659. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3660. array('label' => array('for' => 'ModelField1')),
  3661. 'option B',
  3662. '/label',
  3663. '/fieldset'
  3664. );
  3665. $this->assertTags($result, $expected);
  3666. $result = $this->Form->input('Model.field', array(
  3667. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3668. 'type' => 'radio',
  3669. 'disabled' => array(2, '3x'),
  3670. ));
  3671. $expected = array(
  3672. 'div' => array('class' => 'input radio'),
  3673. 'fieldset' => array(),
  3674. 'legend' => array(),
  3675. 'Field',
  3676. '/legend',
  3677. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3678. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3679. array('label' => array('for' => 'ModelField1')),
  3680. 'first',
  3681. '/label',
  3682. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3683. array('label' => array('for' => 'ModelField2')),
  3684. 'second',
  3685. '/label',
  3686. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3687. array('label' => array('for' => 'ModelField2x')),
  3688. '2x',
  3689. '/label',
  3690. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3691. array('label' => array('for' => 'ModelField3')),
  3692. 'third',
  3693. '/label',
  3694. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3695. array('label' => array('for' => 'ModelField3x')),
  3696. '3x',
  3697. '/label',
  3698. '/fieldset',
  3699. '/div'
  3700. );
  3701. $this->assertTags($result, $expected);
  3702. $result = $this->Form->input('Model.field', array(
  3703. 'type' => 'radio',
  3704. 'options' => array(
  3705. 1 => 'A',
  3706. 2 => 'B',
  3707. 3 => 'C'
  3708. ),
  3709. 'disabled' => array(1)
  3710. ));
  3711. $expected = array(
  3712. 'div' => array('class' => 'input radio'),
  3713. 'fieldset' => array(),
  3714. 'legend' => array(),
  3715. 'Field',
  3716. '/legend',
  3717. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3718. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3719. array('label' => array('for' => 'ModelField1')),
  3720. 'A',
  3721. '/label',
  3722. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3723. array('label' => array('for' => 'ModelField2')),
  3724. 'B',
  3725. '/label',
  3726. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3727. array('label' => array('for' => 'ModelField3')),
  3728. 'C',
  3729. '/label',
  3730. '/fieldset',
  3731. '/div'
  3732. );
  3733. $this->assertTags($result, $expected);
  3734. }
  3735. /**
  3736. * test disabling the hidden input for radio buttons
  3737. *
  3738. * @return void
  3739. */
  3740. public function testRadioHiddenInputDisabling() {
  3741. $result = $this->Form->input('Model.1.field', array(
  3742. 'type' => 'radio',
  3743. 'options' => array('option A'),
  3744. 'hiddenField' => false
  3745. )
  3746. );
  3747. $expected = array(
  3748. 'div' => array('class' => 'input radio'),
  3749. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3750. 'label' => array('for' => 'Model1Field0'),
  3751. 'option A',
  3752. '/label',
  3753. '/div'
  3754. );
  3755. $this->assertTags($result, $expected);
  3756. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3757. $expected = array(
  3758. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3759. 'label' => array('for' => 'Model1Field0'),
  3760. 'option A',
  3761. '/label'
  3762. );
  3763. $this->assertTags($result, $expected);
  3764. }
  3765. /**
  3766. * test adding an empty option for radio buttons
  3767. *
  3768. * @return void
  3769. */
  3770. public function testRadioAddEmptyOption() {
  3771. $result = $this->Form->input('Model.1.field', array(
  3772. 'type' => 'radio',
  3773. 'options' => array('option A'),
  3774. 'empty' => true,
  3775. 'hiddenField' => false
  3776. ));
  3777. $expected = array(
  3778. 'div' => array('class' => 'input radio'),
  3779. 'fieldset' => array(),
  3780. 'legend' => array(),
  3781. 'Field',
  3782. '/legend',
  3783. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3784. array('label' => array('for' => 'Model1Field')),
  3785. __('empty'),
  3786. '/label',
  3787. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3788. array('label' => array('for' => 'Model1Field0')),
  3789. 'option A',
  3790. '/label',
  3791. '/fieldset',
  3792. '/div'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $result = $this->Form->input('Model.1.field', array(
  3796. 'type' => 'radio',
  3797. 'options' => array('option A'),
  3798. 'empty' => 'CustomEmptyLabel',
  3799. 'hiddenField' => false
  3800. ));
  3801. $expected = array(
  3802. 'div' => array('class' => 'input radio'),
  3803. 'fieldset' => array(),
  3804. 'legend' => array(),
  3805. 'Field',
  3806. '/legend',
  3807. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3808. array('label' => array('for' => 'Model1Field')),
  3809. 'CustomEmptyLabel',
  3810. '/label',
  3811. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3812. array('label' => array('for' => 'Model1Field0')),
  3813. 'option A',
  3814. '/label',
  3815. '/fieldset',
  3816. '/div'
  3817. );
  3818. $this->assertTags($result, $expected);
  3819. $result = $this->Form->input('Model.1.field', array(
  3820. 'type' => 'radio',
  3821. 'options' => array('option A'),
  3822. 'empty' => false,
  3823. 'hiddenField' => false
  3824. ));
  3825. $this->assertTextNotContains('"Model1Field"', $result);
  3826. }
  3827. /**
  3828. * Test that radio() accepts an array for label
  3829. *
  3830. * @return void
  3831. */
  3832. public function testRadioLabelArray() {
  3833. $result = $this->Form->input('Model.field', array(
  3834. 'type' => 'radio',
  3835. 'legend' => false,
  3836. 'label' => array(
  3837. 'class' => 'checkbox float-left',
  3838. ),
  3839. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3840. ));
  3841. $this->assertTextContains(
  3842. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3843. $result
  3844. );
  3845. }
  3846. /**
  3847. * Test that label id's match the input element id's when radio is called after create().
  3848. *
  3849. * @return void
  3850. */
  3851. public function testRadioWithCreate() {
  3852. $this->Form->create('Model');
  3853. $result = $this->Form->radio('recipient',
  3854. array('1' => '1', '2' => '2', '3' => '3'),
  3855. array('legend' => false, 'value' => '1')
  3856. );
  3857. $this->assertTextNotContains(
  3858. '<label for="ModelModelRecipient1">1</label>',
  3859. $result
  3860. );
  3861. $this->assertTextContains(
  3862. '<label for="ModelRecipient1">1</label>',
  3863. $result
  3864. );
  3865. }
  3866. /**
  3867. * testDomIdSuffix method
  3868. *
  3869. * @return void
  3870. */
  3871. public function testDomIdSuffix() {
  3872. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3873. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3874. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3875. $this->assertEquals('AbcX=FooY=Bar', $result);
  3876. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3877. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3878. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3879. $this->assertEquals('AbcXFooYBar', $result);
  3880. }
  3881. /**
  3882. * testDomIdSuffixCollisionResolvement()
  3883. *
  3884. * @return void
  3885. */
  3886. public function testDomIdSuffixCollisionResolvement() {
  3887. $result = $this->Form->domIdSuffix('a>b');
  3888. $this->assertEquals('AB', $result);
  3889. $result = $this->Form->domIdSuffix('a<b');
  3890. $this->assertEquals('AB1', $result);
  3891. $result = $this->Form->domIdSuffix('a\'b');
  3892. $this->assertEquals('AB2', $result);
  3893. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3894. $this->assertEquals('1StringWith1-dollar', $result);
  3895. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3896. $this->assertEquals('1StringWith1-dollar1', $result);
  3897. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3898. $this->assertEquals('1StringWith1-dollar2', $result);
  3899. }
  3900. /**
  3901. * testSelect method
  3902. *
  3903. * Test select element generation.
  3904. *
  3905. * @return void
  3906. */
  3907. public function testSelect() {
  3908. $result = $this->Form->select('Model.field', array());
  3909. $expected = array(
  3910. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3911. array('option' => array('value' => '')),
  3912. '/option',
  3913. '/select'
  3914. );
  3915. $this->assertTags($result, $expected);
  3916. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3917. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3918. $expected = array(
  3919. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3920. array('option' => array('value' => '')),
  3921. '/option',
  3922. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3923. 'good',
  3924. '/option',
  3925. array('option' => array('value' => 'other')),
  3926. 'bad',
  3927. '/option',
  3928. '/select'
  3929. );
  3930. $this->assertTags($result, $expected);
  3931. $this->Form->request->data = array();
  3932. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3933. $expected = array(
  3934. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3935. array('option' => array('value' => '')),
  3936. '/option',
  3937. array('option' => array('value' => 'value')),
  3938. 'good',
  3939. '/option',
  3940. array('option' => array('value' => 'other')),
  3941. 'bad',
  3942. '/option',
  3943. '/select'
  3944. );
  3945. $this->assertTags($result, $expected);
  3946. $result = $this->Form->select(
  3947. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3948. array('empty' => false)
  3949. );
  3950. $expected = array(
  3951. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3952. array('option' => array('value' => 'first')),
  3953. 'first &quot;html&quot; &lt;chars&gt;',
  3954. '/option',
  3955. array('option' => array('value' => 'second')),
  3956. 'value',
  3957. '/option',
  3958. '/select'
  3959. );
  3960. $this->assertTags($result, $expected);
  3961. $result = $this->Form->select(
  3962. 'Model.field',
  3963. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3964. array('escape' => false, 'empty' => false)
  3965. );
  3966. $expected = array(
  3967. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3968. array('option' => array('value' => 'first')),
  3969. 'first "html" <chars>',
  3970. '/option',
  3971. array('option' => array('value' => 'second')),
  3972. 'value',
  3973. '/option',
  3974. '/select'
  3975. );
  3976. $this->assertTags($result, $expected);
  3977. $options = array(
  3978. array('value' => 'first', 'name' => 'First'),
  3979. array('value' => 'first', 'name' => 'Another First'),
  3980. );
  3981. $result = $this->Form->select(
  3982. 'Model.field',
  3983. $options,
  3984. array('escape' => false, 'empty' => false)
  3985. );
  3986. $expected = array(
  3987. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3988. array('option' => array('value' => 'first')),
  3989. 'First',
  3990. '/option',
  3991. array('option' => array('value' => 'first')),
  3992. 'Another First',
  3993. '/option',
  3994. '/select'
  3995. );
  3996. $this->assertTags($result, $expected);
  3997. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3998. $result = $this->Form->select(
  3999. 'Model.contact_id',
  4000. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4001. array('escape' => false, 'empty' => 'pick something')
  4002. );
  4003. $expected = array(
  4004. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4005. array('option' => array('value' => '')), 'pick something', '/option',
  4006. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4007. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4008. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4009. '/select'
  4010. );
  4011. $this->assertTags($result, $expected);
  4012. $this->Form->request->data['Model']['field'] = 0;
  4013. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4014. $expected = array(
  4015. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4016. array('option' => array('value' => '')), '/option',
  4017. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4018. array('option' => array('value' => '1')), 'Yes', '/option',
  4019. '/select'
  4020. );
  4021. $this->assertTags($result, $expected);
  4022. $this->Form->request->data['Model']['field'] = 50;
  4023. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4024. $expected = array(
  4025. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4026. array('option' => array('value' => '')), '/option',
  4027. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4028. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4029. '/select'
  4030. );
  4031. $this->assertTags($result, $expected);
  4032. $result = $this->Form->select('Contact.required_one', array('option A'));
  4033. $expected = array(
  4034. 'select' => array(
  4035. 'name' => 'data[Contact][required_one]',
  4036. 'id' => 'ContactRequiredOne',
  4037. 'required' => 'required'
  4038. ),
  4039. array('option' => array('value' => '')), '/option',
  4040. array('option' => array('value' => '0')), 'option A', '/option',
  4041. '/select'
  4042. );
  4043. $this->assertTags($result, $expected);
  4044. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4045. $expected = array(
  4046. 'select' => array(
  4047. 'name' => 'data[Contact][required_one]',
  4048. 'id' => 'ContactRequiredOne',
  4049. 'disabled' => 'disabled'
  4050. ),
  4051. array('option' => array('value' => '')), '/option',
  4052. array('option' => array('value' => '0')), 'option A', '/option',
  4053. '/select'
  4054. );
  4055. $this->assertTags($result, $expected);
  4056. }
  4057. /**
  4058. * test that select() with optiongroups listens to the escape param.
  4059. *
  4060. * @return void
  4061. */
  4062. public function testSelectOptionGroupEscaping() {
  4063. $options = array(
  4064. '>< Key' => array(
  4065. 1 => 'One',
  4066. 2 => 'Two'
  4067. )
  4068. );
  4069. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4070. $expected = array(
  4071. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4072. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4073. array('option' => array('value' => '1')), 'One', '/option',
  4074. array('option' => array('value' => '2')), 'Two', '/option',
  4075. '/optgroup',
  4076. '/select'
  4077. );
  4078. $this->assertTags($result, $expected);
  4079. $options = array(
  4080. '>< Key' => array(
  4081. 1 => 'One',
  4082. 2 => 'Two'
  4083. )
  4084. );
  4085. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4086. $expected = array(
  4087. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4088. 'optgroup' => array('label' => '>< Key'),
  4089. array('option' => array('value' => '1')), 'One', '/option',
  4090. array('option' => array('value' => '2')), 'Two', '/option',
  4091. '/optgroup',
  4092. '/select'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. }
  4096. /**
  4097. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4098. *
  4099. * @return void
  4100. */
  4101. public function testSelectWithNullAttributes() {
  4102. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4103. $expected = array(
  4104. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4105. array('option' => array('value' => '0')),
  4106. 'first',
  4107. '/option',
  4108. array('option' => array('value' => '1')),
  4109. 'second',
  4110. '/option',
  4111. '/select'
  4112. );
  4113. $this->assertTags($result, $expected);
  4114. }
  4115. /**
  4116. * testNestedSelect method
  4117. *
  4118. * test select element generation with optgroups
  4119. *
  4120. * @return void
  4121. */
  4122. public function testNestedSelect() {
  4123. $result = $this->Form->select(
  4124. 'Model.field',
  4125. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4126. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4127. )), array('empty' => false)
  4128. );
  4129. $expected = array(
  4130. 'select' => array('name' => 'data[Model][field]',
  4131. 'id' => 'ModelField'),
  4132. array('option' => array('value' => 1)),
  4133. 'One',
  4134. '/option',
  4135. array('option' => array('value' => 2)),
  4136. 'Two',
  4137. '/option',
  4138. array('optgroup' => array('label' => 'Three')),
  4139. array('option' => array('value' => 4)),
  4140. 'Four',
  4141. '/option',
  4142. array('option' => array('value' => 5)),
  4143. 'Five',
  4144. '/option',
  4145. '/optgroup',
  4146. '/select'
  4147. );
  4148. $this->assertTags($result, $expected);
  4149. $result = $this->Form->select(
  4150. 'Model.field',
  4151. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4152. array('showParents' => true, 'empty' => false)
  4153. );
  4154. $expected = array(
  4155. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4156. array('option' => array('value' => 1)),
  4157. 'One',
  4158. '/option',
  4159. array('option' => array('value' => 2)),
  4160. 'Two',
  4161. '/option',
  4162. array('optgroup' => array('label' => 'Three')),
  4163. array('option' => array('value' => 3)),
  4164. 'Three',
  4165. '/option',
  4166. array('option' => array('value' => 4)),
  4167. 'Four',
  4168. '/option',
  4169. '/optgroup',
  4170. '/select'
  4171. );
  4172. $this->assertTags($result, $expected);
  4173. }
  4174. /**
  4175. * testSelectMultiple method
  4176. *
  4177. * test generation of multiple select elements
  4178. *
  4179. * @return void
  4180. */
  4181. public function testSelectMultiple() {
  4182. $options = array('first', 'second', 'third');
  4183. $result = $this->Form->select(
  4184. 'Model.multi_field', $options, array('multiple' => true)
  4185. );
  4186. $expected = array(
  4187. 'input' => array(
  4188. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4189. ),
  4190. 'select' => array(
  4191. 'name' => 'data[Model][multi_field][]',
  4192. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4193. ),
  4194. array('option' => array('value' => '0')),
  4195. 'first',
  4196. '/option',
  4197. array('option' => array('value' => '1')),
  4198. 'second',
  4199. '/option',
  4200. array('option' => array('value' => '2')),
  4201. 'third',
  4202. '/option',
  4203. '/select'
  4204. );
  4205. $this->assertTags($result, $expected);
  4206. $result = $this->Form->select(
  4207. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4208. );
  4209. $expected = array(
  4210. 'input' => array(
  4211. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4212. ),
  4213. 'select' => array(
  4214. 'name' => 'data[Model][multi_field][]',
  4215. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4216. ),
  4217. array('option' => array('value' => '0')),
  4218. 'first',
  4219. '/option',
  4220. array('option' => array('value' => '1')),
  4221. 'second',
  4222. '/option',
  4223. array('option' => array('value' => '2')),
  4224. 'third',
  4225. '/option',
  4226. '/select'
  4227. );
  4228. $this->assertTags($result, $expected);
  4229. $result = $this->Form->select(
  4230. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4231. );
  4232. $expected = array(
  4233. 'input' => array(
  4234. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4235. ),
  4236. 'select' => array(
  4237. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4238. 'multiple' => 'multiple'
  4239. ),
  4240. array('option' => array('value' => '0', 'selected' => 'selected')),
  4241. 'first',
  4242. '/option',
  4243. array('option' => array('value' => '1', 'selected' => 'selected')),
  4244. 'second',
  4245. '/option',
  4246. array('option' => array('value' => '2')),
  4247. 'third',
  4248. '/option',
  4249. '/select'
  4250. );
  4251. $this->assertTags($result, $expected);
  4252. $result = $this->Form->select(
  4253. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4254. );
  4255. $expected = array(
  4256. 'select' => array(
  4257. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4258. ),
  4259. array('option' => array('value' => '0', 'selected' => 'selected')),
  4260. 'first',
  4261. '/option',
  4262. array('option' => array('value' => '1', 'selected' => 'selected')),
  4263. 'second',
  4264. '/option',
  4265. array('option' => array('value' => '2')),
  4266. 'third',
  4267. '/option',
  4268. '/select'
  4269. );
  4270. $this->assertTags($result, $expected);
  4271. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4272. $selected = array('2', '3x');
  4273. $result = $this->Form->select(
  4274. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4275. );
  4276. $expected = array(
  4277. 'input' => array(
  4278. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4279. ),
  4280. 'select' => array(
  4281. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4282. ),
  4283. array('option' => array('value' => '1')),
  4284. 'One',
  4285. '/option',
  4286. array('option' => array('value' => '2', 'selected' => 'selected')),
  4287. 'Two',
  4288. '/option',
  4289. array('option' => array('value' => '3')),
  4290. 'Three',
  4291. '/option',
  4292. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4293. 'Stringy',
  4294. '/option',
  4295. '/select'
  4296. );
  4297. $this->assertTags($result, $expected);
  4298. $result = $this->Form->select('Contact.required_one', array(
  4299. '1' => 'option A',
  4300. '2' => 'option B'
  4301. ), array('multiple' => true));
  4302. $expected = array(
  4303. 'input' => array(
  4304. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4305. ),
  4306. 'select' => array(
  4307. 'name' => 'data[Contact][required_one][]',
  4308. 'id' => 'ContactRequiredOne',
  4309. 'required' => 'required',
  4310. 'multiple' => 'multiple'
  4311. ),
  4312. array('option' => array('value' => '1')), 'option A', '/option',
  4313. array('option' => array('value' => '2')), 'option B', '/option',
  4314. '/select'
  4315. );
  4316. $this->assertTags($result, $expected);
  4317. $result = $this->Form->select(
  4318. 'Model.multi_field',
  4319. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4320. array('multiple' => true)
  4321. );
  4322. $expected = array(
  4323. 'input' => array(
  4324. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4325. 'id' => 'ModelMultiField_'
  4326. ),
  4327. array('select' => array('name' => 'data[Model][multi_field][]',
  4328. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4329. )),
  4330. array('option' => array('value' => 'a&gt;b')),
  4331. 'first',
  4332. '/option',
  4333. array('option' => array('value' => 'a&lt;b')),
  4334. 'second',
  4335. '/option',
  4336. array('option' => array(
  4337. 'value' => 'a&quot;b'
  4338. )),
  4339. 'third',
  4340. '/option',
  4341. '/select'
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. }
  4345. /**
  4346. * Test generating multiple select with disabled elements.
  4347. *
  4348. * @return void
  4349. */
  4350. public function testSelectMultipleWithDisabledElements() {
  4351. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4352. $disabled = array(2, 3);
  4353. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4354. $expected = array(
  4355. 'input' => array(
  4356. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4357. ),
  4358. 'select' => array(
  4359. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4360. ),
  4361. array('option' => array('value' => '1')),
  4362. 'One',
  4363. '/option',
  4364. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4365. 'Two',
  4366. '/option',
  4367. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4368. 'Three',
  4369. '/option',
  4370. array('option' => array('value' => '3x')),
  4371. 'Stringy',
  4372. '/option',
  4373. '/select'
  4374. );
  4375. $this->assertTags($result, $expected);
  4376. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4377. $disabled = array('2', '3x');
  4378. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4379. $expected = array(
  4380. array('div' => array('class' => 'input select')),
  4381. array('label' => array('for' => 'ContactMultiple')),
  4382. 'Multiple',
  4383. '/label',
  4384. 'input' => array(
  4385. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4386. ),
  4387. 'select' => array(
  4388. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4389. ),
  4390. array('option' => array('value' => '1')),
  4391. 'One',
  4392. '/option',
  4393. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4394. 'Two',
  4395. '/option',
  4396. array('option' => array('value' => '3')),
  4397. 'Three',
  4398. '/option',
  4399. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4400. 'Stringy',
  4401. '/option',
  4402. '/select',
  4403. '/div'
  4404. );
  4405. $this->assertTags($result, $expected);
  4406. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4407. $disabled = true;
  4408. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4409. $expected = array(
  4410. array('div' => array('class' => 'input select')),
  4411. array('label' => array('for' => 'ContactMultiple')),
  4412. 'Multiple',
  4413. '/label',
  4414. 'input' => array(
  4415. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4416. ),
  4417. 'select' => array(
  4418. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4419. ),
  4420. array('option' => array('value' => '1')),
  4421. 'One',
  4422. '/option',
  4423. array('option' => array('value' => '2')),
  4424. 'Two',
  4425. '/option',
  4426. array('option' => array('value' => '3')),
  4427. 'Three',
  4428. '/option',
  4429. array('option' => array('value' => '3x')),
  4430. 'Stringy',
  4431. '/option',
  4432. '/select',
  4433. '/div'
  4434. );
  4435. $this->assertTags($result, $expected);
  4436. }
  4437. /**
  4438. * Test generating select with disabled elements.
  4439. *
  4440. * @return void
  4441. */
  4442. public function testSelectWithDisabledElements() {
  4443. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4444. $disabled = array(2, 3);
  4445. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4446. $expected = array(
  4447. 'select' => array(
  4448. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4449. ),
  4450. array('option' => array('value' => '')),
  4451. '/option',
  4452. array('option' => array('value' => '1')),
  4453. 'One',
  4454. '/option',
  4455. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4456. 'Two',
  4457. '/option',
  4458. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4459. 'Three',
  4460. '/option',
  4461. array('option' => array('value' => '3x')),
  4462. 'Stringy',
  4463. '/option',
  4464. '/select'
  4465. );
  4466. $this->assertTags($result, $expected);
  4467. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4468. $disabled = array('2', '3x');
  4469. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4470. $expected = array(
  4471. array('div' => array('class' => 'input select')),
  4472. array('label' => array('for' => 'ModelField')),
  4473. 'Field',
  4474. '/label',
  4475. 'select' => array(
  4476. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4477. ),
  4478. array('option' => array('value' => '1')),
  4479. 'One',
  4480. '/option',
  4481. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4482. 'Two',
  4483. '/option',
  4484. array('option' => array('value' => '3')),
  4485. 'Three',
  4486. '/option',
  4487. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4488. 'Stringy',
  4489. '/option',
  4490. '/select',
  4491. '/div'
  4492. );
  4493. $this->assertTags($result, $expected);
  4494. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4495. $disabled = true;
  4496. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4497. $expected = array(
  4498. array('div' => array('class' => 'input select')),
  4499. array('label' => array('for' => 'ModelField')),
  4500. 'Field',
  4501. '/label',
  4502. 'select' => array(
  4503. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4504. ),
  4505. array('option' => array('value' => '1')),
  4506. 'One',
  4507. '/option',
  4508. array('option' => array('value' => '2')),
  4509. 'Two',
  4510. '/option',
  4511. array('option' => array('value' => '3')),
  4512. 'Three',
  4513. '/option',
  4514. array('option' => array('value' => '3x')),
  4515. 'Stringy',
  4516. '/option',
  4517. '/select',
  4518. '/div'
  4519. );
  4520. $this->assertTags($result, $expected);
  4521. }
  4522. /**
  4523. * test generation of habtm select boxes.
  4524. *
  4525. * @return void
  4526. */
  4527. public function testHabtmSelectBox() {
  4528. $this->View->viewVars['contactTags'] = array(
  4529. 1 => 'blue',
  4530. 2 => 'red',
  4531. 3 => 'green'
  4532. );
  4533. $this->Form->request->data = array(
  4534. 'Contact' => array(),
  4535. 'ContactTag' => array(
  4536. array(
  4537. 'id' => '1',
  4538. 'name' => 'blue'
  4539. ),
  4540. array(
  4541. 'id' => 3,
  4542. 'name' => 'green'
  4543. )
  4544. )
  4545. );
  4546. $this->Form->create('Contact');
  4547. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4548. $expected = array(
  4549. 'input' => array(
  4550. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4551. ),
  4552. 'select' => array(
  4553. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4554. 'multiple' => 'multiple'
  4555. ),
  4556. array('option' => array('value' => '1', 'selected' => 'selected')),
  4557. 'blue',
  4558. '/option',
  4559. array('option' => array('value' => '2')),
  4560. 'red',
  4561. '/option',
  4562. array('option' => array('value' => '3', 'selected' => 'selected')),
  4563. 'green',
  4564. '/option',
  4565. '/select'
  4566. );
  4567. $this->assertTags($result, $expected);
  4568. // make sure only 50 is selected, and not 50f5c0cf
  4569. $this->View->viewVars['contactTags'] = array(
  4570. '1' => 'blue',
  4571. '50f5c0cf' => 'red',
  4572. '50' => 'green'
  4573. );
  4574. $this->Form->request->data = array(
  4575. 'Contact' => array(),
  4576. 'ContactTag' => array(
  4577. array(
  4578. 'id' => 1,
  4579. 'name' => 'blue'
  4580. ),
  4581. array(
  4582. 'id' => 50,
  4583. 'name' => 'green'
  4584. )
  4585. )
  4586. );
  4587. $this->Form->create('Contact');
  4588. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4589. $expected = array(
  4590. 'input' => array(
  4591. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4592. ),
  4593. 'select' => array(
  4594. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4595. 'multiple' => 'multiple'
  4596. ),
  4597. array('option' => array('value' => '1', 'selected' => 'selected')),
  4598. 'blue',
  4599. '/option',
  4600. array('option' => array('value' => '50f5c0cf')),
  4601. 'red',
  4602. '/option',
  4603. array('option' => array('value' => '50', 'selected' => 'selected')),
  4604. 'green',
  4605. '/option',
  4606. '/select'
  4607. );
  4608. $this->assertTags($result, $expected);
  4609. }
  4610. /**
  4611. * test generation of multi select elements in checkbox format
  4612. *
  4613. * @return void
  4614. */
  4615. public function testSelectMultipleCheckboxes() {
  4616. $result = $this->Form->select(
  4617. 'Model.multi_field',
  4618. array('first', 'second', 'third'),
  4619. array('multiple' => 'checkbox')
  4620. );
  4621. $expected = array(
  4622. 'input' => array(
  4623. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4624. ),
  4625. array('div' => array('class' => 'checkbox')),
  4626. array('input' => array(
  4627. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4628. 'value' => '0', 'id' => 'ModelMultiField0'
  4629. )),
  4630. array('label' => array('for' => 'ModelMultiField0')),
  4631. 'first',
  4632. '/label',
  4633. '/div',
  4634. array('div' => array('class' => 'checkbox')),
  4635. array('input' => array(
  4636. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4637. 'value' => '1', 'id' => 'ModelMultiField1'
  4638. )),
  4639. array('label' => array('for' => 'ModelMultiField1')),
  4640. 'second',
  4641. '/label',
  4642. '/div',
  4643. array('div' => array('class' => 'checkbox')),
  4644. array('input' => array(
  4645. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4646. 'value' => '2', 'id' => 'ModelMultiField2'
  4647. )),
  4648. array('label' => array('for' => 'ModelMultiField2')),
  4649. 'third',
  4650. '/label',
  4651. '/div'
  4652. );
  4653. $this->assertTags($result, $expected);
  4654. $result = $this->Form->select(
  4655. 'Model.multi_field',
  4656. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4657. array('multiple' => 'checkbox')
  4658. );
  4659. $expected = array(
  4660. 'input' => array(
  4661. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4662. ),
  4663. array('div' => array('class' => 'checkbox')),
  4664. array('input' => array(
  4665. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4666. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4667. )),
  4668. array('label' => array('for' => 'ModelMultiFieldA')),
  4669. 'first',
  4670. '/label',
  4671. '/div',
  4672. array('div' => array('class' => 'checkbox')),
  4673. array('input' => array(
  4674. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4675. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4676. )),
  4677. array('label' => array('for' => 'ModelMultiFieldB')),
  4678. 'second',
  4679. '/label',
  4680. '/div',
  4681. array('div' => array('class' => 'checkbox')),
  4682. array('input' => array(
  4683. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4684. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4685. )),
  4686. array('label' => array('for' => 'ModelMultiFieldC')),
  4687. 'third',
  4688. '/label',
  4689. '/div'
  4690. );
  4691. $this->assertTags($result, $expected);
  4692. $result = $this->Form->select(
  4693. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4694. );
  4695. $expected = array(
  4696. 'input' => array(
  4697. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4698. ),
  4699. array('div' => array('class' => 'checkbox')),
  4700. array('input' => array(
  4701. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4702. 'value' => '1', 'id' => 'ModelMultiField1'
  4703. )),
  4704. array('label' => array('for' => 'ModelMultiField1')),
  4705. 'first',
  4706. '/label',
  4707. '/div'
  4708. );
  4709. $this->assertTags($result, $expected);
  4710. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4711. $result = $this->Form->select(
  4712. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4713. );
  4714. $expected = array(
  4715. 'input' => array(
  4716. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4717. ),
  4718. array('div' => array('class' => 'checkbox')),
  4719. array('input' => array(
  4720. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4721. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4722. )),
  4723. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4724. 'first',
  4725. '/label',
  4726. '/div',
  4727. array('div' => array('class' => 'checkbox')),
  4728. array('input' => array(
  4729. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4730. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4731. )),
  4732. array('label' => array('for' => 'ModelTagsArray')),
  4733. 'Array',
  4734. '/label',
  4735. '/div'
  4736. );
  4737. $this->assertTags($result, $expected);
  4738. $result = $this->Form->select(
  4739. 'Model.multi_field',
  4740. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4741. array('multiple' => 'checkbox')
  4742. );
  4743. $expected = array(
  4744. 'input' => array(
  4745. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4746. ),
  4747. array('div' => array('class' => 'checkbox')),
  4748. array('input' => array(
  4749. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4750. 'value' => 'a+', 'id' => 'ModelMultiFieldA+'
  4751. )),
  4752. array('label' => array('for' => 'ModelMultiFieldA+')),
  4753. 'first',
  4754. '/label',
  4755. '/div',
  4756. array('div' => array('class' => 'checkbox')),
  4757. array('input' => array(
  4758. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4759. 'value' => 'a++', 'id' => 'ModelMultiFieldA++'
  4760. )),
  4761. array('label' => array('for' => 'ModelMultiFieldA++')),
  4762. 'second',
  4763. '/label',
  4764. '/div',
  4765. array('div' => array('class' => 'checkbox')),
  4766. array('input' => array(
  4767. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4768. 'value' => 'a+++', 'id' => 'ModelMultiFieldA+++'
  4769. )),
  4770. array('label' => array('for' => 'ModelMultiFieldA+++')),
  4771. 'third',
  4772. '/label',
  4773. '/div'
  4774. );
  4775. $this->assertTags($result, $expected);
  4776. $result = $this->Form->select(
  4777. 'Model.multi_field',
  4778. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4779. array('multiple' => 'checkbox')
  4780. );
  4781. $expected = array(
  4782. 'input' => array(
  4783. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4784. ),
  4785. array('div' => array('class' => 'checkbox')),
  4786. array('input' => array(
  4787. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4788. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4789. )),
  4790. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4791. 'first',
  4792. '/label',
  4793. '/div',
  4794. array('div' => array('class' => 'checkbox')),
  4795. array('input' => array(
  4796. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4797. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4798. )),
  4799. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4800. 'second',
  4801. '/label',
  4802. '/div',
  4803. array('div' => array('class' => 'checkbox')),
  4804. array('input' => array(
  4805. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4806. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4807. )),
  4808. array('label' => array('for' => 'ModelMultiFieldAB')),
  4809. 'third',
  4810. '/label',
  4811. '/div'
  4812. );
  4813. $this->assertTags($result, $expected);
  4814. }
  4815. /**
  4816. * test multiple checkboxes with div styles.
  4817. *
  4818. * @return void
  4819. */
  4820. public function testSelectMultipleCheckboxDiv() {
  4821. $result = $this->Form->select(
  4822. 'Model.tags',
  4823. array('first', 'second'),
  4824. array('multiple' => 'checkbox', 'class' => 'my-class')
  4825. );
  4826. $expected = array(
  4827. 'input' => array(
  4828. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4829. ),
  4830. array('div' => array('class' => 'my-class')),
  4831. array('input' => array(
  4832. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4833. 'value' => '0', 'id' => 'ModelTags0'
  4834. )),
  4835. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4836. '/div',
  4837. array('div' => array('class' => 'my-class')),
  4838. array('input' => array(
  4839. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4840. 'value' => '1', 'id' => 'ModelTags1'
  4841. )),
  4842. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4843. '/div'
  4844. );
  4845. $this->assertTags($result, $expected);
  4846. $result = $this->Form->input('Model.tags', array(
  4847. 'options' => array('first', 'second'),
  4848. 'multiple' => 'checkbox',
  4849. 'class' => 'my-class',
  4850. 'div' => false,
  4851. 'label' => false
  4852. ));
  4853. $this->assertTags($result, $expected);
  4854. $Contact = ClassRegistry::getObject('Contact');
  4855. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4856. $result = $this->Form->input('Contact.tags', array(
  4857. 'options' => array('one'),
  4858. 'multiple' => 'checkbox',
  4859. 'label' => false,
  4860. 'div' => false
  4861. ));
  4862. $expected = array(
  4863. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4864. array('div' => array('class' => 'checkbox form-error')),
  4865. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4866. array('label' => array('for' => 'ContactTags0')),
  4867. 'one',
  4868. '/label',
  4869. '/div'
  4870. );
  4871. $this->assertTags($result, $expected);
  4872. $result = $this->Form->input('Contact.tags', array(
  4873. 'options' => array('one'),
  4874. 'multiple' => 'checkbox',
  4875. 'class' => 'mycheckbox',
  4876. 'label' => false,
  4877. 'div' => false
  4878. ));
  4879. $expected = array(
  4880. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4881. array('div' => array('class' => 'mycheckbox form-error')),
  4882. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4883. array('label' => array('for' => 'ContactTags0')),
  4884. 'one',
  4885. '/label',
  4886. '/div'
  4887. );
  4888. $this->assertTags($result, $expected);
  4889. }
  4890. /**
  4891. * Checks the security hash array generated for multiple-input checkbox elements
  4892. *
  4893. * @return void
  4894. */
  4895. public function testSelectMultipleCheckboxSecurity() {
  4896. $this->Form->request['_Token'] = array('key' => 'testKey');
  4897. $this->assertEquals(array(), $this->Form->fields);
  4898. $result = $this->Form->select(
  4899. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4900. array('multiple' => 'checkbox')
  4901. );
  4902. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4903. $result = $this->Form->secure($this->Form->fields);
  4904. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4905. $this->assertRegExp('/"' . $key . '"/', $result);
  4906. }
  4907. /**
  4908. * Multiple select elements should always be secured as they always participate
  4909. * in the POST data.
  4910. *
  4911. * @return void
  4912. */
  4913. public function testSelectMultipleSecureWithNoOptions() {
  4914. $this->Form->request['_Token'] = array('key' => 'testkey');
  4915. $this->assertEquals(array(), $this->Form->fields);
  4916. $this->Form->select(
  4917. 'Model.select',
  4918. array(),
  4919. array('multiple' => true)
  4920. );
  4921. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4922. }
  4923. /**
  4924. * When a select box has no options it should not be added to the fields list
  4925. * as it always fail post validation.
  4926. *
  4927. * @return void
  4928. */
  4929. public function testSelectNoSecureWithNoOptions() {
  4930. $this->Form->request['_Token'] = array('key' => 'testkey');
  4931. $this->assertEquals(array(), $this->Form->fields);
  4932. $this->Form->select(
  4933. 'Model.select',
  4934. array()
  4935. );
  4936. $this->assertEquals(array(), $this->Form->fields);
  4937. $this->Form->select(
  4938. 'Model.select',
  4939. array(),
  4940. array('empty' => true)
  4941. );
  4942. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4943. }
  4944. /**
  4945. * testInputMultipleCheckboxes method
  4946. *
  4947. * test input() resulting in multi select elements being generated.
  4948. *
  4949. * @return void
  4950. */
  4951. public function testInputMultipleCheckboxes() {
  4952. $result = $this->Form->input('Model.multi_field', array(
  4953. 'options' => array('first', 'second', 'third'),
  4954. 'multiple' => 'checkbox'
  4955. ));
  4956. $expected = array(
  4957. array('div' => array('class' => 'input select')),
  4958. array('label' => array('for' => 'ModelMultiField')),
  4959. 'Multi Field',
  4960. '/label',
  4961. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4962. array('div' => array('class' => 'checkbox')),
  4963. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4964. array('label' => array('for' => 'ModelMultiField0')),
  4965. 'first',
  4966. '/label',
  4967. '/div',
  4968. array('div' => array('class' => 'checkbox')),
  4969. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4970. array('label' => array('for' => 'ModelMultiField1')),
  4971. 'second',
  4972. '/label',
  4973. '/div',
  4974. array('div' => array('class' => 'checkbox')),
  4975. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4976. array('label' => array('for' => 'ModelMultiField2')),
  4977. 'third',
  4978. '/label',
  4979. '/div',
  4980. '/div'
  4981. );
  4982. $this->assertTags($result, $expected);
  4983. $result = $this->Form->input('Model.multi_field', array(
  4984. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4985. 'multiple' => 'checkbox'
  4986. ));
  4987. $expected = array(
  4988. array('div' => array('class' => 'input select')),
  4989. array('label' => array('for' => 'ModelMultiField')),
  4990. 'Multi Field',
  4991. '/label',
  4992. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4993. array('div' => array('class' => 'checkbox')),
  4994. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4995. array('label' => array('for' => 'ModelMultiFieldA')),
  4996. 'first',
  4997. '/label',
  4998. '/div',
  4999. array('div' => array('class' => 'checkbox')),
  5000. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5001. array('label' => array('for' => 'ModelMultiFieldB')),
  5002. 'second',
  5003. '/label',
  5004. '/div',
  5005. array('div' => array('class' => 'checkbox')),
  5006. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5007. array('label' => array('for' => 'ModelMultiFieldC')),
  5008. 'third',
  5009. '/label',
  5010. '/div',
  5011. '/div'
  5012. );
  5013. $this->assertTags($result, $expected);
  5014. $result = $this->Form->input('Model.multi_field', array(
  5015. 'options' => array('1' => 'first'),
  5016. 'multiple' => 'checkbox',
  5017. 'label' => false,
  5018. 'div' => false
  5019. ));
  5020. $expected = array(
  5021. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5022. array('div' => array('class' => 'checkbox')),
  5023. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5024. array('label' => array('for' => 'ModelMultiField1')),
  5025. 'first',
  5026. '/label',
  5027. '/div'
  5028. );
  5029. $this->assertTags($result, $expected);
  5030. $result = $this->Form->input('Model.multi_field', array(
  5031. 'options' => array('2' => 'second'),
  5032. 'multiple' => 'checkbox',
  5033. 'label' => false,
  5034. 'div' => false
  5035. ));
  5036. $expected = array(
  5037. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5038. array('div' => array('class' => 'checkbox')),
  5039. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5040. array('label' => array('for' => 'ModelMultiField2')),
  5041. 'second',
  5042. '/label',
  5043. '/div'
  5044. );
  5045. $this->assertTags($result, $expected);
  5046. }
  5047. /**
  5048. * testSelectHiddenFieldOmission method
  5049. *
  5050. * test that select() with 'hiddenField' => false omits the hidden field
  5051. *
  5052. * @return void
  5053. */
  5054. public function testSelectHiddenFieldOmission() {
  5055. $result = $this->Form->select('Model.multi_field',
  5056. array('first', 'second'),
  5057. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5058. );
  5059. $expected = array(
  5060. array('div' => array('class' => 'checkbox')),
  5061. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5062. array('label' => array('for' => 'ModelMultiField0')),
  5063. 'first',
  5064. '/label',
  5065. '/div',
  5066. array('div' => array('class' => 'checkbox')),
  5067. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5068. array('label' => array('for' => 'ModelMultiField1')),
  5069. 'second',
  5070. '/label',
  5071. '/div'
  5072. );
  5073. $this->assertTags($result, $expected);
  5074. $result = $this->Form->input('Model.multi_field', array(
  5075. 'options' => array('first', 'second'),
  5076. 'multiple' => 'checkbox',
  5077. 'hiddenField' => false
  5078. ));
  5079. $expected = array(
  5080. array('div' => array('class' => 'input select')),
  5081. array('label' => array('for' => 'ModelMultiField')),
  5082. 'Multi Field',
  5083. '/label',
  5084. array('div' => array('class' => 'checkbox')),
  5085. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5086. array('label' => array('for' => 'ModelMultiField0')),
  5087. 'first',
  5088. '/label',
  5089. '/div',
  5090. array('div' => array('class' => 'checkbox')),
  5091. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5092. array('label' => array('for' => 'ModelMultiField1')),
  5093. 'second',
  5094. '/label',
  5095. '/div',
  5096. '/div'
  5097. );
  5098. $this->assertTags($result, $expected);
  5099. }
  5100. /**
  5101. * test that select() with multiple = checkbox works with overriding name attribute.
  5102. *
  5103. * @return void
  5104. */
  5105. public function testSelectCheckboxMultipleOverrideName() {
  5106. $result = $this->Form->input('category', array(
  5107. 'type' => 'select',
  5108. 'multiple' => 'checkbox',
  5109. 'name' => 'data[fish]',
  5110. 'options' => array('1', '2'),
  5111. 'div' => false,
  5112. 'label' => false,
  5113. ));
  5114. $expected = array(
  5115. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5116. array('div' => array('class' => 'checkbox')),
  5117. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5118. array('label' => array('for' => 'Category0')), '1', '/label',
  5119. '/div',
  5120. array('div' => array('class' => 'checkbox')),
  5121. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5122. array('label' => array('for' => 'Category1')), '2', '/label',
  5123. '/div'
  5124. );
  5125. $this->assertTags($result, $expected);
  5126. }
  5127. /**
  5128. * Test that 'id' overrides all the checkbox id's as well.
  5129. *
  5130. * @return void
  5131. */
  5132. public function testSelectCheckboxMultipleId() {
  5133. $result = $this->Form->select(
  5134. 'Model.multi_field',
  5135. array('first', 'second', 'third'),
  5136. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5137. );
  5138. $expected = array(
  5139. 'input' => array(
  5140. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5141. ),
  5142. array('div' => array('class' => 'checkbox')),
  5143. array('input' => array(
  5144. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5145. 'value' => '0', 'id' => 'CustomId0'
  5146. )),
  5147. array('label' => array('for' => 'CustomId0')),
  5148. 'first',
  5149. '/label',
  5150. '/div',
  5151. array('div' => array('class' => 'checkbox')),
  5152. array('input' => array(
  5153. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5154. 'value' => '1', 'id' => 'CustomId1'
  5155. )),
  5156. array('label' => array('for' => 'CustomId1')),
  5157. 'second',
  5158. '/label',
  5159. '/div',
  5160. array('div' => array('class' => 'checkbox')),
  5161. array('input' => array(
  5162. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5163. 'value' => '2', 'id' => 'CustomId2'
  5164. )),
  5165. array('label' => array('for' => 'CustomId2')),
  5166. 'third',
  5167. '/label',
  5168. '/div'
  5169. );
  5170. $this->assertTags($result, $expected);
  5171. }
  5172. /**
  5173. * testCheckbox method
  5174. *
  5175. * Test generation of checkboxes
  5176. *
  5177. * @return void
  5178. */
  5179. public function testCheckbox() {
  5180. $result = $this->Form->checkbox('Model.field');
  5181. $expected = array(
  5182. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5183. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5184. );
  5185. $this->assertTags($result, $expected);
  5186. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5187. $expected = array(
  5188. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5189. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5190. );
  5191. $this->assertTags($result, $expected);
  5192. $Contact = ClassRegistry::getObject('Contact');
  5193. $Contact->validationErrors['field'] = 1;
  5194. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5195. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5196. $expected = array(
  5197. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5198. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5199. );
  5200. $this->assertTags($result, $expected);
  5201. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5202. $expected = array(
  5203. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5204. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5205. );
  5206. $this->assertTags($result, $expected);
  5207. $this->Form->request->data['Contact']['field'] = '';
  5208. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5209. $expected = array(
  5210. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5211. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5212. );
  5213. $this->assertTags($result, $expected);
  5214. $Contact->validationErrors = array();
  5215. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5216. $expected = array(
  5217. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5218. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5219. );
  5220. $this->assertTags($result, $expected);
  5221. $this->Form->request->data['Contact']['published'] = 1;
  5222. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5223. $expected = array(
  5224. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5225. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5226. );
  5227. $this->assertTags($result, $expected);
  5228. $this->Form->request->data['Contact']['published'] = 0;
  5229. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5230. $expected = array(
  5231. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5232. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5233. );
  5234. $this->assertTags($result, $expected);
  5235. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5236. $expected = array(
  5237. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5238. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5239. );
  5240. $this->assertTags($result, $expected);
  5241. $result = $this->Form->checkbox('CustomField.1.value');
  5242. $expected = array(
  5243. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5244. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5245. );
  5246. $this->assertTags($result, $expected);
  5247. }
  5248. /**
  5249. * test checkbox() with a custom name attribute
  5250. *
  5251. * @return void
  5252. */
  5253. public function testCheckboxCustomNameAttribute() {
  5254. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5255. $expected = array(
  5256. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5257. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5258. );
  5259. $this->assertTags($result, $expected);
  5260. }
  5261. /**
  5262. * test the checked option for checkboxes.
  5263. *
  5264. * @return void
  5265. */
  5266. public function testCheckboxCheckedOption() {
  5267. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5268. $expected = array(
  5269. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5270. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5274. $expected = array(
  5275. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5276. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5277. );
  5278. $this->assertTags($result, $expected);
  5279. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5280. $expected = array(
  5281. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5282. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5286. $expected = array(
  5287. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5288. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5289. );
  5290. $this->assertTags($result, $expected);
  5291. $this->Form->request->data['Model']['field'] = 1;
  5292. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5293. $expected = array(
  5294. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5295. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5296. );
  5297. $this->assertTags($result, $expected);
  5298. }
  5299. /**
  5300. * Test that disabled attribute works on both the checkbox and hidden input.
  5301. *
  5302. * @return void
  5303. */
  5304. public function testCheckboxDisabling() {
  5305. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5306. $expected = array(
  5307. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5308. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5309. );
  5310. $this->assertTags($result, $expected);
  5311. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5312. $expected = array(
  5313. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5314. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5315. );
  5316. $this->assertTags($result, $expected);
  5317. }
  5318. /**
  5319. * Test that the hidden input for checkboxes can be omitted or set to a
  5320. * specific value.
  5321. *
  5322. * @return void
  5323. */
  5324. public function testCheckboxHiddenField() {
  5325. $result = $this->Form->input('UserForm.something', array(
  5326. 'type' => 'checkbox',
  5327. 'hiddenField' => false
  5328. ));
  5329. $expected = array(
  5330. 'div' => array('class' => 'input checkbox'),
  5331. array('input' => array(
  5332. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5333. 'value' => '1', 'id' => 'UserFormSomething'
  5334. )),
  5335. 'label' => array('for' => 'UserFormSomething'),
  5336. 'Something',
  5337. '/label',
  5338. '/div'
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $result = $this->Form->input('UserForm.something', array(
  5342. 'type' => 'checkbox',
  5343. 'value' => 'Y',
  5344. 'hiddenField' => 'N',
  5345. ));
  5346. $expected = array(
  5347. 'div' => array('class' => 'input checkbox'),
  5348. array('input' => array(
  5349. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5350. 'value' => 'N', 'id' => 'UserFormSomething_'
  5351. )),
  5352. array('input' => array(
  5353. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5354. 'value' => 'Y', 'id' => 'UserFormSomething'
  5355. )),
  5356. 'label' => array('for' => 'UserFormSomething'),
  5357. 'Something',
  5358. '/label',
  5359. '/div'
  5360. );
  5361. $this->assertTags($result, $expected);
  5362. }
  5363. /**
  5364. * testDateTime method
  5365. *
  5366. * Test generation of date/time select elements
  5367. *
  5368. * @return void
  5369. */
  5370. public function testDateTime() {
  5371. extract($this->dateRegex);
  5372. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5373. $now = strtotime('now');
  5374. $expected = array(
  5375. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5376. $daysRegex,
  5377. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5378. date('j', $now),
  5379. '/option',
  5380. '*/select',
  5381. '-',
  5382. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5383. $monthsRegex,
  5384. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5385. date('F', $now),
  5386. '/option',
  5387. '*/select',
  5388. '-',
  5389. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5390. $yearsRegex,
  5391. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5392. date('Y', $now),
  5393. '/option',
  5394. '*/select',
  5395. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5396. $hoursRegex,
  5397. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5398. date('g', $now),
  5399. '/option',
  5400. '*/select',
  5401. ':',
  5402. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5403. $minutesRegex,
  5404. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5405. date('i', $now),
  5406. '/option',
  5407. '*/select',
  5408. ' ',
  5409. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5410. $meridianRegex,
  5411. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5412. date('a', $now),
  5413. '/option',
  5414. '*/select'
  5415. );
  5416. $this->assertTags($result, $expected);
  5417. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5418. $expected = array(
  5419. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5420. $daysRegex,
  5421. array('option' => array('value' => '')),
  5422. '/option',
  5423. '*/select',
  5424. '-',
  5425. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5426. $monthsRegex,
  5427. array('option' => array('value' => '')),
  5428. '/option',
  5429. '*/select',
  5430. '-',
  5431. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5432. $yearsRegex,
  5433. array('option' => array('value' => '')),
  5434. '/option',
  5435. '*/select',
  5436. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5437. $hoursRegex,
  5438. array('option' => array('value' => '')),
  5439. '/option',
  5440. '*/select',
  5441. ':',
  5442. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5443. $minutesRegex,
  5444. array('option' => array('value' => '')),
  5445. '/option',
  5446. '*/select',
  5447. ' ',
  5448. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5449. $meridianRegex,
  5450. array('option' => array('value' => '')),
  5451. '/option',
  5452. '*/select'
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5456. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5457. $this->assertTags($result, $expected);
  5458. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5459. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5460. $this->assertTags($result, $expected);
  5461. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5462. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5463. $expected = array(
  5464. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5465. $daysRegex,
  5466. array('option' => array('value' => '')),
  5467. '/option',
  5468. '*/select',
  5469. '-',
  5470. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5471. $monthsRegex,
  5472. array('option' => array('value' => '')),
  5473. '/option',
  5474. '*/select',
  5475. '-',
  5476. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5477. $yearsRegex,
  5478. array('option' => array('value' => '')),
  5479. '/option',
  5480. '*/select',
  5481. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5482. $hoursRegex,
  5483. array('option' => array('value' => '')),
  5484. '/option',
  5485. '*/select',
  5486. ':',
  5487. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5488. $minutesRegex,
  5489. array('option' => array('value' => '')),
  5490. '/option',
  5491. array('option' => array('value' => '00')),
  5492. '00',
  5493. '/option',
  5494. array('option' => array('value' => '05')),
  5495. '05',
  5496. '/option',
  5497. array('option' => array('value' => '10')),
  5498. '10',
  5499. '/option',
  5500. '*/select',
  5501. ' ',
  5502. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5503. $meridianRegex,
  5504. array('option' => array('value' => '')),
  5505. '/option',
  5506. '*/select'
  5507. );
  5508. $this->assertTags($result, $expected);
  5509. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5510. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5511. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5512. $this->Form->request->data['Contact']['data'] = null;
  5513. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5514. $expected = array(
  5515. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5516. $daysRegex,
  5517. array('option' => array('value' => '')),
  5518. '/option',
  5519. '*/select',
  5520. '-',
  5521. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5522. $monthsRegex,
  5523. array('option' => array('value' => '')),
  5524. '/option',
  5525. '*/select',
  5526. '-',
  5527. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5528. $yearsRegex,
  5529. array('option' => array('value' => '')),
  5530. '/option',
  5531. '*/select',
  5532. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5533. $hoursRegex,
  5534. array('option' => array('value' => '')),
  5535. '/option',
  5536. '*/select',
  5537. ':',
  5538. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5539. $minutesRegex,
  5540. array('option' => array('value' => '')),
  5541. '/option',
  5542. '*/select',
  5543. ' ',
  5544. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5545. $meridianRegex,
  5546. array('option' => array('value' => '')),
  5547. '/option',
  5548. '*/select'
  5549. );
  5550. $this->assertTags($result, $expected);
  5551. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5552. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5553. $now = strtotime($this->Form->data['Model']['field']);
  5554. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5555. $expected = array(
  5556. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5557. $daysRegex,
  5558. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5559. date('j', $now),
  5560. '/option',
  5561. '*/select',
  5562. '-',
  5563. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5564. $monthsRegex,
  5565. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5566. date('F', $now),
  5567. '/option',
  5568. '*/select',
  5569. '-',
  5570. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5571. $yearsRegex,
  5572. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5573. date('Y', $now),
  5574. '/option',
  5575. '*/select',
  5576. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5577. $hoursRegex,
  5578. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5579. date('g', $now),
  5580. '/option',
  5581. '*/select',
  5582. ':',
  5583. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5584. $minutesRegex,
  5585. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5586. date('i', $now),
  5587. '/option',
  5588. '*/select',
  5589. ' ',
  5590. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5591. $meridianRegex,
  5592. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5593. date('a', $now),
  5594. '/option',
  5595. '*/select'
  5596. );
  5597. $this->assertTags($result, $expected);
  5598. $selected = strtotime('2008-10-26 12:33:00');
  5599. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5600. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5601. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5602. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5603. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5604. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5605. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5606. $this->Form->create('Contact');
  5607. $result = $this->Form->input('published');
  5608. $now = strtotime('now');
  5609. $expected = array(
  5610. 'div' => array('class' => 'input date'),
  5611. 'label' => array('for' => 'ContactPublishedMonth'),
  5612. 'Published',
  5613. '/label',
  5614. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5615. $monthsRegex,
  5616. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5617. date('F', $now),
  5618. '/option',
  5619. '*/select',
  5620. '-',
  5621. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5622. $daysRegex,
  5623. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5624. date('j', $now),
  5625. '/option',
  5626. '*/select',
  5627. '-',
  5628. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5629. $yearsRegex,
  5630. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5631. date('Y', $now),
  5632. '/option',
  5633. '*/select',
  5634. '/div'
  5635. );
  5636. $this->assertTags($result, $expected);
  5637. $result = $this->Form->input('published2', array('type' => 'date'));
  5638. $now = strtotime('now');
  5639. $expected = array(
  5640. 'div' => array('class' => 'input date'),
  5641. 'label' => array('for' => 'ContactPublished2Month'),
  5642. 'Published2',
  5643. '/label',
  5644. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5645. $monthsRegex,
  5646. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5647. date('F', $now),
  5648. '/option',
  5649. '*/select',
  5650. '-',
  5651. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5652. $daysRegex,
  5653. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5654. date('j', $now),
  5655. '/option',
  5656. '*/select',
  5657. '-',
  5658. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5659. $yearsRegex,
  5660. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5661. date('Y', $now),
  5662. '/option',
  5663. '*/select',
  5664. '/div'
  5665. );
  5666. $this->assertTags($result, $expected);
  5667. $this->Form->create('Contact');
  5668. $result = $this->Form->input('published', array('monthNames' => false));
  5669. $now = strtotime('now');
  5670. $expected = array(
  5671. 'div' => array('class' => 'input date'),
  5672. 'label' => array('for' => 'ContactPublishedMonth'),
  5673. 'Published',
  5674. '/label',
  5675. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5676. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5677. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5678. date('m', $now),
  5679. '/option',
  5680. '*/select',
  5681. '-',
  5682. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5683. $daysRegex,
  5684. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5685. date('j', $now),
  5686. '/option',
  5687. '*/select',
  5688. '-',
  5689. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5690. $yearsRegex,
  5691. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5692. date('Y', $now),
  5693. '/option',
  5694. '*/select',
  5695. '/div'
  5696. );
  5697. $this->assertTags($result, $expected);
  5698. $result = $this->Form->input('published', array(
  5699. 'timeFormat' => 24,
  5700. 'interval' => 5,
  5701. 'selected' => strtotime('2009-09-03 13:37:00'),
  5702. 'type' => 'datetime'
  5703. ));
  5704. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5705. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5706. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5707. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5708. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5709. }
  5710. /**
  5711. * Test dateTime with rounding
  5712. *
  5713. * @return void
  5714. */
  5715. public function testDateTimeRounding() {
  5716. $this->Form->request->data['Contact'] = array(
  5717. 'date' => array(
  5718. 'day' => '13',
  5719. 'month' => '12',
  5720. 'year' => '2010',
  5721. 'hour' => '04',
  5722. 'min' => '19',
  5723. 'meridian' => 'AM'
  5724. )
  5725. );
  5726. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5727. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5728. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5729. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5730. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5731. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5732. }
  5733. /**
  5734. * Test that empty values don't trigger errors.
  5735. *
  5736. * @return void
  5737. */
  5738. public function testDateTimeNoErrorsOnEmptyData() {
  5739. $this->Form->request->data['Contact'] = array(
  5740. 'date' => array(
  5741. 'day' => '',
  5742. 'month' => '',
  5743. 'year' => '',
  5744. 'hour' => '',
  5745. 'min' => '',
  5746. 'meridian' => ''
  5747. )
  5748. );
  5749. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5750. $this->assertNotEmpty($result);
  5751. }
  5752. /**
  5753. * test that datetime() and default values work.
  5754. *
  5755. * @return void
  5756. */
  5757. public function testDatetimeWithDefault() {
  5758. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5759. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5760. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5761. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5762. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5763. 'default' => '2009-06-01 11:15:30'
  5764. ));
  5765. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5766. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5767. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5768. }
  5769. /**
  5770. * test that bogus non-date time data doesn't cause errors.
  5771. *
  5772. * @return void
  5773. */
  5774. public function testDateTimeWithBogusData() {
  5775. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5776. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5777. }
  5778. /**
  5779. * testDateTimeEmptyAsArray
  5780. *
  5781. * @return void
  5782. */
  5783. public function testDateTimeEmptyAsArray() {
  5784. $result = $this->Form->dateTime('Contact.date',
  5785. 'DMY',
  5786. '12',
  5787. array(
  5788. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5789. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5790. )
  5791. )
  5792. );
  5793. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5794. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5795. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5796. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5797. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5798. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5799. $result = $this->Form->dateTime('Contact.date',
  5800. 'DMY',
  5801. '12',
  5802. array(
  5803. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5804. )
  5805. );
  5806. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5807. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5808. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5809. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5810. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5811. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5812. }
  5813. /**
  5814. * testFormDateTimeMulti method
  5815. *
  5816. * test multiple datetime element generation
  5817. *
  5818. * @return void
  5819. */
  5820. public function testFormDateTimeMulti() {
  5821. extract($this->dateRegex);
  5822. $result = $this->Form->dateTime('Contact.1.updated');
  5823. $expected = array(
  5824. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5825. $daysRegex,
  5826. array('option' => array('value' => '')),
  5827. '/option',
  5828. '*/select',
  5829. '-',
  5830. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5831. $monthsRegex,
  5832. array('option' => array('value' => '')),
  5833. '/option',
  5834. '*/select',
  5835. '-',
  5836. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5837. $yearsRegex,
  5838. array('option' => array('value' => '')),
  5839. '/option',
  5840. '*/select',
  5841. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5842. $hoursRegex,
  5843. array('option' => array('value' => '')),
  5844. '/option',
  5845. '*/select',
  5846. ':',
  5847. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5848. $minutesRegex,
  5849. array('option' => array('value' => '')),
  5850. '/option',
  5851. '*/select',
  5852. ' ',
  5853. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5854. $meridianRegex,
  5855. array('option' => array('value' => '')),
  5856. '/option',
  5857. '*/select'
  5858. );
  5859. $this->assertTags($result, $expected);
  5860. $result = $this->Form->dateTime('Contact.2.updated');
  5861. $expected = array(
  5862. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5863. $daysRegex,
  5864. array('option' => array('value' => '')),
  5865. '/option',
  5866. '*/select',
  5867. '-',
  5868. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5869. $monthsRegex,
  5870. array('option' => array('value' => '')),
  5871. '/option',
  5872. '*/select',
  5873. '-',
  5874. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5875. $yearsRegex,
  5876. array('option' => array('value' => '')),
  5877. '/option',
  5878. '*/select',
  5879. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5880. $hoursRegex,
  5881. array('option' => array('value' => '')),
  5882. '/option',
  5883. '*/select',
  5884. ':',
  5885. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5886. $minutesRegex,
  5887. array('option' => array('value' => '')),
  5888. '/option',
  5889. '*/select',
  5890. ' ',
  5891. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5892. $meridianRegex,
  5893. array('option' => array('value' => '')),
  5894. '/option',
  5895. '*/select'
  5896. );
  5897. $this->assertTags($result, $expected);
  5898. }
  5899. /**
  5900. * When changing the date format, the label should always focus the first select box when
  5901. * clicked.
  5902. *
  5903. * @return void
  5904. */
  5905. public function testDateTimeLabelIdMatchesFirstInput() {
  5906. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5907. $this->assertContains('label for="ModelDateMonth"', $result);
  5908. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5909. $this->assertContains('label for="ModelDateDay"', $result);
  5910. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5911. $this->assertContains('label for="ModelDateYear"', $result);
  5912. }
  5913. /**
  5914. * testMonth method
  5915. *
  5916. * @return void
  5917. */
  5918. public function testMonth() {
  5919. $result = $this->Form->month('Model.field');
  5920. $expected = array(
  5921. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5922. array('option' => array('value' => '')),
  5923. '/option',
  5924. array('option' => array('value' => '01')),
  5925. date('F', strtotime('2008-01-01 00:00:00')),
  5926. '/option',
  5927. array('option' => array('value' => '02')),
  5928. date('F', strtotime('2008-02-01 00:00:00')),
  5929. '/option',
  5930. '*/select',
  5931. );
  5932. $this->assertTags($result, $expected);
  5933. $result = $this->Form->month('Model.field', array('empty' => true));
  5934. $expected = array(
  5935. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5936. array('option' => array('value' => '')),
  5937. '/option',
  5938. array('option' => array('value' => '01')),
  5939. date('F', strtotime('2008-01-01 00:00:00')),
  5940. '/option',
  5941. array('option' => array('value' => '02')),
  5942. date('F', strtotime('2008-02-01 00:00:00')),
  5943. '/option',
  5944. '*/select',
  5945. );
  5946. $this->assertTags($result, $expected);
  5947. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5948. $expected = array(
  5949. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5950. array('option' => array('value' => '')),
  5951. '/option',
  5952. array('option' => array('value' => '01')),
  5953. '01',
  5954. '/option',
  5955. array('option' => array('value' => '02')),
  5956. '02',
  5957. '/option',
  5958. '*/select',
  5959. );
  5960. $this->assertTags($result, $expected);
  5961. $monthNames = array(
  5962. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5963. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5964. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5965. $expected = array(
  5966. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5967. array('option' => array('value' => '')),
  5968. '/option',
  5969. array('option' => array('value' => '01')),
  5970. 'Jan',
  5971. '/option',
  5972. array('option' => array('value' => '02')),
  5973. 'Feb',
  5974. '/option',
  5975. '*/select',
  5976. );
  5977. $this->assertTags($result, $expected);
  5978. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5979. $result = $this->Form->month('Project.release');
  5980. $expected = array(
  5981. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  5982. array('option' => array('value' => '')),
  5983. '/option',
  5984. array('option' => array('value' => '01')),
  5985. 'January',
  5986. '/option',
  5987. array('option' => array('value' => '02', 'selected' => 'selected')),
  5988. 'February',
  5989. '/option',
  5990. '*/select',
  5991. );
  5992. $this->assertTags($result, $expected);
  5993. }
  5994. /**
  5995. * testDay method
  5996. *
  5997. * @return void
  5998. */
  5999. public function testDay() {
  6000. extract($this->dateRegex);
  6001. $result = $this->Form->day('Model.field', array('value' => false));
  6002. $expected = array(
  6003. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6004. array('option' => array('value' => '')),
  6005. '/option',
  6006. array('option' => array('value' => '01')),
  6007. '1',
  6008. '/option',
  6009. array('option' => array('value' => '02')),
  6010. '2',
  6011. '/option',
  6012. $daysRegex,
  6013. '/select',
  6014. );
  6015. $this->assertTags($result, $expected);
  6016. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6017. $result = $this->Form->day('Model.field');
  6018. $expected = array(
  6019. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6020. array('option' => array('value' => '')),
  6021. '/option',
  6022. array('option' => array('value' => '01')),
  6023. '1',
  6024. '/option',
  6025. array('option' => array('value' => '02')),
  6026. '2',
  6027. '/option',
  6028. $daysRegex,
  6029. array('option' => array('value' => '10', 'selected' => 'selected')),
  6030. '10',
  6031. '/option',
  6032. $daysRegex,
  6033. '/select',
  6034. );
  6035. $this->assertTags($result, $expected);
  6036. $this->Form->request->data['Model']['field'] = '';
  6037. $result = $this->Form->day('Model.field', array('value' => '10'));
  6038. $expected = array(
  6039. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6040. array('option' => array('value' => '')),
  6041. '/option',
  6042. array('option' => array('value' => '01')),
  6043. '1',
  6044. '/option',
  6045. array('option' => array('value' => '02')),
  6046. '2',
  6047. '/option',
  6048. $daysRegex,
  6049. array('option' => array('value' => '10', 'selected' => 'selected')),
  6050. '10',
  6051. '/option',
  6052. $daysRegex,
  6053. '/select',
  6054. );
  6055. $this->assertTags($result, $expected);
  6056. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6057. $result = $this->Form->day('Model.field', array('value' => true));
  6058. $expected = array(
  6059. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6060. array('option' => array('value' => '')),
  6061. '/option',
  6062. array('option' => array('value' => '01')),
  6063. '1',
  6064. '/option',
  6065. array('option' => array('value' => '02')),
  6066. '2',
  6067. '/option',
  6068. $daysRegex,
  6069. array('option' => array('value' => '10', 'selected' => 'selected')),
  6070. '10',
  6071. '/option',
  6072. $daysRegex,
  6073. '/select',
  6074. );
  6075. $this->assertTags($result, $expected);
  6076. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6077. $result = $this->Form->day('Project.release');
  6078. $expected = array(
  6079. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6080. array('option' => array('value' => '')),
  6081. '/option',
  6082. array('option' => array('value' => '01')),
  6083. '1',
  6084. '/option',
  6085. array('option' => array('value' => '02')),
  6086. '2',
  6087. '/option',
  6088. $daysRegex,
  6089. array('option' => array('value' => '10', 'selected' => 'selected')),
  6090. '10',
  6091. '/option',
  6092. $daysRegex,
  6093. '/select',
  6094. );
  6095. $this->assertTags($result, $expected);
  6096. }
  6097. /**
  6098. * testMinute method
  6099. *
  6100. * @return void
  6101. */
  6102. public function testMinute() {
  6103. extract($this->dateRegex);
  6104. $result = $this->Form->minute('Model.field');
  6105. $expected = array(
  6106. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6107. array('option' => array('value' => '')),
  6108. '/option',
  6109. array('option' => array('value' => '00')),
  6110. '00',
  6111. '/option',
  6112. array('option' => array('value' => '01')),
  6113. '01',
  6114. '/option',
  6115. array('option' => array('value' => '02')),
  6116. '02',
  6117. '/option',
  6118. $minutesRegex,
  6119. '/select',
  6120. );
  6121. $this->assertTags($result, $expected);
  6122. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6123. $result = $this->Form->minute('Model.field');
  6124. $expected = array(
  6125. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6126. array('option' => array('value' => '')),
  6127. '/option',
  6128. array('option' => array('value' => '00')),
  6129. '00',
  6130. '/option',
  6131. array('option' => array('value' => '01')),
  6132. '01',
  6133. '/option',
  6134. array('option' => array('value' => '02')),
  6135. '02',
  6136. '/option',
  6137. $minutesRegex,
  6138. array('option' => array('value' => '12', 'selected' => 'selected')),
  6139. '12',
  6140. '/option',
  6141. $minutesRegex,
  6142. '/select',
  6143. );
  6144. $this->assertTags($result, $expected);
  6145. $this->Form->request->data['Model']['field'] = '';
  6146. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6147. $expected = array(
  6148. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6149. array('option' => array('value' => '')),
  6150. '/option',
  6151. array('option' => array('value' => '00')),
  6152. '00',
  6153. '/option',
  6154. array('option' => array('value' => '05')),
  6155. '05',
  6156. '/option',
  6157. array('option' => array('value' => '10')),
  6158. '10',
  6159. '/option',
  6160. $minutesRegex,
  6161. '/select',
  6162. );
  6163. $this->assertTags($result, $expected);
  6164. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6165. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6166. $expected = array(
  6167. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6168. array('option' => array('value' => '')),
  6169. '/option',
  6170. array('option' => array('value' => '00')),
  6171. '00',
  6172. '/option',
  6173. array('option' => array('value' => '05')),
  6174. '05',
  6175. '/option',
  6176. array('option' => array('value' => '10', 'selected' => 'selected')),
  6177. '10',
  6178. '/option',
  6179. $minutesRegex,
  6180. '/select',
  6181. );
  6182. $this->assertTags($result, $expected);
  6183. }
  6184. /**
  6185. * testHour method
  6186. *
  6187. * @return void
  6188. */
  6189. public function testHour() {
  6190. extract($this->dateRegex);
  6191. $result = $this->Form->hour('Model.field', false);
  6192. $expected = array(
  6193. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6194. array('option' => array('value' => '')),
  6195. '/option',
  6196. array('option' => array('value' => '01')),
  6197. '1',
  6198. '/option',
  6199. array('option' => array('value' => '02')),
  6200. '2',
  6201. '/option',
  6202. $hoursRegex,
  6203. '/select',
  6204. );
  6205. $this->assertTags($result, $expected);
  6206. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6207. $result = $this->Form->hour('Model.field', false);
  6208. $expected = array(
  6209. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6210. array('option' => array('value' => '')),
  6211. '/option',
  6212. array('option' => array('value' => '01')),
  6213. '1',
  6214. '/option',
  6215. array('option' => array('value' => '02')),
  6216. '2',
  6217. '/option',
  6218. $hoursRegex,
  6219. array('option' => array('value' => '12', 'selected' => 'selected')),
  6220. '12',
  6221. '/option',
  6222. '/select',
  6223. );
  6224. $this->assertTags($result, $expected);
  6225. $this->Form->request->data['Model']['field'] = '';
  6226. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6227. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6228. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6229. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6230. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6231. $result = $this->Form->hour('Model.field', true);
  6232. $expected = array(
  6233. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6234. array('option' => array('value' => '')),
  6235. '/option',
  6236. array('option' => array('value' => '00', 'selected' => 'selected')),
  6237. '0',
  6238. '/option',
  6239. array('option' => array('value' => '01')),
  6240. '1',
  6241. '/option',
  6242. array('option' => array('value' => '02')),
  6243. '2',
  6244. '/option',
  6245. $hoursRegex,
  6246. '/select',
  6247. );
  6248. $this->assertTags($result, $expected);
  6249. unset($this->Form->request->data['Model']['field']);
  6250. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6251. $thisHour = date('H');
  6252. $optValue = date('G');
  6253. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6254. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6255. $result = $this->Form->hour('Model.field', true);
  6256. $expected = array(
  6257. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6258. array('option' => array('value' => '')),
  6259. '/option',
  6260. array('option' => array('value' => '00')),
  6261. '0',
  6262. '/option',
  6263. array('option' => array('value' => '01', 'selected' => 'selected')),
  6264. '1',
  6265. '/option',
  6266. array('option' => array('value' => '02')),
  6267. '2',
  6268. '/option',
  6269. $hoursRegex,
  6270. '/select',
  6271. );
  6272. $this->assertTags($result, $expected);
  6273. }
  6274. /**
  6275. * testYear method
  6276. *
  6277. * @return void
  6278. */
  6279. public function testYear() {
  6280. $result = $this->Form->year('Model.field', 2006, 2007);
  6281. $expected = array(
  6282. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6283. array('option' => array('value' => '')),
  6284. '/option',
  6285. array('option' => array('value' => '2007')),
  6286. '2007',
  6287. '/option',
  6288. array('option' => array('value' => '2006')),
  6289. '2006',
  6290. '/option',
  6291. '/select',
  6292. );
  6293. $this->assertTags($result, $expected);
  6294. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6295. $expected = array(
  6296. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6297. array('option' => array('value' => '')),
  6298. '/option',
  6299. array('option' => array('value' => '2006')),
  6300. '2006',
  6301. '/option',
  6302. array('option' => array('value' => '2007')),
  6303. '2007',
  6304. '/option',
  6305. '/select',
  6306. );
  6307. $this->assertTags($result, $expected);
  6308. $this->request->data['Contact']['published'] = '';
  6309. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6310. $expected = array(
  6311. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6312. array('option' => array('value' => '')),
  6313. '/option',
  6314. array('option' => array('value' => '2007')),
  6315. '2007',
  6316. '/option',
  6317. array('option' => array('value' => '2006')),
  6318. '2006',
  6319. '/option',
  6320. '/select',
  6321. );
  6322. $this->assertTags($result, $expected);
  6323. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6324. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6325. $expected = array(
  6326. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6327. array('option' => array('value' => '2007')),
  6328. '2007',
  6329. '/option',
  6330. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6331. '2006',
  6332. '/option',
  6333. '/select',
  6334. );
  6335. $this->assertTags($result, $expected);
  6336. $this->Form->request->data['Contact']['published'] = '';
  6337. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6338. $expected = array(
  6339. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6340. array('option' => array('value' => '')),
  6341. '/option',
  6342. array('option' => array('value' => '2007')),
  6343. '2007',
  6344. '/option',
  6345. array('option' => array('value' => '2006')),
  6346. '2006',
  6347. '/option',
  6348. '/select',
  6349. );
  6350. $this->assertTags($result, $expected);
  6351. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6352. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6353. $expected = array(
  6354. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6355. array('option' => array('value' => '2007')),
  6356. '2007',
  6357. '/option',
  6358. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6359. '2006',
  6360. '/option',
  6361. '/select',
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. $this->Form->request->data['Contact']['published'] = '';
  6365. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6366. $expected = array(
  6367. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6368. array('option' => array('value' => '')),
  6369. '/option',
  6370. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6371. '2007',
  6372. '/option',
  6373. array('option' => array('value' => '2006')),
  6374. '2006',
  6375. '/option',
  6376. '/select',
  6377. );
  6378. $this->assertTags($result, $expected);
  6379. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6380. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6381. $expected = array(
  6382. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6383. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6384. '2007',
  6385. '/option',
  6386. array('option' => array('value' => '2006')),
  6387. '2006',
  6388. '/option',
  6389. '/select',
  6390. );
  6391. $this->assertTags($result, $expected);
  6392. $this->Form->request->data['Contact']['published'] = '';
  6393. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6394. $expected = array(
  6395. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6396. array('option' => array('value' => '2008')),
  6397. '2008',
  6398. '/option',
  6399. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6400. '2007',
  6401. '/option',
  6402. array('option' => array('value' => '2006')),
  6403. '2006',
  6404. '/option',
  6405. '/select',
  6406. );
  6407. $this->assertTags($result, $expected);
  6408. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6409. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6410. $expected = array(
  6411. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6412. array('option' => array('value' => '2008')),
  6413. '2008',
  6414. '/option',
  6415. array('option' => array('value' => '2007')),
  6416. '2007',
  6417. '/option',
  6418. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6419. '2006',
  6420. '/option',
  6421. '/select',
  6422. );
  6423. $this->assertTags($result, $expected);
  6424. $this->Form->request->data = array();
  6425. $this->Form->create('Contact');
  6426. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6427. $expected = array(
  6428. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6429. array('option' => array('value' => '2008')),
  6430. '2008',
  6431. '/option',
  6432. array('option' => array('value' => '2007')),
  6433. '2007',
  6434. '/option',
  6435. array('option' => array('value' => '2006')),
  6436. '2006',
  6437. '/option',
  6438. '/select',
  6439. );
  6440. $this->assertTags($result, $expected);
  6441. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6442. $this->assertContains('data[Contact][published][year]', $result);
  6443. }
  6444. /**
  6445. * testYearAutoExpandRange method
  6446. *
  6447. * @return void
  6448. */
  6449. public function testYearAutoExpandRange() {
  6450. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6451. $result = $this->Form->year('User.birthday');
  6452. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6453. $result = $matches[1];
  6454. $expected = range(date('Y') + 20, 1930);
  6455. $this->assertEquals($expected, $result);
  6456. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6457. $result = $this->Form->year('Project.release');
  6458. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6459. $result = $matches[1];
  6460. $expected = range(2050, date('Y') - 20);
  6461. $this->assertEquals($expected, $result);
  6462. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6463. $result = $this->Form->year('Project.release', 1890, 1900);
  6464. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6465. $result = $matches[1];
  6466. $expected = range(1900, 1881);
  6467. $this->assertEquals($expected, $result);
  6468. }
  6469. /**
  6470. * testInputDate method
  6471. *
  6472. * Test various inputs with type date and different dateFormat values.
  6473. * Failing to provide a dateFormat key should not error.
  6474. * It should simply not pre-select any value then.
  6475. *
  6476. * @return void
  6477. */
  6478. public function testInputDate() {
  6479. $this->Form->request->data = array(
  6480. 'User' => array(
  6481. 'month_year' => array('month' => date('m')),
  6482. 'just_year' => array('month' => date('m')),
  6483. 'just_month' => array('year' => date('Y')),
  6484. 'just_day' => array('month' => date('m')),
  6485. )
  6486. );
  6487. $this->Form->create('User');
  6488. $result = $this->Form->input('month_year',
  6489. array(
  6490. 'label' => false,
  6491. 'div' => false,
  6492. 'type' => 'date',
  6493. 'dateFormat' => 'MY',
  6494. 'minYear' => 2006,
  6495. 'maxYear' => 2008
  6496. )
  6497. );
  6498. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6499. $this->assertNotContains('value="2008" selected="selected"', $result);
  6500. $result = $this->Form->input('just_year',
  6501. array(
  6502. 'type' => 'date',
  6503. 'label' => false,
  6504. 'dateFormat' => 'Y',
  6505. 'minYear' => date('Y'),
  6506. 'maxYear' => date('Y', strtotime('+20 years'))
  6507. )
  6508. );
  6509. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6510. $result = $this->Form->input('just_month',
  6511. array(
  6512. 'type' => 'date',
  6513. 'label' => false,
  6514. 'dateFormat' => 'M',
  6515. 'empty' => false,
  6516. )
  6517. );
  6518. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6519. $result = $this->Form->input('just_day',
  6520. array(
  6521. 'type' => 'date',
  6522. 'label' => false,
  6523. 'dateFormat' => 'D',
  6524. 'empty' => false,
  6525. )
  6526. );
  6527. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6528. }
  6529. /**
  6530. * testInputDateMaxYear method
  6531. *
  6532. * Let's say we want to only allow users born from 2006 to 2008 to register
  6533. * This being the first singup page, we still don't have any data
  6534. *
  6535. * @return void
  6536. */
  6537. public function testInputDateMaxYear() {
  6538. $this->Form->request->data = array();
  6539. $this->Form->create('User');
  6540. $result = $this->Form->input('birthday',
  6541. array(
  6542. 'label' => false,
  6543. 'div' => false,
  6544. 'type' => 'date',
  6545. 'dateFormat' => 'DMY',
  6546. 'minYear' => 2006,
  6547. 'maxYear' => 2008
  6548. )
  6549. );
  6550. $this->assertContains('value="2008" selected="selected"', $result);
  6551. }
  6552. /**
  6553. * testTextArea method
  6554. *
  6555. * @return void
  6556. */
  6557. public function testTextArea() {
  6558. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6559. $result = $this->Form->textarea('Model.field');
  6560. $expected = array(
  6561. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6562. 'some test data',
  6563. '/textarea',
  6564. );
  6565. $this->assertTags($result, $expected);
  6566. $result = $this->Form->textarea('Model.tmp');
  6567. $expected = array(
  6568. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6569. '/textarea',
  6570. );
  6571. $this->assertTags($result, $expected);
  6572. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6573. $result = $this->Form->textarea('Model.field');
  6574. $expected = array(
  6575. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6576. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6577. '/textarea',
  6578. );
  6579. $this->assertTags($result, $expected);
  6580. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6581. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6582. $expected = array(
  6583. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6584. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6585. '/textarea',
  6586. );
  6587. $this->assertTags($result, $expected);
  6588. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6589. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6590. $expected = array(
  6591. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6592. '/textarea'
  6593. );
  6594. $this->assertTags($result, $expected);
  6595. }
  6596. /**
  6597. * testTextAreaWithStupidCharacters method
  6598. *
  6599. * test text area with non-ascii characters
  6600. *
  6601. * @return void
  6602. */
  6603. public function testTextAreaWithStupidCharacters() {
  6604. $this->loadFixtures('Post');
  6605. $result = $this->Form->input('Post.content', array(
  6606. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6607. ));
  6608. $expected = array(
  6609. 'div' => array('class' => 'input textarea'),
  6610. 'label' => array('for' => 'PostContent'),
  6611. 'Current Text',
  6612. '/label',
  6613. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6614. 'GREAT®',
  6615. '/textarea',
  6616. '/div'
  6617. );
  6618. $this->assertTags($result, $expected);
  6619. }
  6620. /**
  6621. * testHiddenField method
  6622. *
  6623. * @return void
  6624. */
  6625. public function testHiddenField() {
  6626. $Contact = ClassRegistry::getObject('Contact');
  6627. $Contact->validationErrors['field'] = 1;
  6628. $this->Form->request->data['Contact']['field'] = 'test';
  6629. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6630. $this->assertTags($result, array(
  6631. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6632. );
  6633. }
  6634. /**
  6635. * testFileUploadField method
  6636. *
  6637. * @return void
  6638. */
  6639. public function testFileUploadField() {
  6640. $result = $this->Form->file('Model.upload');
  6641. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6642. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6643. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6644. $expected = array(
  6645. 'div' => array('class' => 'input file'),
  6646. 'label' => array('for' => 'ModelUpload'),
  6647. 'Upload',
  6648. '/label',
  6649. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6650. '/div'
  6651. );
  6652. $this->assertTags($result, $expected);
  6653. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6654. $result = $this->Form->file('Model.upload');
  6655. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6656. }
  6657. /**
  6658. * test File upload input on a model not used in create();
  6659. *
  6660. * @return void
  6661. */
  6662. public function testFileUploadOnOtherModel() {
  6663. $this->Form->create('ValidateUser', array('type' => 'file'));
  6664. $result = $this->Form->file('ValidateProfile.city');
  6665. $expected = array(
  6666. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6667. );
  6668. $this->assertTags($result, $expected);
  6669. }
  6670. /**
  6671. * testButton method
  6672. *
  6673. * @return void
  6674. */
  6675. public function testButton() {
  6676. $result = $this->Form->button('Hi');
  6677. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6678. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6679. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6680. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6681. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6682. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6683. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6684. $result = $this->Form->button('No type', array('type' => false));
  6685. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6686. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6687. $this->assertNotRegExp('/\&039/', $result);
  6688. }
  6689. /**
  6690. * Test that button() makes unlocked fields by default.
  6691. *
  6692. * @return void
  6693. */
  6694. public function testButtonUnlockedByDefault() {
  6695. $this->Form->request->params['_Token']['key'] = 'secured';
  6696. $this->Form->button('Save', array('name' => 'save'));
  6697. $this->Form->button('Clear');
  6698. $result = $this->Form->unlockField();
  6699. $this->assertEquals(array('save'), $result);
  6700. }
  6701. /**
  6702. * testPostButton method
  6703. *
  6704. * @return void
  6705. */
  6706. public function testPostButton() {
  6707. $result = $this->Form->postButton('Hi', '/controller/action');
  6708. $this->assertTags($result, array(
  6709. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6710. 'div' => array('style' => 'display:none;'),
  6711. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6712. '/div',
  6713. 'button' => array('type' => 'submit'),
  6714. 'Hi',
  6715. '/button',
  6716. '/form'
  6717. ));
  6718. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6719. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6720. }
  6721. /**
  6722. * Test that postButton adds _Token fields.
  6723. *
  6724. * @return void
  6725. */
  6726. public function testSecurePostButton() {
  6727. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6728. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6729. $expected = array(
  6730. 'form' => array(
  6731. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6732. ),
  6733. array('div' => array('style' => 'display:none;')),
  6734. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6735. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6736. '/div',
  6737. 'button' => array('type' => 'submit'),
  6738. 'Delete',
  6739. '/button',
  6740. array('div' => array('style' => 'display:none;')),
  6741. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6742. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6743. '/div',
  6744. '/form',
  6745. );
  6746. $this->assertTags($result, $expected);
  6747. }
  6748. /**
  6749. * testPostLink method
  6750. *
  6751. * @return void
  6752. */
  6753. public function testPostLink() {
  6754. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6755. $this->assertTags($result, array(
  6756. 'form' => array(
  6757. 'method' => 'post', 'action' => '/posts/delete/1',
  6758. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6759. ),
  6760. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6761. '/form',
  6762. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6763. 'Delete',
  6764. '/a'
  6765. ));
  6766. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6767. $this->assertTags($result, array(
  6768. 'form' => array(
  6769. 'method' => 'post', 'action' => '/posts/delete/1',
  6770. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6771. ),
  6772. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6773. '/form',
  6774. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6775. 'Delete',
  6776. '/a'
  6777. ));
  6778. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6779. $this->assertTags($result, array(
  6780. 'form' => array(
  6781. 'method' => 'post', 'action' => '/posts/delete/1',
  6782. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6783. ),
  6784. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6785. '/form',
  6786. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6787. 'Delete',
  6788. '/a'
  6789. ));
  6790. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6791. $this->assertTags($result, array(
  6792. 'form' => array(
  6793. 'method' => 'post', 'action' => '/posts/delete/1',
  6794. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6795. ),
  6796. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6797. '/form',
  6798. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6799. 'Delete',
  6800. '/a'
  6801. ));
  6802. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6803. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6804. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6805. $this->assertTags($result, array(
  6806. 'form' => array(
  6807. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6808. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6809. ),
  6810. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6811. '/form',
  6812. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6813. 'Delete',
  6814. '/a'
  6815. ));
  6816. $result = $this->Form->postLink(
  6817. '',
  6818. array('controller' => 'items', 'action' => 'delete', 10),
  6819. array('class' => 'btn btn-danger', 'escape' => false),
  6820. 'Confirm thing'
  6821. );
  6822. $this->assertTags($result, array(
  6823. 'form' => array(
  6824. 'method' => 'post', 'action' => '/items/delete/10',
  6825. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6826. ),
  6827. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6828. '/form',
  6829. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6830. '/a'
  6831. ));
  6832. }
  6833. /**
  6834. * test creating postLinks after a GET form.
  6835. *
  6836. * @return void
  6837. */
  6838. public function testPostLinkAfterGetForm() {
  6839. $this->Form->request->params['_Token']['key'] = 'testkey';
  6840. $this->Form->create('User', array('type' => 'get'));
  6841. $this->Form->end();
  6842. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6843. $this->assertTags($result, array(
  6844. 'form' => array(
  6845. 'method' => 'post', 'action' => '/posts/delete/1',
  6846. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6847. ),
  6848. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6849. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6850. 'div' => array('style' => 'display:none;'),
  6851. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6852. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6853. '/div',
  6854. '/form',
  6855. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6856. 'Delete',
  6857. '/a'
  6858. ));
  6859. }
  6860. /**
  6861. * Test that postLink adds _Token fields.
  6862. *
  6863. * @return void
  6864. */
  6865. public function testSecurePostLink() {
  6866. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6867. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6868. $expected = array(
  6869. 'form' => array(
  6870. 'method' => 'post', 'action' => '/posts/delete/1',
  6871. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6872. ),
  6873. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6874. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6875. 'div' => array('style' => 'display:none;'),
  6876. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6877. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6878. '/div',
  6879. '/form',
  6880. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6881. 'Delete',
  6882. '/a'
  6883. );
  6884. $this->assertTags($result, $expected);
  6885. }
  6886. /**
  6887. * testSubmitButton method
  6888. *
  6889. * @return void
  6890. */
  6891. public function testSubmitButton() {
  6892. $result = $this->Form->submit('');
  6893. $expected = array(
  6894. 'div' => array('class' => 'submit'),
  6895. 'input' => array('type' => 'submit', 'value' => ''),
  6896. '/div'
  6897. );
  6898. $this->assertTags($result, $expected);
  6899. $result = $this->Form->submit('Test Submit');
  6900. $expected = array(
  6901. 'div' => array('class' => 'submit'),
  6902. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6903. '/div'
  6904. );
  6905. $this->assertTags($result, $expected);
  6906. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6907. $expected = array(
  6908. 'span' => array('class' => 'submit'),
  6909. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6910. '/span'
  6911. );
  6912. $this->assertTags($result, $expected);
  6913. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6914. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6915. $this->assertTags($result, $expected);
  6916. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6917. $expected = array(
  6918. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6919. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6920. '/div'
  6921. );
  6922. $this->assertTags($result, $expected);
  6923. $result = $this->Form->submit('Next >');
  6924. $expected = array(
  6925. 'div' => array('class' => 'submit'),
  6926. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6927. '/div'
  6928. );
  6929. $this->assertTags($result, $expected);
  6930. $result = $this->Form->submit('Next >', array('escape' => false));
  6931. $expected = array(
  6932. 'div' => array('class' => 'submit'),
  6933. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6934. '/div'
  6935. );
  6936. $this->assertTags($result, $expected);
  6937. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6938. $expected = array(
  6939. 'div' => array('class' => 'submit'),
  6940. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6941. '/div'
  6942. );
  6943. $this->assertTags($result, $expected);
  6944. $before = '--before--';
  6945. $after = '--after--';
  6946. $result = $this->Form->submit('Test', array('before' => $before));
  6947. $expected = array(
  6948. 'div' => array('class' => 'submit'),
  6949. '--before--',
  6950. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6951. '/div'
  6952. );
  6953. $this->assertTags($result, $expected);
  6954. $result = $this->Form->submit('Test', array('after' => $after));
  6955. $expected = array(
  6956. 'div' => array('class' => 'submit'),
  6957. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6958. '--after--',
  6959. '/div'
  6960. );
  6961. $this->assertTags($result, $expected);
  6962. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6963. $expected = array(
  6964. 'div' => array('class' => 'submit'),
  6965. '--before--',
  6966. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6967. '--after--',
  6968. '/div'
  6969. );
  6970. $this->assertTags($result, $expected);
  6971. }
  6972. /**
  6973. * test image submit types.
  6974. *
  6975. * @return void
  6976. */
  6977. public function testSubmitImage() {
  6978. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6979. $expected = array(
  6980. 'div' => array('class' => 'submit'),
  6981. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6982. '/div'
  6983. );
  6984. $this->assertTags($result, $expected);
  6985. $result = $this->Form->submit('/relative/cake.power.gif');
  6986. $expected = array(
  6987. 'div' => array('class' => 'submit'),
  6988. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6989. '/div'
  6990. );
  6991. $this->assertTags($result, $expected);
  6992. $result = $this->Form->submit('cake.power.gif');
  6993. $expected = array(
  6994. 'div' => array('class' => 'submit'),
  6995. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6996. '/div'
  6997. );
  6998. $this->assertTags($result, $expected);
  6999. $result = $this->Form->submit('Not.an.image');
  7000. $expected = array(
  7001. 'div' => array('class' => 'submit'),
  7002. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7003. '/div'
  7004. );
  7005. $this->assertTags($result, $expected);
  7006. $after = '--after--';
  7007. $before = '--before--';
  7008. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7009. $expected = array(
  7010. 'div' => array('class' => 'submit'),
  7011. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7012. '--after--',
  7013. '/div'
  7014. );
  7015. $this->assertTags($result, $expected);
  7016. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7017. $expected = array(
  7018. 'div' => array('class' => 'submit'),
  7019. '--before--',
  7020. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7021. '/div'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7025. $expected = array(
  7026. 'div' => array('class' => 'submit'),
  7027. '--before--',
  7028. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7029. '--after--',
  7030. '/div'
  7031. );
  7032. $this->assertTags($result, $expected);
  7033. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7034. $expected = array(
  7035. 'div' => array('class' => 'submit'),
  7036. '--before--',
  7037. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7038. '--after--',
  7039. '/div'
  7040. );
  7041. $this->assertTags($result, $expected);
  7042. }
  7043. /**
  7044. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7045. * be submitted at a time.
  7046. *
  7047. * @return void
  7048. */
  7049. public function testSubmitUnlockedByDefault() {
  7050. $this->Form->request->params['_Token']['key'] = 'secured';
  7051. $this->Form->submit('Go go');
  7052. $this->Form->submit('Save', array('name' => 'save'));
  7053. $result = $this->Form->unlockField();
  7054. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7055. }
  7056. /**
  7057. * Test submit image with timestamps.
  7058. *
  7059. * @return void
  7060. */
  7061. public function testSubmitImageTimestamp() {
  7062. Configure::write('Asset.timestamp', 'force');
  7063. $result = $this->Form->submit('cake.power.gif');
  7064. $expected = array(
  7065. 'div' => array('class' => 'submit'),
  7066. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7067. '/div'
  7068. );
  7069. $this->assertTags($result, $expected);
  7070. }
  7071. /**
  7072. * test the create() method
  7073. *
  7074. * @return void
  7075. */
  7076. public function testCreate() {
  7077. $result = $this->Form->create('Contact');
  7078. $encoding = strtolower(Configure::read('App.encoding'));
  7079. $expected = array(
  7080. 'form' => array(
  7081. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7082. 'accept-charset' => $encoding
  7083. ),
  7084. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7085. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7086. '/div'
  7087. );
  7088. $this->assertTags($result, $expected);
  7089. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7090. $expected = array('form' => array(
  7091. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7092. 'accept-charset' => $encoding
  7093. ));
  7094. $this->assertTags($result, $expected);
  7095. $result = $this->Form->create('Contact', array('type' => 'get'));
  7096. $expected = array('form' => array(
  7097. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7098. 'accept-charset' => $encoding
  7099. ));
  7100. $this->assertTags($result, $expected);
  7101. $result = $this->Form->create('Contact', array('type' => 'put'));
  7102. $expected = array(
  7103. 'form' => array(
  7104. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7105. 'accept-charset' => $encoding
  7106. ),
  7107. 'div' => array('style' => 'display:none;'),
  7108. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7109. '/div'
  7110. );
  7111. $this->assertTags($result, $expected);
  7112. $result = $this->Form->create('Contact', array('type' => 'file'));
  7113. $expected = array(
  7114. 'form' => array(
  7115. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7116. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7117. ),
  7118. 'div' => array('style' => 'display:none;'),
  7119. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7120. '/div'
  7121. );
  7122. $this->assertTags($result, $expected);
  7123. $this->Form->request->data['Contact']['id'] = 1;
  7124. $this->Form->request->here = '/contacts/edit/1';
  7125. $this->Form->request['action'] = 'edit';
  7126. $result = $this->Form->create('Contact');
  7127. $expected = array(
  7128. 'form' => array(
  7129. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7130. 'accept-charset' => $encoding
  7131. ),
  7132. 'div' => array('style' => 'display:none;'),
  7133. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7134. '/div'
  7135. );
  7136. $this->assertTags($result, $expected);
  7137. $this->Form->request->data['Contact']['id'] = 1;
  7138. $this->Form->request->here = '/contacts/edit/1';
  7139. $this->Form->request['action'] = 'edit';
  7140. $result = $this->Form->create('Contact', array('type' => 'file'));
  7141. $expected = array(
  7142. 'form' => array(
  7143. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7144. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7145. ),
  7146. 'div' => array('style' => 'display:none;'),
  7147. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7148. '/div'
  7149. );
  7150. $this->assertTags($result, $expected);
  7151. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7152. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7153. $expected = array(
  7154. 'form' => array(
  7155. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7156. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7157. ),
  7158. 'div' => array('style' => 'display:none;'),
  7159. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7160. '/div'
  7161. );
  7162. $this->assertTags($result, $expected);
  7163. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7164. $expected = array(
  7165. 'form' => array(
  7166. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7167. 'accept-charset' => $encoding
  7168. ),
  7169. 'div' => array('style' => 'display:none;'),
  7170. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7171. '/div'
  7172. );
  7173. $this->assertTags($result, $expected);
  7174. $this->Form->request['action'] = 'add';
  7175. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7176. $expected = array(
  7177. 'form' => array(
  7178. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7179. 'accept-charset' => $encoding
  7180. ),
  7181. 'div' => array('style' => 'display:none;'),
  7182. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7183. '/div'
  7184. );
  7185. $this->assertTags($result, $expected);
  7186. $result = $this->Form->create('User', array('action' => 'login'));
  7187. $expected = array(
  7188. 'form' => array(
  7189. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7190. 'accept-charset' => $encoding
  7191. ),
  7192. 'div' => array('style' => 'display:none;'),
  7193. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7194. '/div'
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. $result = $this->Form->create('User', array('url' => '/users/login'));
  7198. $expected = array(
  7199. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7200. 'div' => array('style' => 'display:none;'),
  7201. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7202. '/div'
  7203. );
  7204. $this->assertTags($result, $expected);
  7205. $this->Form->request['controller'] = 'pages';
  7206. $result = $this->Form->create('User', array('action' => 'signup'));
  7207. $expected = array(
  7208. 'form' => array(
  7209. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7210. 'accept-charset' => $encoding
  7211. ),
  7212. 'div' => array('style' => 'display:none;'),
  7213. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7214. '/div'
  7215. );
  7216. $this->assertTags($result, $expected);
  7217. $this->Form->request->data = array();
  7218. $this->Form->request['controller'] = 'contacts';
  7219. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7220. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7221. $expected = array(
  7222. 'form' => array(
  7223. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7224. 'accept-charset' => 'utf-8'
  7225. ),
  7226. 'div' => array('style' => 'display:none;'),
  7227. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7228. '/div'
  7229. );
  7230. $this->assertTags($result, $expected);
  7231. }
  7232. /**
  7233. * Test the onsubmit option for create()
  7234. *
  7235. * @return void
  7236. */
  7237. public function testCreateOnSubmit() {
  7238. $this->Form->request->data = array();
  7239. $this->Form->request['controller'] = 'contacts';
  7240. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7241. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7242. $expected = array(
  7243. 'form' => array(
  7244. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7245. 'accept-charset' => 'utf-8'
  7246. ),
  7247. 'div' => array('style' => 'display:none;'),
  7248. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7249. '/div'
  7250. );
  7251. $this->assertTags($result, $expected);
  7252. $this->Form->request->data = array();
  7253. $this->Form->request['controller'] = 'contacts';
  7254. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7255. $result = $this->Form->create(array(
  7256. 'url' => array('action' => 'index', 'param'),
  7257. 'default' => false,
  7258. 'onsubmit' => 'someFunction();'
  7259. ));
  7260. $expected = array(
  7261. 'form' => array(
  7262. 'id' => 'ContactAddForm', 'method' => 'post',
  7263. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7264. 'action' => '/contacts/index/param',
  7265. 'accept-charset' => 'utf-8'
  7266. ),
  7267. 'div' => array('style' => 'display:none;'),
  7268. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7269. '/div'
  7270. );
  7271. $this->assertTags($result, $expected);
  7272. }
  7273. /**
  7274. * test create() with automatic url generation
  7275. *
  7276. * @return void
  7277. */
  7278. public function testCreateAutoUrl() {
  7279. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7280. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7281. $this->Form->request->base = '/base_url';
  7282. $result = $this->Form->create('Contact');
  7283. $expected = array(
  7284. 'form' => array(
  7285. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7286. 'accept-charset' => 'utf-8'
  7287. ),
  7288. 'div' => array('style' => 'display:none;'),
  7289. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7290. '/div'
  7291. );
  7292. $this->assertTags($result, $expected);
  7293. $this->Form->request['action'] = 'delete';
  7294. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7295. $this->Form->request->base = '/base_url';
  7296. $result = $this->Form->create('Contact');
  7297. $expected = array(
  7298. 'form' => array(
  7299. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7300. 'accept-charset' => 'utf-8'
  7301. ),
  7302. 'div' => array('style' => 'display:none;'),
  7303. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7304. '/div'
  7305. );
  7306. $this->assertTags($result, $expected);
  7307. }
  7308. /**
  7309. * test create() with a custom route
  7310. *
  7311. * @return void
  7312. */
  7313. public function testCreateCustomRoute() {
  7314. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7315. $encoding = strtolower(Configure::read('App.encoding'));
  7316. $result = $this->Form->create('User', array('action' => 'login'));
  7317. $expected = array(
  7318. 'form' => array(
  7319. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7320. 'accept-charset' => $encoding
  7321. ),
  7322. 'div' => array('style' => 'display:none;'),
  7323. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7324. '/div'
  7325. );
  7326. $this->assertTags($result, $expected);
  7327. }
  7328. /**
  7329. * test that inputDefaults are stored and used.
  7330. *
  7331. * @return void
  7332. */
  7333. public function testCreateWithInputDefaults() {
  7334. $this->Form->create('User', array(
  7335. 'inputDefaults' => array(
  7336. 'div' => false,
  7337. 'label' => false,
  7338. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7339. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7340. )
  7341. ));
  7342. $result = $this->Form->input('username');
  7343. $expected = array(
  7344. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7345. );
  7346. $this->assertTags($result, $expected);
  7347. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7348. $expected = array(
  7349. 'div' => array('class' => 'input text'),
  7350. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7351. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7352. '/div'
  7353. );
  7354. $this->assertTags($result, $expected);
  7355. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7356. $expected = array(
  7357. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7358. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7359. );
  7360. $this->assertTags($result, $expected);
  7361. $this->Form->create('User', array(
  7362. 'inputDefaults' => array(
  7363. 'div' => false,
  7364. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7365. )
  7366. ));
  7367. $result = $this->Form->input('username', array('div' => true));
  7368. $expected = array(
  7369. 'div' => array('class' => 'input text'),
  7370. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7371. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7372. '/div'
  7373. );
  7374. $this->assertTags($result, $expected);
  7375. }
  7376. /**
  7377. * test automatic accept-charset overriding
  7378. *
  7379. * @return void
  7380. */
  7381. public function testCreateWithAcceptCharset() {
  7382. $result = $this->Form->create('UserForm', array(
  7383. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7384. )
  7385. );
  7386. $expected = array(
  7387. 'form' => array(
  7388. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7389. 'accept-charset' => 'iso-8859-1'
  7390. ),
  7391. 'div' => array('style' => 'display:none;'),
  7392. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7393. '/div'
  7394. );
  7395. $this->assertTags($result, $expected);
  7396. }
  7397. /**
  7398. * Test base form URL when url param is passed with multiple parameters (&)
  7399. *
  7400. */
  7401. public function testCreateQuerystringrequest() {
  7402. $encoding = strtolower(Configure::read('App.encoding'));
  7403. $result = $this->Form->create('Contact', array(
  7404. 'type' => 'post',
  7405. 'escape' => false,
  7406. 'url' => array(
  7407. 'controller' => 'controller',
  7408. 'action' => 'action',
  7409. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7410. )
  7411. ));
  7412. $expected = array(
  7413. 'form' => array(
  7414. 'id' => 'ContactAddForm',
  7415. 'method' => 'post',
  7416. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7417. 'accept-charset' => $encoding
  7418. ),
  7419. 'div' => array('style' => 'display:none;'),
  7420. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7421. '/div'
  7422. );
  7423. $this->assertTags($result, $expected);
  7424. $result = $this->Form->create('Contact', array(
  7425. 'type' => 'post',
  7426. 'url' => array(
  7427. 'controller' => 'controller',
  7428. 'action' => 'action',
  7429. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7430. )
  7431. ));
  7432. $expected = array(
  7433. 'form' => array(
  7434. 'id' => 'ContactAddForm',
  7435. 'method' => 'post',
  7436. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7437. 'accept-charset' => $encoding
  7438. ),
  7439. 'div' => array('style' => 'display:none;'),
  7440. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7441. '/div'
  7442. );
  7443. $this->assertTags($result, $expected);
  7444. }
  7445. /**
  7446. * test that create() doesn't cause errors by multiple id's being in the primary key
  7447. * as could happen with multiple select or checkboxes.
  7448. *
  7449. * @return void
  7450. */
  7451. public function testCreateWithMultipleIdInData() {
  7452. $encoding = strtolower(Configure::read('App.encoding'));
  7453. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7454. $result = $this->Form->create('Contact');
  7455. $expected = array(
  7456. 'form' => array(
  7457. 'id' => 'ContactAddForm',
  7458. 'method' => 'post',
  7459. 'action' => '/contacts/add',
  7460. 'accept-charset' => $encoding
  7461. ),
  7462. 'div' => array('style' => 'display:none;'),
  7463. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7464. '/div'
  7465. );
  7466. $this->assertTags($result, $expected);
  7467. }
  7468. /**
  7469. * test that create() doesn't add in extra passed params.
  7470. *
  7471. * @return void
  7472. */
  7473. public function testCreatePassedArgs() {
  7474. $encoding = strtolower(Configure::read('App.encoding'));
  7475. $this->Form->request->data['Contact']['id'] = 1;
  7476. $result = $this->Form->create('Contact', array(
  7477. 'type' => 'post',
  7478. 'escape' => false,
  7479. 'url' => array(
  7480. 'action' => 'edit',
  7481. 'myparam'
  7482. )
  7483. ));
  7484. $expected = array(
  7485. 'form' => array(
  7486. 'id' => 'ContactAddForm',
  7487. 'method' => 'post',
  7488. 'action' => '/contacts/edit/myparam',
  7489. 'accept-charset' => $encoding
  7490. ),
  7491. 'div' => array('style' => 'display:none;'),
  7492. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7493. '/div'
  7494. );
  7495. $this->assertTags($result, $expected);
  7496. }
  7497. /**
  7498. * test creating a get form, and get form inputs.
  7499. *
  7500. * @return void
  7501. */
  7502. public function testGetFormCreate() {
  7503. $encoding = strtolower(Configure::read('App.encoding'));
  7504. $result = $this->Form->create('Contact', array('type' => 'get'));
  7505. $this->assertTags($result, array('form' => array(
  7506. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7507. 'accept-charset' => $encoding
  7508. )));
  7509. $result = $this->Form->text('Contact.name');
  7510. $this->assertTags($result, array('input' => array(
  7511. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7512. )));
  7513. $result = $this->Form->password('password');
  7514. $this->assertTags($result, array('input' => array(
  7515. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7516. )));
  7517. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7518. $result = $this->Form->text('user_form');
  7519. $this->assertTags($result, array('input' => array(
  7520. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7521. )));
  7522. }
  7523. /**
  7524. * test get form, and inputs when the model param is false
  7525. *
  7526. * @return void
  7527. */
  7528. public function testGetFormWithFalseModel() {
  7529. $encoding = strtolower(Configure::read('App.encoding'));
  7530. $this->Form->request['controller'] = 'contact_test';
  7531. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7532. $expected = array('form' => array(
  7533. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7534. 'accept-charset' => $encoding
  7535. ));
  7536. $this->assertTags($result, $expected);
  7537. $result = $this->Form->text('reason');
  7538. $expected = array(
  7539. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7540. );
  7541. $this->assertTags($result, $expected);
  7542. }
  7543. /**
  7544. * test that datetime() works with GET style forms.
  7545. *
  7546. * @return void
  7547. */
  7548. public function testDateTimeWithGetForms() {
  7549. extract($this->dateRegex);
  7550. $this->Form->create('Contact', array('type' => 'get'));
  7551. $result = $this->Form->datetime('created');
  7552. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7553. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7554. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7555. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7556. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7557. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7558. }
  7559. /**
  7560. * testEditFormWithData method
  7561. *
  7562. * test auto populating form elements from submitted data.
  7563. *
  7564. * @return void
  7565. */
  7566. public function testEditFormWithData() {
  7567. $this->Form->request->data = array('Person' => array(
  7568. 'id' => 1,
  7569. 'first_name' => 'Nate',
  7570. 'last_name' => 'Abele',
  7571. 'email' => 'nate@example.com'
  7572. ));
  7573. $this->Form->request->addParams(array(
  7574. 'models' => array('Person'),
  7575. 'controller' => 'people',
  7576. 'action' => 'add'
  7577. ));
  7578. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7579. $this->Form->create();
  7580. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7581. $expected = array(
  7582. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7583. 'select' => array(
  7584. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7585. ),
  7586. array('option' => array('value' => 1)), 'Nate', '/option',
  7587. array('option' => array('value' => 2)), 'Garrett', '/option',
  7588. array('option' => array('value' => 3)), 'Larry', '/option',
  7589. '/select'
  7590. );
  7591. $this->assertTags($result, $expected);
  7592. }
  7593. /**
  7594. * Test that required fields are created for various types of validation.
  7595. *
  7596. * @return void
  7597. */
  7598. public function testFormInputRequiredDetection() {
  7599. $this->Form->create('Contact');
  7600. $result = $this->Form->input('Contact.non_existing');
  7601. $expected = array(
  7602. 'div' => array('class' => 'input text'),
  7603. 'label' => array('for' => 'ContactNonExisting'),
  7604. 'Non Existing',
  7605. '/label',
  7606. 'input' => array(
  7607. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7608. 'id' => 'ContactNonExisting'
  7609. ),
  7610. '/div'
  7611. );
  7612. $this->assertTags($result, $expected);
  7613. $result = $this->Form->input('Contact.imrequired');
  7614. $expected = array(
  7615. 'div' => array('class' => 'input text required'),
  7616. 'label' => array('for' => 'ContactImrequired'),
  7617. 'Imrequired',
  7618. '/label',
  7619. 'input' => array(
  7620. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7621. 'id' => 'ContactImrequired',
  7622. 'required' => 'required'
  7623. ),
  7624. '/div'
  7625. );
  7626. $this->assertTags($result, $expected);
  7627. $result = $this->Form->input('Contact.imalsorequired');
  7628. $expected = array(
  7629. 'div' => array('class' => 'input text required'),
  7630. 'label' => array('for' => 'ContactImalsorequired'),
  7631. 'Imalsorequired',
  7632. '/label',
  7633. 'input' => array(
  7634. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7635. 'id' => 'ContactImalsorequired',
  7636. 'required' => 'required'
  7637. ),
  7638. '/div'
  7639. );
  7640. $this->assertTags($result, $expected);
  7641. $result = $this->Form->input('Contact.imrequiredtoo');
  7642. $expected = array(
  7643. 'div' => array('class' => 'input text required'),
  7644. 'label' => array('for' => 'ContactImrequiredtoo'),
  7645. 'Imrequiredtoo',
  7646. '/label',
  7647. 'input' => array(
  7648. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7649. 'id' => 'ContactImrequiredtoo',
  7650. 'required' => 'required'
  7651. ),
  7652. '/div'
  7653. );
  7654. $this->assertTags($result, $expected);
  7655. $result = $this->Form->input('Contact.required_one');
  7656. $expected = array(
  7657. 'div' => array('class' => 'input text required'),
  7658. 'label' => array('for' => 'ContactRequiredOne'),
  7659. 'Required One',
  7660. '/label',
  7661. 'input' => array(
  7662. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7663. 'id' => 'ContactRequiredOne',
  7664. 'required' => 'required'
  7665. ),
  7666. '/div'
  7667. );
  7668. $this->assertTags($result, $expected);
  7669. $result = $this->Form->input('Contact.string_required');
  7670. $expected = array(
  7671. 'div' => array('class' => 'input text required'),
  7672. 'label' => array('for' => 'ContactStringRequired'),
  7673. 'String Required',
  7674. '/label',
  7675. 'input' => array(
  7676. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7677. 'id' => 'ContactStringRequired',
  7678. 'required' => 'required'
  7679. ),
  7680. '/div'
  7681. );
  7682. $this->assertTags($result, $expected);
  7683. $result = $this->Form->input('Contact.imnotrequired');
  7684. $expected = array(
  7685. 'div' => array('class' => 'input text'),
  7686. 'label' => array('for' => 'ContactImnotrequired'),
  7687. 'Imnotrequired',
  7688. '/label',
  7689. 'input' => array(
  7690. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7691. 'id' => 'ContactImnotrequired'
  7692. ),
  7693. '/div'
  7694. );
  7695. $this->assertTags($result, $expected);
  7696. $result = $this->Form->input('Contact.imalsonotrequired');
  7697. $expected = array(
  7698. 'div' => array('class' => 'input text'),
  7699. 'label' => array('for' => 'ContactImalsonotrequired'),
  7700. 'Imalsonotrequired',
  7701. '/label',
  7702. 'input' => array(
  7703. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7704. 'id' => 'ContactImalsonotrequired'
  7705. ),
  7706. '/div'
  7707. );
  7708. $this->assertTags($result, $expected);
  7709. $result = $this->Form->input('Contact.imalsonotrequired2');
  7710. $expected = array(
  7711. 'div' => array('class' => 'input text'),
  7712. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7713. 'Imalsonotrequired2',
  7714. '/label',
  7715. 'input' => array(
  7716. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7717. 'id' => 'ContactImalsonotrequired2'
  7718. ),
  7719. '/div'
  7720. );
  7721. $this->assertTags($result, $expected);
  7722. $result = $this->Form->input('Contact.imnotrequiredeither');
  7723. $expected = array(
  7724. 'div' => array('class' => 'input text'),
  7725. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7726. 'Imnotrequiredeither',
  7727. '/label',
  7728. 'input' => array(
  7729. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7730. 'id' => 'ContactImnotrequiredeither'
  7731. ),
  7732. '/div'
  7733. );
  7734. $this->assertTags($result, $expected);
  7735. $result = $this->Form->input('Contact.iamrequiredalways');
  7736. $expected = array(
  7737. 'div' => array('class' => 'input text required'),
  7738. 'label' => array('for' => 'ContactIamrequiredalways'),
  7739. 'Iamrequiredalways',
  7740. '/label',
  7741. 'input' => array(
  7742. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7743. 'id' => 'ContactIamrequiredalways',
  7744. 'required' => 'required'
  7745. ),
  7746. '/div'
  7747. );
  7748. $this->assertTags($result, $expected);
  7749. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7750. $expected = array(
  7751. 'div' => array('class' => 'input checkbox required'),
  7752. array('input' => array(
  7753. 'type' => 'hidden',
  7754. 'name' => 'data[Contact][boolean_field]',
  7755. 'id' => 'ContactBooleanField_',
  7756. 'value' => '0'
  7757. )),
  7758. array('input' => array(
  7759. 'type' => 'checkbox',
  7760. 'name' => 'data[Contact][boolean_field]',
  7761. 'value' => '1',
  7762. 'id' => 'ContactBooleanField'
  7763. )),
  7764. 'label' => array('for' => 'ContactBooleanField'),
  7765. 'Boolean Field',
  7766. '/label',
  7767. '/div'
  7768. );
  7769. $this->assertTags($result, $expected);
  7770. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7771. $expected = array(
  7772. 'div' => array('class' => 'input checkbox required'),
  7773. array('input' => array(
  7774. 'type' => 'hidden',
  7775. 'name' => 'data[Contact][boolean_field]',
  7776. 'id' => 'ContactBooleanField_',
  7777. 'value' => '0'
  7778. )),
  7779. array('input' => array(
  7780. 'type' => 'checkbox',
  7781. 'name' => 'data[Contact][boolean_field]',
  7782. 'value' => '1',
  7783. 'id' => 'ContactBooleanField',
  7784. 'required' => 'required'
  7785. )),
  7786. 'label' => array('for' => 'ContactBooleanField'),
  7787. 'Boolean Field',
  7788. '/label',
  7789. '/div'
  7790. );
  7791. $this->assertTags($result, $expected);
  7792. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7793. $expected = array(
  7794. 'div' => array('class' => 'input file required'),
  7795. 'label' => array('for' => 'ContactIamrequiredalways'),
  7796. 'Iamrequiredalways',
  7797. '/label',
  7798. 'input' => array(
  7799. 'type' => 'file',
  7800. 'name' => 'data[Contact][iamrequiredalways]',
  7801. 'id' => 'ContactIamrequiredalways',
  7802. 'required' => 'required'
  7803. ),
  7804. '/div'
  7805. );
  7806. $this->assertTags($result, $expected);
  7807. }
  7808. /**
  7809. * Test that required fields are created when only using ModelValidator::add().
  7810. *
  7811. * @return void
  7812. */
  7813. public function testFormInputRequiredDetectionModelValidator() {
  7814. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7815. $this->Form->create('ContactTag');
  7816. $result = $this->Form->input('ContactTag.iwillberequired');
  7817. $expected = array(
  7818. 'div' => array('class' => 'input text required'),
  7819. 'label' => array('for' => 'ContactTagIwillberequired'),
  7820. 'Iwillberequired',
  7821. '/label',
  7822. 'input' => array(
  7823. 'name' => 'data[ContactTag][iwillberequired]',
  7824. 'type' => 'text',
  7825. 'id' => 'ContactTagIwillberequired',
  7826. 'required' => 'required'
  7827. ),
  7828. '/div'
  7829. );
  7830. $this->assertTags($result, $expected);
  7831. }
  7832. /**
  7833. * testFormMagicInput method
  7834. *
  7835. * @return void
  7836. */
  7837. public function testFormMagicInput() {
  7838. $encoding = strtolower(Configure::read('App.encoding'));
  7839. $result = $this->Form->create('Contact');
  7840. $expected = array(
  7841. 'form' => array(
  7842. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7843. 'accept-charset' => $encoding
  7844. ),
  7845. 'div' => array('style' => 'display:none;'),
  7846. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7847. '/div'
  7848. );
  7849. $this->assertTags($result, $expected);
  7850. $result = $this->Form->input('name');
  7851. $expected = array(
  7852. 'div' => array('class' => 'input text'),
  7853. 'label' => array('for' => 'ContactName'),
  7854. 'Name',
  7855. '/label',
  7856. 'input' => array(
  7857. 'type' => 'text', 'name' => 'data[Contact][name]',
  7858. 'id' => 'ContactName', 'maxlength' => '255'
  7859. ),
  7860. '/div'
  7861. );
  7862. $this->assertTags($result, $expected);
  7863. $result = $this->Form->input('non_existing_field_in_contact_model');
  7864. $expected = array(
  7865. 'div' => array('class' => 'input text'),
  7866. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7867. 'Non Existing Field In Contact Model',
  7868. '/label',
  7869. 'input' => array(
  7870. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7871. 'id' => 'ContactNonExistingFieldInContactModel'
  7872. ),
  7873. '/div'
  7874. );
  7875. $this->assertTags($result, $expected);
  7876. $result = $this->Form->input('Address.street');
  7877. $expected = array(
  7878. 'div' => array('class' => 'input text'),
  7879. 'label' => array('for' => 'AddressStreet'),
  7880. 'Street',
  7881. '/label',
  7882. 'input' => array(
  7883. 'type' => 'text', 'name' => 'data[Address][street]',
  7884. 'id' => 'AddressStreet'
  7885. ),
  7886. '/div'
  7887. );
  7888. $this->assertTags($result, $expected);
  7889. $result = $this->Form->input('Address.non_existing_field_in_model');
  7890. $expected = array(
  7891. 'div' => array('class' => 'input text'),
  7892. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7893. 'Non Existing Field In Model',
  7894. '/label',
  7895. 'input' => array(
  7896. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  7897. 'id' => 'AddressNonExistingFieldInModel'
  7898. ),
  7899. '/div'
  7900. );
  7901. $this->assertTags($result, $expected);
  7902. $result = $this->Form->input('name', array('div' => false));
  7903. $expected = array(
  7904. 'label' => array('for' => 'ContactName'),
  7905. 'Name',
  7906. '/label',
  7907. 'input' => array(
  7908. 'type' => 'text', 'name' => 'data[Contact][name]',
  7909. 'id' => 'ContactName', 'maxlength' => '255'
  7910. )
  7911. );
  7912. $this->assertTags($result, $expected);
  7913. extract($this->dateRegex);
  7914. $now = strtotime('now');
  7915. $result = $this->Form->input('Contact.published', array('div' => false));
  7916. $expected = array(
  7917. 'label' => array('for' => 'ContactPublishedMonth'),
  7918. 'Published',
  7919. '/label',
  7920. array('select' => array(
  7921. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  7922. )),
  7923. $monthsRegex,
  7924. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7925. date('F', $now),
  7926. '/option',
  7927. '*/select',
  7928. '-',
  7929. array('select' => array(
  7930. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  7931. )),
  7932. $daysRegex,
  7933. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7934. date('j', $now),
  7935. '/option',
  7936. '*/select',
  7937. '-',
  7938. array('select' => array(
  7939. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  7940. )),
  7941. $yearsRegex,
  7942. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7943. date('Y', $now),
  7944. '*/select'
  7945. );
  7946. $this->assertTags($result, $expected);
  7947. $result = $this->Form->input('Contact.updated', array('div' => false));
  7948. $expected = array(
  7949. 'label' => array('for' => 'ContactUpdatedMonth'),
  7950. 'Updated',
  7951. '/label',
  7952. array('select' => array(
  7953. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  7954. )),
  7955. $monthsRegex,
  7956. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7957. date('F', $now),
  7958. '/option',
  7959. '*/select',
  7960. '-',
  7961. array('select' => array(
  7962. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  7963. )),
  7964. $daysRegex,
  7965. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7966. date('j', $now),
  7967. '/option',
  7968. '*/select',
  7969. '-',
  7970. array('select' => array(
  7971. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  7972. )),
  7973. $yearsRegex,
  7974. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7975. date('Y', $now),
  7976. '*/select'
  7977. );
  7978. $this->assertTags($result, $expected);
  7979. $result = $this->Form->input('UserForm.stuff');
  7980. $expected = array(
  7981. 'div' => array('class' => 'input text'),
  7982. 'label' => array('for' => 'UserFormStuff'),
  7983. 'Stuff',
  7984. '/label',
  7985. 'input' => array(
  7986. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  7987. 'id' => 'UserFormStuff', 'maxlength' => 10
  7988. ),
  7989. '/div'
  7990. );
  7991. $this->assertTags($result, $expected);
  7992. }
  7993. /**
  7994. * testForMagicInputNonExistingNorValidated method
  7995. *
  7996. * @return void
  7997. */
  7998. public function testForMagicInputNonExistingNorValidated() {
  7999. $encoding = strtolower(Configure::read('App.encoding'));
  8000. $result = $this->Form->create('Contact');
  8001. $expected = array(
  8002. 'form' => array(
  8003. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8004. 'accept-charset' => $encoding
  8005. ),
  8006. 'div' => array('style' => 'display:none;'),
  8007. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8008. '/div'
  8009. );
  8010. $this->assertTags($result, $expected);
  8011. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8012. $expected = array(
  8013. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8014. 'Non Existing Nor Validated',
  8015. '/label',
  8016. 'input' => array(
  8017. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8018. 'id' => 'ContactNonExistingNorValidated'
  8019. )
  8020. );
  8021. $this->assertTags($result, $expected);
  8022. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8023. 'div' => false, 'value' => 'my value'
  8024. ));
  8025. $expected = array(
  8026. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8027. 'Non Existing Nor Validated',
  8028. '/label',
  8029. 'input' => array(
  8030. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8031. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8032. )
  8033. );
  8034. $this->assertTags($result, $expected);
  8035. $this->Form->request->data = array(
  8036. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8037. ));
  8038. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8039. $expected = array(
  8040. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8041. 'Non Existing Nor Validated',
  8042. '/label',
  8043. 'input' => array(
  8044. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8045. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8046. )
  8047. );
  8048. $this->assertTags($result, $expected);
  8049. }
  8050. /**
  8051. * testFormMagicInputLabel method
  8052. *
  8053. * @return void
  8054. */
  8055. public function testFormMagicInputLabel() {
  8056. $encoding = strtolower(Configure::read('App.encoding'));
  8057. $result = $this->Form->create('Contact');
  8058. $expected = array(
  8059. 'form' => array(
  8060. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8061. 'accept-charset' => $encoding
  8062. ),
  8063. 'div' => array('style' => 'display:none;'),
  8064. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8065. '/div'
  8066. );
  8067. $this->assertTags($result, $expected);
  8068. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8069. $this->assertTags($result, array('input' => array(
  8070. 'name' => 'data[Contact][name]', 'type' => 'text',
  8071. 'id' => 'ContactName', 'maxlength' => '255')
  8072. ));
  8073. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8074. $expected = array(
  8075. 'label' => array('for' => 'ContactName'),
  8076. 'My label',
  8077. '/label',
  8078. 'input' => array(
  8079. 'type' => 'text', 'name' => 'data[Contact][name]',
  8080. 'id' => 'ContactName', 'maxlength' => '255'
  8081. )
  8082. );
  8083. $this->assertTags($result, $expected);
  8084. $result = $this->Form->input('Contact.name', array(
  8085. 'div' => false, 'label' => array('class' => 'mandatory')
  8086. ));
  8087. $expected = array(
  8088. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8089. 'Name',
  8090. '/label',
  8091. 'input' => array(
  8092. 'type' => 'text', 'name' => 'data[Contact][name]',
  8093. 'id' => 'ContactName', 'maxlength' => '255'
  8094. )
  8095. );
  8096. $this->assertTags($result, $expected);
  8097. $result = $this->Form->input('Contact.name', array(
  8098. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8099. ));
  8100. $expected = array(
  8101. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8102. 'My label',
  8103. '/label',
  8104. 'input' => array(
  8105. 'type' => 'text', 'name' => 'data[Contact][name]',
  8106. 'id' => 'ContactName', 'maxlength' => '255'
  8107. )
  8108. );
  8109. $this->assertTags($result, $expected);
  8110. $result = $this->Form->input('Contact.name', array(
  8111. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8112. ));
  8113. $expected = array(
  8114. 'label' => array('for' => 'my_id'),
  8115. 'Name',
  8116. '/label',
  8117. 'input' => array(
  8118. 'type' => 'text', 'name' => 'data[Contact][name]',
  8119. 'id' => 'my_id', 'maxlength' => '255'
  8120. )
  8121. );
  8122. $this->assertTags($result, $expected);
  8123. $result = $this->Form->input('1.id');
  8124. $this->assertTags($result, array('input' => array(
  8125. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8126. 'id' => 'Contact1Id'
  8127. )));
  8128. $result = $this->Form->input("1.name");
  8129. $expected = array(
  8130. 'div' => array('class' => 'input text'),
  8131. 'label' => array('for' => 'Contact1Name'),
  8132. 'Name',
  8133. '/label',
  8134. 'input' => array(
  8135. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8136. 'id' => 'Contact1Name', 'maxlength' => '255'
  8137. ),
  8138. '/div'
  8139. );
  8140. $this->assertTags($result, $expected);
  8141. $result = $this->Form->input('Contact.1.id');
  8142. $this->assertTags($result, array(
  8143. 'input' => array(
  8144. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8145. 'id' => 'Contact1Id'
  8146. )
  8147. ));
  8148. $result = $this->Form->input("Model.1.name");
  8149. $expected = array(
  8150. 'div' => array('class' => 'input text'),
  8151. 'label' => array('for' => 'Model1Name'),
  8152. 'Name',
  8153. '/label',
  8154. 'input' => array(
  8155. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8156. 'id' => 'Model1Name'
  8157. ),
  8158. '/div'
  8159. );
  8160. $this->assertTags($result, $expected);
  8161. }
  8162. /**
  8163. * testFormEnd method
  8164. *
  8165. * @return void
  8166. */
  8167. public function testFormEnd() {
  8168. $this->assertEquals('</form>', $this->Form->end());
  8169. $result = $this->Form->end('');
  8170. $expected = array(
  8171. 'div' => array('class' => 'submit'),
  8172. 'input' => array('type' => 'submit', 'value' => ''),
  8173. '/div',
  8174. '/form'
  8175. );
  8176. $this->assertTags($result, $expected);
  8177. $result = $this->Form->end(array('label' => ''));
  8178. $expected = array(
  8179. 'div' => array('class' => 'submit'),
  8180. 'input' => array('type' => 'submit', 'value' => ''),
  8181. '/div',
  8182. '/form'
  8183. );
  8184. $this->assertTags($result, $expected);
  8185. $result = $this->Form->end('save');
  8186. $expected = array(
  8187. 'div' => array('class' => 'submit'),
  8188. 'input' => array('type' => 'submit', 'value' => 'save'),
  8189. '/div',
  8190. '/form'
  8191. );
  8192. $this->assertTags($result, $expected);
  8193. $result = $this->Form->end(array('label' => 'save'));
  8194. $expected = array(
  8195. 'div' => array('class' => 'submit'),
  8196. 'input' => array('type' => 'submit', 'value' => 'save'),
  8197. '/div',
  8198. '/form'
  8199. );
  8200. $this->assertTags($result, $expected);
  8201. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8202. $expected = array(
  8203. 'div' => array('class' => 'submit'),
  8204. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8205. '/div',
  8206. '/form'
  8207. );
  8208. $this->assertTags($result, $expected);
  8209. $result = $this->Form->end(array('name' => 'Whatever'));
  8210. $expected = array(
  8211. 'div' => array('class' => 'submit'),
  8212. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8213. '/div',
  8214. '/form'
  8215. );
  8216. $this->assertTags($result, $expected);
  8217. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8218. $expected = array(
  8219. 'div' => array('class' => 'good'),
  8220. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8221. '/div',
  8222. '/form'
  8223. );
  8224. $this->assertTags($result, $expected);
  8225. $result = $this->Form->end(array(
  8226. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8227. ));
  8228. $expected = array(
  8229. 'div' => array('class' => 'good'),
  8230. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8231. '/div',
  8232. '/form'
  8233. );
  8234. $this->assertTags($result, $expected);
  8235. }
  8236. /**
  8237. * testMultipleFormWithIdFields method
  8238. *
  8239. * @return void
  8240. */
  8241. public function testMultipleFormWithIdFields() {
  8242. $this->Form->create('UserForm');
  8243. $result = $this->Form->input('id');
  8244. $this->assertTags($result, array('input' => array(
  8245. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8246. )));
  8247. $result = $this->Form->input('ValidateItem.id');
  8248. $this->assertTags($result, array('input' => array(
  8249. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8250. 'id' => 'ValidateItemId'
  8251. )));
  8252. $result = $this->Form->input('ValidateUser.id');
  8253. $this->assertTags($result, array('input' => array(
  8254. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8255. 'id' => 'ValidateUserId'
  8256. )));
  8257. }
  8258. /**
  8259. * testDbLessModel method
  8260. *
  8261. * @return void
  8262. */
  8263. public function testDbLessModel() {
  8264. $this->Form->create('TestMail');
  8265. $result = $this->Form->input('name');
  8266. $expected = array(
  8267. 'div' => array('class' => 'input text'),
  8268. 'label' => array('for' => 'TestMailName'),
  8269. 'Name',
  8270. '/label',
  8271. 'input' => array(
  8272. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8273. 'id' => 'TestMailName'
  8274. ),
  8275. '/div'
  8276. );
  8277. $this->assertTags($result, $expected);
  8278. ClassRegistry::init('TestMail');
  8279. $this->Form->create('TestMail');
  8280. $result = $this->Form->input('name');
  8281. $expected = array(
  8282. 'div' => array('class' => 'input text'),
  8283. 'label' => array('for' => 'TestMailName'),
  8284. 'Name',
  8285. '/label',
  8286. 'input' => array(
  8287. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8288. 'id' => 'TestMailName'
  8289. ),
  8290. '/div'
  8291. );
  8292. $this->assertTags($result, $expected);
  8293. }
  8294. /**
  8295. * testBrokenness method
  8296. *
  8297. * @return void
  8298. */
  8299. public function testBrokenness() {
  8300. /*
  8301. * #4 This test has two parents and four children. By default (as of r7117) both
  8302. * parents are show but the first parent is missing a child. This is the inconsistency
  8303. * in the default behaviour - one parent has all children, the other does not - dependent
  8304. * on the data values.
  8305. */
  8306. $result = $this->Form->select('Model.field', array(
  8307. 'Fred' => array(
  8308. 'freds_son_1' => 'Fred',
  8309. 'freds_son_2' => 'Freddie'
  8310. ),
  8311. 'Bert' => array(
  8312. 'berts_son_1' => 'Albert',
  8313. 'berts_son_2' => 'Bertie')
  8314. ),
  8315. array('showParents' => true, 'empty' => false)
  8316. );
  8317. $expected = array(
  8318. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8319. array('optgroup' => array('label' => 'Fred')),
  8320. array('option' => array('value' => 'freds_son_1')),
  8321. 'Fred',
  8322. '/option',
  8323. array('option' => array('value' => 'freds_son_2')),
  8324. 'Freddie',
  8325. '/option',
  8326. '/optgroup',
  8327. array('optgroup' => array('label' => 'Bert')),
  8328. array('option' => array('value' => 'berts_son_1')),
  8329. 'Albert',
  8330. '/option',
  8331. array('option' => array('value' => 'berts_son_2')),
  8332. 'Bertie',
  8333. '/option',
  8334. '/optgroup',
  8335. '/select'
  8336. );
  8337. $this->assertTags($result, $expected);
  8338. /*
  8339. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8340. * changed, so we should expect the same select list data, just with a different name
  8341. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8342. * This is where data corruption can occur, because when a select value is missing from
  8343. * a list a form will substitute the first value in the list - without the user knowing.
  8344. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8345. * affect the availability of 3 => 'Three' as a valid option.
  8346. */
  8347. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8348. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8349. ));
  8350. $result = $this->Form->select(
  8351. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8352. );
  8353. $expected = array(
  8354. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8355. array('option' => array('value' => 1)),
  8356. 'One',
  8357. '/option',
  8358. array('option' => array('value' => 2)),
  8359. 'Two',
  8360. '/option',
  8361. array('optgroup' => array('label' => 'Three')),
  8362. array('option' => array('value' => 3)),
  8363. 'Three',
  8364. '/option',
  8365. array('option' => array('value' => 4)),
  8366. 'Four',
  8367. '/option',
  8368. array('option' => array('value' => 5)),
  8369. 'Five',
  8370. '/option',
  8371. '/optgroup',
  8372. '/select'
  8373. );
  8374. $this->assertTags($result, $expected);
  8375. }
  8376. /**
  8377. * Test the generation of fields for a multi record form.
  8378. *
  8379. * @return void
  8380. */
  8381. public function testMultiRecordForm() {
  8382. $this->Form->create('ValidateProfile');
  8383. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8384. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8385. $expected = array(
  8386. 'div' => array('class' => 'input textarea'),
  8387. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8388. 'Name',
  8389. '/label',
  8390. 'textarea' => array(
  8391. 'id' => 'ValidateProfile1ValidateItem2Name',
  8392. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8393. 'cols' => 30,
  8394. 'rows' => 6
  8395. ),
  8396. 'Value',
  8397. '/textarea',
  8398. '/div'
  8399. );
  8400. $this->assertTags($result, $expected);
  8401. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8402. $expected = array(
  8403. 'div' => array('class' => 'input date'),
  8404. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8405. 'Created',
  8406. '/label',
  8407. array('select' => array(
  8408. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8409. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8410. )
  8411. ),
  8412. array('option' => array('value' => '')), '/option',
  8413. $this->dateRegex['monthsRegex'],
  8414. '/select', '-',
  8415. array('select' => array(
  8416. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8417. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8418. )
  8419. ),
  8420. array('option' => array('value' => '')), '/option',
  8421. $this->dateRegex['daysRegex'],
  8422. '/select', '-',
  8423. array('select' => array(
  8424. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8425. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8426. )
  8427. ),
  8428. array('option' => array('value' => '')), '/option',
  8429. $this->dateRegex['yearsRegex'],
  8430. '/select',
  8431. '/div'
  8432. );
  8433. $this->assertTags($result, $expected);
  8434. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8435. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8436. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8437. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8438. $expected = array(
  8439. 'div' => array('class' => 'input select error'),
  8440. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8441. 'Profile',
  8442. '/label',
  8443. 'select' => array(
  8444. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8445. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8446. 'class' => 'form-error'
  8447. ),
  8448. '/select',
  8449. array('div' => array('class' => 'error-message')),
  8450. 'Error',
  8451. '/div',
  8452. '/div'
  8453. );
  8454. $this->assertTags($result, $expected);
  8455. }
  8456. /**
  8457. * test the correct display of multi-record form validation errors.
  8458. *
  8459. * @return void
  8460. */
  8461. public function testMultiRecordFormValidationErrors() {
  8462. $this->Form->create('ValidateProfile');
  8463. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8464. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8465. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8466. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8467. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8468. $result = $this->Form->error('ValidateProfile.2.city');
  8469. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8470. $result = $this->Form->error('2.city');
  8471. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8472. }
  8473. /**
  8474. * test the correct display of multi-record form validation errors.
  8475. *
  8476. * @return void
  8477. */
  8478. public function testSaveManyRecordFormValidationErrors() {
  8479. $this->Form->create('ValidateUser');
  8480. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8481. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8482. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8483. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8484. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8485. $result = $this->Form->error('ValidateUser.0.city');
  8486. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8487. }
  8488. /**
  8489. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8490. *
  8491. * @return void
  8492. */
  8493. public function testInputTemplate() {
  8494. $result = $this->Form->input('Contact.email', array(
  8495. 'type' => 'text', 'format' => array('input')
  8496. ));
  8497. $expected = array(
  8498. 'div' => array('class' => 'input text'),
  8499. 'input' => array(
  8500. 'type' => 'text', 'name' => 'data[Contact][email]',
  8501. 'id' => 'ContactEmail'
  8502. ),
  8503. '/div'
  8504. );
  8505. $this->assertTags($result, $expected);
  8506. $result = $this->Form->input('Contact.email', array(
  8507. 'type' => 'text', 'format' => array('input', 'label'),
  8508. 'label' => '<em>Email (required)</em>'
  8509. ));
  8510. $expected = array(
  8511. 'div' => array('class' => 'input text'),
  8512. array('input' => array(
  8513. 'type' => 'text', 'name' => 'data[Contact][email]',
  8514. 'id' => 'ContactEmail'
  8515. )),
  8516. 'label' => array('for' => 'ContactEmail'),
  8517. 'em' => array(),
  8518. 'Email (required)',
  8519. '/em',
  8520. '/label',
  8521. '/div'
  8522. );
  8523. $this->assertTags($result, $expected);
  8524. $result = $this->Form->input('Contact.email', array(
  8525. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8526. 'between' => '<div>Something in the middle</div>',
  8527. 'after' => '<span>Some text at the end</span>'
  8528. ));
  8529. $expected = array(
  8530. 'div' => array('class' => 'input text'),
  8531. array('input' => array(
  8532. 'type' => 'text', 'name' => 'data[Contact][email]',
  8533. 'id' => 'ContactEmail'
  8534. )),
  8535. array('div' => array()),
  8536. 'Something in the middle',
  8537. '/div',
  8538. 'label' => array('for' => 'ContactEmail'),
  8539. 'Email',
  8540. '/label',
  8541. 'span' => array(),
  8542. 'Some text at the end',
  8543. '/span',
  8544. '/div'
  8545. );
  8546. $this->assertTags($result, $expected);
  8547. $result = $this->Form->input('Contact.method', array(
  8548. 'type' => 'radio',
  8549. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8550. 'between' => 'I am between',
  8551. ));
  8552. $expected = array(
  8553. 'div' => array('class' => 'input radio'),
  8554. 'fieldset' => array(),
  8555. 'legend' => array(),
  8556. 'Method',
  8557. '/legend',
  8558. 'I am between',
  8559. 'input' => array(
  8560. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8561. 'value' => '', 'id' => 'ContactMethod_'
  8562. ),
  8563. array('input' => array(
  8564. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8565. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8566. )),
  8567. array('label' => array('for' => 'ContactMethodEmail')),
  8568. 'Email',
  8569. '/label',
  8570. array('input' => array(
  8571. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8572. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8573. )),
  8574. array('label' => array('for' => 'ContactMethodPigeon')),
  8575. 'Pigeon',
  8576. '/label',
  8577. '/fieldset',
  8578. '/div',
  8579. );
  8580. $this->assertTags($result, $expected);
  8581. }
  8582. /**
  8583. * test that some html5 inputs + FormHelper::__call() work
  8584. *
  8585. * @return void
  8586. */
  8587. public function testHtml5Inputs() {
  8588. $result = $this->Form->email('User.email');
  8589. $expected = array(
  8590. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8591. );
  8592. $this->assertTags($result, $expected);
  8593. $result = $this->Form->search('User.query');
  8594. $expected = array(
  8595. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8596. );
  8597. $this->assertTags($result, $expected);
  8598. $result = $this->Form->search('User.query', array('value' => 'test'));
  8599. $expected = array(
  8600. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8601. );
  8602. $this->assertTags($result, $expected);
  8603. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8604. $expected = array(
  8605. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8606. );
  8607. $this->assertTags($result, $expected);
  8608. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8609. $expected = array(
  8610. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8611. );
  8612. $this->assertTags($result, $expected);
  8613. }
  8614. /**
  8615. *
  8616. * @expectedException CakeException
  8617. * @return void
  8618. */
  8619. public function testHtml5InputException() {
  8620. $this->Form->email();
  8621. }
  8622. /**
  8623. * Tests that a model can be loaded from the model names passed in the request object
  8624. *
  8625. * @return void
  8626. */
  8627. public function testIntrospectModelFromRequest() {
  8628. $this->loadFixtures('Post');
  8629. App::build(array(
  8630. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8631. ));
  8632. CakePlugin::load('TestPlugin');
  8633. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8634. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8635. $this->Form->create('TestPluginPost');
  8636. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8637. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8638. CakePlugin::unload();
  8639. App::build();
  8640. }
  8641. /**
  8642. * Tests that it is possible to set the validation errors directly in the helper for a field
  8643. *
  8644. * @return void
  8645. */
  8646. public function testCustomValidationErrors() {
  8647. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8648. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8649. $this->assertEquals('Badness!', $result);
  8650. }
  8651. /**
  8652. * Tests that the 'on' key validates as expected on create
  8653. *
  8654. * @return void
  8655. */
  8656. public function testRequiredOnCreate() {
  8657. $this->Form->create('Contact');
  8658. $result = $this->Form->input('Contact.imrequiredonupdate');
  8659. $expected = array(
  8660. 'div' => array('class' => 'input text'),
  8661. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8662. 'Imrequiredonupdate',
  8663. '/label',
  8664. 'input' => array(
  8665. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8666. 'id' => 'ContactImrequiredonupdate'
  8667. ),
  8668. '/div'
  8669. );
  8670. $this->assertTags($result, $expected);
  8671. $result = $this->Form->input('Contact.imrequiredoncreate');
  8672. $expected = array(
  8673. 'div' => array('class' => 'input text required'),
  8674. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8675. 'Imrequiredoncreate',
  8676. '/label',
  8677. 'input' => array(
  8678. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8679. 'id' => 'ContactImrequiredoncreate',
  8680. 'required' => 'required'
  8681. ),
  8682. '/div'
  8683. );
  8684. $this->assertTags($result, $expected);
  8685. $result = $this->Form->input('Contact.imrequiredonboth');
  8686. $expected = array(
  8687. 'div' => array('class' => 'input text required'),
  8688. 'label' => array('for' => 'ContactImrequiredonboth'),
  8689. 'Imrequiredonboth',
  8690. '/label',
  8691. 'input' => array(
  8692. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8693. 'id' => 'ContactImrequiredonboth',
  8694. 'required' => 'required'
  8695. ),
  8696. '/div'
  8697. );
  8698. $this->assertTags($result, $expected);
  8699. $this->Form->inputDefaults(array('required' => false));
  8700. $result = $this->Form->input('Contact.imrequired');
  8701. $expected = array(
  8702. 'div' => array('class' => 'input text'),
  8703. 'label' => array('for' => 'ContactImrequired'),
  8704. 'Imrequired',
  8705. '/label',
  8706. 'input' => array(
  8707. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8708. 'id' => 'ContactImrequired'
  8709. ),
  8710. '/div'
  8711. );
  8712. $this->assertTags($result, $expected);
  8713. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8714. $this->assertTags($result, $expected);
  8715. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8716. $expected = array(
  8717. 'div' => array('class' => 'input text required'),
  8718. 'label' => array('for' => 'ContactImrequired'),
  8719. 'Imrequired',
  8720. '/label',
  8721. 'input' => array(
  8722. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8723. 'id' => 'ContactImrequired'
  8724. ),
  8725. '/div'
  8726. );
  8727. $this->assertTags($result, $expected);
  8728. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8729. $this->assertTags($result, $expected);
  8730. }
  8731. /**
  8732. * Tests that the 'on' key validates as expected on update
  8733. *
  8734. * @return void
  8735. */
  8736. public function testRequiredOnUpdate() {
  8737. $this->Form->request->data['Contact']['id'] = 1;
  8738. $this->Form->create('Contact');
  8739. $result = $this->Form->input('Contact.imrequiredonupdate');
  8740. $expected = array(
  8741. 'div' => array('class' => 'input text required'),
  8742. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8743. 'Imrequiredonupdate',
  8744. '/label',
  8745. 'input' => array(
  8746. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8747. 'id' => 'ContactImrequiredonupdate',
  8748. 'required' => 'required'
  8749. ),
  8750. '/div'
  8751. );
  8752. $this->assertTags($result, $expected);
  8753. $result = $this->Form->input('Contact.imrequiredoncreate');
  8754. $expected = array(
  8755. 'div' => array('class' => 'input text'),
  8756. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8757. 'Imrequiredoncreate',
  8758. '/label',
  8759. 'input' => array(
  8760. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8761. 'id' => 'ContactImrequiredoncreate'
  8762. ),
  8763. '/div'
  8764. );
  8765. $this->assertTags($result, $expected);
  8766. $result = $this->Form->input('Contact.imrequiredonboth');
  8767. $expected = array(
  8768. 'div' => array('class' => 'input text required'),
  8769. 'label' => array('for' => 'ContactImrequiredonboth'),
  8770. 'Imrequiredonboth',
  8771. '/label',
  8772. 'input' => array(
  8773. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8774. 'id' => 'ContactImrequiredonboth',
  8775. 'required' => 'required'
  8776. ),
  8777. '/div'
  8778. );
  8779. $this->assertTags($result, $expected);
  8780. $result = $this->Form->input('Contact.imrequired');
  8781. $expected = array(
  8782. 'div' => array('class' => 'input text required'),
  8783. 'label' => array('for' => 'ContactImrequired'),
  8784. 'Imrequired',
  8785. '/label',
  8786. 'input' => array(
  8787. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8788. 'id' => 'ContactImrequired',
  8789. 'required' => 'required'
  8790. ),
  8791. '/div'
  8792. );
  8793. $this->assertTags($result, $expected);
  8794. }
  8795. /**
  8796. * Test inputDefaults setter and getter
  8797. *
  8798. * @return void
  8799. */
  8800. public function testInputDefaults() {
  8801. $this->Form->create('Contact');
  8802. $this->Form->inputDefaults(array(
  8803. 'label' => false,
  8804. 'div' => array(
  8805. 'style' => 'color: #000;'
  8806. )
  8807. ));
  8808. $result = $this->Form->input('Contact.field1');
  8809. $expected = array(
  8810. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8811. 'input' => array(
  8812. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8813. 'id' => 'ContactField1'
  8814. ),
  8815. '/div'
  8816. );
  8817. $this->assertTags($result, $expected);
  8818. $this->Form->inputDefaults(array(
  8819. 'div' => false,
  8820. 'label' => 'Label',
  8821. ));
  8822. $result = $this->Form->input('Contact.field1');
  8823. $expected = array(
  8824. 'label' => array('for' => 'ContactField1'),
  8825. 'Label',
  8826. '/label',
  8827. 'input' => array(
  8828. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8829. 'id' => 'ContactField1'
  8830. ),
  8831. );
  8832. $this->assertTags($result, $expected);
  8833. $this->Form->inputDefaults(array(
  8834. 'label' => false,
  8835. ), true);
  8836. $result = $this->Form->input('Contact.field1');
  8837. $expected = array(
  8838. 'input' => array(
  8839. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8840. 'id' => 'ContactField1'
  8841. ),
  8842. );
  8843. $this->assertTags($result, $expected);
  8844. $result = $this->Form->inputDefaults();
  8845. $expected = array(
  8846. 'div' => false,
  8847. 'label' => false,
  8848. );
  8849. $this->assertEquals($expected, $result);
  8850. }
  8851. }