FormHelperTest.php 305 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680
  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. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Table;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\Utility\ClassRegistry;
  27. use Cake\Utility\Security;
  28. use Cake\View\Helper\FormHelper;
  29. use Cake\View\Helper\HtmlHelper;
  30. use Cake\View\View;
  31. /**
  32. * ContactTestController class
  33. *
  34. */
  35. class ContactTestController extends Controller {
  36. /**
  37. * uses property
  38. *
  39. * @var mixed null
  40. */
  41. public $uses = null;
  42. }
  43. /**
  44. * Contact class
  45. *
  46. */
  47. class ContactsTable extends Table {
  48. /**
  49. * Default schema
  50. *
  51. * @var array
  52. */
  53. protected $_schema = array(
  54. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  55. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  56. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  57. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  58. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  59. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  60. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  61. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  62. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  63. );
  64. /**
  65. * validate property
  66. *
  67. * @var array
  68. */
  69. public $validate = array(
  70. 'non_existing' => array(),
  71. 'idontexist' => array(),
  72. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  73. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  74. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  75. 'imrequiredonboth' => array(
  76. 'required' => array('rule' => 'alphaNumeric'),
  77. ),
  78. 'string_required' => 'notEmpty',
  79. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  80. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  81. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  82. 'imnotrequired' => array(
  83. '_allowEmpty' => true,
  84. array('rule' => 'alphaNumeric')
  85. ),
  86. 'imalsonotrequired' => array(
  87. '_allowEmpty' => true,
  88. 'alpha' => array('rule' => 'alphaNumeric'),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imnotrequiredeither' => array(
  92. '_allowEmpty' => true,
  93. array('rule' => array('between', 5, 30))
  94. ),
  95. 'iamrequiredalways' => array(
  96. 'email' => array('rule' => 'email'),
  97. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  98. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  99. ),
  100. 'boolean_field' => array('rule' => 'boolean')
  101. );
  102. /**
  103. * schema method
  104. *
  105. * @return void
  106. */
  107. public function setSchema($schema) {
  108. $this->_schema = $schema;
  109. }
  110. /**
  111. * hasAndBelongsToMany property
  112. *
  113. * @var array
  114. */
  115. public $hasAndBelongsToMany = array(
  116. 'ContactTag' => array(
  117. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  118. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  119. )
  120. );
  121. /**
  122. * hasAndBelongsToMany property
  123. *
  124. * @var array
  125. */
  126. public $belongsTo = array(
  127. 'User' => array('className' => 'UserForm'
  128. ));
  129. }
  130. /**
  131. * ContactTagsContact class
  132. *
  133. */
  134. class ContactTagsContactsTable extends Table {
  135. /**
  136. * Default schema
  137. *
  138. * @var array
  139. */
  140. protected $_schema = array(
  141. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  142. 'contact_tag_id' => array(
  143. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  144. )
  145. );
  146. /**
  147. * schema method
  148. *
  149. * @return void
  150. */
  151. public function setSchema($schema) {
  152. $this->_schema = $schema;
  153. }
  154. }
  155. /**
  156. * ContactNonStandardPk class
  157. *
  158. */
  159. class ContactNonStandardPk extends ContactsTable {
  160. /**
  161. * primaryKey property
  162. *
  163. * @var string
  164. */
  165. public $primaryKey = 'pk';
  166. /**
  167. * schema method
  168. *
  169. * @return void
  170. */
  171. public function schema($field = false) {
  172. $this->_schema = parent::schema();
  173. $this->_schema['pk'] = $this->_schema['id'];
  174. unset($this->_schema['id']);
  175. return $this->_schema;
  176. }
  177. }
  178. /**
  179. * ContactTag class
  180. *
  181. */
  182. class ContactTagsTable extends Table {
  183. /**
  184. * schema definition
  185. *
  186. * @var array
  187. */
  188. protected $_schema = array(
  189. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  190. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  191. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  192. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  193. );
  194. }
  195. /**
  196. * UserForm class
  197. *
  198. */
  199. class UserFormsTable extends Table {
  200. /**
  201. * hasMany property
  202. *
  203. * @var array
  204. */
  205. public $hasMany = array(
  206. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  207. ));
  208. /**
  209. * schema definition
  210. *
  211. * @var array
  212. */
  213. protected $_schema = array(
  214. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  215. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  216. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  217. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  218. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  219. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  220. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  221. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  222. );
  223. }
  224. /**
  225. * OpenidUrl class
  226. *
  227. */
  228. class OpenidUrlsTable extends Table {
  229. /**
  230. * belongsTo property
  231. *
  232. * @var array
  233. */
  234. public $belongsTo = array('UserForm' => array(
  235. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  236. ));
  237. /**
  238. * validate property
  239. *
  240. * @var array
  241. */
  242. public $validate = array('openid_not_registered' => array());
  243. /**
  244. * schema method
  245. *
  246. * @var array
  247. */
  248. protected $_schema = array(
  249. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  250. 'user_form_id' => array(
  251. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  252. ),
  253. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  254. );
  255. /**
  256. * beforeValidate method
  257. *
  258. * @return void
  259. */
  260. public function beforeValidate($options = array()) {
  261. $this->invalidate('openid_not_registered');
  262. return true;
  263. }
  264. }
  265. /**
  266. * ValidateUser class
  267. *
  268. */
  269. class ValidateUsersTable extends Table {
  270. /**
  271. * hasOne property
  272. *
  273. * @var array
  274. */
  275. public $hasOne = array('ValidateProfile' => array(
  276. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  277. ));
  278. /**
  279. * schema method
  280. *
  281. * @var array
  282. */
  283. protected $_schema = array(
  284. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  285. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  286. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  287. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  288. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  289. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  290. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  291. );
  292. /**
  293. * beforeValidate method
  294. *
  295. * @return void
  296. */
  297. public function beforeValidate($options = array()) {
  298. $this->invalidate('email');
  299. return false;
  300. }
  301. }
  302. /**
  303. * ValidateProfile class
  304. *
  305. */
  306. class ValidateProfilesTable extends Table {
  307. /**
  308. * useTable property
  309. *
  310. * @var boolean
  311. */
  312. public $useTable = false;
  313. /**
  314. * schema property
  315. *
  316. * @var array
  317. */
  318. protected $_schema = array(
  319. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  320. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  321. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  322. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  323. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  324. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  325. );
  326. /**
  327. * hasOne property
  328. *
  329. * @var array
  330. */
  331. public $hasOne = array('ValidateItem' => array(
  332. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  333. ));
  334. /**
  335. * belongsTo property
  336. *
  337. * @var array
  338. */
  339. public $belongsTo = array('ValidateUser' => array(
  340. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  341. ));
  342. /**
  343. * beforeValidate method
  344. *
  345. * @return void
  346. */
  347. public function beforeValidate($options = array()) {
  348. $this->invalidate('full_name');
  349. $this->invalidate('city');
  350. return false;
  351. }
  352. }
  353. /**
  354. * ValidateItem class
  355. *
  356. */
  357. class ValidateItemsTable extends Table {
  358. /**
  359. * schema property
  360. *
  361. * @var array
  362. */
  363. protected $_schema = array(
  364. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  365. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  366. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  367. 'description' => array(
  368. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  369. ),
  370. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  371. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  372. );
  373. /**
  374. * belongsTo property
  375. *
  376. * @var array
  377. */
  378. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  379. /**
  380. * beforeValidate method
  381. *
  382. * @return void
  383. */
  384. public function beforeValidate($options = array()) {
  385. $this->invalidate('description');
  386. return false;
  387. }
  388. }
  389. /**
  390. * TestMail class
  391. *
  392. */
  393. class TestMailsTable extends Table {
  394. }
  395. /**
  396. * FormHelperTest class
  397. *
  398. * @property FormHelper $Form
  399. */
  400. class FormHelperTest extends TestCase {
  401. /**
  402. * Fixtures to be used
  403. *
  404. * @var array
  405. */
  406. public $fixtures = array('core.post');
  407. /**
  408. * Do not load the fixtures by default
  409. *
  410. * @var boolean
  411. */
  412. public $autoFixtures = false;
  413. /**
  414. * setUp method
  415. *
  416. * @return void
  417. */
  418. public function setUp() {
  419. parent::setUp();
  420. $this->markTestIncomplete('Need to revisit once models work again.');
  421. Configure::write('Config.language', 'eng');
  422. Configure::write('App.base', '');
  423. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  424. Configure::delete('Asset');
  425. $this->Controller = new ContactTestController();
  426. $this->View = new View($this->Controller);
  427. $this->Form = new FormHelper($this->View);
  428. $this->Form->Html = new HtmlHelper($this->View);
  429. $this->Form->request = new Request('contacts/add');
  430. $this->Form->request->here = '/contacts/add';
  431. $this->Form->request['action'] = 'add';
  432. $this->Form->request->webroot = '';
  433. $this->Form->request->base = '';
  434. ClassRegistry::addObject('Contact', new Contact());
  435. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  436. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  437. ClassRegistry::addObject('User', new UserForm());
  438. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  439. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  440. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  441. $this->dateRegex = array(
  442. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  443. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  444. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  445. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  446. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  447. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  448. );
  449. Configure::write('Security.salt', 'foo!');
  450. Router::connect('/:controller', array('action' => 'index'));
  451. Router::connect('/:controller/:action/*');
  452. }
  453. /**
  454. * tearDown method
  455. *
  456. * @return void
  457. */
  458. public function tearDown() {
  459. parent::tearDown();
  460. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  461. }
  462. /**
  463. * testFormCreateWithSecurity method
  464. *
  465. * Test form->create() with security key.
  466. *
  467. * @return void
  468. */
  469. public function testCreateWithSecurity() {
  470. $this->Form->request->params['_csrfToken'] = 'testKey';
  471. $encoding = strtolower(Configure::read('App.encoding'));
  472. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  473. $expected = array(
  474. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  475. 'div' => array('style' => 'display:none;'),
  476. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  477. array('input' => array(
  478. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey', 'id'
  479. )),
  480. '/div'
  481. );
  482. $this->assertTags($result, $expected);
  483. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  484. $expected['form']['id'] = 'MyForm';
  485. $this->assertTags($result, $expected);
  486. }
  487. /**
  488. * testFormCreateGetNoSecurity method
  489. *
  490. * Test form->create() with no security key as its a get form
  491. *
  492. * @return void
  493. */
  494. public function testCreateEndGetNoSecurity() {
  495. $this->Form->request->params['_csrfToken'] = 'testKey';
  496. $encoding = strtolower(Configure::read('App.encoding'));
  497. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  498. $this->assertNotContains('testKey', $result);
  499. $result = $this->Form->end('Save');
  500. $this->assertNotContains('testKey', $result);
  501. }
  502. /**
  503. * test that create() clears the fields property so it starts fresh
  504. *
  505. * @return void
  506. */
  507. public function testCreateClearingFields() {
  508. $this->Form->fields = array('model_id');
  509. $this->Form->create('Contact');
  510. $this->assertEquals(array(), $this->Form->fields);
  511. }
  512. /**
  513. * Tests form hash generation with model-less data
  514. *
  515. * @return void
  516. */
  517. public function testValidateHashNoModel() {
  518. $this->Form->request->params['_csrfToken'] = 'foo';
  519. $result = $this->Form->secure(array('anything'));
  520. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  521. }
  522. /**
  523. * Tests that models with identical field names get resolved properly
  524. *
  525. * @return void
  526. */
  527. public function testDuplicateFieldNameResolution() {
  528. $result = $this->Form->create('ValidateUser');
  529. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  530. $result = $this->Form->input('ValidateItem.name');
  531. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  532. $result = $this->Form->input('ValidateUser.name');
  533. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  534. $this->assertContains('name="ValidateUser[name]"', $result);
  535. $this->assertContains('type="text"', $result);
  536. $result = $this->Form->input('ValidateItem.name');
  537. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  538. $this->assertContains('name="ValidateItem[name]"', $result);
  539. $this->assertContains('<textarea', $result);
  540. $result = $this->Form->input('name');
  541. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  542. $this->assertContains('name="ValidateUser[name]"', $result);
  543. $this->assertContains('type="text"', $result);
  544. }
  545. /**
  546. * Tests that hidden fields generated for checkboxes don't get locked
  547. *
  548. * @return void
  549. */
  550. public function testNoCheckboxLocking() {
  551. $this->Form->request->params['_csrfToken'] = 'foo';
  552. $this->assertSame(array(), $this->Form->fields);
  553. $this->Form->checkbox('check', array('value' => '1'));
  554. $this->assertSame($this->Form->fields, array('check'));
  555. }
  556. /**
  557. * testFormSecurityFields method
  558. *
  559. * Test generation of secure form hash generation.
  560. *
  561. * @return void
  562. */
  563. public function testFormSecurityFields() {
  564. $key = 'testKey';
  565. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  566. $this->Form->request->params['_csrfToken'] = $key;
  567. $result = $this->Form->secure($fields);
  568. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  569. $hash .= ':' . 'Model.valid';
  570. $hash = urlencode($hash);
  571. $expected = array(
  572. 'div' => array('style' => 'display:none;'),
  573. array('input' => array(
  574. 'type' => 'hidden', 'name' => '_Token[fields]',
  575. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  576. )),
  577. array('input' => array(
  578. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  579. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  580. )),
  581. '/div'
  582. );
  583. $this->assertTags($result, $expected);
  584. $path = CAKE . 'Test/TestApp/Config/';
  585. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  586. $result = $this->Form->secure($fields);
  587. $expected = array(
  588. 'div' => array('class' => 'hidden'),
  589. array('input' => array(
  590. 'type' => 'hidden', 'name' => '_Token[fields]',
  591. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  592. )),
  593. array('input' => array(
  594. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  595. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  596. )),
  597. '/div'
  598. );
  599. $this->assertTags($result, $expected);
  600. }
  601. /**
  602. * Tests correct generation of number fields for double and float fields
  603. *
  604. * @return void
  605. */
  606. public function testTextFieldGenerationForFloats() {
  607. $model = ClassRegistry::getObject('Contact');
  608. $model->setSchema(array('foo' => array(
  609. 'type' => 'float',
  610. 'null' => false,
  611. 'default' => null,
  612. 'length' => 10
  613. )));
  614. $this->Form->create('Contact');
  615. $result = $this->Form->input('foo');
  616. $expected = array(
  617. 'div' => array('class' => 'input number'),
  618. 'label' => array('for' => 'ContactFoo'),
  619. 'Foo',
  620. '/label',
  621. array('input' => array(
  622. 'type' => 'number',
  623. 'name' => 'Contact[foo]',
  624. 'id' => 'ContactFoo',
  625. 'step' => 'any'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $result = $this->Form->input('foo', array('step' => 0.5));
  631. $expected = array(
  632. 'div' => array('class' => 'input number'),
  633. 'label' => array('for' => 'ContactFoo'),
  634. 'Foo',
  635. '/label',
  636. array('input' => array(
  637. 'type' => 'number',
  638. 'name' => 'Contact[foo]',
  639. 'id' => 'ContactFoo',
  640. 'step' => '0.5'
  641. )),
  642. '/div'
  643. );
  644. $this->assertTags($result, $expected);
  645. }
  646. /**
  647. * Tests correct generation of number fields for integer fields
  648. *
  649. * @return void
  650. */
  651. public function testTextFieldTypeNumberGenerationForIntegers() {
  652. $model = ClassRegistry::getObject('Contact');
  653. $model->setSchema(array('foo' => array(
  654. 'type' => 'integer',
  655. 'null' => false,
  656. 'default' => null,
  657. 'length' => null
  658. )));
  659. $this->Form->create('Contact');
  660. $result = $this->Form->input('foo');
  661. $expected = array(
  662. 'div' => array('class' => 'input number'),
  663. 'label' => array('for' => 'ContactFoo'),
  664. 'Foo',
  665. '/label',
  666. array('input' => array(
  667. 'type' => 'number', 'name' => 'Contact[foo]',
  668. 'id' => 'ContactFoo'
  669. )),
  670. '/div'
  671. );
  672. $this->assertTags($result, $expected);
  673. }
  674. /**
  675. * Tests correct generation of file upload fields for binary fields
  676. *
  677. * @return void
  678. */
  679. public function testFileUploadFieldTypeGenerationForBinaries() {
  680. $model = ClassRegistry::getObject('Contact');
  681. $model->setSchema(array('foo' => array(
  682. 'type' => 'binary',
  683. 'null' => false,
  684. 'default' => null,
  685. 'length' => 1024
  686. )));
  687. $this->Form->create('Contact');
  688. $result = $this->Form->input('foo');
  689. $expected = array(
  690. 'div' => array('class' => 'input file'),
  691. 'label' => array('for' => 'ContactFoo'),
  692. 'Foo',
  693. '/label',
  694. array('input' => array(
  695. 'type' => 'file', 'name' => 'data[Contact][foo]',
  696. 'id' => 'ContactFoo'
  697. )),
  698. '/div'
  699. );
  700. $this->assertTags($result, $expected);
  701. }
  702. /**
  703. * testFormSecurityMultipleFields method
  704. *
  705. * Test secure() with multiple row form. Ensure hash is correct.
  706. *
  707. * @return void
  708. */
  709. public function testFormSecurityMultipleFields() {
  710. $key = 'testKey';
  711. $fields = array(
  712. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  713. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  714. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  715. );
  716. $this->Form->request->params['_csrfToken'] = $key;
  717. $result = $this->Form->secure($fields);
  718. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  719. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  720. $expected = array(
  721. 'div' => array('style' => 'display:none;'),
  722. array('input' => array(
  723. 'type' => 'hidden', 'name' => '_Token[fields]',
  724. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  725. )),
  726. array('input' => array(
  727. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  728. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  729. )),
  730. '/div'
  731. );
  732. $this->assertTags($result, $expected);
  733. }
  734. /**
  735. * testFormSecurityMultipleSubmitButtons
  736. *
  737. * test form submit generation and ensure that _Token is only created on end()
  738. *
  739. * @return void
  740. */
  741. public function testFormSecurityMultipleSubmitButtons() {
  742. $key = 'testKey';
  743. $this->Form->request->params['_csrfToken'] = $key;
  744. $this->Form->create('Addresses');
  745. $this->Form->input('Address.title');
  746. $this->Form->input('Address.first_name');
  747. $result = $this->Form->submit('Save', array('name' => 'save'));
  748. $expected = array(
  749. 'div' => array('class' => 'submit'),
  750. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  751. '/div',
  752. );
  753. $this->assertTags($result, $expected);
  754. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  755. $expected = array(
  756. 'div' => array('class' => 'submit'),
  757. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  758. '/div',
  759. );
  760. $this->assertTags($result, $expected);
  761. $result = $this->Form->end(null);
  762. $expected = array(
  763. 'div' => array('style' => 'display:none;'),
  764. array('input' => array(
  765. 'type' => 'hidden', 'name' => '_Token[fields]',
  766. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  767. )),
  768. array('input' => array(
  769. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  770. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  771. )),
  772. '/div'
  773. );
  774. $this->assertTags($result, $expected);
  775. }
  776. /**
  777. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  778. *
  779. * @return void
  780. */
  781. public function testSecurityButtonNestedNamed() {
  782. $key = 'testKey';
  783. $this->Form->request->params['_csrfToken'] = $key;
  784. $this->Form->create('Addresses');
  785. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  786. $result = $this->Form->unlockField();
  787. $this->assertEquals(array('Address.button'), $result);
  788. }
  789. /**
  790. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  791. *
  792. * @return void
  793. */
  794. public function testSecuritySubmitNestedNamed() {
  795. $key = 'testKey';
  796. $this->Form->request->params['_csrfToken'] = $key;
  797. $this->Form->create('Addresses');
  798. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  799. $result = $this->Form->unlockField();
  800. $this->assertEquals(array('Address.button'), $result);
  801. }
  802. /**
  803. * Test that the correct fields are unlocked for image submits with no names.
  804. *
  805. * @return void
  806. */
  807. public function testSecuritySubmitImageNoName() {
  808. $key = 'testKey';
  809. $this->Form->request->params['_csrfToken'] = $key;
  810. $this->Form->create('User');
  811. $result = $this->Form->submit('save.png');
  812. $expected = array(
  813. 'div' => array('class' => 'submit'),
  814. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  815. '/div'
  816. );
  817. $this->assertTags($result, $expected);
  818. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  819. }
  820. /**
  821. * Test that the correct fields are unlocked for image submits with names.
  822. *
  823. * @return void
  824. */
  825. public function testSecuritySubmitImageName() {
  826. $key = 'testKey';
  827. $this->Form->request->params['_csrfToken'] = $key;
  828. $this->Form->create('User');
  829. $result = $this->Form->submit('save.png', array('name' => 'test'));
  830. $expected = array(
  831. 'div' => array('class' => 'submit'),
  832. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  833. '/div'
  834. );
  835. $this->assertTags($result, $expected);
  836. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  837. }
  838. /**
  839. * testFormSecurityMultipleInputFields method
  840. *
  841. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  842. *
  843. * @return void
  844. */
  845. public function testFormSecurityMultipleInputFields() {
  846. $key = 'testKey';
  847. $this->Form->request->params['_csrfToken'] = $key;
  848. $this->Form->create('Addresses');
  849. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  850. $this->Form->input('Addresses.0.title');
  851. $this->Form->input('Addresses.0.first_name');
  852. $this->Form->input('Addresses.0.last_name');
  853. $this->Form->input('Addresses.0.address');
  854. $this->Form->input('Addresses.0.city');
  855. $this->Form->input('Addresses.0.phone');
  856. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  857. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  858. $this->Form->input('Addresses.1.title');
  859. $this->Form->input('Addresses.1.first_name');
  860. $this->Form->input('Addresses.1.last_name');
  861. $this->Form->input('Addresses.1.address');
  862. $this->Form->input('Addresses.1.city');
  863. $this->Form->input('Addresses.1.phone');
  864. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  865. $result = $this->Form->secure($this->Form->fields);
  866. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  867. $expected = array(
  868. 'div' => array('style' => 'display:none;'),
  869. array('input' => array(
  870. 'type' => 'hidden', 'name' => '_Token[fields]',
  871. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  872. )),
  873. array('input' => array(
  874. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  875. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  876. )),
  877. '/div'
  878. );
  879. $this->assertTags($result, $expected);
  880. }
  881. /**
  882. * Test form security with Model.field.0 style inputs
  883. *
  884. * @return void
  885. */
  886. public function testFormSecurityArrayFields() {
  887. $key = 'testKey';
  888. $this->Form->request->params['_csrfToken'] = $key;
  889. $this->Form->create('Address');
  890. $this->Form->input('Address.primary.1');
  891. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  892. $this->Form->input('Address.secondary.1.0');
  893. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  894. }
  895. /**
  896. * testFormSecurityMultipleInputDisabledFields method
  897. *
  898. * test secure form generation with multiple records and disabled fields.
  899. *
  900. * @return void
  901. */
  902. public function testFormSecurityMultipleInputDisabledFields() {
  903. $key = 'testKey';
  904. $this->Form->request->params['_csrfToken'] = $key;
  905. $this->Form->request->params['_Token'] = array(
  906. 'unlockedFields' => array('first_name', 'address')
  907. );
  908. $this->Form->create();
  909. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  910. $this->Form->input('Addresses.0.title');
  911. $this->Form->input('Addresses.0.first_name');
  912. $this->Form->input('Addresses.0.last_name');
  913. $this->Form->input('Addresses.0.address');
  914. $this->Form->input('Addresses.0.city');
  915. $this->Form->input('Addresses.0.phone');
  916. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  917. $this->Form->input('Addresses.1.title');
  918. $this->Form->input('Addresses.1.first_name');
  919. $this->Form->input('Addresses.1.last_name');
  920. $this->Form->input('Addresses.1.address');
  921. $this->Form->input('Addresses.1.city');
  922. $this->Form->input('Addresses.1.phone');
  923. $result = $this->Form->secure($this->Form->fields);
  924. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  925. $expected = array(
  926. 'div' => array('style' => 'display:none;'),
  927. array('input' => array(
  928. 'type' => 'hidden', 'name' => '_Token[fields]',
  929. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  930. )),
  931. array('input' => array(
  932. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  933. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  934. )),
  935. '/div'
  936. );
  937. $this->assertTags($result, $expected);
  938. }
  939. /**
  940. * testFormSecurityInputDisabledFields method
  941. *
  942. * Test single record form with disabled fields.
  943. *
  944. * @return void
  945. */
  946. public function testFormSecurityInputUnlockedFields() {
  947. $key = 'testKey';
  948. $this->Form->request->params['_csrfToken'] = $key;
  949. $this->Form->request['_Token'] = array(
  950. 'unlockedFields' => array('first_name', 'address')
  951. );
  952. $this->Form->create();
  953. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  954. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  955. $this->Form->input('Addresses.title');
  956. $this->Form->input('Addresses.first_name');
  957. $this->Form->input('Addresses.last_name');
  958. $this->Form->input('Addresses.address');
  959. $this->Form->input('Addresses.city');
  960. $this->Form->input('Addresses.phone');
  961. $result = $this->Form->fields;
  962. $expected = array(
  963. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  964. 'Addresses.city', 'Addresses.phone'
  965. );
  966. $this->assertEquals($expected, $result);
  967. $result = $this->Form->secure($expected);
  968. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  969. $expected = array(
  970. 'div' => array('style' => 'display:none;'),
  971. array('input' => array(
  972. 'type' => 'hidden', 'name' => '_Token[fields]',
  973. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  974. )),
  975. array('input' => array(
  976. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  977. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  978. )),
  979. '/div'
  980. );
  981. $this->assertTags($result, $expected);
  982. }
  983. /**
  984. * test securing inputs with custom name attributes.
  985. *
  986. * @return void
  987. */
  988. public function testFormSecureWithCustomNameAttribute() {
  989. $this->Form->request->params['_csrfToken'] = 'testKey';
  990. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  991. $this->assertEquals('User.custom', $this->Form->fields[0]);
  992. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  993. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  994. }
  995. /**
  996. * testFormSecuredInput method
  997. *
  998. * Test generation of entire secure form, assertions made on input() output.
  999. *
  1000. * @return void
  1001. */
  1002. public function testFormSecuredInput() {
  1003. $this->Form->request->params['_csrfToken'] = 'testKey';
  1004. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1005. $encoding = strtolower(Configure::read('App.encoding'));
  1006. $expected = array(
  1007. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1008. 'div' => array('style' => 'display:none;'),
  1009. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1010. array('input' => array(
  1011. 'type' => 'hidden', 'name' => '_csrfToken',
  1012. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1013. )),
  1014. '/div'
  1015. );
  1016. $this->assertTags($result, $expected);
  1017. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1018. $expected = array(
  1019. 'div' => array('class' => 'input text'),
  1020. 'label' => array('for' => 'UserFormPublished'),
  1021. 'Published',
  1022. '/label',
  1023. array('input' => array(
  1024. 'type' => 'text', 'name' => 'UserForm[published]',
  1025. 'id' => 'UserFormPublished'
  1026. )),
  1027. '/div'
  1028. );
  1029. $this->assertTags($result, $expected);
  1030. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1031. $expected = array(
  1032. 'div' => array('class' => 'input text'),
  1033. 'label' => array('for' => 'UserFormOther'),
  1034. 'Other',
  1035. '/label',
  1036. array('input' => array(
  1037. 'type' => 'text', 'name' => 'UserForm[other]',
  1038. 'id' => 'UserFormOther'
  1039. )),
  1040. '/div'
  1041. );
  1042. $this->assertTags($result, $expected);
  1043. $result = $this->Form->hidden('UserForm.stuff');
  1044. $expected = array(
  1045. 'input' => array(
  1046. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1047. 'id' => 'UserFormStuff'
  1048. ));
  1049. $this->assertTags($result, $expected);
  1050. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1051. $expected = array('input' => array(
  1052. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1053. 'value' => '0', 'id' => 'UserFormHidden'
  1054. ));
  1055. $this->assertTags($result, $expected);
  1056. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1057. $expected = array(
  1058. 'div' => array('class' => 'input checkbox'),
  1059. array('input' => array(
  1060. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1061. 'value' => '0', 'id' => 'UserFormSomething_'
  1062. )),
  1063. array('input' => array(
  1064. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1065. 'value' => '1', 'id' => 'UserFormSomething'
  1066. )),
  1067. 'label' => array('for' => 'UserFormSomething'),
  1068. 'Something',
  1069. '/label',
  1070. '/div'
  1071. );
  1072. $this->assertTags($result, $expected);
  1073. $result = $this->Form->fields;
  1074. $expected = array(
  1075. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1076. 'UserForm.hidden' => '0', 'UserForm.something'
  1077. );
  1078. $this->assertEquals($expected, $result);
  1079. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1080. $result = $this->Form->secure($this->Form->fields);
  1081. $expected = array(
  1082. 'div' => array('style' => 'display:none;'),
  1083. array('input' => array(
  1084. 'type' => 'hidden', 'name' => '_Token[fields]',
  1085. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1086. )),
  1087. array('input' => array(
  1088. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1089. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1090. )),
  1091. '/div'
  1092. );
  1093. $this->assertTags($result, $expected);
  1094. }
  1095. /**
  1096. * Test secured inputs with custom names.
  1097. *
  1098. * @return void
  1099. */
  1100. public function testSecuredInputCustomName() {
  1101. $this->Form->request->params['_csrfToken'] = 'testKey';
  1102. $this->assertEquals(array(), $this->Form->fields);
  1103. $this->Form->input('text_input', array(
  1104. 'name' => 'data[Option][General.default_role]',
  1105. ));
  1106. $expected = array('Option.General.default_role');
  1107. $this->assertEquals($expected, $this->Form->fields);
  1108. $this->Form->input('select_box', array(
  1109. 'name' => 'data[Option][General.select_role]',
  1110. 'type' => 'select',
  1111. 'options' => array(1, 2),
  1112. ));
  1113. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1114. $this->assertEquals($expected, $this->Form->fields);
  1115. }
  1116. /**
  1117. * Tests that the correct keys are added to the field hash index
  1118. *
  1119. * @return void
  1120. */
  1121. public function testFormSecuredFileInput() {
  1122. $this->Form->request->params['_csrfToken'] = 'testKey';
  1123. $this->assertEquals(array(), $this->Form->fields);
  1124. $this->Form->file('Attachment.file');
  1125. $expected = array(
  1126. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1127. 'Attachment.file.error', 'Attachment.file.size'
  1128. );
  1129. $this->assertEquals($expected, $this->Form->fields);
  1130. }
  1131. /**
  1132. * test that multiple selects keys are added to field hash
  1133. *
  1134. * @return void
  1135. */
  1136. public function testFormSecuredMultipleSelect() {
  1137. $this->Form->request->params['_csrfToken'] = 'testKey';
  1138. $this->assertEquals(array(), $this->Form->fields);
  1139. $options = array('1' => 'one', '2' => 'two');
  1140. $this->Form->select('Model.select', $options);
  1141. $expected = array('Model.select');
  1142. $this->assertEquals($expected, $this->Form->fields);
  1143. $this->Form->fields = array();
  1144. $this->Form->select('Model.select', $options, array('multiple' => true));
  1145. $this->assertEquals($expected, $this->Form->fields);
  1146. }
  1147. /**
  1148. * testFormSecuredRadio method
  1149. *
  1150. * @return void
  1151. */
  1152. public function testFormSecuredRadio() {
  1153. $this->Form->request->params['_csrfToken'] = 'testKey';
  1154. $this->assertEquals(array(), $this->Form->fields);
  1155. $options = array('1' => 'option1', '2' => 'option2');
  1156. $this->Form->radio('Test.test', $options);
  1157. $expected = array('Test.test');
  1158. $this->assertEquals($expected, $this->Form->fields);
  1159. }
  1160. /**
  1161. * Test that when disabled is in a list based attribute array it works.
  1162. *
  1163. * @return void
  1164. */
  1165. public function testFormSecuredAndDisabledNotAssoc() {
  1166. $this->Form->request->params['_csrfToken'] = 'testKey';
  1167. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1168. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1169. $this->Form->text('Model.text', array('disabled'));
  1170. $this->Form->textarea('Model.textarea', array('disabled'));
  1171. $this->Form->password('Model.password', array('disabled'));
  1172. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1173. $expected = array(
  1174. 'Model.radio' => ''
  1175. );
  1176. $this->assertEquals($expected, $this->Form->fields);
  1177. }
  1178. /**
  1179. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1180. * hashing.
  1181. *
  1182. * @return void
  1183. */
  1184. public function testFormSecuredAndDisabled() {
  1185. $this->Form->request->params['_csrfToken'] = 'testKey';
  1186. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1187. $this->Form->text('Model.text', array('disabled' => true));
  1188. $this->Form->password('Model.text', array('disabled' => true));
  1189. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1190. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1191. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1192. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1193. $this->Form->month('Model.month', array('disabled' => true));
  1194. $this->Form->day('Model.day', array('disabled' => true));
  1195. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1196. $this->Form->minute('Model.minute', array('disabled' => true));
  1197. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1198. $expected = array(
  1199. 'Model.radio' => ''
  1200. );
  1201. $this->assertEquals($expected, $this->Form->fields);
  1202. }
  1203. /**
  1204. * testDisableSecurityUsingForm method
  1205. *
  1206. * @return void
  1207. */
  1208. public function testDisableSecurityUsingForm() {
  1209. $this->Form->request->params['_csrfToken'] = 'testKey';
  1210. $this->Form->request['_Token'] = array(
  1211. 'disabledFields' => array()
  1212. );
  1213. $this->Form->create();
  1214. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1215. $this->Form->input('Addresses.title');
  1216. $this->Form->input('Addresses.first_name', array('secure' => false));
  1217. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1218. $this->Form->input('Addresses.zip', array(
  1219. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1220. ));
  1221. $result = $this->Form->fields;
  1222. $expected = array(
  1223. 'Addresses.id' => '123456', 'Addresses.title',
  1224. );
  1225. $this->assertEquals($expected, $result);
  1226. }
  1227. /**
  1228. * test disableField
  1229. *
  1230. * @return void
  1231. */
  1232. public function testUnlockFieldAddsToList() {
  1233. $this->Form->request->params['_csrfToken'] = 'testKey';
  1234. $this->Form->request['_Token'] = array(
  1235. 'unlockedFields' => array()
  1236. );
  1237. $this->Form->create('Contact');
  1238. $this->Form->unlockField('Contact.name');
  1239. $this->Form->text('Contact.name');
  1240. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1241. $this->assertEquals(array(), $this->Form->fields);
  1242. }
  1243. /**
  1244. * test unlockField removing from fields array.
  1245. *
  1246. * @return void
  1247. */
  1248. public function testUnlockFieldRemovingFromFields() {
  1249. $this->Form->request->params['_csrfToken'] = 'testKey';
  1250. $this->Form->request['_Token'] = array(
  1251. 'unlockedFields' => array()
  1252. );
  1253. $this->Form->create('Contact');
  1254. $this->Form->hidden('Contact.id', array('value' => 1));
  1255. $this->Form->text('Contact.name');
  1256. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1257. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1258. $this->Form->unlockField('Contact.name');
  1259. $this->Form->unlockField('Contact.id');
  1260. $this->assertEquals(array(), $this->Form->fields);
  1261. }
  1262. /**
  1263. * testTagIsInvalid method
  1264. *
  1265. * @return void
  1266. */
  1267. public function testTagIsInvalid() {
  1268. $Contact = ClassRegistry::getObject('Contact');
  1269. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1270. $this->Form->setEntity('Contact.0.email');
  1271. $result = $this->Form->tagIsInvalid();
  1272. $this->assertEquals($expected, $result);
  1273. $this->Form->setEntity('Contact.1.email');
  1274. $result = $this->Form->tagIsInvalid();
  1275. $this->assertFalse($result);
  1276. $this->Form->setEntity('Contact.0.name');
  1277. $result = $this->Form->tagIsInvalid();
  1278. $this->assertFalse($result);
  1279. }
  1280. /**
  1281. * Test tagIsInvalid with validation errors from a saveMany
  1282. *
  1283. * @return void
  1284. */
  1285. public function testTagIsInvalidSaveMany() {
  1286. $Contact = ClassRegistry::getObject('Contact');
  1287. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1288. $this->Form->create('Contact');
  1289. $this->Form->setEntity('0.email');
  1290. $result = $this->Form->tagIsInvalid();
  1291. $this->assertEquals($expected, $result);
  1292. $this->Form->setEntity('0.Contact.email');
  1293. $result = $this->Form->tagIsInvalid();
  1294. $this->assertEquals($expected, $result);
  1295. }
  1296. /**
  1297. * Test validation errors.
  1298. *
  1299. * @return void
  1300. */
  1301. public function testPasswordValidation() {
  1302. $Contact = ClassRegistry::getObject('Contact');
  1303. $Contact->validationErrors['password'] = array('Please provide a password');
  1304. $result = $this->Form->input('Contact.password');
  1305. $expected = array(
  1306. 'div' => array('class' => 'input password error'),
  1307. 'label' => array('for' => 'ContactPassword'),
  1308. 'Password',
  1309. '/label',
  1310. 'input' => array(
  1311. 'type' => 'password', 'name' => 'Contact[password]',
  1312. 'id' => 'ContactPassword', 'class' => 'form-error'
  1313. ),
  1314. array('div' => array('class' => 'error-message')),
  1315. 'Please provide a password',
  1316. '/div',
  1317. '/div'
  1318. );
  1319. $this->assertTags($result, $expected);
  1320. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1321. $expected = array(
  1322. 'div' => array('class' => 'input password error'),
  1323. 'label' => array('for' => 'ContactPassword'),
  1324. 'Password',
  1325. '/label',
  1326. 'input' => array(
  1327. 'type' => 'password', 'name' => 'Contact[password]',
  1328. 'id' => 'ContactPassword', 'class' => 'form-error'
  1329. ),
  1330. '/div'
  1331. );
  1332. $this->assertTags($result, $expected);
  1333. }
  1334. /**
  1335. * Test validation errors, when validation message is an empty string.
  1336. *
  1337. * @return void
  1338. */
  1339. public function testEmptyErrorValidation() {
  1340. $this->Form->validationErrors['Contact']['password'] = '';
  1341. $result = $this->Form->input('Contact.password');
  1342. $expected = array(
  1343. 'div' => array('class' => 'input password error'),
  1344. 'label' => array('for' => 'ContactPassword'),
  1345. 'Password',
  1346. '/label',
  1347. 'input' => array(
  1348. 'type' => 'password', 'name' => 'Contact[password]',
  1349. 'id' => 'ContactPassword', 'class' => 'form-error'
  1350. ),
  1351. array('div' => array('class' => 'error-message')),
  1352. array(),
  1353. '/div',
  1354. '/div'
  1355. );
  1356. $this->assertTags($result, $expected);
  1357. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1358. $expected = array(
  1359. 'div' => array('class' => 'input password error'),
  1360. 'label' => array('for' => 'ContactPassword'),
  1361. 'Password',
  1362. '/label',
  1363. 'input' => array(
  1364. 'type' => 'password', 'name' => 'Contact[password]',
  1365. 'id' => 'ContactPassword', 'class' => 'form-error'
  1366. ),
  1367. '/div'
  1368. );
  1369. $this->assertTags($result, $expected);
  1370. }
  1371. /**
  1372. * Test validation errors, when calling input() overriding validation message by an empty string.
  1373. *
  1374. * @return void
  1375. */
  1376. public function testEmptyInputErrorValidation() {
  1377. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1378. $result = $this->Form->input('Contact.password', array('error' => ''));
  1379. $expected = array(
  1380. 'div' => array('class' => 'input password error'),
  1381. 'label' => array('for' => 'ContactPassword'),
  1382. 'Password',
  1383. '/label',
  1384. 'input' => array(
  1385. 'type' => 'password', 'name' => 'Contact[password]',
  1386. 'id' => 'ContactPassword', 'class' => 'form-error'
  1387. ),
  1388. array('div' => array('class' => 'error-message')),
  1389. array(),
  1390. '/div',
  1391. '/div'
  1392. );
  1393. $this->assertTags($result, $expected);
  1394. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1395. $expected = array(
  1396. 'div' => array('class' => 'input password error'),
  1397. 'label' => array('for' => 'ContactPassword'),
  1398. 'Password',
  1399. '/label',
  1400. 'input' => array(
  1401. 'type' => 'password', 'name' => 'Contact[password]',
  1402. 'id' => 'ContactPassword', 'class' => 'form-error'
  1403. ),
  1404. '/div'
  1405. );
  1406. $this->assertTags($result, $expected);
  1407. }
  1408. /**
  1409. * testFormValidationAssociated method
  1410. *
  1411. * test display of form errors in conjunction with model::validates.
  1412. *
  1413. * @return void
  1414. */
  1415. public function testFormValidationAssociated() {
  1416. $this->UserForm = ClassRegistry::getObject('UserForm');
  1417. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1418. $data = array(
  1419. 'UserForm' => array('name' => 'user'),
  1420. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1421. );
  1422. $result = $this->UserForm->OpenidUrl->create($data);
  1423. $this->assertFalse(empty($result));
  1424. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1425. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1426. $encoding = strtolower(Configure::read('App.encoding'));
  1427. $expected = array(
  1428. 'form' => array(
  1429. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1430. 'accept-charset' => $encoding
  1431. ),
  1432. 'div' => array('style' => 'display:none;'),
  1433. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1434. '/div'
  1435. );
  1436. $this->assertTags($result, $expected);
  1437. $result = $this->Form->error(
  1438. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1439. );
  1440. $this->assertEquals('Error, not registered', $result);
  1441. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1442. }
  1443. /**
  1444. * testFormValidationAssociatedFirstLevel method
  1445. *
  1446. * test form error display with associated model.
  1447. *
  1448. * @return void
  1449. */
  1450. public function testFormValidationAssociatedFirstLevel() {
  1451. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1452. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1453. $data = array(
  1454. 'ValidateUser' => array('name' => 'mariano'),
  1455. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1456. );
  1457. $result = $this->ValidateUser->create($data);
  1458. $this->assertFalse(empty($result));
  1459. $this->assertFalse($this->ValidateUser->validates());
  1460. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1461. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1462. $encoding = strtolower(Configure::read('App.encoding'));
  1463. $expected = array(
  1464. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1465. 'div' => array('style' => 'display:none;'),
  1466. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1467. '/div'
  1468. );
  1469. $this->assertTags($result, $expected);
  1470. $result = $this->Form->error(
  1471. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1472. );
  1473. $this->assertEquals('Invalid email', $result);
  1474. $result = $this->Form->error(
  1475. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1476. );
  1477. $this->assertEquals('Invalid name', $result);
  1478. $result = $this->Form->error(
  1479. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1480. );
  1481. $this->assertEquals('Invalid city', $result);
  1482. unset($this->ValidateUser->ValidateProfile);
  1483. unset($this->ValidateUser);
  1484. }
  1485. /**
  1486. * testFormValidationAssociatedSecondLevel method
  1487. *
  1488. * test form error display with associated model.
  1489. *
  1490. * @return void
  1491. */
  1492. public function testFormValidationAssociatedSecondLevel() {
  1493. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1494. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1495. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1496. $data = array(
  1497. 'ValidateUser' => array('name' => 'mariano'),
  1498. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1499. 'ValidateItem' => array('name' => 'Item')
  1500. );
  1501. $result = $this->ValidateUser->create($data);
  1502. $this->assertFalse(empty($result));
  1503. $this->assertFalse($this->ValidateUser->validates());
  1504. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1505. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1506. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1507. $encoding = strtolower(Configure::read('App.encoding'));
  1508. $expected = array(
  1509. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1510. 'div' => array('style' => 'display:none;'),
  1511. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1512. '/div'
  1513. );
  1514. $this->assertTags($result, $expected);
  1515. $result = $this->Form->error(
  1516. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1517. );
  1518. $this->assertEquals('Invalid email', $result);
  1519. $result = $this->Form->error(
  1520. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1521. );
  1522. $this->assertEquals('Invalid name', $result);
  1523. $result = $this->Form->error(
  1524. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1525. );
  1526. $result = $this->Form->error(
  1527. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1528. );
  1529. $this->assertEquals('Invalid description', $result);
  1530. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1531. unset($this->ValidateUser->ValidateProfile);
  1532. unset($this->ValidateUser);
  1533. }
  1534. /**
  1535. * testFormValidationMultiRecord method
  1536. *
  1537. * test form error display with multiple records.
  1538. *
  1539. * @return void
  1540. */
  1541. public function testFormValidationMultiRecord() {
  1542. $Contact = ClassRegistry::getObject('Contact');
  1543. $Contact->validationErrors[2] = array(
  1544. 'name' => array('The provided value is invalid')
  1545. );
  1546. $result = $this->Form->input('Contact.2.name');
  1547. $expected = array(
  1548. 'div' => array('class' => 'input text error'),
  1549. 'label' => array('for' => 'Contact2Name'),
  1550. 'Name',
  1551. '/label',
  1552. 'input' => array(
  1553. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  1554. 'class' => 'form-error', 'maxlength' => 255
  1555. ),
  1556. array('div' => array('class' => 'error-message')),
  1557. 'The provided value is invalid',
  1558. '/div',
  1559. '/div'
  1560. );
  1561. $this->assertTags($result, $expected);
  1562. }
  1563. /**
  1564. * testMultipleInputValidation method
  1565. *
  1566. * test multiple record form validation error display.
  1567. *
  1568. * @return void
  1569. */
  1570. public function testMultipleInputValidation() {
  1571. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1572. $Address->validationErrors[0] = array(
  1573. 'title' => array('This field cannot be empty'),
  1574. 'first_name' => array('This field cannot be empty')
  1575. );
  1576. $Address->validationErrors[1] = array(
  1577. 'last_name' => array('You must have a last name')
  1578. );
  1579. $this->Form->create();
  1580. $result = $this->Form->input('Address.0.title');
  1581. $expected = array(
  1582. 'div' => array('class'),
  1583. 'label' => array('for'),
  1584. 'preg:/[^<]+/',
  1585. '/label',
  1586. 'input' => array(
  1587. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1588. ),
  1589. array('div' => array('class' => 'error-message')),
  1590. 'This field cannot be empty',
  1591. '/div',
  1592. '/div'
  1593. );
  1594. $this->assertTags($result, $expected);
  1595. $result = $this->Form->input('Address.0.first_name');
  1596. $expected = array(
  1597. 'div' => array('class'),
  1598. 'label' => array('for'),
  1599. 'preg:/[^<]+/',
  1600. '/label',
  1601. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1602. array('div' => array('class' => 'error-message')),
  1603. 'This field cannot be empty',
  1604. '/div',
  1605. '/div'
  1606. );
  1607. $this->assertTags($result, $expected);
  1608. $result = $this->Form->input('Address.0.last_name');
  1609. $expected = array(
  1610. 'div' => array('class'),
  1611. 'label' => array('for'),
  1612. 'preg:/[^<]+/',
  1613. '/label',
  1614. 'input' => array('type' => 'text', 'name', 'id'),
  1615. '/div'
  1616. );
  1617. $this->assertTags($result, $expected);
  1618. $result = $this->Form->input('Address.1.last_name');
  1619. $expected = array(
  1620. 'div' => array('class'),
  1621. 'label' => array('for'),
  1622. 'preg:/[^<]+/',
  1623. '/label',
  1624. 'input' => array(
  1625. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1626. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1627. ),
  1628. array('div' => array('class' => 'error-message')),
  1629. 'You must have a last name',
  1630. '/div',
  1631. '/div'
  1632. );
  1633. $this->assertTags($result, $expected);
  1634. }
  1635. /**
  1636. * testInput method
  1637. *
  1638. * Test various incarnations of input().
  1639. *
  1640. * @return void
  1641. */
  1642. public function testInput() {
  1643. $result = $this->Form->input('ValidateUser.balance');
  1644. $expected = array(
  1645. 'div' => array('class'),
  1646. 'label' => array('for'),
  1647. 'Balance',
  1648. '/label',
  1649. 'input' => array('name', 'type' => 'number', 'id'),
  1650. '/div',
  1651. );
  1652. $this->assertTags($result, $expected);
  1653. $result = $this->Form->input('ValidateUser.cost_decimal');
  1654. $expected = array(
  1655. 'div' => array('class'),
  1656. 'label' => array('for'),
  1657. 'Cost Decimal',
  1658. '/label',
  1659. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1660. '/div',
  1661. );
  1662. $this->assertTags($result, $expected);
  1663. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1664. $expected = array(
  1665. 'div' => array('class' => 'input email'),
  1666. 'label' => array('for' => 'custom'),
  1667. 'Email',
  1668. '/label',
  1669. array('input' => array(
  1670. 'type' => 'email', 'name' => 'Contact[email]',
  1671. 'id' => 'custom', 'maxlength' => 255
  1672. )),
  1673. '/div'
  1674. );
  1675. $this->assertTags($result, $expected);
  1676. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1677. $expected = array(
  1678. '<div',
  1679. 'label' => array('for' => 'ContactEmail'),
  1680. 'Email',
  1681. '/label',
  1682. array('input' => array(
  1683. 'type' => 'email', 'name' => 'Contact[email]',
  1684. 'id' => 'ContactEmail', 'maxlength' => 255
  1685. )),
  1686. '/div'
  1687. );
  1688. $this->assertTags($result, $expected);
  1689. $result = $this->Form->hidden('Contact.idontexist');
  1690. $expected = array('input' => array(
  1691. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  1692. 'id' => 'ContactIdontexist'
  1693. ));
  1694. $this->assertTags($result, $expected);
  1695. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1696. $expected = array(
  1697. 'div' => array('class' => 'input text'),
  1698. 'label' => array('for' => 'ContactEmail'),
  1699. 'Email',
  1700. '/label',
  1701. array('input' => array(
  1702. 'type' => 'text', 'name' => 'Contact[email]',
  1703. 'id' => 'ContactEmail'
  1704. )),
  1705. '/div'
  1706. );
  1707. $this->assertTags($result, $expected);
  1708. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1709. $expected = array(
  1710. 'div' => array('class' => 'input text'),
  1711. 'label' => array('for' => 'Contact5Email'),
  1712. 'Email',
  1713. '/label',
  1714. array('input' => array(
  1715. 'type' => 'text', 'name' => 'Contact[5][email]',
  1716. 'id' => 'Contact5Email'
  1717. )),
  1718. '/div'
  1719. );
  1720. $this->assertTags($result, $expected);
  1721. $result = $this->Form->input('Contact.password');
  1722. $expected = array(
  1723. 'div' => array('class' => 'input password'),
  1724. 'label' => array('for' => 'ContactPassword'),
  1725. 'Password',
  1726. '/label',
  1727. array('input' => array(
  1728. 'type' => 'password', 'name' => 'Contact[password]',
  1729. 'id' => 'ContactPassword'
  1730. )),
  1731. '/div'
  1732. );
  1733. $this->assertTags($result, $expected);
  1734. $result = $this->Form->input('Contact.email', array(
  1735. 'type' => 'file', 'class' => 'textbox'
  1736. ));
  1737. $expected = array(
  1738. 'div' => array('class' => 'input file'),
  1739. 'label' => array('for' => 'ContactEmail'),
  1740. 'Email',
  1741. '/label',
  1742. array('input' => array(
  1743. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1744. 'id' => 'ContactEmail'
  1745. )),
  1746. '/div'
  1747. );
  1748. $this->assertTags($result, $expected);
  1749. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1750. $result = $this->Form->input('Contact.phone');
  1751. $expected = array(
  1752. 'div' => array('class' => 'input tel'),
  1753. 'label' => array('for' => 'ContactPhone'),
  1754. 'Phone',
  1755. '/label',
  1756. array('input' => array(
  1757. 'type' => 'tel', 'name' => 'Contact[phone]',
  1758. 'value' => 'Hello &amp; World &gt; weird chars',
  1759. 'id' => 'ContactPhone', 'maxlength' => 255
  1760. )),
  1761. '/div'
  1762. );
  1763. $this->assertTags($result, $expected);
  1764. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1765. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1766. $expected = array(
  1767. 'div' => array('class' => 'input text'),
  1768. 'label' => array('for' => 'myId'),
  1769. 'Field',
  1770. '/label',
  1771. 'input' => array(
  1772. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1773. 'value' => 'My value', 'id' => 'myId'
  1774. ),
  1775. '/div'
  1776. );
  1777. $this->assertTags($result, $expected);
  1778. unset($this->Form->request->data);
  1779. $Contact = ClassRegistry::getObject('Contact');
  1780. $Contact->validationErrors['field'] = array('Badness!');
  1781. $result = $this->Form->input('Contact.field');
  1782. $expected = array(
  1783. 'div' => array('class' => 'input text error'),
  1784. 'label' => array('for' => 'ContactField'),
  1785. 'Field',
  1786. '/label',
  1787. 'input' => array(
  1788. 'type' => 'text', 'name' => 'Contact[field]',
  1789. 'id' => 'ContactField', 'class' => 'form-error'
  1790. ),
  1791. array('div' => array('class' => 'error-message')),
  1792. 'Badness!',
  1793. '/div',
  1794. '/div'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. $result = $this->Form->input('Contact.field', array(
  1798. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1799. ));
  1800. $expected = array(
  1801. 'label' => array('for' => 'ContactField'),
  1802. 'Field',
  1803. '/label',
  1804. 'input' => array(
  1805. 'type' => 'text', 'name' => 'Contact[field]',
  1806. 'id' => 'ContactField', 'class' => 'form-error'
  1807. ),
  1808. array('span' => array('class' => 'error-message')),
  1809. 'Badness!',
  1810. '/span'
  1811. );
  1812. $this->assertTags($result, $expected);
  1813. $result = $this->Form->input('Contact.field', array(
  1814. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1815. ));
  1816. $expected = array(
  1817. 'div' => array('class' => 'input text error'),
  1818. 'label' => array('for' => 'ContactField'),
  1819. 'Field',
  1820. '/label',
  1821. 'input' => array(
  1822. 'type' => 'text', 'name' => 'Contact[field]',
  1823. 'id' => 'ContactField', 'class' => 'form-error'
  1824. ),
  1825. array('div' => array('class' => 'error')),
  1826. 'Badness!',
  1827. '/div'
  1828. );
  1829. $this->assertTags($result, $expected);
  1830. $result = $this->Form->input('Contact.field', array(
  1831. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1832. ));
  1833. $expected = array(
  1834. 'span' => array('class' => 'input text error'),
  1835. 'label' => array('for' => 'ContactField'),
  1836. 'Field',
  1837. '/label',
  1838. 'input' => array(
  1839. 'type' => 'text', 'name' => 'Contact[field]',
  1840. 'id' => 'ContactField', 'class' => 'form-error'
  1841. ),
  1842. 'Badness!',
  1843. '/span'
  1844. );
  1845. $this->assertTags($result, $expected);
  1846. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1847. $expected = array(
  1848. 'div' => array('class' => 'input text error'),
  1849. 'label' => array('for' => 'ContactField'),
  1850. 'Field',
  1851. '/label',
  1852. 'input' => array(
  1853. 'type' => 'text', 'name' => 'Contact[field]',
  1854. 'id' => 'ContactField', 'class' => 'form-error'
  1855. ),
  1856. 'A message to you, Rudy',
  1857. array('div' => array('class' => 'error-message')),
  1858. 'Badness!',
  1859. '/div',
  1860. '/div'
  1861. );
  1862. $this->assertTags($result, $expected);
  1863. $this->Form->setEntity(null);
  1864. $this->Form->setEntity('Contact.field');
  1865. $result = $this->Form->input('Contact.field', array(
  1866. 'after' => 'A message to you, Rudy', 'error' => false
  1867. ));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input text'),
  1870. 'label' => array('for' => 'ContactField'),
  1871. 'Field',
  1872. '/label',
  1873. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1874. 'A message to you, Rudy',
  1875. '/div'
  1876. );
  1877. $this->assertTags($result, $expected);
  1878. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1879. $expected = array(
  1880. 'div' => array('class' => 'input text'),
  1881. 'label' => array('for' => 'ObjectField'),
  1882. 'Field',
  1883. '/label',
  1884. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  1885. 'A message to you, Rudy',
  1886. '/div'
  1887. );
  1888. $this->assertTags($result, $expected);
  1889. $Contact->validationErrors['field'] = array('minLength');
  1890. $result = $this->Form->input('Contact.field', array(
  1891. 'error' => array(
  1892. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1893. 'maxLength' => 'login too large'
  1894. )
  1895. ));
  1896. $expected = array(
  1897. 'div' => array('class' => 'input text error'),
  1898. 'label' => array('for' => 'ContactField'),
  1899. 'Field',
  1900. '/label',
  1901. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1902. array('div' => array('class' => 'error-message')),
  1903. 'Le login doit contenir au moins 2 caractères',
  1904. '/div',
  1905. '/div'
  1906. );
  1907. $this->assertTags($result, $expected);
  1908. $Contact->validationErrors['field'] = array('maxLength');
  1909. $result = $this->Form->input('Contact.field', array(
  1910. 'error' => array(
  1911. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1912. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1913. 'maxLength' => 'login too large',
  1914. )
  1915. ));
  1916. $expected = array(
  1917. 'div' => array('class' => 'input text error'),
  1918. 'label' => array('for' => 'ContactField'),
  1919. 'Field',
  1920. '/label',
  1921. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1922. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1923. 'login too large',
  1924. '/span',
  1925. '/div'
  1926. );
  1927. $this->assertTags($result, $expected);
  1928. }
  1929. /**
  1930. * Test that inputs with 0 can be created.
  1931. *
  1932. * @return void
  1933. */
  1934. public function testInputZero() {
  1935. $this->Form->create('User');
  1936. $result = $this->Form->input('0');
  1937. $expected = array(
  1938. 'div' => array('class' => 'input text'),
  1939. 'label' => array('for' => 'User0'), '/label',
  1940. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. }
  1945. /**
  1946. * test input() with checkbox creation
  1947. *
  1948. * @return void
  1949. */
  1950. public function testInputCheckbox() {
  1951. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1952. $expected = array(
  1953. 'div' => array('class' => 'input checkbox'),
  1954. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1955. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1956. '/div'
  1957. );
  1958. $this->assertTags($result, $expected);
  1959. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1960. $expected = array(
  1961. 'div' => array('class' => 'input checkbox'),
  1962. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1963. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1964. '/div'
  1965. );
  1966. $this->assertTags($result, $expected);
  1967. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1968. $expected = array(
  1969. 'div' => array('class' => 'input checkbox'),
  1970. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1971. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1972. '/div'
  1973. );
  1974. $this->assertTags($result, $expected);
  1975. $result = $this->Form->input('User.disabled', array(
  1976. 'label' => 'Disabled',
  1977. 'type' => 'checkbox',
  1978. 'data-foo' => 'disabled'
  1979. ));
  1980. $expected = array(
  1981. 'div' => array('class' => 'input checkbox'),
  1982. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1983. array('input' => array(
  1984. 'type' => 'checkbox',
  1985. 'name' => 'data[User][disabled]',
  1986. 'value' => '1',
  1987. 'id' => 'UserDisabled',
  1988. 'data-foo' => 'disabled'
  1989. )),
  1990. 'label' => array('for' => 'UserDisabled'),
  1991. 'Disabled',
  1992. '/label',
  1993. '/div'
  1994. );
  1995. $this->assertTags($result, $expected);
  1996. }
  1997. /**
  1998. * test form->input() with time types.
  1999. *
  2000. */
  2001. public function testInputTime() {
  2002. extract($this->dateRegex);
  2003. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2004. $result = explode(':', $result);
  2005. $this->assertRegExp('/option value="23"/', $result[0]);
  2006. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2007. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2008. $result = explode(':', $result);
  2009. $this->assertRegExp('/option value="23"/', $result[0]);
  2010. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2011. $result = $this->Form->input('Model.field', array(
  2012. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2013. ));
  2014. $result = explode(':', $result);
  2015. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2016. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2017. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2018. $result = $this->Form->input('Model.field', array(
  2019. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2020. ));
  2021. $result = explode(':', $result);
  2022. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2023. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2024. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2025. $result = $this->Form->input('prueba', array(
  2026. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2027. 'maxYear' => date('Y') + 1, 'interval' => 15
  2028. ));
  2029. $result = explode(':', $result);
  2030. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2031. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2032. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2033. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2034. $result = $this->Form->input('Random.start_time', array(
  2035. 'type' => 'time',
  2036. 'selected' => '18:15'
  2037. ));
  2038. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2039. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2040. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2041. $result = $this->Form->input('published', array('type' => 'time'));
  2042. $now = strtotime('now');
  2043. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2044. $now = strtotime('2013-03-09 00:42:21');
  2045. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2046. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2047. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2048. }
  2049. /**
  2050. * Test interval + selected near the hour roll over.
  2051. *
  2052. * @return void
  2053. */
  2054. public function testTimeSelectedWithInterval() {
  2055. $result = $this->Form->input('Model.start_time', array(
  2056. 'type' => 'time',
  2057. 'interval' => 15,
  2058. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2059. ));
  2060. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2061. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2062. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2063. $result = $this->Form->input('Model.start_time', array(
  2064. 'type' => 'time',
  2065. 'interval' => 15,
  2066. 'selected' => '2012-10-23 15:57:00'
  2067. ));
  2068. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2069. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2070. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2071. $result = $this->Form->input('Model.start_time', array(
  2072. 'timeFormat' => 24,
  2073. 'type' => 'time',
  2074. 'interval' => 15,
  2075. 'selected' => '15:57'
  2076. ));
  2077. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2078. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2079. $result = $this->Form->input('Model.start_time', array(
  2080. 'timeFormat' => 24,
  2081. 'type' => 'time',
  2082. 'interval' => 15,
  2083. 'selected' => '23:57'
  2084. ));
  2085. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2086. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2087. $result = $this->Form->input('Model.created', array(
  2088. 'timeFormat' => 24,
  2089. 'type' => 'datetime',
  2090. 'interval' => 15,
  2091. 'selected' => '2012-09-30 23:56'
  2092. ));
  2093. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2094. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2095. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2096. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2097. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2098. }
  2099. /**
  2100. * Test time with selected values around 12:xx:xx
  2101. *
  2102. * @return void
  2103. */
  2104. public function testTimeSelectedWithIntervalTwelve() {
  2105. $result = $this->Form->input('Model.start_time', array(
  2106. 'type' => 'time',
  2107. 'timeFormat' => 12,
  2108. 'interval' => 15,
  2109. 'selected' => '00:00:00'
  2110. ));
  2111. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2112. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2113. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2114. $result = $this->Form->input('Model.start_time', array(
  2115. 'type' => 'time',
  2116. 'timeFormat' => 12,
  2117. 'interval' => 15,
  2118. 'selected' => '12:00:00'
  2119. ));
  2120. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2121. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2122. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2123. $result = $this->Form->input('Model.start_time', array(
  2124. 'type' => 'time',
  2125. 'timeFormat' => 12,
  2126. 'interval' => 15,
  2127. 'selected' => '12:15:00'
  2128. ));
  2129. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2130. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2131. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2132. }
  2133. /**
  2134. * Test interval & timeFormat = 12
  2135. *
  2136. * @return void
  2137. */
  2138. public function testInputTimeWithIntervalAnd12HourFormat() {
  2139. $result = $this->Form->input('Model.start_time', array(
  2140. 'type' => 'time',
  2141. 'timeFormat' => 12,
  2142. 'interval' => 5,
  2143. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2144. ));
  2145. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2146. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2147. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2148. $result = $this->Form->input('Model.start_time', array(
  2149. 'type' => 'time',
  2150. 'timeFormat' => '12',
  2151. 'interval' => 5,
  2152. 'selected' => '2013-04-19 16:30:00'
  2153. ));
  2154. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2155. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2156. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2157. $result = $this->Form->input('Model.start_time', array(
  2158. 'type' => 'time',
  2159. 'timeFormat' => '12',
  2160. 'interval' => 10,
  2161. 'selected' => '2013-05-19 00:33:00'
  2162. ));
  2163. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2164. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2165. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2166. $result = $this->Form->input('Model.start_time', array(
  2167. 'type' => 'time',
  2168. 'timeFormat' => '12',
  2169. 'interval' => 10,
  2170. 'selected' => '2013-05-19 13:33:00'
  2171. ));
  2172. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2173. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2174. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2175. $result = $this->Form->input('Model.start_time', array(
  2176. 'type' => 'time',
  2177. 'timeFormat' => '12',
  2178. 'interval' => 10,
  2179. 'selected' => '2013-05-19 01:33:00'
  2180. ));
  2181. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2182. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2183. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2184. }
  2185. /**
  2186. * test form->input() with datetime, date and time types
  2187. *
  2188. * @return void
  2189. */
  2190. public function testInputDatetime() {
  2191. extract($this->dateRegex);
  2192. $result = $this->Form->input('prueba', array(
  2193. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2194. 'maxYear' => date('Y') + 1, 'interval' => 15
  2195. ));
  2196. $result = explode(':', $result);
  2197. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2198. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2199. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2200. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2201. //related to ticket #5013
  2202. $result = $this->Form->input('Contact.date', array(
  2203. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2204. ));
  2205. $this->assertRegExp('/class="customClass"/', $result);
  2206. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2207. $result = $this->Form->input('Contact.date', array(
  2208. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2209. ));
  2210. $this->assertRegExp('/id="customIdDay"/', $result);
  2211. $this->assertRegExp('/id="customIdMonth"/', $result);
  2212. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2213. $result = $this->Form->input('Model.field', array(
  2214. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2215. ));
  2216. $this->assertRegExp('/id="customIDDay"/', $result);
  2217. $this->assertRegExp('/id="customIDHour"/', $result);
  2218. $result = explode('</select><select', $result);
  2219. $result = explode(':', $result[1]);
  2220. $this->assertRegExp('/option value="23"/', $result[0]);
  2221. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2222. $result = $this->Form->input('Model.field', array(
  2223. 'type' => 'datetime', 'timeFormat' => 12
  2224. ));
  2225. $result = explode('</select><select', $result);
  2226. $result = explode(':', $result[1]);
  2227. $this->assertRegExp('/option value="12"/', $result[0]);
  2228. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2229. $this->Form->request->data = array('Contact' => array('created' => null));
  2230. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2231. $expected = array(
  2232. 'div' => array('class' => 'input date'),
  2233. 'label' => array('for' => 'ContactCreatedMonth'),
  2234. 'Created',
  2235. '/label',
  2236. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2237. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2238. $monthsRegex,
  2239. '/select', '-',
  2240. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2241. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2242. $daysRegex,
  2243. '/select', '-',
  2244. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2245. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2246. $yearsRegex,
  2247. '/select',
  2248. '/div'
  2249. );
  2250. $this->assertTags($result, $expected);
  2251. $this->Form->request->data = array('Contact' => array('created' => null));
  2252. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2253. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2254. $this->Form->request->data = array('Contact' => array('created' => null));
  2255. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2256. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2257. $result = $this->Form->input('Contact.created', array(
  2258. 'type' => 'date',
  2259. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2260. 'timeFormat' => 'NONE'
  2261. ));
  2262. $this->assertRegExp('/for\="created-month"/', $result);
  2263. }
  2264. /**
  2265. * Test generating checkboxes in a loop.
  2266. *
  2267. * @return void
  2268. */
  2269. public function testInputCheckboxesInLoop() {
  2270. for ($i = 1; $i < 5; $i++) {
  2271. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2272. $expected = array(
  2273. 'div' => array('class' => 'input checkbox'),
  2274. 'input' => array('type' => 'hidden', 'name' => "Contact[{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2275. array('input' => array('type' => 'checkbox', 'name' => "Contact[{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2276. 'label' => array('for' => "Contact{$i}Email"),
  2277. 'Email',
  2278. '/label',
  2279. '/div'
  2280. );
  2281. $this->assertTags($result, $expected);
  2282. }
  2283. }
  2284. /**
  2285. * Test generating checkboxes with disabled elements.
  2286. *
  2287. * @return void
  2288. */
  2289. public function testInputCheckboxWithDisabledElements() {
  2290. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2291. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2292. $expected = array(
  2293. array('div' => array('class' => 'input select')),
  2294. array('label' => array('for' => "ContactMultiple")),
  2295. 'Multiple',
  2296. '/label',
  2297. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2298. array('div' => array('class' => 'checkbox')),
  2299. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2300. array('label' => array('for' => "ContactMultiple1")),
  2301. 'One',
  2302. '/label',
  2303. '/div',
  2304. array('div' => array('class' => 'checkbox')),
  2305. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2306. array('label' => array('for' => "ContactMultiple2")),
  2307. 'Two',
  2308. '/label',
  2309. '/div',
  2310. array('div' => array('class' => 'checkbox')),
  2311. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2312. array('label' => array('for' => "ContactMultiple3")),
  2313. 'Three',
  2314. '/label',
  2315. '/div',
  2316. '/div'
  2317. );
  2318. $this->assertTags($result, $expected);
  2319. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2320. $this->assertTags($result, $expected);
  2321. $disabled = array('2', 3);
  2322. $expected = array(
  2323. array('div' => array('class' => 'input select')),
  2324. array('label' => array('for' => "ContactMultiple")),
  2325. 'Multiple',
  2326. '/label',
  2327. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2328. array('div' => array('class' => 'checkbox')),
  2329. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2330. array('label' => array('for' => "ContactMultiple1")),
  2331. 'One',
  2332. '/label',
  2333. '/div',
  2334. array('div' => array('class' => 'checkbox')),
  2335. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2336. array('label' => array('for' => "ContactMultiple2")),
  2337. 'Two',
  2338. '/label',
  2339. '/div',
  2340. array('div' => array('class' => 'checkbox')),
  2341. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2342. array('label' => array('for' => "ContactMultiple3")),
  2343. 'Three',
  2344. '/label',
  2345. '/div',
  2346. '/div'
  2347. );
  2348. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2349. $this->assertTags($result, $expected);
  2350. // make sure 50 does only disable 50, and not 50f5c0cf
  2351. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2352. $disabled = array(50);
  2353. $expected = array(
  2354. array('div' => array('class' => 'input select')),
  2355. array('label' => array('for' => "ContactMultiple")),
  2356. 'Multiple',
  2357. '/label',
  2358. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2359. array('div' => array('class' => 'checkbox')),
  2360. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2361. array('label' => array('for' => "ContactMultiple50")),
  2362. 'Fifty',
  2363. '/label',
  2364. '/div',
  2365. array('div' => array('class' => 'checkbox')),
  2366. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2367. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2368. 'Stringy',
  2369. '/label',
  2370. '/div',
  2371. '/div'
  2372. );
  2373. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2374. $this->assertTags($result, $expected);
  2375. }
  2376. /**
  2377. * test input name with leading integer, ensure attributes are generated correctly.
  2378. *
  2379. * @return void
  2380. */
  2381. public function testInputWithLeadingInteger() {
  2382. $result = $this->Form->text('0.Node.title');
  2383. $expected = array(
  2384. 'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2385. );
  2386. $this->assertTags($result, $expected);
  2387. }
  2388. /**
  2389. * test form->input() with select type inputs.
  2390. *
  2391. * @return void
  2392. */
  2393. public function testInputSelectType() {
  2394. $result = $this->Form->input('email', array(
  2395. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2396. );
  2397. $expected = array(
  2398. 'div' => array('class' => 'input select'),
  2399. 'label' => array('for' => 'email'),
  2400. 'Email',
  2401. '/label',
  2402. array('select' => array('name' => 'email', 'id' => 'email')),
  2403. array('option' => array('value' => '')),
  2404. '/option',
  2405. array('option' => array('value' => 'è')),
  2406. 'Firést',
  2407. '/option',
  2408. array('option' => array('value' => 'é')),
  2409. 'Secoènd',
  2410. '/option',
  2411. '/select',
  2412. '/div'
  2413. );
  2414. $this->assertTags($result, $expected);
  2415. $result = $this->Form->input('email', array(
  2416. 'options' => array('First', 'Second'), 'empty' => true)
  2417. );
  2418. $expected = array(
  2419. 'div' => array('class' => 'input select'),
  2420. 'label' => array('for' => 'email'),
  2421. 'Email',
  2422. '/label',
  2423. array('select' => array('name' => 'email', 'id' => 'email')),
  2424. array('option' => array('value' => '')),
  2425. '/option',
  2426. array('option' => array('value' => '0')),
  2427. 'First',
  2428. '/option',
  2429. array('option' => array('value' => '1')),
  2430. 'Second',
  2431. '/option',
  2432. '/select',
  2433. '/div'
  2434. );
  2435. $this->assertTags($result, $expected);
  2436. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2437. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2438. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2439. $expected = array(
  2440. 'div' => array('class' => 'input select'),
  2441. 'label' => array('for' => 'ModelUserId'),
  2442. 'User',
  2443. '/label',
  2444. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2445. array('option' => array('value' => '')),
  2446. '/option',
  2447. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2448. 'good',
  2449. '/option',
  2450. array('option' => array('value' => 'other')),
  2451. 'bad',
  2452. '/option',
  2453. '/select',
  2454. '/div'
  2455. );
  2456. $this->assertTags($result, $expected);
  2457. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2458. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2459. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2460. $expected = array(
  2461. 'div' => array('class' => 'input select'),
  2462. 'label' => array('for' => 'ThingUserId'),
  2463. 'User',
  2464. '/label',
  2465. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2466. array('option' => array('value' => '')),
  2467. 'Some Empty',
  2468. '/option',
  2469. array('option' => array('value' => 'value')),
  2470. 'good',
  2471. '/option',
  2472. array('option' => array('value' => 'other')),
  2473. 'bad',
  2474. '/option',
  2475. '/select',
  2476. '/div'
  2477. );
  2478. $this->assertTags($result, $expected);
  2479. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2480. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2481. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2482. $expected = array(
  2483. 'div' => array('class' => 'input select'),
  2484. 'label' => array('for' => 'ThingUserId'),
  2485. 'User',
  2486. '/label',
  2487. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2488. array('option' => array('value' => '')),
  2489. 'Some Empty',
  2490. '/option',
  2491. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2492. 'good',
  2493. '/option',
  2494. array('option' => array('value' => 'other')),
  2495. 'bad',
  2496. '/option',
  2497. '/select',
  2498. '/div'
  2499. );
  2500. $this->assertTags($result, $expected);
  2501. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2502. $this->Form->request->data = array('User' => array('User' => array('value')));
  2503. $result = $this->Form->input('User.User', array('empty' => true));
  2504. $expected = array(
  2505. 'div' => array('class' => 'input select'),
  2506. 'label' => array('for' => 'UserUser'),
  2507. 'User',
  2508. '/label',
  2509. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  2510. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2511. array('option' => array('value' => '')),
  2512. '/option',
  2513. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2514. 'good',
  2515. '/option',
  2516. array('option' => array('value' => 'other')),
  2517. 'bad',
  2518. '/option',
  2519. '/select',
  2520. '/div'
  2521. );
  2522. $this->assertTags($result, $expected);
  2523. $this->Form->data = array();
  2524. $result = $this->Form->input('Publisher.id', array(
  2525. 'label' => 'Publisher',
  2526. 'type' => 'select',
  2527. 'multiple' => 'checkbox',
  2528. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2529. ));
  2530. $expected = array(
  2531. array('div' => array('class' => 'input select')),
  2532. array('label' => array('for' => 'PublisherId')),
  2533. 'Publisher',
  2534. '/label',
  2535. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  2536. array('div' => array('class' => 'checkbox')),
  2537. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2538. array('label' => array('for' => 'PublisherIdValue1')),
  2539. 'Label 1',
  2540. '/label',
  2541. '/div',
  2542. array('div' => array('class' => 'checkbox')),
  2543. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2544. array('label' => array('for' => 'PublisherIdValue2')),
  2545. 'Label 2',
  2546. '/label',
  2547. '/div',
  2548. '/div'
  2549. );
  2550. $this->assertTags($result, $expected);
  2551. }
  2552. /**
  2553. * test that input() and a non standard primary key makes a hidden input by default.
  2554. *
  2555. * @return void
  2556. */
  2557. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2558. $this->Form->create('User');
  2559. $this->Form->fieldset = array(
  2560. 'User' => array(
  2561. 'fields' => array(
  2562. 'model_id' => array('type' => 'integer')
  2563. ),
  2564. 'validates' => array(),
  2565. 'key' => 'model_id'
  2566. )
  2567. );
  2568. $result = $this->Form->input('model_id');
  2569. $expected = array(
  2570. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  2571. );
  2572. $this->assertTags($result, $expected);
  2573. }
  2574. /**
  2575. * test that overriding the magic select type widget is possible
  2576. *
  2577. * @return void
  2578. */
  2579. public function testInputOverridingMagicSelectType() {
  2580. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2581. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2582. $expected = array(
  2583. 'div' => array('class' => 'input text'),
  2584. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2585. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2586. '/div'
  2587. );
  2588. $this->assertTags($result, $expected);
  2589. //Check that magic types still work for plural/singular vars
  2590. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2591. $result = $this->Form->input('Model.type');
  2592. $expected = array(
  2593. 'div' => array('class' => 'input select'),
  2594. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2595. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  2596. array('option' => array('value' => 'value')), 'good', '/option',
  2597. array('option' => array('value' => 'other')), 'bad', '/option',
  2598. '/select',
  2599. '/div'
  2600. );
  2601. $this->assertTags($result, $expected);
  2602. }
  2603. /**
  2604. * Test that inferred types do not override developer input
  2605. *
  2606. * @return void
  2607. */
  2608. public function testInputMagicTypeDoesNotOverride() {
  2609. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2610. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2611. $expected = array(
  2612. 'div' => array('class' => 'input checkbox'),
  2613. array('input' => array(
  2614. 'type' => 'hidden',
  2615. 'name' => 'data[Model][user]',
  2616. 'id' => 'ModelUser_',
  2617. 'value' => 0,
  2618. )),
  2619. array('input' => array(
  2620. 'name' => 'data[Model][user]',
  2621. 'type' => 'checkbox',
  2622. 'id' => 'ModelUser',
  2623. 'value' => 1
  2624. )),
  2625. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2626. '/div'
  2627. );
  2628. $this->assertTags($result, $expected);
  2629. }
  2630. /**
  2631. * Test that magic input() selects are created for type=number
  2632. *
  2633. * @return void
  2634. */
  2635. public function testInputMagicSelectForTypeNumber() {
  2636. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2637. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2638. $result = $this->Form->input('ValidateUser.balance');
  2639. $expected = array(
  2640. 'div' => array('class' => 'input select'),
  2641. 'label' => array('for' => 'ValidateUserBalance'),
  2642. 'Balance',
  2643. '/label',
  2644. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  2645. array('option' => array('value' => '0')),
  2646. 'nothing',
  2647. '/option',
  2648. array('option' => array('value' => '1', 'selected' => 'selected')),
  2649. 'some',
  2650. '/option',
  2651. array('option' => array('value' => '100')),
  2652. 'a lot',
  2653. '/option',
  2654. '/select',
  2655. '/div'
  2656. );
  2657. $this->assertTags($result, $expected);
  2658. }
  2659. /**
  2660. * Test that magic input() selects can easily be converted into radio types without error.
  2661. *
  2662. * @return void
  2663. */
  2664. public function testInputMagicSelectChangeToRadio() {
  2665. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2666. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2667. $this->assertRegExp('/input type="radio"/', $result);
  2668. }
  2669. /**
  2670. * fields with the same name as the model should work.
  2671. *
  2672. * @return void
  2673. */
  2674. public function testInputWithMatchingFieldAndModelName() {
  2675. $this->Form->create('User');
  2676. $this->Form->fieldset = array(
  2677. 'User' => array(
  2678. 'fields' => array(
  2679. 'User' => array('type' => 'text')
  2680. ),
  2681. 'validates' => array(),
  2682. 'key' => 'id'
  2683. )
  2684. );
  2685. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2686. $result = $this->Form->input('User', array('type' => 'text'));
  2687. $expected = array(
  2688. 'div' => array('class' => 'input text'),
  2689. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2690. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2691. '/div'
  2692. );
  2693. $this->assertTags($result, $expected);
  2694. }
  2695. /**
  2696. * testFormInputs method
  2697. *
  2698. * test correct results from form::inputs().
  2699. *
  2700. * @return void
  2701. */
  2702. public function testFormInputs() {
  2703. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2704. $result = $this->Form->inputs('The Legend');
  2705. $expected = array(
  2706. '<fieldset',
  2707. '<legend',
  2708. 'The Legend',
  2709. '/legend',
  2710. '*/fieldset',
  2711. );
  2712. $this->assertTags($result, $expected);
  2713. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2714. $expected = array(
  2715. 'fieldset' => array('class' => 'classy-stuff'),
  2716. '<legend',
  2717. 'Field of Dreams',
  2718. '/legend',
  2719. '*/fieldset'
  2720. );
  2721. $this->assertTags($result, $expected);
  2722. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2723. $this->assertTags($result, $expected);
  2724. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2725. $this->assertTags($result, $expected);
  2726. $this->Form->create('Contact');
  2727. $this->Form->request['prefix'] = 'admin';
  2728. $this->Form->request['action'] = 'admin_edit';
  2729. $result = $this->Form->inputs();
  2730. $expected = array(
  2731. '<fieldset',
  2732. '<legend',
  2733. 'Edit Contact',
  2734. '/legend',
  2735. '*/fieldset',
  2736. );
  2737. $this->assertTags($result, $expected);
  2738. $this->Form->create('Contact');
  2739. $result = $this->Form->inputs(false);
  2740. $expected = array(
  2741. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2742. array('div' => array('class' => 'input text')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input email')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input tel')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input password')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input date')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input date')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input datetime')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input number')),
  2757. '*/div',
  2758. array('div' => array('class' => 'input select')),
  2759. '*/div',
  2760. );
  2761. $this->assertTags($result, $expected);
  2762. $this->Form->create('Contact');
  2763. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2764. $expected = array(
  2765. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2766. array('div' => array('class' => 'input text')),
  2767. '*/div',
  2768. array('div' => array('class' => 'input email')),
  2769. '*/div',
  2770. array('div' => array('class' => 'input tel')),
  2771. '*/div',
  2772. array('div' => array('class' => 'input password')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input date')),
  2775. '*/div',
  2776. array('div' => array('class' => 'input date')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input datetime')),
  2779. '*/div',
  2780. array('div' => array('class' => 'input number')),
  2781. '*/div',
  2782. array('div' => array('class' => 'input select')),
  2783. '*/div',
  2784. );
  2785. $this->assertTags($result, $expected);
  2786. $this->Form->create('Contact');
  2787. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2788. $this->assertTags($result, $expected);
  2789. $this->Form->create('Contact');
  2790. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2791. $expected = array(
  2792. 'fieldset' => array(),
  2793. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2794. array('div' => array('class' => 'input text')),
  2795. '*/div',
  2796. array('div' => array('class' => 'input email')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input tel')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input password')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input date')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input datetime')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input number')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input select')),
  2811. '*/div',
  2812. '/fieldset'
  2813. );
  2814. $this->assertTags($result, $expected);
  2815. $this->Form->create('Contact');
  2816. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2817. $expected = array(
  2818. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2819. array('div' => array('class' => 'input text')),
  2820. '*/div',
  2821. array('div' => array('class' => 'input email')),
  2822. '*/div',
  2823. array('div' => array('class' => 'input tel')),
  2824. '*/div',
  2825. array('div' => array('class' => 'input password')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input date')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input date')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input datetime')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input number')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input select')),
  2836. '*/div',
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $this->Form->create('Contact');
  2840. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2841. $this->assertTags($result, $expected);
  2842. $this->Form->create('Contact');
  2843. $result = $this->Form->inputs('Hello');
  2844. $expected = array(
  2845. 'fieldset' => array(),
  2846. 'legend' => array(),
  2847. 'Hello',
  2848. '/legend',
  2849. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2850. array('div' => array('class' => 'input text')),
  2851. '*/div',
  2852. array('div' => array('class' => 'input email')),
  2853. '*/div',
  2854. array('div' => array('class' => 'input tel')),
  2855. '*/div',
  2856. array('div' => array('class' => 'input password')),
  2857. '*/div',
  2858. array('div' => array('class' => 'input date')),
  2859. '*/div',
  2860. array('div' => array('class' => 'input date')),
  2861. '*/div',
  2862. array('div' => array('class' => 'input datetime')),
  2863. '*/div',
  2864. array('div' => array('class' => 'input number')),
  2865. '*/div',
  2866. array('div' => array('class' => 'input select')),
  2867. '*/div',
  2868. '/fieldset'
  2869. );
  2870. $this->assertTags($result, $expected);
  2871. $this->Form->create('Contact');
  2872. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2873. $expected = array(
  2874. 'fieldset' => array(),
  2875. 'legend' => array(),
  2876. 'Hello',
  2877. '/legend',
  2878. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2879. array('div' => array('class' => 'input text')),
  2880. '*/div',
  2881. array('div' => array('class' => 'input email')),
  2882. '*/div',
  2883. array('div' => array('class' => 'input tel')),
  2884. '*/div',
  2885. array('div' => array('class' => 'input password')),
  2886. '*/div',
  2887. array('div' => array('class' => 'input date')),
  2888. '*/div',
  2889. array('div' => array('class' => 'input date')),
  2890. '*/div',
  2891. array('div' => array('class' => 'input datetime')),
  2892. '*/div',
  2893. array('div' => array('class' => 'input number')),
  2894. '*/div',
  2895. array('div' => array('class' => 'input select')),
  2896. '*/div',
  2897. '/fieldset'
  2898. );
  2899. $this->assertTags($result, $expected);
  2900. $this->Form->create('Contact');
  2901. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2902. $this->assertTags($result, $expected);
  2903. $this->Form->end();
  2904. $this->Form->create(false);
  2905. $expected = array(
  2906. 'fieldset' => array(),
  2907. array('div' => array('class' => 'input text')),
  2908. 'label' => array('for' => 'foo'),
  2909. 'Foo',
  2910. '/label',
  2911. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2912. '*/div',
  2913. '/fieldset'
  2914. );
  2915. $result = $this->Form->inputs(
  2916. array('foo' => array('type' => 'text')),
  2917. array(),
  2918. array('legend' => false)
  2919. );
  2920. $this->assertTags($result, $expected);
  2921. }
  2922. /**
  2923. * Tests inputs() works with plugin models
  2924. *
  2925. * @return void
  2926. */
  2927. public function testInputsPluginModel() {
  2928. $this->loadFixtures('Post');
  2929. Plugin::load('TestPlugin');
  2930. $this->Form->request['models'] = array(
  2931. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2932. );
  2933. $this->Form->create('TestPlugin.TestPluginPost');
  2934. $result = $this->Form->inputs();
  2935. $this->assertContains('TestPluginPost[id]', $result);
  2936. $this->assertContains('TestPluginPost[author_id]', $result);
  2937. $this->assertContains('TestPluginPost[title]', $result);
  2938. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2939. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2940. $this->assertEquals('TestPluginPost', $this->Form->model());
  2941. }
  2942. /**
  2943. * testSelectAsCheckbox method
  2944. *
  2945. * test multi-select widget with checkbox formatting.
  2946. *
  2947. * @return void
  2948. */
  2949. public function testSelectAsCheckbox() {
  2950. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2951. $expected = array(
  2952. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2953. array('div' => array('class' => 'checkbox')),
  2954. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2955. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2956. 'first',
  2957. '/label',
  2958. '/div',
  2959. array('div' => array('class' => 'checkbox')),
  2960. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2961. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2962. 'second',
  2963. '/label',
  2964. '/div',
  2965. array('div' => array('class' => 'checkbox')),
  2966. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2967. array('label' => array('for' => 'ModelMultiField2')),
  2968. 'third',
  2969. '/label',
  2970. '/div',
  2971. );
  2972. $this->assertTags($result, $expected);
  2973. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2974. $expected = array(
  2975. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2976. array('div' => array('class' => 'checkbox')),
  2977. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/22')),
  2978. array('label' => array('for' => 'ModelMultiField1/22')),
  2979. 'half',
  2980. '/label',
  2981. '/div',
  2982. );
  2983. $this->assertTags($result, $expected);
  2984. }
  2985. /**
  2986. * testLabel method
  2987. *
  2988. * test label generation.
  2989. *
  2990. * @return void
  2991. */
  2992. public function testLabel() {
  2993. $this->Form->text('Person.name');
  2994. $result = $this->Form->label();
  2995. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2996. $this->Form->text('Person.name');
  2997. $result = $this->Form->label();
  2998. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2999. $result = $this->Form->label('Person.first_name');
  3000. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3001. $result = $this->Form->label('Person.first_name', 'Your first name');
  3002. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3003. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3004. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3005. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3006. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3007. $result = $this->Form->label('Person.first_name', '');
  3008. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3009. $result = $this->Form->label('Person.2.name', '');
  3010. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3011. }
  3012. /**
  3013. * testTextbox method
  3014. *
  3015. * test textbox element generation
  3016. *
  3017. * @return void
  3018. */
  3019. public function testTextbox() {
  3020. $result = $this->Form->text('Model.field');
  3021. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField')));
  3022. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3023. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField')));
  3024. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3025. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3026. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3027. $result = $this->Form->text('Model.text');
  3028. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3029. $Contact = ClassRegistry::getObject('Contact');
  3030. $Contact->validationErrors['text'] = array(true);
  3031. $this->Form->request->data['Contact']['text'] = 'test';
  3032. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3033. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3034. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3035. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3036. $expected = array(
  3037. 'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3038. );
  3039. $this->assertTags($result, $expected);
  3040. }
  3041. /**
  3042. * testDefaultValue method
  3043. *
  3044. * Test default value setting
  3045. *
  3046. * @return void
  3047. */
  3048. public function testDefaultValue() {
  3049. $this->Form->request->data['Model']['field'] = 'test';
  3050. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3051. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField')));
  3052. unset($this->Form->request->data['Model']['field']);
  3053. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3054. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField')));
  3055. }
  3056. /**
  3057. * testCheckboxDefaultValue method
  3058. *
  3059. * Test default value setting on checkbox() method
  3060. *
  3061. * @return void
  3062. */
  3063. public function testCheckboxDefaultValue() {
  3064. $this->Form->request->data['Model']['field'] = false;
  3065. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3066. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3067. unset($this->Form->request->data['Model']['field']);
  3068. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3069. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3070. $this->Form->request->data['Model']['field'] = true;
  3071. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3072. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3073. unset($this->Form->request->data['Model']['field']);
  3074. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3075. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3076. }
  3077. /**
  3078. * testError method
  3079. *
  3080. * Test field error generation
  3081. *
  3082. * @return void
  3083. */
  3084. public function testError() {
  3085. $Contact = ClassRegistry::getObject('Contact');
  3086. $Contact->validationErrors['field'] = array(1);
  3087. $result = $this->Form->error('Contact.field');
  3088. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3089. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3090. $this->assertEquals('Error in field Field', $result);
  3091. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3092. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3093. $this->assertEquals('This field contains invalid input', $result);
  3094. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3095. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3096. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3097. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3098. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3099. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3100. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3101. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3102. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3103. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3104. $this->assertEquals('<strong>Badness!</strong>', $result);
  3105. $Contact->validationErrors['field'] = array("email");
  3106. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3107. $expected = array(
  3108. 'div' => array('class' => 'field-error'),
  3109. 'No good!',
  3110. '/div'
  3111. );
  3112. $this->assertTags($result, $expected);
  3113. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3114. $result = $this->Form->error('Contact.field', array(
  3115. 'notEmpty' => 'Cannot be empty',
  3116. 'email' => 'No good!'
  3117. ));
  3118. $expected = array(
  3119. 'div' => array('class' => 'error-message'),
  3120. 'ul' => array(),
  3121. '<li', 'Cannot be empty', '/li',
  3122. '<li', 'No good!', '/li',
  3123. '<li', 'Something else', '/li',
  3124. '/ul',
  3125. '/div'
  3126. );
  3127. $this->assertTags($result, $expected);
  3128. // Testing error messages list options
  3129. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3130. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3131. $expected = array(
  3132. 'div' => array('class' => 'error-message'),
  3133. 'ol' => array(),
  3134. '<li', 'notEmpty', '/li',
  3135. '<li', 'email', '/li',
  3136. '/ol',
  3137. '/div'
  3138. );
  3139. $this->assertTags($result, $expected);
  3140. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3141. $expected = array(
  3142. 'div' => array('class' => 'error-message'),
  3143. 'ol' => array(),
  3144. '<li', 'notEmpty', '/li',
  3145. '<li', 'email', '/li',
  3146. '/ol',
  3147. '/div'
  3148. );
  3149. $this->assertTags($result, $expected);
  3150. $result = $this->Form->error('Contact.field', null, array(
  3151. 'listOptions' => array(
  3152. 'class' => 'ul-class',
  3153. 'itemOptions' => array(
  3154. 'class' => 'li-class'
  3155. )
  3156. )
  3157. ));
  3158. $expected = array(
  3159. 'div' => array('class' => 'error-message'),
  3160. 'ul' => array('class' => 'ul-class'),
  3161. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3162. array('li' => array('class' => 'li-class')), 'email', '/li',
  3163. '/ul',
  3164. '/div'
  3165. );
  3166. $this->assertTags($result, $expected);
  3167. }
  3168. /**
  3169. * test error options when using form->input();
  3170. *
  3171. * @return void
  3172. */
  3173. public function testInputErrorEscape() {
  3174. $this->Form->create('ValidateProfile');
  3175. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3176. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3177. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3178. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3179. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3180. $this->assertRegExp('/required<br>/', $result);
  3181. }
  3182. /**
  3183. * testPassword method
  3184. *
  3185. * Test password element generation
  3186. *
  3187. * @return void
  3188. */
  3189. public function testPassword() {
  3190. $Contact = ClassRegistry::getObject('Contact');
  3191. $result = $this->Form->password('Contact.field');
  3192. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField')));
  3193. $Contact->validationErrors['passwd'] = 1;
  3194. $this->Form->request->data['Contact']['passwd'] = 'test';
  3195. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3196. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3197. }
  3198. /**
  3199. * testRadio method
  3200. *
  3201. * Test radio element set generation
  3202. *
  3203. * @return void
  3204. */
  3205. public function testRadio() {
  3206. $result = $this->Form->radio('Model.field', array('option A'));
  3207. $expected = array(
  3208. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3209. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3210. 'label' => array('for' => 'ModelField0'),
  3211. 'option A',
  3212. '/label'
  3213. );
  3214. $this->assertTags($result, $expected);
  3215. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3216. $expected = array(
  3217. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3218. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField1/2')),
  3219. 'label' => array('for' => 'ModelField1/2'),
  3220. 'half',
  3221. '/label'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3225. $expected = array(
  3226. 'fieldset' => array(),
  3227. 'legend' => array(),
  3228. 'Field',
  3229. '/legend',
  3230. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3231. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3232. array('label' => array('for' => 'ModelField0')),
  3233. 'option A',
  3234. '/label',
  3235. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3236. array('label' => array('for' => 'ModelField1')),
  3237. 'option B',
  3238. '/label',
  3239. '/fieldset'
  3240. );
  3241. $this->assertTags($result, $expected);
  3242. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3243. $expected = array(
  3244. 'fieldset' => array(),
  3245. 'legend' => array(),
  3246. 'Field',
  3247. '/legend',
  3248. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3249. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3250. array('label' => array('for' => 'ModelField0')),
  3251. 'option A',
  3252. '/label',
  3253. 'br' => array(),
  3254. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3255. array('label' => array('for' => 'ModelField1')),
  3256. 'option B',
  3257. '/label',
  3258. '/fieldset'
  3259. );
  3260. $this->assertTags($result, $expected);
  3261. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3262. $expected = array(
  3263. 'fieldset' => array(),
  3264. 'legend' => array(),
  3265. 'Gender',
  3266. '/legend',
  3267. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3268. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3269. array('label' => array('for' => 'EmployeeGenderMale')),
  3270. 'Male',
  3271. '/label',
  3272. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3273. array('label' => array('for' => 'EmployeeGenderFemale')),
  3274. 'Female',
  3275. '/label',
  3276. '/fieldset',
  3277. );
  3278. $this->assertTags($result, $expected);
  3279. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3280. $expected = array(
  3281. 'fieldset' => array(),
  3282. 'legend' => array(),
  3283. 'Gender',
  3284. '/legend',
  3285. 'input' => array('type' => 'hidden', 'name' => 'Officer[gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3286. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3287. array('label' => array('for' => 'OfficerGenderMale')),
  3288. 'Male',
  3289. '/label',
  3290. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3291. array('label' => array('for' => 'OfficerGenderFemale')),
  3292. 'Female',
  3293. '/label',
  3294. '/fieldset',
  3295. );
  3296. $this->assertTags($result, $expected);
  3297. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3298. $expected = array(
  3299. 'input' => array('type' => 'hidden', 'name' => 'Contact[1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3300. array('input' => array('type' => 'radio', 'name' => 'Contact[1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0', 'required' => 'required')),
  3301. 'label' => array('for' => 'Contact1Imrequired0'),
  3302. 'option A',
  3303. '/label'
  3304. );
  3305. $this->assertTags($result, $expected);
  3306. $result = $this->Form->radio('Model.1.field', array('option A'));
  3307. $expected = array(
  3308. 'input' => array('type' => 'hidden', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3309. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3310. 'label' => array('for' => 'Model1Field0'),
  3311. 'option A',
  3312. '/label'
  3313. );
  3314. $this->assertTags($result, $expected);
  3315. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3316. $expected = array(
  3317. 'fieldset' => array(),
  3318. 'legend' => array(),
  3319. 'Field',
  3320. '/legend',
  3321. 'input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'ModelField_'),
  3322. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'ModelField0')),
  3323. array('label' => array('for' => 'ModelField0')),
  3324. 'option A',
  3325. '/label',
  3326. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'ModelField1')),
  3327. array('label' => array('for' => 'ModelField1')),
  3328. 'option B',
  3329. '/label',
  3330. '/fieldset'
  3331. );
  3332. $this->assertTags($result, $expected);
  3333. $result = $this->Form->radio(
  3334. 'Model.field',
  3335. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3336. );
  3337. $expected = array(
  3338. 'fieldset' => array(),
  3339. 'legend' => array(),
  3340. 'Field',
  3341. '/legend',
  3342. 'input' => array(
  3343. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3344. 'id' => 'ModelField_', 'value' => '',
  3345. ),
  3346. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3347. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3348. array('label' => array('for' => 'ModelFieldAB')),
  3349. 'first',
  3350. '/label',
  3351. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3352. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3353. array('label' => array('for' => 'ModelFieldAB1')),
  3354. 'second',
  3355. '/label',
  3356. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3357. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3358. array('label' => array('for' => 'ModelFieldAB2')),
  3359. 'third',
  3360. '/label',
  3361. '/fieldset'
  3362. );
  3363. $this->assertTags($result, $expected);
  3364. }
  3365. /**
  3366. * Test radio inputs with between as string or array. Also ensure
  3367. * that an array with less between elements works.
  3368. *
  3369. * @return void
  3370. */
  3371. public function testRadioBetween() {
  3372. $result = $this->Form->radio(
  3373. 'Model.field',
  3374. array('option A', 'option B'),
  3375. array('between' => 'I am between')
  3376. );
  3377. $expected = array(
  3378. 'fieldset' => array(),
  3379. 'legend' => array(),
  3380. 'Field',
  3381. '/legend',
  3382. 'I am between',
  3383. 'input' => array(
  3384. 'type' => 'hidden', 'name' => 'Model[field]',
  3385. 'value' => '', 'id' => 'ModelField_'
  3386. ),
  3387. array('input' => array(
  3388. 'type' => 'radio', 'name' => 'Model[field]',
  3389. 'value' => '0', 'id' => 'ModelField0'
  3390. )),
  3391. array('label' => array('for' => 'ModelField0')),
  3392. 'option A',
  3393. '/label',
  3394. array('input' => array(
  3395. 'type' => 'radio', 'name' => 'Model[field]',
  3396. 'value' => '1', 'id' => 'ModelField1'
  3397. )),
  3398. array('label' => array('for' => 'ModelField1')),
  3399. 'option B',
  3400. '/label',
  3401. '/fieldset'
  3402. );
  3403. $this->assertTags($result, $expected);
  3404. $result = $this->Form->radio(
  3405. 'Model.field',
  3406. array('option A', 'option B', 'option C'),
  3407. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3408. );
  3409. $expected = array(
  3410. 'fieldset' => array(),
  3411. 'legend' => array(),
  3412. 'Field',
  3413. '/legend',
  3414. 'input' => array(
  3415. 'type' => 'hidden', 'name' => 'Model[field]',
  3416. 'value' => '', 'id' => 'ModelField_'
  3417. ),
  3418. array('input' => array(
  3419. 'type' => 'radio', 'name' => 'Model[field]',
  3420. 'value' => '0', 'id' => 'ModelField0'
  3421. )),
  3422. array('label' => array('for' => 'ModelField0')),
  3423. 'option A',
  3424. '/label',
  3425. 'between A',
  3426. '--separator--',
  3427. array('input' => array(
  3428. 'type' => 'radio', 'name' => 'Model[field]',
  3429. 'value' => '1', 'id' => 'ModelField1'
  3430. )),
  3431. array('label' => array('for' => 'ModelField1')),
  3432. 'option B',
  3433. '/label',
  3434. 'between B',
  3435. '--separator--',
  3436. array('input' => array(
  3437. 'type' => 'radio', 'name' => 'Model[field]',
  3438. 'value' => '2', 'id' => 'ModelField2'
  3439. )),
  3440. array('label' => array('for' => 'ModelField2')),
  3441. 'option C',
  3442. '/label',
  3443. 'between C',
  3444. '/fieldset'
  3445. );
  3446. $this->assertTags($result, $expected);
  3447. $result = $this->Form->input('Model.field', array(
  3448. 'options' => array('1' => 'first', '2' => 'second'),
  3449. 'type' => 'radio',
  3450. 'before' => '--before--',
  3451. 'after' => '--after--',
  3452. 'separator' => '--separator--',
  3453. 'between' => array('--between first--', '--between second--')
  3454. ));
  3455. $expected = array(
  3456. 'div' => array('class' => 'input radio'),
  3457. '--before--',
  3458. 'fieldset' => array(),
  3459. 'legend' => array(),
  3460. 'Field',
  3461. '/legend',
  3462. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3463. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3464. array('label' => array('for' => 'ModelField1')),
  3465. 'first',
  3466. '/label',
  3467. '--between first--',
  3468. '--separator--',
  3469. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3470. array('label' => array('for' => 'ModelField2')),
  3471. 'second',
  3472. '/label',
  3473. '--between second--',
  3474. '/fieldset',
  3475. '--after--',
  3476. '/div'
  3477. );
  3478. $this->assertTags($result, $expected);
  3479. $result = $this->Form->input('Model.field', array(
  3480. 'options' => array('1' => 'first', '2' => 'second'),
  3481. 'type' => 'radio',
  3482. 'before' => '--before--',
  3483. 'after' => '--after--',
  3484. 'separator' => '--separator--',
  3485. 'between' => array('--between first--')
  3486. ));
  3487. $expected = array(
  3488. 'div' => array('class' => 'input radio'),
  3489. '--before--',
  3490. 'fieldset' => array(),
  3491. 'legend' => array(),
  3492. 'Field',
  3493. '/legend',
  3494. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3495. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3496. array('label' => array('for' => 'ModelField1')),
  3497. 'first',
  3498. '/label',
  3499. '--between first--',
  3500. '--separator--',
  3501. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3502. array('label' => array('for' => 'ModelField2')),
  3503. 'second',
  3504. '/label',
  3505. '/fieldset',
  3506. '--after--',
  3507. '/div'
  3508. );
  3509. $this->assertTags($result, $expected);
  3510. }
  3511. /**
  3512. * Test that radios with a 0 value are selected under the correct conditions.
  3513. * Also ensure that values that are booleanish are handled correctly.
  3514. *
  3515. * @return void
  3516. */
  3517. public function testRadioOptionWithBooleanishValues() {
  3518. $expected = array(
  3519. 'fieldset' => array(),
  3520. 'legend' => array(),
  3521. 'Field',
  3522. '/legend',
  3523. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3524. array('label' => array('for' => 'ModelField1')),
  3525. 'Yes',
  3526. '/label',
  3527. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3528. array('label' => array('for' => 'ModelField0')),
  3529. 'No',
  3530. '/label',
  3531. '/fieldset'
  3532. );
  3533. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3534. $this->assertTags($result, $expected);
  3535. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3536. $this->assertTags($result, $expected);
  3537. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3538. $this->assertTags($result, $expected);
  3539. $expected = array(
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3545. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3546. array('label' => array('for' => 'ModelField1')),
  3547. 'Yes',
  3548. '/label',
  3549. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3550. array('label' => array('for' => 'ModelField0')),
  3551. 'No',
  3552. '/label',
  3553. '/fieldset'
  3554. );
  3555. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3556. $this->assertTags($result, $expected);
  3557. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3558. $this->assertTags($result, $expected);
  3559. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3560. $this->assertTags($result, $expected);
  3561. $expected = array(
  3562. 'fieldset' => array(),
  3563. 'legend' => array(),
  3564. 'Field',
  3565. '/legend',
  3566. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3567. array('label' => array('for' => 'ModelField1')),
  3568. 'Yes',
  3569. '/label',
  3570. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3571. array('label' => array('for' => 'ModelField0')),
  3572. 'No',
  3573. '/label',
  3574. '/fieldset'
  3575. );
  3576. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3577. $this->assertTags($result, $expected);
  3578. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3579. $this->assertTags($result, $expected);
  3580. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3581. $this->assertTags($result, $expected);
  3582. }
  3583. /**
  3584. * test disabled radio options
  3585. *
  3586. * @return void
  3587. */
  3588. public function testRadioDisabled() {
  3589. $result = $this->Form->radio(
  3590. 'Model.field',
  3591. array('option A', 'option B'),
  3592. array('disabled' => array(0), 'value' => '0')
  3593. );
  3594. $expected = array(
  3595. 'fieldset' => array(),
  3596. 'legend' => array(),
  3597. 'Field',
  3598. '/legend',
  3599. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3600. array('label' => array('for' => 'ModelField0')),
  3601. 'option A',
  3602. '/label',
  3603. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3604. array('label' => array('for' => 'ModelField1')),
  3605. 'option B',
  3606. '/label',
  3607. '/fieldset'
  3608. );
  3609. $this->assertTags($result, $expected);
  3610. $result = $this->Form->radio(
  3611. 'Model.field',
  3612. array('option A', 'option B'),
  3613. array('disabled' => true, 'value' => '0')
  3614. );
  3615. $expected = array(
  3616. 'fieldset' => array(),
  3617. 'legend' => array(),
  3618. 'Field',
  3619. '/legend',
  3620. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3621. array('label' => array('for' => 'ModelField0')),
  3622. 'option A',
  3623. '/label',
  3624. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3625. array('label' => array('for' => 'ModelField1')),
  3626. 'option B',
  3627. '/label',
  3628. '/fieldset'
  3629. );
  3630. $this->assertTags($result, $expected);
  3631. $result = $this->Form->radio(
  3632. 'Model.field',
  3633. array('option A', 'option B'),
  3634. array('disabled' => 'disabled', 'value' => '0')
  3635. );
  3636. $expected = array(
  3637. 'fieldset' => array(),
  3638. 'legend' => array(),
  3639. 'Field',
  3640. '/legend',
  3641. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3642. array('label' => array('for' => 'ModelField0')),
  3643. 'option A',
  3644. '/label',
  3645. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3646. array('label' => array('for' => 'ModelField1')),
  3647. 'option B',
  3648. '/label',
  3649. '/fieldset'
  3650. );
  3651. $this->assertTags($result, $expected);
  3652. $result = $this->Form->input('Model.field', array(
  3653. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3654. 'type' => 'radio',
  3655. 'disabled' => array(2, '3x'),
  3656. ));
  3657. $expected = array(
  3658. 'div' => array('class' => 'input radio'),
  3659. 'fieldset' => array(),
  3660. 'legend' => array(),
  3661. 'Field',
  3662. '/legend',
  3663. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3664. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3665. array('label' => array('for' => 'ModelField1')),
  3666. 'first',
  3667. '/label',
  3668. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3669. array('label' => array('for' => 'ModelField2')),
  3670. 'second',
  3671. '/label',
  3672. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3673. array('label' => array('for' => 'ModelField2x')),
  3674. '2x',
  3675. '/label',
  3676. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '3', 'id' => 'ModelField3')),
  3677. array('label' => array('for' => 'ModelField3')),
  3678. 'third',
  3679. '/label',
  3680. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3681. array('label' => array('for' => 'ModelField3x')),
  3682. '3x',
  3683. '/label',
  3684. '/fieldset',
  3685. '/div'
  3686. );
  3687. $this->assertTags($result, $expected);
  3688. $result = $this->Form->input('Model.field', array(
  3689. 'type' => 'radio',
  3690. 'options' => array(
  3691. 1 => 'A',
  3692. 2 => 'B',
  3693. 3 => 'C'
  3694. ),
  3695. 'disabled' => array(1)
  3696. ));
  3697. $expected = array(
  3698. 'div' => array('class' => 'input radio'),
  3699. 'fieldset' => array(),
  3700. 'legend' => array(),
  3701. 'Field',
  3702. '/legend',
  3703. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3704. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3705. array('label' => array('for' => 'ModelField1')),
  3706. 'A',
  3707. '/label',
  3708. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3709. array('label' => array('for' => 'ModelField2')),
  3710. 'B',
  3711. '/label',
  3712. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3713. array('label' => array('for' => 'ModelField3')),
  3714. 'C',
  3715. '/label',
  3716. '/fieldset',
  3717. '/div'
  3718. );
  3719. $this->assertTags($result, $expected);
  3720. }
  3721. /**
  3722. * test disabling the hidden input for radio buttons
  3723. *
  3724. * @return void
  3725. */
  3726. public function testRadioHiddenInputDisabling() {
  3727. $result = $this->Form->input('Model.1.field', array(
  3728. 'type' => 'radio',
  3729. 'options' => array('option A'),
  3730. 'hiddenField' => false
  3731. )
  3732. );
  3733. $expected = array(
  3734. 'div' => array('class' => 'input radio'),
  3735. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3736. 'label' => array('for' => 'Model1Field0'),
  3737. 'option A',
  3738. '/label',
  3739. '/div'
  3740. );
  3741. $this->assertTags($result, $expected);
  3742. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3743. $expected = array(
  3744. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3745. 'label' => array('for' => 'Model1Field0'),
  3746. 'option A',
  3747. '/label'
  3748. );
  3749. $this->assertTags($result, $expected);
  3750. }
  3751. /**
  3752. * test adding an empty option for radio buttons
  3753. *
  3754. * @return void
  3755. */
  3756. public function testRadioAddEmptyOption() {
  3757. $result = $this->Form->input('Model.1.field', array(
  3758. 'type' => 'radio',
  3759. 'options' => array('option A'),
  3760. 'empty' => true,
  3761. 'hiddenField' => false
  3762. ));
  3763. $expected = array(
  3764. 'div' => array('class' => 'input radio'),
  3765. 'fieldset' => array(),
  3766. 'legend' => array(),
  3767. 'Field',
  3768. '/legend',
  3769. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3770. array('label' => array('for' => 'Model1Field')),
  3771. __('empty'),
  3772. '/label',
  3773. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3774. array('label' => array('for' => 'Model1Field0')),
  3775. 'option A',
  3776. '/label',
  3777. '/fieldset',
  3778. '/div'
  3779. );
  3780. $this->assertTags($result, $expected);
  3781. $result = $this->Form->input('Model.1.field', array(
  3782. 'type' => 'radio',
  3783. 'options' => array('option A'),
  3784. 'empty' => 'CustomEmptyLabel',
  3785. 'hiddenField' => false
  3786. ));
  3787. $expected = array(
  3788. 'div' => array('class' => 'input radio'),
  3789. 'fieldset' => array(),
  3790. 'legend' => array(),
  3791. 'Field',
  3792. '/legend',
  3793. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3794. array('label' => array('for' => 'Model1Field')),
  3795. 'CustomEmptyLabel',
  3796. '/label',
  3797. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3798. array('label' => array('for' => 'Model1Field0')),
  3799. 'option A',
  3800. '/label',
  3801. '/fieldset',
  3802. '/div'
  3803. );
  3804. $this->assertTags($result, $expected);
  3805. $result = $this->Form->input('Model.1.field', array(
  3806. 'type' => 'radio',
  3807. 'options' => array('option A'),
  3808. 'empty' => false,
  3809. 'hiddenField' => false
  3810. ));
  3811. $this->assertTextNotContains('"Model1Field"', $result);
  3812. }
  3813. /**
  3814. * Test that radio() accepts an array for label
  3815. *
  3816. * @return void
  3817. */
  3818. public function testRadioLabelArray() {
  3819. $result = $this->Form->input('Model.field', array(
  3820. 'type' => 'radio',
  3821. 'legend' => false,
  3822. 'label' => array(
  3823. 'class' => 'checkbox float-left',
  3824. ),
  3825. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3826. ));
  3827. $this->assertTextContains(
  3828. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3829. $result
  3830. );
  3831. }
  3832. /**
  3833. * Test that label id's match the input element id's when radio is called after create().
  3834. *
  3835. * @return void
  3836. */
  3837. public function testRadioWithCreate() {
  3838. $this->Form->create('Model');
  3839. $result = $this->Form->radio('recipient',
  3840. array('1' => '1', '2' => '2', '3' => '3'),
  3841. array('legend' => false, 'value' => '1')
  3842. );
  3843. $this->assertTextNotContains(
  3844. '<label for="ModelModelRecipient1">1</label>',
  3845. $result
  3846. );
  3847. $this->assertTextContains(
  3848. '<label for="ModelRecipient1">1</label>',
  3849. $result
  3850. );
  3851. }
  3852. /**
  3853. * testDomIdSuffix method
  3854. *
  3855. * @return void
  3856. */
  3857. public function testDomIdSuffix() {
  3858. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3859. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3860. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3861. $this->assertEquals('AbcX=FooY=Bar', $result);
  3862. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3863. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3864. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3865. $this->assertEquals('AbcXFooYBar', $result);
  3866. }
  3867. /**
  3868. * testDomIdSuffixCollisionResolvement()
  3869. *
  3870. * @return void
  3871. */
  3872. public function testDomIdSuffixCollisionResolvement() {
  3873. $result = $this->Form->domIdSuffix('a>b');
  3874. $this->assertEquals('AB', $result);
  3875. $result = $this->Form->domIdSuffix('a<b');
  3876. $this->assertEquals('AB1', $result);
  3877. $result = $this->Form->domIdSuffix('a\'b');
  3878. $this->assertEquals('AB2', $result);
  3879. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3880. $this->assertEquals('1StringWith1-dollar', $result);
  3881. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3882. $this->assertEquals('1StringWith1-dollar1', $result);
  3883. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3884. $this->assertEquals('1StringWith1-dollar2', $result);
  3885. }
  3886. /**
  3887. * testSelect method
  3888. *
  3889. * Test select element generation.
  3890. *
  3891. * @return void
  3892. */
  3893. public function testSelect() {
  3894. $result = $this->Form->select('Model.field', array());
  3895. $expected = array(
  3896. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3897. array('option' => array('value' => '')),
  3898. '/option',
  3899. '/select'
  3900. );
  3901. $this->assertTags($result, $expected);
  3902. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3903. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3904. $expected = array(
  3905. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3906. array('option' => array('value' => '')),
  3907. '/option',
  3908. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3909. 'good',
  3910. '/option',
  3911. array('option' => array('value' => 'other')),
  3912. 'bad',
  3913. '/option',
  3914. '/select'
  3915. );
  3916. $this->assertTags($result, $expected);
  3917. $this->Form->request->data = array();
  3918. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3919. $expected = array(
  3920. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3921. array('option' => array('value' => '')),
  3922. '/option',
  3923. array('option' => array('value' => 'value')),
  3924. 'good',
  3925. '/option',
  3926. array('option' => array('value' => 'other')),
  3927. 'bad',
  3928. '/option',
  3929. '/select'
  3930. );
  3931. $this->assertTags($result, $expected);
  3932. $result = $this->Form->select(
  3933. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3934. array('empty' => false)
  3935. );
  3936. $expected = array(
  3937. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3938. array('option' => array('value' => 'first')),
  3939. 'first &quot;html&quot; &lt;chars&gt;',
  3940. '/option',
  3941. array('option' => array('value' => 'second')),
  3942. 'value',
  3943. '/option',
  3944. '/select'
  3945. );
  3946. $this->assertTags($result, $expected);
  3947. $result = $this->Form->select(
  3948. 'Model.field',
  3949. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3950. array('escape' => false, 'empty' => false)
  3951. );
  3952. $expected = array(
  3953. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3954. array('option' => array('value' => 'first')),
  3955. 'first "html" <chars>',
  3956. '/option',
  3957. array('option' => array('value' => 'second')),
  3958. 'value',
  3959. '/option',
  3960. '/select'
  3961. );
  3962. $this->assertTags($result, $expected);
  3963. $options = array(
  3964. array('value' => 'first', 'name' => 'First'),
  3965. array('value' => 'first', 'name' => 'Another First'),
  3966. );
  3967. $result = $this->Form->select(
  3968. 'Model.field',
  3969. $options,
  3970. array('escape' => false, 'empty' => false)
  3971. );
  3972. $expected = array(
  3973. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3974. array('option' => array('value' => 'first')),
  3975. 'First',
  3976. '/option',
  3977. array('option' => array('value' => 'first')),
  3978. 'Another First',
  3979. '/option',
  3980. '/select'
  3981. );
  3982. $this->assertTags($result, $expected);
  3983. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3984. $result = $this->Form->select(
  3985. 'Model.contact_id',
  3986. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3987. array('escape' => false, 'empty' => 'pick something')
  3988. );
  3989. $expected = array(
  3990. 'select' => array('name' => 'Model[contact_id]', 'id' => 'ModelContactId'),
  3991. array('option' => array('value' => '')), 'pick something', '/option',
  3992. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3993. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3994. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3995. '/select'
  3996. );
  3997. $this->assertTags($result, $expected);
  3998. $this->Form->request->data['Model']['field'] = 0;
  3999. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4000. $expected = array(
  4001. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4002. array('option' => array('value' => '')), '/option',
  4003. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4004. array('option' => array('value' => '1')), 'Yes', '/option',
  4005. '/select'
  4006. );
  4007. $this->assertTags($result, $expected);
  4008. $this->Form->request->data['Model']['field'] = 50;
  4009. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4010. $expected = array(
  4011. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4012. array('option' => array('value' => '')), '/option',
  4013. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4014. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4015. '/select'
  4016. );
  4017. $this->assertTags($result, $expected);
  4018. $result = $this->Form->select('Contact.required_one', array('option A'));
  4019. $expected = array(
  4020. 'select' => array(
  4021. 'name' => 'data[Contact][required_one]',
  4022. 'id' => 'ContactRequiredOne',
  4023. 'required' => 'required'
  4024. ),
  4025. array('option' => array('value' => '')), '/option',
  4026. array('option' => array('value' => '0')), 'option A', '/option',
  4027. '/select'
  4028. );
  4029. $this->assertTags($result, $expected);
  4030. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4031. $expected = array(
  4032. 'select' => array(
  4033. 'name' => 'data[Contact][required_one]',
  4034. 'id' => 'ContactRequiredOne',
  4035. 'disabled' => 'disabled'
  4036. ),
  4037. array('option' => array('value' => '')), '/option',
  4038. array('option' => array('value' => '0')), 'option A', '/option',
  4039. '/select'
  4040. );
  4041. $this->assertTags($result, $expected);
  4042. }
  4043. /**
  4044. * test that select() with optiongroups listens to the escape param.
  4045. *
  4046. * @return void
  4047. */
  4048. public function testSelectOptionGroupEscaping() {
  4049. $options = array(
  4050. '>< Key' => array(
  4051. 1 => 'One',
  4052. 2 => 'Two'
  4053. )
  4054. );
  4055. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4056. $expected = array(
  4057. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4058. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4059. array('option' => array('value' => '1')), 'One', '/option',
  4060. array('option' => array('value' => '2')), 'Two', '/option',
  4061. '/optgroup',
  4062. '/select'
  4063. );
  4064. $this->assertTags($result, $expected);
  4065. $options = array(
  4066. '>< Key' => array(
  4067. 1 => 'One',
  4068. 2 => 'Two'
  4069. )
  4070. );
  4071. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4072. $expected = array(
  4073. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4074. 'optgroup' => array('label' => '>< Key'),
  4075. array('option' => array('value' => '1')), 'One', '/option',
  4076. array('option' => array('value' => '2')), 'Two', '/option',
  4077. '/optgroup',
  4078. '/select'
  4079. );
  4080. $this->assertTags($result, $expected);
  4081. }
  4082. /**
  4083. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4084. *
  4085. * @return void
  4086. */
  4087. public function testSelectWithNullAttributes() {
  4088. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4089. $expected = array(
  4090. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4091. array('option' => array('value' => '0')),
  4092. 'first',
  4093. '/option',
  4094. array('option' => array('value' => '1')),
  4095. 'second',
  4096. '/option',
  4097. '/select'
  4098. );
  4099. $this->assertTags($result, $expected);
  4100. }
  4101. /**
  4102. * testNestedSelect method
  4103. *
  4104. * test select element generation with optgroups
  4105. *
  4106. * @return void
  4107. */
  4108. public function testNestedSelect() {
  4109. $result = $this->Form->select(
  4110. 'Model.field',
  4111. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4112. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4113. )), array('empty' => false)
  4114. );
  4115. $expected = array(
  4116. 'select' => array('name' => 'Model[field]',
  4117. 'id' => 'ModelField'),
  4118. array('option' => array('value' => 1)),
  4119. 'One',
  4120. '/option',
  4121. array('option' => array('value' => 2)),
  4122. 'Two',
  4123. '/option',
  4124. array('optgroup' => array('label' => 'Three')),
  4125. array('option' => array('value' => 4)),
  4126. 'Four',
  4127. '/option',
  4128. array('option' => array('value' => 5)),
  4129. 'Five',
  4130. '/option',
  4131. '/optgroup',
  4132. '/select'
  4133. );
  4134. $this->assertTags($result, $expected);
  4135. $result = $this->Form->select(
  4136. 'Model.field',
  4137. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4138. array('showParents' => true, 'empty' => false)
  4139. );
  4140. $expected = array(
  4141. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  4142. array('option' => array('value' => 1)),
  4143. 'One',
  4144. '/option',
  4145. array('option' => array('value' => 2)),
  4146. 'Two',
  4147. '/option',
  4148. array('optgroup' => array('label' => 'Three')),
  4149. array('option' => array('value' => 3)),
  4150. 'Three',
  4151. '/option',
  4152. array('option' => array('value' => 4)),
  4153. 'Four',
  4154. '/option',
  4155. '/optgroup',
  4156. '/select'
  4157. );
  4158. $this->assertTags($result, $expected);
  4159. }
  4160. /**
  4161. * testSelectMultiple method
  4162. *
  4163. * test generation of multiple select elements
  4164. *
  4165. * @return void
  4166. */
  4167. public function testSelectMultiple() {
  4168. $options = array('first', 'second', 'third');
  4169. $result = $this->Form->select(
  4170. 'Model.multi_field', $options, array('multiple' => true)
  4171. );
  4172. $expected = array(
  4173. 'input' => array(
  4174. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4175. ),
  4176. 'select' => array(
  4177. 'name' => 'Model[multi_field][]',
  4178. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4179. ),
  4180. array('option' => array('value' => '0')),
  4181. 'first',
  4182. '/option',
  4183. array('option' => array('value' => '1')),
  4184. 'second',
  4185. '/option',
  4186. array('option' => array('value' => '2')),
  4187. 'third',
  4188. '/option',
  4189. '/select'
  4190. );
  4191. $this->assertTags($result, $expected);
  4192. $result = $this->Form->select(
  4193. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4194. );
  4195. $expected = array(
  4196. 'input' => array(
  4197. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4198. ),
  4199. 'select' => array(
  4200. 'name' => 'Model[multi_field][]',
  4201. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4202. ),
  4203. array('option' => array('value' => '0')),
  4204. 'first',
  4205. '/option',
  4206. array('option' => array('value' => '1')),
  4207. 'second',
  4208. '/option',
  4209. array('option' => array('value' => '2')),
  4210. 'third',
  4211. '/option',
  4212. '/select'
  4213. );
  4214. $this->assertTags($result, $expected);
  4215. $result = $this->Form->select(
  4216. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4217. );
  4218. $expected = array(
  4219. 'input' => array(
  4220. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4221. ),
  4222. 'select' => array(
  4223. 'name' => 'Model[multi_field][]', 'id' => 'ModelMultiField',
  4224. 'multiple' => 'multiple'
  4225. ),
  4226. array('option' => array('value' => '0', 'selected' => 'selected')),
  4227. 'first',
  4228. '/option',
  4229. array('option' => array('value' => '1', 'selected' => 'selected')),
  4230. 'second',
  4231. '/option',
  4232. array('option' => array('value' => '2')),
  4233. 'third',
  4234. '/option',
  4235. '/select'
  4236. );
  4237. $this->assertTags($result, $expected);
  4238. $result = $this->Form->select(
  4239. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4240. );
  4241. $expected = array(
  4242. 'select' => array(
  4243. 'name' => 'Model[multi_field]', 'id' => 'ModelMultiField'
  4244. ),
  4245. array('option' => array('value' => '0', 'selected' => 'selected')),
  4246. 'first',
  4247. '/option',
  4248. array('option' => array('value' => '1', 'selected' => 'selected')),
  4249. 'second',
  4250. '/option',
  4251. array('option' => array('value' => '2')),
  4252. 'third',
  4253. '/option',
  4254. '/select'
  4255. );
  4256. $this->assertTags($result, $expected);
  4257. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4258. $selected = array('2', '3x');
  4259. $result = $this->Form->select(
  4260. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4261. );
  4262. $expected = array(
  4263. 'input' => array(
  4264. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4265. ),
  4266. 'select' => array(
  4267. 'name' => 'Model[multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4268. ),
  4269. array('option' => array('value' => '1')),
  4270. 'One',
  4271. '/option',
  4272. array('option' => array('value' => '2', 'selected' => 'selected')),
  4273. 'Two',
  4274. '/option',
  4275. array('option' => array('value' => '3')),
  4276. 'Three',
  4277. '/option',
  4278. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4279. 'Stringy',
  4280. '/option',
  4281. '/select'
  4282. );
  4283. $this->assertTags($result, $expected);
  4284. $result = $this->Form->select('Contact.required_one', array(
  4285. '1' => 'option A',
  4286. '2' => 'option B'
  4287. ), array('multiple' => true));
  4288. $expected = array(
  4289. 'input' => array(
  4290. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4291. ),
  4292. 'select' => array(
  4293. 'name' => 'data[Contact][required_one][]',
  4294. 'id' => 'ContactRequiredOne',
  4295. 'required' => 'required',
  4296. 'multiple' => 'multiple'
  4297. ),
  4298. array('option' => array('value' => '1')), 'option A', '/option',
  4299. array('option' => array('value' => '2')), 'option B', '/option',
  4300. '/select'
  4301. );
  4302. $this->assertTags($result, $expected);
  4303. $result = $this->Form->select(
  4304. 'Model.multi_field',
  4305. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4306. array('multiple' => true)
  4307. );
  4308. $expected = array(
  4309. 'input' => array(
  4310. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4311. 'id' => 'ModelMultiField_'
  4312. ),
  4313. array('select' => array('name' => 'data[Model][multi_field][]',
  4314. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4315. )),
  4316. array('option' => array('value' => 'a&gt;b')),
  4317. 'first',
  4318. '/option',
  4319. array('option' => array('value' => 'a&lt;b')),
  4320. 'second',
  4321. '/option',
  4322. array('option' => array(
  4323. 'value' => 'a&quot;b'
  4324. )),
  4325. 'third',
  4326. '/option',
  4327. '/select'
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. }
  4331. /**
  4332. * Test generating multiple select with disabled elements.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testSelectMultipleWithDisabledElements() {
  4337. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4338. $disabled = array(2, 3);
  4339. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4340. $expected = array(
  4341. 'input' => array(
  4342. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4343. ),
  4344. 'select' => array(
  4345. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4346. ),
  4347. array('option' => array('value' => '1')),
  4348. 'One',
  4349. '/option',
  4350. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4351. 'Two',
  4352. '/option',
  4353. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4354. 'Three',
  4355. '/option',
  4356. array('option' => array('value' => '3x')),
  4357. 'Stringy',
  4358. '/option',
  4359. '/select'
  4360. );
  4361. $this->assertTags($result, $expected);
  4362. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4363. $disabled = array('2', '3x');
  4364. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4365. $expected = array(
  4366. array('div' => array('class' => 'input select')),
  4367. array('label' => array('for' => 'ContactMultiple')),
  4368. 'Multiple',
  4369. '/label',
  4370. 'input' => array(
  4371. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4372. ),
  4373. 'select' => array(
  4374. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4375. ),
  4376. array('option' => array('value' => '1')),
  4377. 'One',
  4378. '/option',
  4379. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4380. 'Two',
  4381. '/option',
  4382. array('option' => array('value' => '3')),
  4383. 'Three',
  4384. '/option',
  4385. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4386. 'Stringy',
  4387. '/option',
  4388. '/select',
  4389. '/div'
  4390. );
  4391. $this->assertTags($result, $expected);
  4392. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4393. $disabled = true;
  4394. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4395. $expected = array(
  4396. array('div' => array('class' => 'input select')),
  4397. array('label' => array('for' => 'ContactMultiple')),
  4398. 'Multiple',
  4399. '/label',
  4400. 'input' => array(
  4401. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4402. ),
  4403. 'select' => array(
  4404. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4405. ),
  4406. array('option' => array('value' => '1')),
  4407. 'One',
  4408. '/option',
  4409. array('option' => array('value' => '2')),
  4410. 'Two',
  4411. '/option',
  4412. array('option' => array('value' => '3')),
  4413. 'Three',
  4414. '/option',
  4415. array('option' => array('value' => '3x')),
  4416. 'Stringy',
  4417. '/option',
  4418. '/select',
  4419. '/div'
  4420. );
  4421. $this->assertTags($result, $expected);
  4422. }
  4423. /**
  4424. * Test generating select with disabled elements.
  4425. *
  4426. * @return void
  4427. */
  4428. public function testSelectWithDisabledElements() {
  4429. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4430. $disabled = array(2, 3);
  4431. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4432. $expected = array(
  4433. 'select' => array(
  4434. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4435. ),
  4436. array('option' => array('value' => '')),
  4437. '/option',
  4438. array('option' => array('value' => '1')),
  4439. 'One',
  4440. '/option',
  4441. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4442. 'Two',
  4443. '/option',
  4444. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4445. 'Three',
  4446. '/option',
  4447. array('option' => array('value' => '3x')),
  4448. 'Stringy',
  4449. '/option',
  4450. '/select'
  4451. );
  4452. $this->assertTags($result, $expected);
  4453. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4454. $disabled = array('2', '3x');
  4455. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4456. $expected = array(
  4457. array('div' => array('class' => 'input select')),
  4458. array('label' => array('for' => 'ModelField')),
  4459. 'Field',
  4460. '/label',
  4461. 'select' => array(
  4462. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4463. ),
  4464. array('option' => array('value' => '1')),
  4465. 'One',
  4466. '/option',
  4467. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4468. 'Two',
  4469. '/option',
  4470. array('option' => array('value' => '3')),
  4471. 'Three',
  4472. '/option',
  4473. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4474. 'Stringy',
  4475. '/option',
  4476. '/select',
  4477. '/div'
  4478. );
  4479. $this->assertTags($result, $expected);
  4480. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4481. $disabled = true;
  4482. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4483. $expected = array(
  4484. array('div' => array('class' => 'input select')),
  4485. array('label' => array('for' => 'ModelField')),
  4486. 'Field',
  4487. '/label',
  4488. 'select' => array(
  4489. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4490. ),
  4491. array('option' => array('value' => '1')),
  4492. 'One',
  4493. '/option',
  4494. array('option' => array('value' => '2')),
  4495. 'Two',
  4496. '/option',
  4497. array('option' => array('value' => '3')),
  4498. 'Three',
  4499. '/option',
  4500. array('option' => array('value' => '3x')),
  4501. 'Stringy',
  4502. '/option',
  4503. '/select',
  4504. '/div'
  4505. );
  4506. $this->assertTags($result, $expected);
  4507. }
  4508. /**
  4509. * test generation of habtm select boxes.
  4510. *
  4511. * @return void
  4512. */
  4513. public function testHabtmSelectBox() {
  4514. $this->View->viewVars['contactTags'] = array(
  4515. 1 => 'blue',
  4516. 2 => 'red',
  4517. 3 => 'green'
  4518. );
  4519. $this->Form->request->data = array(
  4520. 'Contact' => array(),
  4521. 'ContactTag' => array(
  4522. array(
  4523. 'id' => '1',
  4524. 'name' => 'blue'
  4525. ),
  4526. array(
  4527. 'id' => 3,
  4528. 'name' => 'green'
  4529. )
  4530. )
  4531. );
  4532. $this->Form->create('Contact');
  4533. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4534. $expected = array(
  4535. 'input' => array(
  4536. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4537. ),
  4538. 'select' => array(
  4539. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4540. 'multiple' => 'multiple'
  4541. ),
  4542. array('option' => array('value' => '1', 'selected' => 'selected')),
  4543. 'blue',
  4544. '/option',
  4545. array('option' => array('value' => '2')),
  4546. 'red',
  4547. '/option',
  4548. array('option' => array('value' => '3', 'selected' => 'selected')),
  4549. 'green',
  4550. '/option',
  4551. '/select'
  4552. );
  4553. $this->assertTags($result, $expected);
  4554. // make sure only 50 is selected, and not 50f5c0cf
  4555. $this->View->viewVars['contactTags'] = array(
  4556. '1' => 'blue',
  4557. '50f5c0cf' => 'red',
  4558. '50' => 'green'
  4559. );
  4560. $this->Form->request->data = array(
  4561. 'Contact' => array(),
  4562. 'ContactTag' => array(
  4563. array(
  4564. 'id' => 1,
  4565. 'name' => 'blue'
  4566. ),
  4567. array(
  4568. 'id' => 50,
  4569. 'name' => 'green'
  4570. )
  4571. )
  4572. );
  4573. $this->Form->create('Contact');
  4574. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4575. $expected = array(
  4576. 'input' => array(
  4577. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4578. ),
  4579. 'select' => array(
  4580. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4581. 'multiple' => 'multiple'
  4582. ),
  4583. array('option' => array('value' => '1', 'selected' => 'selected')),
  4584. 'blue',
  4585. '/option',
  4586. array('option' => array('value' => '50f5c0cf')),
  4587. 'red',
  4588. '/option',
  4589. array('option' => array('value' => '50', 'selected' => 'selected')),
  4590. 'green',
  4591. '/option',
  4592. '/select'
  4593. );
  4594. $this->assertTags($result, $expected);
  4595. }
  4596. /**
  4597. * test generation of multi select elements in checkbox format
  4598. *
  4599. * @return void
  4600. */
  4601. public function testSelectMultipleCheckboxes() {
  4602. $result = $this->Form->select(
  4603. 'Model.multi_field',
  4604. array('first', 'second', 'third'),
  4605. array('multiple' => 'checkbox')
  4606. );
  4607. $expected = array(
  4608. 'input' => array(
  4609. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4610. ),
  4611. array('div' => array('class' => 'checkbox')),
  4612. array('input' => array(
  4613. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4614. 'value' => '0', 'id' => 'ModelMultiField0'
  4615. )),
  4616. array('label' => array('for' => 'ModelMultiField0')),
  4617. 'first',
  4618. '/label',
  4619. '/div',
  4620. array('div' => array('class' => 'checkbox')),
  4621. array('input' => array(
  4622. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4623. 'value' => '1', 'id' => 'ModelMultiField1'
  4624. )),
  4625. array('label' => array('for' => 'ModelMultiField1')),
  4626. 'second',
  4627. '/label',
  4628. '/div',
  4629. array('div' => array('class' => 'checkbox')),
  4630. array('input' => array(
  4631. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4632. 'value' => '2', 'id' => 'ModelMultiField2'
  4633. )),
  4634. array('label' => array('for' => 'ModelMultiField2')),
  4635. 'third',
  4636. '/label',
  4637. '/div'
  4638. );
  4639. $this->assertTags($result, $expected);
  4640. $result = $this->Form->select(
  4641. 'Model.multi_field',
  4642. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4643. array('multiple' => 'checkbox')
  4644. );
  4645. $expected = array(
  4646. 'input' => array(
  4647. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4648. ),
  4649. array('div' => array('class' => 'checkbox')),
  4650. array('input' => array(
  4651. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4652. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4653. )),
  4654. array('label' => array('for' => 'ModelMultiFieldA')),
  4655. 'first',
  4656. '/label',
  4657. '/div',
  4658. array('div' => array('class' => 'checkbox')),
  4659. array('input' => array(
  4660. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4661. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4662. )),
  4663. array('label' => array('for' => 'ModelMultiFieldB')),
  4664. 'second',
  4665. '/label',
  4666. '/div',
  4667. array('div' => array('class' => 'checkbox')),
  4668. array('input' => array(
  4669. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4670. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4671. )),
  4672. array('label' => array('for' => 'ModelMultiFieldC')),
  4673. 'third',
  4674. '/label',
  4675. '/div'
  4676. );
  4677. $this->assertTags($result, $expected);
  4678. $result = $this->Form->select(
  4679. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4680. );
  4681. $expected = array(
  4682. 'input' => array(
  4683. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4684. ),
  4685. array('div' => array('class' => 'checkbox')),
  4686. array('input' => array(
  4687. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4688. 'value' => '1', 'id' => 'ModelMultiField1'
  4689. )),
  4690. array('label' => array('for' => 'ModelMultiField1')),
  4691. 'first',
  4692. '/label',
  4693. '/div'
  4694. );
  4695. $this->assertTags($result, $expected);
  4696. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4697. $result = $this->Form->select(
  4698. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4699. );
  4700. $expected = array(
  4701. 'input' => array(
  4702. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4703. ),
  4704. array('div' => array('class' => 'checkbox')),
  4705. array('input' => array(
  4706. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4707. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4708. )),
  4709. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4710. 'first',
  4711. '/label',
  4712. '/div',
  4713. array('div' => array('class' => 'checkbox')),
  4714. array('input' => array(
  4715. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4716. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4717. )),
  4718. array('label' => array('for' => 'ModelTagsArray')),
  4719. 'Array',
  4720. '/label',
  4721. '/div'
  4722. );
  4723. $this->assertTags($result, $expected);
  4724. $result = $this->Form->select(
  4725. 'Model.multi_field',
  4726. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4727. array('multiple' => 'checkbox')
  4728. );
  4729. $expected = array(
  4730. 'input' => array(
  4731. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4732. ),
  4733. array('div' => array('class' => 'checkbox')),
  4734. array('input' => array(
  4735. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4736. 'value' => 'a+', 'id' => 'ModelMultiFieldA+'
  4737. )),
  4738. array('label' => array('for' => 'ModelMultiFieldA+')),
  4739. 'first',
  4740. '/label',
  4741. '/div',
  4742. array('div' => array('class' => 'checkbox')),
  4743. array('input' => array(
  4744. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4745. 'value' => 'a++', 'id' => 'ModelMultiFieldA++'
  4746. )),
  4747. array('label' => array('for' => 'ModelMultiFieldA++')),
  4748. 'second',
  4749. '/label',
  4750. '/div',
  4751. array('div' => array('class' => 'checkbox')),
  4752. array('input' => array(
  4753. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4754. 'value' => 'a+++', 'id' => 'ModelMultiFieldA+++'
  4755. )),
  4756. array('label' => array('for' => 'ModelMultiFieldA+++')),
  4757. 'third',
  4758. '/label',
  4759. '/div'
  4760. );
  4761. $this->assertTags($result, $expected);
  4762. $result = $this->Form->select(
  4763. 'Model.multi_field',
  4764. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4765. array('multiple' => 'checkbox')
  4766. );
  4767. $expected = array(
  4768. 'input' => array(
  4769. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4770. ),
  4771. array('div' => array('class' => 'checkbox')),
  4772. array('input' => array(
  4773. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4774. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4775. )),
  4776. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4777. 'first',
  4778. '/label',
  4779. '/div',
  4780. array('div' => array('class' => 'checkbox')),
  4781. array('input' => array(
  4782. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4783. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4784. )),
  4785. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4786. 'second',
  4787. '/label',
  4788. '/div',
  4789. array('div' => array('class' => 'checkbox')),
  4790. array('input' => array(
  4791. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4792. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4793. )),
  4794. array('label' => array('for' => 'ModelMultiFieldAB')),
  4795. 'third',
  4796. '/label',
  4797. '/div'
  4798. );
  4799. $this->assertTags($result, $expected);
  4800. }
  4801. /**
  4802. * test multiple checkboxes with div styles.
  4803. *
  4804. * @return void
  4805. */
  4806. public function testSelectMultipleCheckboxDiv() {
  4807. $result = $this->Form->select(
  4808. 'Model.tags',
  4809. array('first', 'second'),
  4810. array('multiple' => 'checkbox', 'class' => 'my-class')
  4811. );
  4812. $expected = array(
  4813. 'input' => array(
  4814. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4815. ),
  4816. array('div' => array('class' => 'my-class')),
  4817. array('input' => array(
  4818. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4819. 'value' => '0', 'id' => 'ModelTags0'
  4820. )),
  4821. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4822. '/div',
  4823. array('div' => array('class' => 'my-class')),
  4824. array('input' => array(
  4825. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4826. 'value' => '1', 'id' => 'ModelTags1'
  4827. )),
  4828. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4829. '/div'
  4830. );
  4831. $this->assertTags($result, $expected);
  4832. $result = $this->Form->input('Model.tags', array(
  4833. 'options' => array('first', 'second'),
  4834. 'multiple' => 'checkbox',
  4835. 'class' => 'my-class',
  4836. 'div' => false,
  4837. 'label' => false
  4838. ));
  4839. $this->assertTags($result, $expected);
  4840. $Contact = ClassRegistry::getObject('Contact');
  4841. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4842. $result = $this->Form->input('Contact.tags', array(
  4843. 'options' => array('one'),
  4844. 'multiple' => 'checkbox',
  4845. 'label' => false,
  4846. 'div' => false
  4847. ));
  4848. $expected = array(
  4849. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4850. array('div' => array('class' => 'checkbox form-error')),
  4851. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4852. array('label' => array('for' => 'ContactTags0')),
  4853. 'one',
  4854. '/label',
  4855. '/div'
  4856. );
  4857. $this->assertTags($result, $expected);
  4858. $result = $this->Form->input('Contact.tags', array(
  4859. 'options' => array('one'),
  4860. 'multiple' => 'checkbox',
  4861. 'class' => 'mycheckbox',
  4862. 'label' => false,
  4863. 'div' => false
  4864. ));
  4865. $expected = array(
  4866. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4867. array('div' => array('class' => 'mycheckbox form-error')),
  4868. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4869. array('label' => array('for' => 'ContactTags0')),
  4870. 'one',
  4871. '/label',
  4872. '/div'
  4873. );
  4874. $this->assertTags($result, $expected);
  4875. }
  4876. /**
  4877. * Checks the security hash array generated for multiple-input checkbox elements
  4878. *
  4879. * @return void
  4880. */
  4881. public function testSelectMultipleCheckboxSecurity() {
  4882. $this->Form->request->params['_csrfToken'] = 'testKey';
  4883. $this->assertEquals(array(), $this->Form->fields);
  4884. $result = $this->Form->select(
  4885. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4886. array('multiple' => 'checkbox')
  4887. );
  4888. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4889. $result = $this->Form->secure($this->Form->fields);
  4890. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4891. $this->assertRegExp('/"' . $key . '"/', $result);
  4892. }
  4893. /**
  4894. * Multiple select elements should always be secured as they always participate
  4895. * in the POST data.
  4896. *
  4897. * @return void
  4898. */
  4899. public function testSelectMultipleSecureWithNoOptions() {
  4900. $this->Form->request->params['_csrfToken'] = 'testkey';
  4901. $this->assertEquals(array(), $this->Form->fields);
  4902. $this->Form->select(
  4903. 'Model.select',
  4904. array(),
  4905. array('multiple' => true)
  4906. );
  4907. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4908. }
  4909. /**
  4910. * When a select box has no options it should not be added to the fields list
  4911. * as it always fail post validation.
  4912. *
  4913. * @return void
  4914. */
  4915. public function testSelectNoSecureWithNoOptions() {
  4916. $this->Form->request->params['_csrfToken'] = 'testkey';
  4917. $this->assertEquals(array(), $this->Form->fields);
  4918. $this->Form->select(
  4919. 'Model.select',
  4920. array()
  4921. );
  4922. $this->assertEquals(array(), $this->Form->fields);
  4923. $this->Form->select(
  4924. 'Model.select',
  4925. array(),
  4926. array('empty' => true)
  4927. );
  4928. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4929. }
  4930. /**
  4931. * testInputMultipleCheckboxes method
  4932. *
  4933. * test input() resulting in multi select elements being generated.
  4934. *
  4935. * @return void
  4936. */
  4937. public function testInputMultipleCheckboxes() {
  4938. $result = $this->Form->input('Model.multi_field', array(
  4939. 'options' => array('first', 'second', 'third'),
  4940. 'multiple' => 'checkbox'
  4941. ));
  4942. $expected = array(
  4943. array('div' => array('class' => 'input select')),
  4944. array('label' => array('for' => 'ModelMultiField')),
  4945. 'Multi Field',
  4946. '/label',
  4947. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4948. array('div' => array('class' => 'checkbox')),
  4949. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4950. array('label' => array('for' => 'ModelMultiField0')),
  4951. 'first',
  4952. '/label',
  4953. '/div',
  4954. array('div' => array('class' => 'checkbox')),
  4955. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4956. array('label' => array('for' => 'ModelMultiField1')),
  4957. 'second',
  4958. '/label',
  4959. '/div',
  4960. array('div' => array('class' => 'checkbox')),
  4961. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4962. array('label' => array('for' => 'ModelMultiField2')),
  4963. 'third',
  4964. '/label',
  4965. '/div',
  4966. '/div'
  4967. );
  4968. $this->assertTags($result, $expected);
  4969. $result = $this->Form->input('Model.multi_field', array(
  4970. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4971. 'multiple' => 'checkbox'
  4972. ));
  4973. $expected = array(
  4974. array('div' => array('class' => 'input select')),
  4975. array('label' => array('for' => 'ModelMultiField')),
  4976. 'Multi Field',
  4977. '/label',
  4978. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4979. array('div' => array('class' => 'checkbox')),
  4980. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4981. array('label' => array('for' => 'ModelMultiFieldA')),
  4982. 'first',
  4983. '/label',
  4984. '/div',
  4985. array('div' => array('class' => 'checkbox')),
  4986. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4987. array('label' => array('for' => 'ModelMultiFieldB')),
  4988. 'second',
  4989. '/label',
  4990. '/div',
  4991. array('div' => array('class' => 'checkbox')),
  4992. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4993. array('label' => array('for' => 'ModelMultiFieldC')),
  4994. 'third',
  4995. '/label',
  4996. '/div',
  4997. '/div'
  4998. );
  4999. $this->assertTags($result, $expected);
  5000. $result = $this->Form->input('Model.multi_field', array(
  5001. 'options' => array('1' => 'first'),
  5002. 'multiple' => 'checkbox',
  5003. 'label' => false,
  5004. 'div' => false
  5005. ));
  5006. $expected = array(
  5007. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5008. array('div' => array('class' => 'checkbox')),
  5009. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5010. array('label' => array('for' => 'ModelMultiField1')),
  5011. 'first',
  5012. '/label',
  5013. '/div'
  5014. );
  5015. $this->assertTags($result, $expected);
  5016. $result = $this->Form->input('Model.multi_field', array(
  5017. 'options' => array('2' => 'second'),
  5018. 'multiple' => 'checkbox',
  5019. 'label' => false,
  5020. 'div' => false
  5021. ));
  5022. $expected = array(
  5023. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5024. array('div' => array('class' => 'checkbox')),
  5025. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5026. array('label' => array('for' => 'ModelMultiField2')),
  5027. 'second',
  5028. '/label',
  5029. '/div'
  5030. );
  5031. $this->assertTags($result, $expected);
  5032. }
  5033. /**
  5034. * testSelectHiddenFieldOmission method
  5035. *
  5036. * test that select() with 'hiddenField' => false omits the hidden field
  5037. *
  5038. * @return void
  5039. */
  5040. public function testSelectHiddenFieldOmission() {
  5041. $result = $this->Form->select('Model.multi_field',
  5042. array('first', 'second'),
  5043. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5044. );
  5045. $expected = array(
  5046. array('div' => array('class' => 'checkbox')),
  5047. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5048. array('label' => array('for' => 'ModelMultiField0')),
  5049. 'first',
  5050. '/label',
  5051. '/div',
  5052. array('div' => array('class' => 'checkbox')),
  5053. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5054. array('label' => array('for' => 'ModelMultiField1')),
  5055. 'second',
  5056. '/label',
  5057. '/div'
  5058. );
  5059. $this->assertTags($result, $expected);
  5060. $result = $this->Form->input('Model.multi_field', array(
  5061. 'options' => array('first', 'second'),
  5062. 'multiple' => 'checkbox',
  5063. 'hiddenField' => false
  5064. ));
  5065. $expected = array(
  5066. array('div' => array('class' => 'input select')),
  5067. array('label' => array('for' => 'ModelMultiField')),
  5068. 'Multi Field',
  5069. '/label',
  5070. array('div' => array('class' => 'checkbox')),
  5071. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5072. array('label' => array('for' => 'ModelMultiField0')),
  5073. 'first',
  5074. '/label',
  5075. '/div',
  5076. array('div' => array('class' => 'checkbox')),
  5077. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5078. array('label' => array('for' => 'ModelMultiField1')),
  5079. 'second',
  5080. '/label',
  5081. '/div',
  5082. '/div'
  5083. );
  5084. $this->assertTags($result, $expected);
  5085. }
  5086. /**
  5087. * test that select() with multiple = checkbox works with overriding name attribute.
  5088. *
  5089. * @return void
  5090. */
  5091. public function testSelectCheckboxMultipleOverrideName() {
  5092. $result = $this->Form->input('category', array(
  5093. 'type' => 'select',
  5094. 'multiple' => 'checkbox',
  5095. 'name' => 'fish',
  5096. 'options' => array('1', '2'),
  5097. 'div' => false,
  5098. 'label' => false,
  5099. ));
  5100. $expected = array(
  5101. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'),
  5102. array('div' => array('class' => 'checkbox')),
  5103. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'Category0')),
  5104. array('label' => array('for' => 'Category0')), '1', '/label',
  5105. '/div',
  5106. array('div' => array('class' => 'checkbox')),
  5107. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'Category1')),
  5108. array('label' => array('for' => 'Category1')), '2', '/label',
  5109. '/div'
  5110. );
  5111. $this->assertTags($result, $expected);
  5112. }
  5113. /**
  5114. * Test that 'id' overrides all the checkbox id's as well.
  5115. *
  5116. * @return void
  5117. */
  5118. public function testSelectCheckboxMultipleId() {
  5119. $result = $this->Form->select(
  5120. 'Model.multi_field',
  5121. array('first', 'second', 'third'),
  5122. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5123. );
  5124. $expected = array(
  5125. 'input' => array(
  5126. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'CustomId'
  5127. ),
  5128. array('div' => array('class' => 'checkbox')),
  5129. array('input' => array(
  5130. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5131. 'value' => '0', 'id' => 'CustomId0'
  5132. )),
  5133. array('label' => array('for' => 'CustomId0')),
  5134. 'first',
  5135. '/label',
  5136. '/div',
  5137. array('div' => array('class' => 'checkbox')),
  5138. array('input' => array(
  5139. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5140. 'value' => '1', 'id' => 'CustomId1'
  5141. )),
  5142. array('label' => array('for' => 'CustomId1')),
  5143. 'second',
  5144. '/label',
  5145. '/div',
  5146. array('div' => array('class' => 'checkbox')),
  5147. array('input' => array(
  5148. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  5149. 'value' => '2', 'id' => 'CustomId2'
  5150. )),
  5151. array('label' => array('for' => 'CustomId2')),
  5152. 'third',
  5153. '/label',
  5154. '/div'
  5155. );
  5156. $this->assertTags($result, $expected);
  5157. }
  5158. /**
  5159. * testCheckbox method
  5160. *
  5161. * Test generation of checkboxes
  5162. *
  5163. * @return void
  5164. */
  5165. public function testCheckbox() {
  5166. $result = $this->Form->checkbox('Model.field');
  5167. $expected = array(
  5168. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5169. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5170. );
  5171. $this->assertTags($result, $expected);
  5172. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5173. $expected = array(
  5174. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'theID_'),
  5175. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  5176. );
  5177. $this->assertTags($result, $expected);
  5178. $Contact = ClassRegistry::getObject('Contact');
  5179. $Contact->validationErrors['field'] = 1;
  5180. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5181. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5182. $expected = array(
  5183. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  5184. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5188. $expected = array(
  5189. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  5190. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5191. );
  5192. $this->assertTags($result, $expected);
  5193. $this->Form->request->data['Contact']['field'] = '';
  5194. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5195. $expected = array(
  5196. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  5197. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5198. );
  5199. $this->assertTags($result, $expected);
  5200. $Contact->validationErrors = array();
  5201. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5202. $expected = array(
  5203. 'input' => array('type' => 'hidden', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  5204. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5205. );
  5206. $this->assertTags($result, $expected);
  5207. $this->Form->request->data['Contact']['published'] = 1;
  5208. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5209. $expected = array(
  5210. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  5211. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5212. );
  5213. $this->assertTags($result, $expected);
  5214. $this->Form->request->data['Contact']['published'] = 0;
  5215. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5216. $expected = array(
  5217. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  5218. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID'))
  5219. );
  5220. $this->assertTags($result, $expected);
  5221. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5222. $expected = array(
  5223. 'input' => array('type' => 'hidden', 'name' => 'Model[CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5224. array('input' => array('type' => 'checkbox', 'name' => 'Model[CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5225. );
  5226. $this->assertTags($result, $expected);
  5227. $result = $this->Form->checkbox('CustomField.1.value');
  5228. $expected = array(
  5229. 'input' => array('type' => 'hidden', 'name' => 'CustomField[1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5230. array('input' => array('type' => 'checkbox', 'name' => 'CustomField[1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5231. );
  5232. $this->assertTags($result, $expected);
  5233. }
  5234. /**
  5235. * test checkbox() with a custom name attribute
  5236. *
  5237. * @return void
  5238. */
  5239. public function testCheckboxCustomNameAttribute() {
  5240. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5241. $expected = array(
  5242. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5243. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5244. );
  5245. $this->assertTags($result, $expected);
  5246. }
  5247. /**
  5248. * test the checked option for checkboxes.
  5249. *
  5250. * @return void
  5251. */
  5252. public function testCheckboxCheckedOption() {
  5253. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5254. $expected = array(
  5255. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5256. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5257. );
  5258. $this->assertTags($result, $expected);
  5259. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5260. $expected = array(
  5261. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5262. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5263. );
  5264. $this->assertTags($result, $expected);
  5265. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5266. $expected = array(
  5267. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5268. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5269. );
  5270. $this->assertTags($result, $expected);
  5271. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5272. $expected = array(
  5273. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5274. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5275. );
  5276. $this->assertTags($result, $expected);
  5277. $this->Form->request->data['Model']['field'] = 1;
  5278. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5279. $expected = array(
  5280. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5281. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5282. );
  5283. $this->assertTags($result, $expected);
  5284. }
  5285. /**
  5286. * Test that disabled attribute works on both the checkbox and hidden input.
  5287. *
  5288. * @return void
  5289. */
  5290. public function testCheckboxDisabling() {
  5291. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5292. $expected = array(
  5293. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5294. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5295. );
  5296. $this->assertTags($result, $expected);
  5297. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5298. $expected = array(
  5299. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5300. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5301. );
  5302. $this->assertTags($result, $expected);
  5303. }
  5304. /**
  5305. * Test that the hidden input for checkboxes can be omitted or set to a
  5306. * specific value.
  5307. *
  5308. * @return void
  5309. */
  5310. public function testCheckboxHiddenField() {
  5311. $result = $this->Form->input('UserForm.something', array(
  5312. 'type' => 'checkbox',
  5313. 'hiddenField' => false
  5314. ));
  5315. $expected = array(
  5316. 'div' => array('class' => 'input checkbox'),
  5317. array('input' => array(
  5318. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5319. 'value' => '1', 'id' => 'UserFormSomething'
  5320. )),
  5321. 'label' => array('for' => 'UserFormSomething'),
  5322. 'Something',
  5323. '/label',
  5324. '/div'
  5325. );
  5326. $this->assertTags($result, $expected);
  5327. $result = $this->Form->input('UserForm.something', array(
  5328. 'type' => 'checkbox',
  5329. 'value' => 'Y',
  5330. 'hiddenField' => 'N',
  5331. ));
  5332. $expected = array(
  5333. 'div' => array('class' => 'input checkbox'),
  5334. array('input' => array(
  5335. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5336. 'value' => 'N', 'id' => 'UserFormSomething_'
  5337. )),
  5338. array('input' => array(
  5339. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5340. 'value' => 'Y', 'id' => 'UserFormSomething'
  5341. )),
  5342. 'label' => array('for' => 'UserFormSomething'),
  5343. 'Something',
  5344. '/label',
  5345. '/div'
  5346. );
  5347. $this->assertTags($result, $expected);
  5348. }
  5349. /**
  5350. * testDateTime method
  5351. *
  5352. * Test generation of date/time select elements
  5353. *
  5354. * @return void
  5355. */
  5356. public function testDateTime() {
  5357. extract($this->dateRegex);
  5358. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5359. $now = strtotime('now');
  5360. $expected = array(
  5361. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5362. $daysRegex,
  5363. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5364. date('j', $now),
  5365. '/option',
  5366. '*/select',
  5367. '-',
  5368. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5369. $monthsRegex,
  5370. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5371. date('F', $now),
  5372. '/option',
  5373. '*/select',
  5374. '-',
  5375. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5376. $yearsRegex,
  5377. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5378. date('Y', $now),
  5379. '/option',
  5380. '*/select',
  5381. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5382. $hoursRegex,
  5383. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5384. date('g', $now),
  5385. '/option',
  5386. '*/select',
  5387. ':',
  5388. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5389. $minutesRegex,
  5390. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5391. date('i', $now),
  5392. '/option',
  5393. '*/select',
  5394. ' ',
  5395. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5396. $meridianRegex,
  5397. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5398. date('a', $now),
  5399. '/option',
  5400. '*/select'
  5401. );
  5402. $this->assertTags($result, $expected);
  5403. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5404. $expected = array(
  5405. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5406. $daysRegex,
  5407. array('option' => array('value' => '')),
  5408. '/option',
  5409. '*/select',
  5410. '-',
  5411. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5412. $monthsRegex,
  5413. array('option' => array('value' => '')),
  5414. '/option',
  5415. '*/select',
  5416. '-',
  5417. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5418. $yearsRegex,
  5419. array('option' => array('value' => '')),
  5420. '/option',
  5421. '*/select',
  5422. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5423. $hoursRegex,
  5424. array('option' => array('value' => '')),
  5425. '/option',
  5426. '*/select',
  5427. ':',
  5428. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5429. $minutesRegex,
  5430. array('option' => array('value' => '')),
  5431. '/option',
  5432. '*/select',
  5433. ' ',
  5434. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5435. $meridianRegex,
  5436. array('option' => array('value' => '')),
  5437. '/option',
  5438. '*/select'
  5439. );
  5440. $this->assertTags($result, $expected);
  5441. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5442. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5443. $this->assertTags($result, $expected);
  5444. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5445. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5446. $this->assertTags($result, $expected);
  5447. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5448. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5449. $expected = array(
  5450. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5451. $daysRegex,
  5452. array('option' => array('value' => '')),
  5453. '/option',
  5454. '*/select',
  5455. '-',
  5456. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5457. $monthsRegex,
  5458. array('option' => array('value' => '')),
  5459. '/option',
  5460. '*/select',
  5461. '-',
  5462. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5463. $yearsRegex,
  5464. array('option' => array('value' => '')),
  5465. '/option',
  5466. '*/select',
  5467. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5468. $hoursRegex,
  5469. array('option' => array('value' => '')),
  5470. '/option',
  5471. '*/select',
  5472. ':',
  5473. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5474. $minutesRegex,
  5475. array('option' => array('value' => '')),
  5476. '/option',
  5477. array('option' => array('value' => '00')),
  5478. '00',
  5479. '/option',
  5480. array('option' => array('value' => '05')),
  5481. '05',
  5482. '/option',
  5483. array('option' => array('value' => '10')),
  5484. '10',
  5485. '/option',
  5486. '*/select',
  5487. ' ',
  5488. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5489. $meridianRegex,
  5490. array('option' => array('value' => '')),
  5491. '/option',
  5492. '*/select'
  5493. );
  5494. $this->assertTags($result, $expected);
  5495. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5496. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5497. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5498. $this->Form->request->data['Contact']['data'] = null;
  5499. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5500. $expected = array(
  5501. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5502. $daysRegex,
  5503. array('option' => array('value' => '')),
  5504. '/option',
  5505. '*/select',
  5506. '-',
  5507. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5508. $monthsRegex,
  5509. array('option' => array('value' => '')),
  5510. '/option',
  5511. '*/select',
  5512. '-',
  5513. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5514. $yearsRegex,
  5515. array('option' => array('value' => '')),
  5516. '/option',
  5517. '*/select',
  5518. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5519. $hoursRegex,
  5520. array('option' => array('value' => '')),
  5521. '/option',
  5522. '*/select',
  5523. ':',
  5524. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5525. $minutesRegex,
  5526. array('option' => array('value' => '')),
  5527. '/option',
  5528. '*/select',
  5529. ' ',
  5530. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5531. $meridianRegex,
  5532. array('option' => array('value' => '')),
  5533. '/option',
  5534. '*/select'
  5535. );
  5536. $this->assertTags($result, $expected);
  5537. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5538. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5539. $now = strtotime($this->Form->data['Model']['field']);
  5540. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5541. $expected = array(
  5542. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5543. $daysRegex,
  5544. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5545. date('j', $now),
  5546. '/option',
  5547. '*/select',
  5548. '-',
  5549. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5550. $monthsRegex,
  5551. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5552. date('F', $now),
  5553. '/option',
  5554. '*/select',
  5555. '-',
  5556. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  5557. $yearsRegex,
  5558. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5559. date('Y', $now),
  5560. '/option',
  5561. '*/select',
  5562. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5563. $hoursRegex,
  5564. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5565. date('g', $now),
  5566. '/option',
  5567. '*/select',
  5568. ':',
  5569. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5570. $minutesRegex,
  5571. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5572. date('i', $now),
  5573. '/option',
  5574. '*/select',
  5575. ' ',
  5576. array('select' => array('name' => 'Model[field][meridian]', 'id' => 'ModelFieldMeridian')),
  5577. $meridianRegex,
  5578. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5579. date('a', $now),
  5580. '/option',
  5581. '*/select'
  5582. );
  5583. $this->assertTags($result, $expected);
  5584. $selected = strtotime('2008-10-26 12:33:00');
  5585. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5586. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5587. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5588. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5589. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5590. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5591. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5592. $this->Form->create('Contact');
  5593. $result = $this->Form->input('published');
  5594. $now = strtotime('now');
  5595. $expected = array(
  5596. 'div' => array('class' => 'input date'),
  5597. 'label' => array('for' => 'ContactPublishedMonth'),
  5598. 'Published',
  5599. '/label',
  5600. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5601. $monthsRegex,
  5602. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5603. date('F', $now),
  5604. '/option',
  5605. '*/select',
  5606. '-',
  5607. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5608. $daysRegex,
  5609. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5610. date('j', $now),
  5611. '/option',
  5612. '*/select',
  5613. '-',
  5614. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5615. $yearsRegex,
  5616. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5617. date('Y', $now),
  5618. '/option',
  5619. '*/select',
  5620. '/div'
  5621. );
  5622. $this->assertTags($result, $expected);
  5623. $result = $this->Form->input('published2', array('type' => 'date'));
  5624. $now = strtotime('now');
  5625. $expected = array(
  5626. 'div' => array('class' => 'input date'),
  5627. 'label' => array('for' => 'ContactPublished2Month'),
  5628. 'Published2',
  5629. '/label',
  5630. array('select' => array('name' => 'Contact[published2][month]', 'id' => 'ContactPublished2Month')),
  5631. $monthsRegex,
  5632. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5633. date('F', $now),
  5634. '/option',
  5635. '*/select',
  5636. '-',
  5637. array('select' => array('name' => 'Contact[published2][day]', 'id' => 'ContactPublished2Day')),
  5638. $daysRegex,
  5639. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5640. date('j', $now),
  5641. '/option',
  5642. '*/select',
  5643. '-',
  5644. array('select' => array('name' => 'Contact[published2][year]', 'id' => 'ContactPublished2Year')),
  5645. $yearsRegex,
  5646. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5647. date('Y', $now),
  5648. '/option',
  5649. '*/select',
  5650. '/div'
  5651. );
  5652. $this->assertTags($result, $expected);
  5653. $this->Form->create('Contact');
  5654. $result = $this->Form->input('published', array('monthNames' => false));
  5655. $now = strtotime('now');
  5656. $expected = array(
  5657. 'div' => array('class' => 'input date'),
  5658. 'label' => array('for' => 'ContactPublishedMonth'),
  5659. 'Published',
  5660. '/label',
  5661. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5662. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5663. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5664. date('m', $now),
  5665. '/option',
  5666. '*/select',
  5667. '-',
  5668. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5669. $daysRegex,
  5670. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5671. date('j', $now),
  5672. '/option',
  5673. '*/select',
  5674. '-',
  5675. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5676. $yearsRegex,
  5677. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5678. date('Y', $now),
  5679. '/option',
  5680. '*/select',
  5681. '/div'
  5682. );
  5683. $this->assertTags($result, $expected);
  5684. $result = $this->Form->input('published', array('type' => 'time'));
  5685. $now = strtotime('now');
  5686. $expected = array(
  5687. 'div' => array('class' => 'input time'),
  5688. 'label' => array('for' => 'ContactPublishedHour'),
  5689. 'Published',
  5690. '/label',
  5691. array('select' => array('name' => 'Contact[published][hour]', 'id' => 'ContactPublishedHour')),
  5692. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5693. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5694. date('g', $now),
  5695. '/option',
  5696. '*/select',
  5697. ':',
  5698. );
  5699. $this->assertTags($result, $expected);
  5700. $result = $this->Form->input('published', array(
  5701. 'timeFormat' => 24,
  5702. 'interval' => 5,
  5703. 'selected' => strtotime('2009-09-03 13:37:00'),
  5704. 'type' => 'datetime'
  5705. ));
  5706. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5707. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5708. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5709. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5710. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5711. }
  5712. /**
  5713. * Test dateTime with rounding
  5714. *
  5715. * @return void
  5716. */
  5717. public function testDateTimeRounding() {
  5718. $this->Form->request->data['Contact'] = array(
  5719. 'date' => array(
  5720. 'day' => '13',
  5721. 'month' => '12',
  5722. 'year' => '2010',
  5723. 'hour' => '04',
  5724. 'min' => '19',
  5725. 'meridian' => 'AM'
  5726. )
  5727. );
  5728. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5729. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5730. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5731. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5732. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5733. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5734. }
  5735. /**
  5736. * Test that empty values don't trigger errors.
  5737. *
  5738. * @return void
  5739. */
  5740. public function testDateTimeNoErrorsOnEmptyData() {
  5741. $this->Form->request->data['Contact'] = array(
  5742. 'date' => array(
  5743. 'day' => '',
  5744. 'month' => '',
  5745. 'year' => '',
  5746. 'hour' => '',
  5747. 'min' => '',
  5748. 'meridian' => ''
  5749. )
  5750. );
  5751. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5752. $this->assertNotEmpty($result);
  5753. }
  5754. /**
  5755. * test that datetime() and default values work.
  5756. *
  5757. * @return void
  5758. */
  5759. public function testDatetimeWithDefault() {
  5760. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5761. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5762. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5763. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5764. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5765. 'default' => '2009-06-01 11:15:30'
  5766. ));
  5767. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5768. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5769. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5770. }
  5771. /**
  5772. * test that bogus non-date time data doesn't cause errors.
  5773. *
  5774. * @return void
  5775. */
  5776. public function testDateTimeWithBogusData() {
  5777. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5778. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5779. }
  5780. /**
  5781. * testDateTime all zeros
  5782. *
  5783. * @return void
  5784. */
  5785. public function testDateTimeAllZeros() {
  5786. $result = $this->Form->dateTime('Contact.date',
  5787. 'DMY',
  5788. false,
  5789. array(
  5790. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5791. 'value' => '0000-00-00'
  5792. )
  5793. );
  5794. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5795. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5796. }
  5797. /**
  5798. * testDateTimeEmptyAsArray
  5799. *
  5800. * @return void
  5801. */
  5802. public function testDateTimeEmptyAsArray() {
  5803. $result = $this->Form->dateTime('Contact.date',
  5804. 'DMY',
  5805. '12',
  5806. array(
  5807. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5808. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5809. )
  5810. )
  5811. );
  5812. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5813. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5814. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5815. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5816. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5817. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5818. $result = $this->Form->dateTime('Contact.date',
  5819. 'DMY',
  5820. '12',
  5821. array(
  5822. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5823. )
  5824. );
  5825. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5826. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5827. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5828. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5829. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5830. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5831. }
  5832. /**
  5833. * testFormDateTimeMulti method
  5834. *
  5835. * test multiple datetime element generation
  5836. *
  5837. * @return void
  5838. */
  5839. public function testFormDateTimeMulti() {
  5840. extract($this->dateRegex);
  5841. $result = $this->Form->dateTime('Contact.1.updated');
  5842. $expected = array(
  5843. array('select' => array('name' => 'Contact[1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5844. $daysRegex,
  5845. array('option' => array('value' => '')),
  5846. '/option',
  5847. '*/select',
  5848. '-',
  5849. array('select' => array('name' => 'Contact[1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5850. $monthsRegex,
  5851. array('option' => array('value' => '')),
  5852. '/option',
  5853. '*/select',
  5854. '-',
  5855. array('select' => array('name' => 'Contact[1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5856. $yearsRegex,
  5857. array('option' => array('value' => '')),
  5858. '/option',
  5859. '*/select',
  5860. array('select' => array('name' => 'Contact[1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5861. $hoursRegex,
  5862. array('option' => array('value' => '')),
  5863. '/option',
  5864. '*/select',
  5865. ':',
  5866. array('select' => array('name' => 'Contact[1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5867. $minutesRegex,
  5868. array('option' => array('value' => '')),
  5869. '/option',
  5870. '*/select',
  5871. ' ',
  5872. array('select' => array('name' => 'Contact[1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5873. $meridianRegex,
  5874. array('option' => array('value' => '')),
  5875. '/option',
  5876. '*/select'
  5877. );
  5878. $this->assertTags($result, $expected);
  5879. $result = $this->Form->dateTime('Contact.2.updated');
  5880. $expected = array(
  5881. array('select' => array('name' => 'Contact[2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5882. $daysRegex,
  5883. array('option' => array('value' => '')),
  5884. '/option',
  5885. '*/select',
  5886. '-',
  5887. array('select' => array('name' => 'Contact[2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5888. $monthsRegex,
  5889. array('option' => array('value' => '')),
  5890. '/option',
  5891. '*/select',
  5892. '-',
  5893. array('select' => array('name' => 'Contact[2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5894. $yearsRegex,
  5895. array('option' => array('value' => '')),
  5896. '/option',
  5897. '*/select',
  5898. array('select' => array('name' => 'Contact[2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5899. $hoursRegex,
  5900. array('option' => array('value' => '')),
  5901. '/option',
  5902. '*/select',
  5903. ':',
  5904. array('select' => array('name' => 'Contact[2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5905. $minutesRegex,
  5906. array('option' => array('value' => '')),
  5907. '/option',
  5908. '*/select',
  5909. ' ',
  5910. array('select' => array('name' => 'Contact[2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5911. $meridianRegex,
  5912. array('option' => array('value' => '')),
  5913. '/option',
  5914. '*/select'
  5915. );
  5916. $this->assertTags($result, $expected);
  5917. }
  5918. /**
  5919. * When changing the date format, the label should always focus the first select box when
  5920. * clicked.
  5921. *
  5922. * @return void
  5923. */
  5924. public function testDateTimeLabelIdMatchesFirstInput() {
  5925. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5926. $this->assertContains('label for="ModelDateMonth"', $result);
  5927. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5928. $this->assertContains('label for="ModelDateDay"', $result);
  5929. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5930. $this->assertContains('label for="ModelDateYear"', $result);
  5931. }
  5932. /**
  5933. * testMonth method
  5934. *
  5935. * @return void
  5936. */
  5937. public function testMonth() {
  5938. $result = $this->Form->month('Model.field');
  5939. $expected = array(
  5940. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5941. array('option' => array('value' => '')),
  5942. '/option',
  5943. array('option' => array('value' => '01')),
  5944. date('F', strtotime('2008-01-01 00:00:00')),
  5945. '/option',
  5946. array('option' => array('value' => '02')),
  5947. date('F', strtotime('2008-02-01 00:00:00')),
  5948. '/option',
  5949. '*/select',
  5950. );
  5951. $this->assertTags($result, $expected);
  5952. $result = $this->Form->month('Model.field', array('empty' => true));
  5953. $expected = array(
  5954. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5955. array('option' => array('value' => '')),
  5956. '/option',
  5957. array('option' => array('value' => '01')),
  5958. date('F', strtotime('2008-01-01 00:00:00')),
  5959. '/option',
  5960. array('option' => array('value' => '02')),
  5961. date('F', strtotime('2008-02-01 00:00:00')),
  5962. '/option',
  5963. '*/select',
  5964. );
  5965. $this->assertTags($result, $expected);
  5966. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5967. $expected = array(
  5968. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5969. array('option' => array('value' => '')),
  5970. '/option',
  5971. array('option' => array('value' => '01')),
  5972. '01',
  5973. '/option',
  5974. array('option' => array('value' => '02')),
  5975. '02',
  5976. '/option',
  5977. '*/select',
  5978. );
  5979. $this->assertTags($result, $expected);
  5980. $monthNames = array(
  5981. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5982. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5983. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5984. $expected = array(
  5985. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5986. array('option' => array('value' => '')),
  5987. '/option',
  5988. array('option' => array('value' => '01')),
  5989. 'Jan',
  5990. '/option',
  5991. array('option' => array('value' => '02')),
  5992. 'Feb',
  5993. '/option',
  5994. '*/select',
  5995. );
  5996. $this->assertTags($result, $expected);
  5997. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5998. $result = $this->Form->month('Project.release');
  5999. $expected = array(
  6000. array('select' => array('name' => 'Project[release][month]', 'id' => 'ProjectReleaseMonth')),
  6001. array('option' => array('value' => '')),
  6002. '/option',
  6003. array('option' => array('value' => '01')),
  6004. 'January',
  6005. '/option',
  6006. array('option' => array('value' => '02', 'selected' => 'selected')),
  6007. 'February',
  6008. '/option',
  6009. '*/select',
  6010. );
  6011. $this->assertTags($result, $expected);
  6012. }
  6013. /**
  6014. * testDay method
  6015. *
  6016. * @return void
  6017. */
  6018. public function testDay() {
  6019. extract($this->dateRegex);
  6020. $result = $this->Form->day('Model.field', array('value' => false));
  6021. $expected = array(
  6022. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6023. array('option' => array('value' => '')),
  6024. '/option',
  6025. array('option' => array('value' => '01')),
  6026. '1',
  6027. '/option',
  6028. array('option' => array('value' => '02')),
  6029. '2',
  6030. '/option',
  6031. $daysRegex,
  6032. '/select',
  6033. );
  6034. $this->assertTags($result, $expected);
  6035. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6036. $result = $this->Form->day('Model.field');
  6037. $expected = array(
  6038. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6039. array('option' => array('value' => '')),
  6040. '/option',
  6041. array('option' => array('value' => '01')),
  6042. '1',
  6043. '/option',
  6044. array('option' => array('value' => '02')),
  6045. '2',
  6046. '/option',
  6047. $daysRegex,
  6048. array('option' => array('value' => '10', 'selected' => 'selected')),
  6049. '10',
  6050. '/option',
  6051. $daysRegex,
  6052. '/select',
  6053. );
  6054. $this->assertTags($result, $expected);
  6055. $this->Form->request->data['Model']['field'] = '';
  6056. $result = $this->Form->day('Model.field', array('value' => '10'));
  6057. $expected = array(
  6058. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6059. array('option' => array('value' => '')),
  6060. '/option',
  6061. array('option' => array('value' => '01')),
  6062. '1',
  6063. '/option',
  6064. array('option' => array('value' => '02')),
  6065. '2',
  6066. '/option',
  6067. $daysRegex,
  6068. array('option' => array('value' => '10', 'selected' => 'selected')),
  6069. '10',
  6070. '/option',
  6071. $daysRegex,
  6072. '/select',
  6073. );
  6074. $this->assertTags($result, $expected);
  6075. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6076. $result = $this->Form->day('Model.field', array('value' => true));
  6077. $expected = array(
  6078. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  6079. array('option' => array('value' => '')),
  6080. '/option',
  6081. array('option' => array('value' => '01')),
  6082. '1',
  6083. '/option',
  6084. array('option' => array('value' => '02')),
  6085. '2',
  6086. '/option',
  6087. $daysRegex,
  6088. array('option' => array('value' => '10', 'selected' => 'selected')),
  6089. '10',
  6090. '/option',
  6091. $daysRegex,
  6092. '/select',
  6093. );
  6094. $this->assertTags($result, $expected);
  6095. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6096. $result = $this->Form->day('Project.release');
  6097. $expected = array(
  6098. array('select' => array('name' => 'Project[release][day]', 'id' => 'ProjectReleaseDay')),
  6099. array('option' => array('value' => '')),
  6100. '/option',
  6101. array('option' => array('value' => '01')),
  6102. '1',
  6103. '/option',
  6104. array('option' => array('value' => '02')),
  6105. '2',
  6106. '/option',
  6107. $daysRegex,
  6108. array('option' => array('value' => '10', 'selected' => 'selected')),
  6109. '10',
  6110. '/option',
  6111. $daysRegex,
  6112. '/select',
  6113. );
  6114. $this->assertTags($result, $expected);
  6115. }
  6116. /**
  6117. * testMinute method
  6118. *
  6119. * @return void
  6120. */
  6121. public function testMinute() {
  6122. extract($this->dateRegex);
  6123. $result = $this->Form->minute('Model.field');
  6124. $expected = array(
  6125. array('select' => array('name' => '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. '/select',
  6139. );
  6140. $this->assertTags($result, $expected);
  6141. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6142. $result = $this->Form->minute('Model.field');
  6143. $expected = array(
  6144. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  6145. array('option' => array('value' => '')),
  6146. '/option',
  6147. array('option' => array('value' => '00')),
  6148. '00',
  6149. '/option',
  6150. array('option' => array('value' => '01')),
  6151. '01',
  6152. '/option',
  6153. array('option' => array('value' => '02')),
  6154. '02',
  6155. '/option',
  6156. $minutesRegex,
  6157. array('option' => array('value' => '12', 'selected' => 'selected')),
  6158. '12',
  6159. '/option',
  6160. $minutesRegex,
  6161. '/select',
  6162. );
  6163. $this->assertTags($result, $expected);
  6164. $this->Form->request->data['Model']['field'] = '';
  6165. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6166. $expected = array(
  6167. array('select' => array('name' => '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')),
  6177. '10',
  6178. '/option',
  6179. $minutesRegex,
  6180. '/select',
  6181. );
  6182. $this->assertTags($result, $expected);
  6183. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6184. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6185. $expected = array(
  6186. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  6187. array('option' => array('value' => '')),
  6188. '/option',
  6189. array('option' => array('value' => '00')),
  6190. '00',
  6191. '/option',
  6192. array('option' => array('value' => '05')),
  6193. '05',
  6194. '/option',
  6195. array('option' => array('value' => '10', 'selected' => 'selected')),
  6196. '10',
  6197. '/option',
  6198. $minutesRegex,
  6199. '/select',
  6200. );
  6201. $this->assertTags($result, $expected);
  6202. }
  6203. /**
  6204. * testHour method
  6205. *
  6206. * @return void
  6207. */
  6208. public function testHour() {
  6209. extract($this->dateRegex);
  6210. $result = $this->Form->hour('Model.field', false);
  6211. $expected = array(
  6212. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6213. array('option' => array('value' => '')),
  6214. '/option',
  6215. array('option' => array('value' => '01')),
  6216. '1',
  6217. '/option',
  6218. array('option' => array('value' => '02')),
  6219. '2',
  6220. '/option',
  6221. $hoursRegex,
  6222. '/select',
  6223. );
  6224. $this->assertTags($result, $expected);
  6225. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6226. $result = $this->Form->hour('Model.field', false);
  6227. $expected = array(
  6228. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6229. array('option' => array('value' => '')),
  6230. '/option',
  6231. array('option' => array('value' => '01')),
  6232. '1',
  6233. '/option',
  6234. array('option' => array('value' => '02')),
  6235. '2',
  6236. '/option',
  6237. $hoursRegex,
  6238. array('option' => array('value' => '12', 'selected' => 'selected')),
  6239. '12',
  6240. '/option',
  6241. '/select',
  6242. );
  6243. $this->assertTags($result, $expected);
  6244. $this->Form->request->data['Model']['field'] = '';
  6245. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6246. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6247. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6248. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6249. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6250. $result = $this->Form->hour('Model.field', true);
  6251. $expected = array(
  6252. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6253. array('option' => array('value' => '')),
  6254. '/option',
  6255. array('option' => array('value' => '00', 'selected' => 'selected')),
  6256. '0',
  6257. '/option',
  6258. array('option' => array('value' => '01')),
  6259. '1',
  6260. '/option',
  6261. array('option' => array('value' => '02')),
  6262. '2',
  6263. '/option',
  6264. $hoursRegex,
  6265. '/select',
  6266. );
  6267. $this->assertTags($result, $expected);
  6268. unset($this->Form->request->data['Model']['field']);
  6269. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6270. $thisHour = date('H');
  6271. $optValue = date('G');
  6272. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6273. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6274. $result = $this->Form->hour('Model.field', true);
  6275. $expected = array(
  6276. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6277. array('option' => array('value' => '')),
  6278. '/option',
  6279. array('option' => array('value' => '00')),
  6280. '0',
  6281. '/option',
  6282. array('option' => array('value' => '01', 'selected' => 'selected')),
  6283. '1',
  6284. '/option',
  6285. array('option' => array('value' => '02')),
  6286. '2',
  6287. '/option',
  6288. $hoursRegex,
  6289. '/select',
  6290. );
  6291. $this->assertTags($result, $expected);
  6292. }
  6293. /**
  6294. * testYear method
  6295. *
  6296. * @return void
  6297. */
  6298. public function testYear() {
  6299. $result = $this->Form->year('Model.field', 2006, 2007);
  6300. $expected = array(
  6301. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6302. array('option' => array('value' => '')),
  6303. '/option',
  6304. array('option' => array('value' => '2007')),
  6305. '2007',
  6306. '/option',
  6307. array('option' => array('value' => '2006')),
  6308. '2006',
  6309. '/option',
  6310. '/select',
  6311. );
  6312. $this->assertTags($result, $expected);
  6313. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6314. $expected = array(
  6315. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6316. array('option' => array('value' => '')),
  6317. '/option',
  6318. array('option' => array('value' => '2006')),
  6319. '2006',
  6320. '/option',
  6321. array('option' => array('value' => '2007')),
  6322. '2007',
  6323. '/option',
  6324. '/select',
  6325. );
  6326. $this->assertTags($result, $expected);
  6327. $this->request->data['Contact']['published'] = '';
  6328. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6329. $expected = array(
  6330. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6331. array('option' => array('value' => '')),
  6332. '/option',
  6333. array('option' => array('value' => '2007')),
  6334. '2007',
  6335. '/option',
  6336. array('option' => array('value' => '2006')),
  6337. '2006',
  6338. '/option',
  6339. '/select',
  6340. );
  6341. $this->assertTags($result, $expected);
  6342. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6343. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6344. $expected = array(
  6345. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6346. array('option' => array('value' => '2007')),
  6347. '2007',
  6348. '/option',
  6349. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6350. '2006',
  6351. '/option',
  6352. '/select',
  6353. );
  6354. $this->assertTags($result, $expected);
  6355. $this->Form->request->data['Contact']['published'] = '';
  6356. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6357. $expected = array(
  6358. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6359. array('option' => array('value' => '')),
  6360. '/option',
  6361. array('option' => array('value' => '2007')),
  6362. '2007',
  6363. '/option',
  6364. array('option' => array('value' => '2006')),
  6365. '2006',
  6366. '/option',
  6367. '/select',
  6368. );
  6369. $this->assertTags($result, $expected);
  6370. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6371. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6372. $expected = array(
  6373. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6374. array('option' => array('value' => '2007')),
  6375. '2007',
  6376. '/option',
  6377. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6378. '2006',
  6379. '/option',
  6380. '/select',
  6381. );
  6382. $this->assertTags($result, $expected);
  6383. $this->Form->request->data['Contact']['published'] = '';
  6384. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6385. $expected = array(
  6386. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6387. array('option' => array('value' => '')),
  6388. '/option',
  6389. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6390. '2007',
  6391. '/option',
  6392. array('option' => array('value' => '2006')),
  6393. '2006',
  6394. '/option',
  6395. '/select',
  6396. );
  6397. $this->assertTags($result, $expected);
  6398. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6399. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6400. $expected = array(
  6401. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6402. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6403. '2007',
  6404. '/option',
  6405. array('option' => array('value' => '2006')),
  6406. '2006',
  6407. '/option',
  6408. '/select',
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. $this->Form->request->data['Contact']['published'] = '';
  6412. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6413. $expected = array(
  6414. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6415. array('option' => array('value' => '2008')),
  6416. '2008',
  6417. '/option',
  6418. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6419. '2007',
  6420. '/option',
  6421. array('option' => array('value' => '2006')),
  6422. '2006',
  6423. '/option',
  6424. '/select',
  6425. );
  6426. $this->assertTags($result, $expected);
  6427. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6428. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6429. $expected = array(
  6430. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6431. array('option' => array('value' => '2008')),
  6432. '2008',
  6433. '/option',
  6434. array('option' => array('value' => '2007')),
  6435. '2007',
  6436. '/option',
  6437. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6438. '2006',
  6439. '/option',
  6440. '/select',
  6441. );
  6442. $this->assertTags($result, $expected);
  6443. $this->Form->request->data = array();
  6444. $this->Form->create('Contact');
  6445. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6446. $expected = array(
  6447. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6448. array('option' => array('value' => '2008')),
  6449. '2008',
  6450. '/option',
  6451. array('option' => array('value' => '2007')),
  6452. '2007',
  6453. '/option',
  6454. array('option' => array('value' => '2006')),
  6455. '2006',
  6456. '/option',
  6457. '/select',
  6458. );
  6459. $this->assertTags($result, $expected);
  6460. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6461. $this->assertContains('Contact[published][year]', $result);
  6462. }
  6463. /**
  6464. * testYearAutoExpandRange method
  6465. *
  6466. * @return void
  6467. */
  6468. public function testYearAutoExpandRange() {
  6469. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6470. $result = $this->Form->year('User.birthday');
  6471. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6472. $result = $matches[1];
  6473. $expected = range(date('Y') + 20, 1930);
  6474. $this->assertEquals($expected, $result);
  6475. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6476. $result = $this->Form->year('Project.release');
  6477. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6478. $result = $matches[1];
  6479. $expected = range(2050, date('Y') - 20);
  6480. $this->assertEquals($expected, $result);
  6481. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6482. $result = $this->Form->year('Project.release', 1890, 1900);
  6483. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6484. $result = $matches[1];
  6485. $expected = range(1900, 1881);
  6486. $this->assertEquals($expected, $result);
  6487. }
  6488. /**
  6489. * testInputDate method
  6490. *
  6491. * Test various inputs with type date and different dateFormat values.
  6492. * Failing to provide a dateFormat key should not error.
  6493. * It should simply not pre-select any value then.
  6494. *
  6495. * @return void
  6496. */
  6497. public function testInputDate() {
  6498. $this->Form->request->data = array(
  6499. 'User' => array(
  6500. 'month_year' => array('month' => date('m')),
  6501. 'just_year' => array('month' => date('m')),
  6502. 'just_month' => array('year' => date('Y')),
  6503. 'just_day' => array('month' => date('m')),
  6504. )
  6505. );
  6506. $this->Form->create('User');
  6507. $result = $this->Form->input('month_year',
  6508. array(
  6509. 'label' => false,
  6510. 'div' => false,
  6511. 'type' => 'date',
  6512. 'dateFormat' => 'MY',
  6513. 'minYear' => 2006,
  6514. 'maxYear' => 2008
  6515. )
  6516. );
  6517. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6518. $this->assertNotContains('value="2008" selected="selected"', $result);
  6519. $result = $this->Form->input('just_year',
  6520. array(
  6521. 'type' => 'date',
  6522. 'label' => false,
  6523. 'dateFormat' => 'Y',
  6524. 'minYear' => date('Y'),
  6525. 'maxYear' => date('Y', strtotime('+20 years'))
  6526. )
  6527. );
  6528. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6529. $result = $this->Form->input('just_month',
  6530. array(
  6531. 'type' => 'date',
  6532. 'label' => false,
  6533. 'dateFormat' => 'M',
  6534. 'empty' => false,
  6535. )
  6536. );
  6537. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6538. $result = $this->Form->input('just_day',
  6539. array(
  6540. 'type' => 'date',
  6541. 'label' => false,
  6542. 'dateFormat' => 'D',
  6543. 'empty' => false,
  6544. )
  6545. );
  6546. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6547. }
  6548. /**
  6549. * testInputDateMaxYear method
  6550. *
  6551. * Let's say we want to only allow users born from 2006 to 2008 to register
  6552. * This being the first singup page, we still don't have any data
  6553. *
  6554. * @return void
  6555. */
  6556. public function testInputDateMaxYear() {
  6557. $this->Form->request->data = array();
  6558. $this->Form->create('User');
  6559. $result = $this->Form->input('birthday',
  6560. array(
  6561. 'label' => false,
  6562. 'div' => false,
  6563. 'type' => 'date',
  6564. 'dateFormat' => 'DMY',
  6565. 'minYear' => 2006,
  6566. 'maxYear' => 2008
  6567. )
  6568. );
  6569. $this->assertContains('value="2008" selected="selected"', $result);
  6570. }
  6571. /**
  6572. * testTextArea method
  6573. *
  6574. * @return void
  6575. */
  6576. public function testTextArea() {
  6577. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6578. $result = $this->Form->textarea('Model.field');
  6579. $expected = array(
  6580. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6581. 'some test data',
  6582. '/textarea',
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. $result = $this->Form->textarea('Model.tmp');
  6586. $expected = array(
  6587. 'textarea' => array('name' => 'Model[tmp]', 'id' => 'ModelTmp'),
  6588. '/textarea',
  6589. );
  6590. $this->assertTags($result, $expected);
  6591. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6592. $result = $this->Form->textarea('Model.field');
  6593. $expected = array(
  6594. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6595. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6596. '/textarea',
  6597. );
  6598. $this->assertTags($result, $expected);
  6599. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6600. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6601. $expected = array(
  6602. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6603. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6604. '/textarea',
  6605. );
  6606. $this->assertTags($result, $expected);
  6607. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6608. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6609. $expected = array(
  6610. 'textarea' => array('name' => 'Model[0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6611. '/textarea'
  6612. );
  6613. $this->assertTags($result, $expected);
  6614. }
  6615. /**
  6616. * testTextAreaWithStupidCharacters method
  6617. *
  6618. * test text area with non-ascii characters
  6619. *
  6620. * @return void
  6621. */
  6622. public function testTextAreaWithStupidCharacters() {
  6623. $this->loadFixtures('Post');
  6624. $result = $this->Form->input('Post.content', array(
  6625. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6626. ));
  6627. $expected = array(
  6628. 'div' => array('class' => 'input textarea'),
  6629. 'label' => array('for' => 'PostContent'),
  6630. 'Current Text',
  6631. '/label',
  6632. 'textarea' => array('name' => 'Post[content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6633. 'GREAT®',
  6634. '/textarea',
  6635. '/div'
  6636. );
  6637. $this->assertTags($result, $expected);
  6638. }
  6639. /**
  6640. * testHiddenField method
  6641. *
  6642. * @return void
  6643. */
  6644. public function testHiddenField() {
  6645. $Contact = ClassRegistry::getObject('Contact');
  6646. $Contact->validationErrors['field'] = 1;
  6647. $this->Form->request->data['Contact']['field'] = 'test';
  6648. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6649. $this->assertTags($result, array(
  6650. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'id' => 'theID', 'value' => 'test'))
  6651. );
  6652. }
  6653. /**
  6654. * testFileUploadField method
  6655. *
  6656. * @return void
  6657. */
  6658. public function testFileUploadField() {
  6659. $result = $this->Form->file('Model.upload');
  6660. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6661. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6662. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6663. $expected = array(
  6664. 'div' => array('class' => 'input file'),
  6665. 'label' => array('for' => 'ModelUpload'),
  6666. 'Upload',
  6667. '/label',
  6668. 'input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload'),
  6669. '/div'
  6670. );
  6671. $this->assertTags($result, $expected);
  6672. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6673. $result = $this->Form->file('Model.upload');
  6674. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6675. }
  6676. /**
  6677. * test File upload input on a model not used in create();
  6678. *
  6679. * @return void
  6680. */
  6681. public function testFileUploadOnOtherModel() {
  6682. $this->Form->create('ValidateUser', array('type' => 'file'));
  6683. $result = $this->Form->file('ValidateProfile.city');
  6684. $expected = array(
  6685. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]', 'id' => 'ValidateProfileCity')
  6686. );
  6687. $this->assertTags($result, $expected);
  6688. }
  6689. /**
  6690. * testButton method
  6691. *
  6692. * @return void
  6693. */
  6694. public function testButton() {
  6695. $result = $this->Form->button('Hi');
  6696. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6697. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6698. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6699. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6700. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6701. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6702. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6703. $result = $this->Form->button('No type', array('type' => false));
  6704. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6705. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6706. $this->assertNotRegExp('/\&039/', $result);
  6707. }
  6708. /**
  6709. * Test that button() makes unlocked fields by default.
  6710. *
  6711. * @return void
  6712. */
  6713. public function testButtonUnlockedByDefault() {
  6714. $this->Form->request->params['_csrfToken'] = 'secured';
  6715. $this->Form->button('Save', array('name' => 'save'));
  6716. $this->Form->button('Clear');
  6717. $result = $this->Form->unlockField();
  6718. $this->assertEquals(array('save'), $result);
  6719. }
  6720. /**
  6721. * testPostButton method
  6722. *
  6723. * @return void
  6724. */
  6725. public function testPostButton() {
  6726. $result = $this->Form->postButton('Hi', '/controller/action');
  6727. $this->assertTags($result, array(
  6728. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6729. 'div' => array('style' => 'display:none;'),
  6730. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6731. '/div',
  6732. 'button' => array('type' => 'submit'),
  6733. 'Hi',
  6734. '/button',
  6735. '/form'
  6736. ));
  6737. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6738. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"/>') !== false);
  6739. }
  6740. /**
  6741. * Test that postButton adds _Token fields.
  6742. *
  6743. * @return void
  6744. */
  6745. public function testSecurePostButton() {
  6746. $this->Form->request->params['_csrfToken'] = 'testkey';
  6747. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6748. $expected = array(
  6749. 'form' => array(
  6750. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6751. ),
  6752. array('div' => array('style' => 'display:none;')),
  6753. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6754. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6755. '/div',
  6756. 'button' => array('type' => 'submit'),
  6757. 'Delete',
  6758. '/button',
  6759. array('div' => array('style' => 'display:none;')),
  6760. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6761. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6762. '/div',
  6763. '/form',
  6764. );
  6765. $this->assertTags($result, $expected);
  6766. }
  6767. /**
  6768. * testPostLink method
  6769. *
  6770. * @return void
  6771. */
  6772. public function testPostLink() {
  6773. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6774. $this->assertTags($result, array(
  6775. 'form' => array(
  6776. 'method' => 'post', 'action' => '/posts/delete/1',
  6777. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6778. ),
  6779. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6780. '/form',
  6781. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6782. 'Delete',
  6783. '/a'
  6784. ));
  6785. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6786. $this->assertTags($result, array(
  6787. 'form' => array(
  6788. 'method' => 'post', 'action' => '/posts/delete/1',
  6789. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6790. ),
  6791. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6792. '/form',
  6793. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6794. 'Delete',
  6795. '/a'
  6796. ));
  6797. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6798. $this->assertTags($result, array(
  6799. 'form' => array(
  6800. 'method' => 'post', 'action' => '/posts/delete/1',
  6801. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6802. ),
  6803. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6804. '/form',
  6805. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6806. 'Delete',
  6807. '/a'
  6808. ));
  6809. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6810. $this->assertTags($result, array(
  6811. 'form' => array(
  6812. 'method' => 'post', 'action' => '/posts/delete/1',
  6813. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6814. ),
  6815. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6816. '/form',
  6817. '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;/'),
  6818. 'Delete',
  6819. '/a'
  6820. ));
  6821. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6822. $this->assertContains('<input type="hidden" name="id" value="1"/>', $result);
  6823. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6824. $this->assertTags($result, array(
  6825. 'form' => array(
  6826. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6827. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6828. ),
  6829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6830. '/form',
  6831. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6832. 'Delete',
  6833. '/a'
  6834. ));
  6835. $result = $this->Form->postLink(
  6836. '',
  6837. array('controller' => 'items', 'action' => 'delete', 10),
  6838. array('class' => 'btn btn-danger', 'escape' => false),
  6839. 'Confirm thing'
  6840. );
  6841. $this->assertTags($result, array(
  6842. 'form' => array(
  6843. 'method' => 'post', 'action' => '/items/delete/10',
  6844. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6845. ),
  6846. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6847. '/form',
  6848. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6849. '/a'
  6850. ));
  6851. }
  6852. /**
  6853. * test creating postLinks after a GET form.
  6854. *
  6855. * @return void
  6856. */
  6857. public function testPostLinkAfterGetForm() {
  6858. $this->Form->request->params['_Token']['key'] = 'testkey';
  6859. $this->Form->create('User', array('type' => 'get'));
  6860. $this->Form->end();
  6861. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6862. $this->assertTags($result, array(
  6863. 'form' => array(
  6864. 'method' => 'post', 'action' => '/posts/delete/1',
  6865. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6866. ),
  6867. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6868. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6869. 'div' => array('style' => 'display:none;'),
  6870. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6871. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6872. '/div',
  6873. '/form',
  6874. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6875. 'Delete',
  6876. '/a'
  6877. ));
  6878. }
  6879. /**
  6880. * Test that postLink adds _Token fields.
  6881. *
  6882. * @return void
  6883. */
  6884. public function testSecurePostLink() {
  6885. $this->Form->request->params['_csrfToken'] = 'testkey';
  6886. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6887. $expected = array(
  6888. 'form' => array(
  6889. 'method' => 'post', 'action' => '/posts/delete/1',
  6890. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6891. ),
  6892. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6893. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6894. 'div' => array('style' => 'display:none;'),
  6895. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6896. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6897. '/div',
  6898. '/form',
  6899. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6900. 'Delete',
  6901. '/a'
  6902. );
  6903. $this->assertTags($result, $expected);
  6904. }
  6905. /**
  6906. * Test that postLink adds form tags to view block
  6907. *
  6908. * @return void
  6909. */
  6910. public function testPostLinkFormBuffer() {
  6911. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  6912. $this->assertTags($result, array(
  6913. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6914. 'Delete',
  6915. '/a'
  6916. ));
  6917. $result = $this->View->fetch('postLink');
  6918. $this->assertTags($result, array(
  6919. 'form' => array(
  6920. 'method' => 'post', 'action' => '/posts/delete/1',
  6921. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6922. ),
  6923. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6924. '/form'
  6925. ));
  6926. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  6927. array('inline' => false, 'method' => 'DELETE')
  6928. );
  6929. $this->assertTags($result, array(
  6930. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6931. 'Delete',
  6932. '/a'
  6933. ));
  6934. $result = $this->View->fetch('postLink');
  6935. $this->assertTags($result, array(
  6936. 'form' => array(
  6937. 'method' => 'post', 'action' => '/posts/delete/1',
  6938. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6939. ),
  6940. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6941. '/form',
  6942. array(
  6943. 'form' => array(
  6944. 'method' => 'post', 'action' => '/posts/delete/2',
  6945. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6946. ),
  6947. ),
  6948. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  6949. '/form'
  6950. ));
  6951. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  6952. $this->assertTags($result, array(
  6953. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6954. 'Delete',
  6955. '/a'
  6956. ));
  6957. $result = $this->View->fetch('foobar');
  6958. $this->assertTags($result, array(
  6959. 'form' => array(
  6960. 'method' => 'post', 'action' => '/posts/delete/1',
  6961. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6962. ),
  6963. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6964. '/form'
  6965. ));
  6966. }
  6967. /**
  6968. * testSubmitButton method
  6969. *
  6970. * @return void
  6971. */
  6972. public function testSubmitButton() {
  6973. $result = $this->Form->submit('');
  6974. $expected = array(
  6975. 'div' => array('class' => 'submit'),
  6976. 'input' => array('type' => 'submit', 'value' => ''),
  6977. '/div'
  6978. );
  6979. $this->assertTags($result, $expected);
  6980. $result = $this->Form->submit('Test Submit');
  6981. $expected = array(
  6982. 'div' => array('class' => 'submit'),
  6983. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6984. '/div'
  6985. );
  6986. $this->assertTags($result, $expected);
  6987. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6988. $expected = array(
  6989. 'span' => array('class' => 'submit'),
  6990. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6991. '/span'
  6992. );
  6993. $this->assertTags($result, $expected);
  6994. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6995. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6996. $this->assertTags($result, $expected);
  6997. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6998. $expected = array(
  6999. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  7000. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7001. '/div'
  7002. );
  7003. $this->assertTags($result, $expected);
  7004. $result = $this->Form->submit('Next >');
  7005. $expected = array(
  7006. 'div' => array('class' => 'submit'),
  7007. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  7008. '/div'
  7009. );
  7010. $this->assertTags($result, $expected);
  7011. $result = $this->Form->submit('Next >', array('escape' => false));
  7012. $expected = array(
  7013. 'div' => array('class' => 'submit'),
  7014. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  7015. '/div'
  7016. );
  7017. $this->assertTags($result, $expected);
  7018. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7019. $expected = array(
  7020. 'div' => array('class' => 'submit'),
  7021. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7022. '/div'
  7023. );
  7024. $this->assertTags($result, $expected);
  7025. $before = '--before--';
  7026. $after = '--after--';
  7027. $result = $this->Form->submit('Test', array('before' => $before));
  7028. $expected = array(
  7029. 'div' => array('class' => 'submit'),
  7030. '--before--',
  7031. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7032. '/div'
  7033. );
  7034. $this->assertTags($result, $expected);
  7035. $result = $this->Form->submit('Test', array('after' => $after));
  7036. $expected = array(
  7037. 'div' => array('class' => 'submit'),
  7038. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7039. '--after--',
  7040. '/div'
  7041. );
  7042. $this->assertTags($result, $expected);
  7043. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7044. $expected = array(
  7045. 'div' => array('class' => 'submit'),
  7046. '--before--',
  7047. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7048. '--after--',
  7049. '/div'
  7050. );
  7051. $this->assertTags($result, $expected);
  7052. }
  7053. /**
  7054. * test image submit types.
  7055. *
  7056. * @return void
  7057. */
  7058. public function testSubmitImage() {
  7059. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7060. $expected = array(
  7061. 'div' => array('class' => 'submit'),
  7062. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7063. '/div'
  7064. );
  7065. $this->assertTags($result, $expected);
  7066. $result = $this->Form->submit('/relative/cake.power.gif');
  7067. $expected = array(
  7068. 'div' => array('class' => 'submit'),
  7069. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7070. '/div'
  7071. );
  7072. $this->assertTags($result, $expected);
  7073. $result = $this->Form->submit('cake.power.gif');
  7074. $expected = array(
  7075. 'div' => array('class' => 'submit'),
  7076. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7077. '/div'
  7078. );
  7079. $this->assertTags($result, $expected);
  7080. $result = $this->Form->submit('Not.an.image');
  7081. $expected = array(
  7082. 'div' => array('class' => 'submit'),
  7083. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7084. '/div'
  7085. );
  7086. $this->assertTags($result, $expected);
  7087. $after = '--after--';
  7088. $before = '--before--';
  7089. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7090. $expected = array(
  7091. 'div' => array('class' => 'submit'),
  7092. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7093. '--after--',
  7094. '/div'
  7095. );
  7096. $this->assertTags($result, $expected);
  7097. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7098. $expected = array(
  7099. 'div' => array('class' => 'submit'),
  7100. '--before--',
  7101. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7102. '/div'
  7103. );
  7104. $this->assertTags($result, $expected);
  7105. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7106. $expected = array(
  7107. 'div' => array('class' => 'submit'),
  7108. '--before--',
  7109. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7110. '--after--',
  7111. '/div'
  7112. );
  7113. $this->assertTags($result, $expected);
  7114. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7115. $expected = array(
  7116. 'div' => array('class' => 'submit'),
  7117. '--before--',
  7118. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7119. '--after--',
  7120. '/div'
  7121. );
  7122. $this->assertTags($result, $expected);
  7123. }
  7124. /**
  7125. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7126. * be submitted at a time.
  7127. *
  7128. * @return void
  7129. */
  7130. public function testSubmitUnlockedByDefault() {
  7131. $this->Form->request->params['_csrfToken'] = 'secured';
  7132. $this->Form->submit('Go go');
  7133. $this->Form->submit('Save', array('name' => 'save'));
  7134. $result = $this->Form->unlockField();
  7135. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7136. }
  7137. /**
  7138. * Test submit image with timestamps.
  7139. *
  7140. * @return void
  7141. */
  7142. public function testSubmitImageTimestamp() {
  7143. Configure::write('Asset.timestamp', 'force');
  7144. $result = $this->Form->submit('cake.power.gif');
  7145. $expected = array(
  7146. 'div' => array('class' => 'submit'),
  7147. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7148. '/div'
  7149. );
  7150. $this->assertTags($result, $expected);
  7151. }
  7152. /**
  7153. * test the create() method
  7154. *
  7155. * @return void
  7156. */
  7157. public function testCreate() {
  7158. $result = $this->Form->create('Contact');
  7159. $encoding = strtolower(Configure::read('App.encoding'));
  7160. $expected = array(
  7161. 'form' => array(
  7162. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7163. 'accept-charset' => $encoding
  7164. ),
  7165. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7166. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7167. '/div'
  7168. );
  7169. $this->assertTags($result, $expected);
  7170. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7171. $expected = array('form' => array(
  7172. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7173. 'accept-charset' => $encoding
  7174. ));
  7175. $this->assertTags($result, $expected);
  7176. $result = $this->Form->create('Contact', array('type' => 'get'));
  7177. $expected = array('form' => array(
  7178. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7179. 'accept-charset' => $encoding
  7180. ));
  7181. $this->assertTags($result, $expected);
  7182. $result = $this->Form->create('Contact', array('type' => 'put'));
  7183. $expected = array(
  7184. 'form' => array(
  7185. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7186. 'accept-charset' => $encoding
  7187. ),
  7188. 'div' => array('style' => 'display:none;'),
  7189. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7190. '/div'
  7191. );
  7192. $this->assertTags($result, $expected);
  7193. $result = $this->Form->create('Contact', array('type' => 'file'));
  7194. $expected = array(
  7195. 'form' => array(
  7196. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7197. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7198. ),
  7199. 'div' => array('style' => 'display:none;'),
  7200. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7201. '/div'
  7202. );
  7203. $this->assertTags($result, $expected);
  7204. $this->Form->request->data['Contact']['id'] = 1;
  7205. $this->Form->request->here = '/contacts/edit/1';
  7206. $this->Form->request['action'] = 'edit';
  7207. $result = $this->Form->create('Contact');
  7208. $expected = array(
  7209. 'form' => array(
  7210. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7211. 'accept-charset' => $encoding
  7212. ),
  7213. 'div' => array('style' => 'display:none;'),
  7214. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7215. '/div'
  7216. );
  7217. $this->assertTags($result, $expected);
  7218. $this->Form->request->data['Contact']['id'] = 1;
  7219. $this->Form->request->here = '/contacts/edit/1';
  7220. $this->Form->request['action'] = 'edit';
  7221. $result = $this->Form->create('Contact', array('type' => 'file'));
  7222. $expected = array(
  7223. 'form' => array(
  7224. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7225. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7226. ),
  7227. 'div' => array('style' => 'display:none;'),
  7228. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7229. '/div'
  7230. );
  7231. $this->assertTags($result, $expected);
  7232. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7233. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7234. $expected = array(
  7235. 'form' => array(
  7236. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7237. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7238. ),
  7239. 'div' => array('style' => 'display:none;'),
  7240. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7241. '/div'
  7242. );
  7243. $this->assertTags($result, $expected);
  7244. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7245. $expected = array(
  7246. 'form' => array(
  7247. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7248. 'accept-charset' => $encoding
  7249. ),
  7250. 'div' => array('style' => 'display:none;'),
  7251. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7252. '/div'
  7253. );
  7254. $this->assertTags($result, $expected);
  7255. $this->Form->request['action'] = 'add';
  7256. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7257. $expected = array(
  7258. 'form' => array(
  7259. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7260. 'accept-charset' => $encoding
  7261. ),
  7262. 'div' => array('style' => 'display:none;'),
  7263. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7264. '/div'
  7265. );
  7266. $this->assertTags($result, $expected);
  7267. $result = $this->Form->create('User', array('action' => 'login'));
  7268. $expected = array(
  7269. 'form' => array(
  7270. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7271. 'accept-charset' => $encoding
  7272. ),
  7273. 'div' => array('style' => 'display:none;'),
  7274. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7275. '/div'
  7276. );
  7277. $this->assertTags($result, $expected);
  7278. $result = $this->Form->create('User', array('url' => '/users/login'));
  7279. $expected = array(
  7280. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7281. 'div' => array('style' => 'display:none;'),
  7282. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7283. '/div'
  7284. );
  7285. $this->assertTags($result, $expected);
  7286. $this->Form->request['controller'] = 'pages';
  7287. $result = $this->Form->create('User', array('action' => 'signup'));
  7288. $expected = array(
  7289. 'form' => array(
  7290. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7291. 'accept-charset' => $encoding
  7292. ),
  7293. 'div' => array('style' => 'display:none;'),
  7294. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7295. '/div'
  7296. );
  7297. $this->assertTags($result, $expected);
  7298. $this->Form->request->data = array();
  7299. $this->Form->request['controller'] = 'contacts';
  7300. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7301. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7302. $expected = array(
  7303. 'form' => array(
  7304. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7305. 'accept-charset' => 'utf-8'
  7306. ),
  7307. 'div' => array('style' => 'display:none;'),
  7308. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7309. '/div'
  7310. );
  7311. $this->assertTags($result, $expected);
  7312. }
  7313. /**
  7314. * Test the onsubmit option for create()
  7315. *
  7316. * @return void
  7317. */
  7318. public function testCreateOnSubmit() {
  7319. $this->Form->request->data = array();
  7320. $this->Form->request['controller'] = 'contacts';
  7321. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7322. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7323. $expected = array(
  7324. 'form' => array(
  7325. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7326. 'accept-charset' => 'utf-8'
  7327. ),
  7328. 'div' => array('style' => 'display:none;'),
  7329. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7330. '/div'
  7331. );
  7332. $this->assertTags($result, $expected);
  7333. $this->Form->request->data = array();
  7334. $this->Form->request['controller'] = 'contacts';
  7335. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7336. $result = $this->Form->create(array(
  7337. 'url' => array('action' => 'index', 'param'),
  7338. 'default' => false,
  7339. 'onsubmit' => 'someFunction();'
  7340. ));
  7341. $expected = array(
  7342. 'form' => array(
  7343. 'id' => 'ContactAddForm', 'method' => 'post',
  7344. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7345. 'action' => '/contacts/index/param',
  7346. 'accept-charset' => 'utf-8'
  7347. ),
  7348. 'div' => array('style' => 'display:none;'),
  7349. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7350. '/div'
  7351. );
  7352. $this->assertTags($result, $expected);
  7353. }
  7354. /**
  7355. * test create() with automatic url generation
  7356. *
  7357. * @return void
  7358. */
  7359. public function testCreateAutoUrl() {
  7360. $this->Form->request['action'] = 'delete';
  7361. $this->Form->request->here = '/contacts/delete/10';
  7362. $this->Form->request->base = '';
  7363. $result = $this->Form->create('Contact');
  7364. $expected = array(
  7365. 'form' => array(
  7366. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10',
  7367. 'accept-charset' => 'utf-8'
  7368. ),
  7369. 'div' => array('style' => 'display:none;'),
  7370. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7371. '/div'
  7372. );
  7373. $this->assertTags($result, $expected);
  7374. }
  7375. /**
  7376. * test create() with a custom route
  7377. *
  7378. * @return void
  7379. */
  7380. public function testCreateCustomRoute() {
  7381. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7382. $encoding = strtolower(Configure::read('App.encoding'));
  7383. $result = $this->Form->create('User', array('action' => 'login'));
  7384. $expected = array(
  7385. 'form' => array(
  7386. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7387. 'accept-charset' => $encoding
  7388. ),
  7389. 'div' => array('style' => 'display:none;'),
  7390. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7391. '/div'
  7392. );
  7393. $this->assertTags($result, $expected);
  7394. }
  7395. /**
  7396. * test that inputDefaults are stored and used.
  7397. *
  7398. * @return void
  7399. */
  7400. public function testCreateWithInputDefaults() {
  7401. $this->Form->create('User', array(
  7402. 'inputDefaults' => array(
  7403. 'div' => false,
  7404. 'label' => false,
  7405. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7406. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7407. )
  7408. ));
  7409. $result = $this->Form->input('username');
  7410. $expected = array(
  7411. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  7412. );
  7413. $this->assertTags($result, $expected);
  7414. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7415. $expected = array(
  7416. 'div' => array('class' => 'input text'),
  7417. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7418. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7419. '/div'
  7420. );
  7421. $this->assertTags($result, $expected);
  7422. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7423. $expected = array(
  7424. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7425. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7426. );
  7427. $this->assertTags($result, $expected);
  7428. $this->Form->create('User', array(
  7429. 'inputDefaults' => array(
  7430. 'div' => false,
  7431. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7432. )
  7433. ));
  7434. $result = $this->Form->input('username', array('div' => true));
  7435. $expected = array(
  7436. 'div' => array('class' => 'input text'),
  7437. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7438. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7439. '/div'
  7440. );
  7441. $this->assertTags($result, $expected);
  7442. }
  7443. /**
  7444. * test automatic accept-charset overriding
  7445. *
  7446. * @return void
  7447. */
  7448. public function testCreateWithAcceptCharset() {
  7449. $result = $this->Form->create('UserForm', array(
  7450. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7451. )
  7452. );
  7453. $expected = array(
  7454. 'form' => array(
  7455. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7456. 'accept-charset' => 'iso-8859-1'
  7457. ),
  7458. 'div' => array('style' => 'display:none;'),
  7459. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7460. '/div'
  7461. );
  7462. $this->assertTags($result, $expected);
  7463. }
  7464. /**
  7465. * Test base form URL when url param is passed with multiple parameters (&)
  7466. *
  7467. */
  7468. public function testCreateQuerystringrequest() {
  7469. $encoding = strtolower(Configure::read('App.encoding'));
  7470. $result = $this->Form->create('Contact', array(
  7471. 'type' => 'post',
  7472. 'escape' => false,
  7473. 'url' => array(
  7474. 'controller' => 'controller',
  7475. 'action' => 'action',
  7476. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7477. )
  7478. ));
  7479. $expected = array(
  7480. 'form' => array(
  7481. 'id' => 'ContactAddForm',
  7482. 'method' => 'post',
  7483. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7484. 'accept-charset' => $encoding
  7485. ),
  7486. 'div' => array('style' => 'display:none;'),
  7487. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7488. '/div'
  7489. );
  7490. $this->assertTags($result, $expected);
  7491. $result = $this->Form->create('Contact', array(
  7492. 'type' => 'post',
  7493. 'url' => array(
  7494. 'controller' => 'controller',
  7495. 'action' => 'action',
  7496. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7497. )
  7498. ));
  7499. $expected = array(
  7500. 'form' => array(
  7501. 'id' => 'ContactAddForm',
  7502. 'method' => 'post',
  7503. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7504. 'accept-charset' => $encoding
  7505. ),
  7506. 'div' => array('style' => 'display:none;'),
  7507. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7508. '/div'
  7509. );
  7510. $this->assertTags($result, $expected);
  7511. }
  7512. /**
  7513. * test that create() doesn't cause errors by multiple id's being in the primary key
  7514. * as could happen with multiple select or checkboxes.
  7515. *
  7516. * @return void
  7517. */
  7518. public function testCreateWithMultipleIdInData() {
  7519. $encoding = strtolower(Configure::read('App.encoding'));
  7520. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7521. $result = $this->Form->create('Contact');
  7522. $expected = array(
  7523. 'form' => array(
  7524. 'id' => 'ContactAddForm',
  7525. 'method' => 'post',
  7526. 'action' => '/contacts/add',
  7527. 'accept-charset' => $encoding
  7528. ),
  7529. 'div' => array('style' => 'display:none;'),
  7530. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7531. '/div'
  7532. );
  7533. $this->assertTags($result, $expected);
  7534. }
  7535. /**
  7536. * test that create() doesn't add in extra passed params.
  7537. *
  7538. * @return void
  7539. */
  7540. public function testCreatePassedArgs() {
  7541. $encoding = strtolower(Configure::read('App.encoding'));
  7542. $this->Form->request->data['Contact']['id'] = 1;
  7543. $result = $this->Form->create('Contact', array(
  7544. 'type' => 'post',
  7545. 'escape' => false,
  7546. 'url' => array(
  7547. 'action' => 'edit',
  7548. 'myparam'
  7549. )
  7550. ));
  7551. $expected = array(
  7552. 'form' => array(
  7553. 'id' => 'ContactAddForm',
  7554. 'method' => 'post',
  7555. 'action' => '/contacts/edit/myparam',
  7556. 'accept-charset' => $encoding
  7557. ),
  7558. 'div' => array('style' => 'display:none;'),
  7559. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7560. '/div'
  7561. );
  7562. $this->assertTags($result, $expected);
  7563. }
  7564. /**
  7565. * test creating a get form, and get form inputs.
  7566. *
  7567. * @return void
  7568. */
  7569. public function testGetFormCreate() {
  7570. $encoding = strtolower(Configure::read('App.encoding'));
  7571. $result = $this->Form->create('Contact', array('type' => 'get'));
  7572. $this->assertTags($result, array('form' => array(
  7573. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7574. 'accept-charset' => $encoding
  7575. )));
  7576. $result = $this->Form->text('Contact.name');
  7577. $this->assertTags($result, array('input' => array(
  7578. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7579. )));
  7580. $result = $this->Form->password('password');
  7581. $this->assertTags($result, array('input' => array(
  7582. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7583. )));
  7584. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7585. $result = $this->Form->text('user_form');
  7586. $this->assertTags($result, array('input' => array(
  7587. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7588. )));
  7589. }
  7590. /**
  7591. * test get form, and inputs when the model param is false
  7592. *
  7593. * @return void
  7594. */
  7595. public function testGetFormWithFalseModel() {
  7596. $encoding = strtolower(Configure::read('App.encoding'));
  7597. $this->Form->request['controller'] = 'contact_test';
  7598. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7599. $expected = array('form' => array(
  7600. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7601. 'accept-charset' => $encoding
  7602. ));
  7603. $this->assertTags($result, $expected);
  7604. $result = $this->Form->text('reason');
  7605. $expected = array(
  7606. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7607. );
  7608. $this->assertTags($result, $expected);
  7609. }
  7610. /**
  7611. * test that datetime() works with GET style forms.
  7612. *
  7613. * @return void
  7614. */
  7615. public function testDateTimeWithGetForms() {
  7616. extract($this->dateRegex);
  7617. $this->Form->create('Contact', array('type' => 'get'));
  7618. $result = $this->Form->datetime('created');
  7619. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7620. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7621. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7622. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7623. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7624. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7625. }
  7626. /**
  7627. * testEditFormWithData method
  7628. *
  7629. * test auto populating form elements from submitted data.
  7630. *
  7631. * @return void
  7632. */
  7633. public function testEditFormWithData() {
  7634. $this->Form->request->data = array('Person' => array(
  7635. 'id' => 1,
  7636. 'first_name' => 'Nate',
  7637. 'last_name' => 'Abele',
  7638. 'email' => 'nate@example.com'
  7639. ));
  7640. $this->Form->request->addParams(array(
  7641. 'models' => array('Person'),
  7642. 'controller' => 'people',
  7643. 'action' => 'add'
  7644. ));
  7645. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7646. $this->Form->create();
  7647. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7648. $expected = array(
  7649. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7650. 'select' => array(
  7651. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7652. ),
  7653. array('option' => array('value' => 1)), 'Nate', '/option',
  7654. array('option' => array('value' => 2)), 'Garrett', '/option',
  7655. array('option' => array('value' => 3)), 'Larry', '/option',
  7656. '/select'
  7657. );
  7658. $this->assertTags($result, $expected);
  7659. }
  7660. /**
  7661. * Test that required fields are created for various types of validation.
  7662. *
  7663. * @return void
  7664. */
  7665. public function testFormInputRequiredDetection() {
  7666. $this->Form->create('Contact');
  7667. $result = $this->Form->input('Contact.non_existing');
  7668. $expected = array(
  7669. 'div' => array('class' => 'input text'),
  7670. 'label' => array('for' => 'ContactNonExisting'),
  7671. 'Non Existing',
  7672. '/label',
  7673. 'input' => array(
  7674. 'type' => 'text', 'name' => 'Contact[non_existing]',
  7675. 'id' => 'ContactNonExisting'
  7676. ),
  7677. '/div'
  7678. );
  7679. $this->assertTags($result, $expected);
  7680. $result = $this->Form->input('Contact.imrequired');
  7681. $expected = array(
  7682. 'div' => array('class' => 'input text required'),
  7683. 'label' => array('for' => 'ContactImrequired'),
  7684. 'Imrequired',
  7685. '/label',
  7686. 'input' => array(
  7687. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7688. 'id' => 'ContactImrequired',
  7689. 'required' => 'required'
  7690. ),
  7691. '/div'
  7692. );
  7693. $this->assertTags($result, $expected);
  7694. $result = $this->Form->input('Contact.imalsorequired');
  7695. $expected = array(
  7696. 'div' => array('class' => 'input text required'),
  7697. 'label' => array('for' => 'ContactImalsorequired'),
  7698. 'Imalsorequired',
  7699. '/label',
  7700. 'input' => array(
  7701. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  7702. 'id' => 'ContactImalsorequired',
  7703. 'required' => 'required'
  7704. ),
  7705. '/div'
  7706. );
  7707. $this->assertTags($result, $expected);
  7708. $result = $this->Form->input('Contact.imrequiredtoo');
  7709. $expected = array(
  7710. 'div' => array('class' => 'input text required'),
  7711. 'label' => array('for' => 'ContactImrequiredtoo'),
  7712. 'Imrequiredtoo',
  7713. '/label',
  7714. 'input' => array(
  7715. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  7716. 'id' => 'ContactImrequiredtoo',
  7717. 'required' => 'required'
  7718. ),
  7719. '/div'
  7720. );
  7721. $this->assertTags($result, $expected);
  7722. $result = $this->Form->input('Contact.required_one');
  7723. $expected = array(
  7724. 'div' => array('class' => 'input text required'),
  7725. 'label' => array('for' => 'ContactRequiredOne'),
  7726. 'Required One',
  7727. '/label',
  7728. 'input' => array(
  7729. 'type' => 'text', 'name' => 'Contact[required_one]',
  7730. 'id' => 'ContactRequiredOne',
  7731. 'required' => 'required'
  7732. ),
  7733. '/div'
  7734. );
  7735. $this->assertTags($result, $expected);
  7736. $result = $this->Form->input('Contact.string_required');
  7737. $expected = array(
  7738. 'div' => array('class' => 'input text required'),
  7739. 'label' => array('for' => 'ContactStringRequired'),
  7740. 'String Required',
  7741. '/label',
  7742. 'input' => array(
  7743. 'type' => 'text', 'name' => 'Contact[string_required]',
  7744. 'id' => 'ContactStringRequired',
  7745. 'required' => 'required'
  7746. ),
  7747. '/div'
  7748. );
  7749. $this->assertTags($result, $expected);
  7750. $result = $this->Form->input('Contact.imnotrequired');
  7751. $expected = array(
  7752. 'div' => array('class' => 'input text'),
  7753. 'label' => array('for' => 'ContactImnotrequired'),
  7754. 'Imnotrequired',
  7755. '/label',
  7756. 'input' => array(
  7757. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  7758. 'id' => 'ContactImnotrequired'
  7759. ),
  7760. '/div'
  7761. );
  7762. $this->assertTags($result, $expected);
  7763. $result = $this->Form->input('Contact.imalsonotrequired');
  7764. $expected = array(
  7765. 'div' => array('class' => 'input text'),
  7766. 'label' => array('for' => 'ContactImalsonotrequired'),
  7767. 'Imalsonotrequired',
  7768. '/label',
  7769. 'input' => array(
  7770. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  7771. 'id' => 'ContactImalsonotrequired'
  7772. ),
  7773. '/div'
  7774. );
  7775. $this->assertTags($result, $expected);
  7776. $result = $this->Form->input('Contact.imalsonotrequired2');
  7777. $expected = array(
  7778. 'div' => array('class' => 'input text'),
  7779. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7780. 'Imalsonotrequired2',
  7781. '/label',
  7782. 'input' => array(
  7783. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  7784. 'id' => 'ContactImalsonotrequired2'
  7785. ),
  7786. '/div'
  7787. );
  7788. $this->assertTags($result, $expected);
  7789. $result = $this->Form->input('Contact.imnotrequiredeither');
  7790. $expected = array(
  7791. 'div' => array('class' => 'input text'),
  7792. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7793. 'Imnotrequiredeither',
  7794. '/label',
  7795. 'input' => array(
  7796. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  7797. 'id' => 'ContactImnotrequiredeither'
  7798. ),
  7799. '/div'
  7800. );
  7801. $this->assertTags($result, $expected);
  7802. $result = $this->Form->input('Contact.iamrequiredalways');
  7803. $expected = array(
  7804. 'div' => array('class' => 'input text required'),
  7805. 'label' => array('for' => 'ContactIamrequiredalways'),
  7806. 'Iamrequiredalways',
  7807. '/label',
  7808. 'input' => array(
  7809. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  7810. 'id' => 'ContactIamrequiredalways',
  7811. 'required' => 'required'
  7812. ),
  7813. '/div'
  7814. );
  7815. $this->assertTags($result, $expected);
  7816. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7817. $expected = array(
  7818. 'div' => array('class' => 'input checkbox required'),
  7819. array('input' => array(
  7820. 'type' => 'hidden',
  7821. 'name' => 'Contact[boolean_field]',
  7822. 'id' => 'ContactBooleanField_',
  7823. 'value' => '0'
  7824. )),
  7825. array('input' => array(
  7826. 'type' => 'checkbox',
  7827. 'name' => 'Contact[boolean_field]',
  7828. 'value' => '1',
  7829. 'id' => 'ContactBooleanField'
  7830. )),
  7831. 'label' => array('for' => 'ContactBooleanField'),
  7832. 'Boolean Field',
  7833. '/label',
  7834. '/div'
  7835. );
  7836. $this->assertTags($result, $expected);
  7837. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7838. $expected = array(
  7839. 'div' => array('class' => 'input checkbox required'),
  7840. array('input' => array(
  7841. 'type' => 'hidden',
  7842. 'name' => 'Contact[boolean_field]',
  7843. 'id' => 'ContactBooleanField_',
  7844. 'value' => '0'
  7845. )),
  7846. array('input' => array(
  7847. 'type' => 'checkbox',
  7848. 'name' => 'Contact[boolean_field]',
  7849. 'value' => '1',
  7850. 'id' => 'ContactBooleanField',
  7851. 'required' => 'required'
  7852. )),
  7853. 'label' => array('for' => 'ContactBooleanField'),
  7854. 'Boolean Field',
  7855. '/label',
  7856. '/div'
  7857. );
  7858. $this->assertTags($result, $expected);
  7859. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7860. $expected = array(
  7861. 'div' => array('class' => 'input file required'),
  7862. 'label' => array('for' => 'ContactIamrequiredalways'),
  7863. 'Iamrequiredalways',
  7864. '/label',
  7865. 'input' => array(
  7866. 'type' => 'file',
  7867. 'name' => 'data[Contact][iamrequiredalways]',
  7868. 'id' => 'ContactIamrequiredalways',
  7869. 'required' => 'required'
  7870. ),
  7871. '/div'
  7872. );
  7873. $this->assertTags($result, $expected);
  7874. }
  7875. /**
  7876. * Test that required fields are created when only using ModelValidator::add().
  7877. *
  7878. * @return void
  7879. */
  7880. public function testFormInputRequiredDetectionModelValidator() {
  7881. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7882. $this->Form->create('ContactTag');
  7883. $result = $this->Form->input('ContactTag.iwillberequired');
  7884. $expected = array(
  7885. 'div' => array('class' => 'input text required'),
  7886. 'label' => array('for' => 'ContactTagIwillberequired'),
  7887. 'Iwillberequired',
  7888. '/label',
  7889. 'input' => array(
  7890. 'name' => 'data[ContactTag][iwillberequired]',
  7891. 'type' => 'text',
  7892. 'id' => 'ContactTagIwillberequired',
  7893. 'required' => 'required'
  7894. ),
  7895. '/div'
  7896. );
  7897. $this->assertTags($result, $expected);
  7898. }
  7899. /**
  7900. * testFormMagicInput method
  7901. *
  7902. * @return void
  7903. */
  7904. public function testFormMagicInput() {
  7905. $encoding = strtolower(Configure::read('App.encoding'));
  7906. $result = $this->Form->create('Contact');
  7907. $expected = array(
  7908. 'form' => array(
  7909. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7910. 'accept-charset' => $encoding
  7911. ),
  7912. 'div' => array('style' => 'display:none;'),
  7913. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7914. '/div'
  7915. );
  7916. $this->assertTags($result, $expected);
  7917. $result = $this->Form->input('name');
  7918. $expected = array(
  7919. 'div' => array('class' => 'input text'),
  7920. 'label' => array('for' => 'ContactName'),
  7921. 'Name',
  7922. '/label',
  7923. 'input' => array(
  7924. 'type' => 'text', 'name' => 'Contact[name]',
  7925. 'id' => 'ContactName', 'maxlength' => '255'
  7926. ),
  7927. '/div'
  7928. );
  7929. $this->assertTags($result, $expected);
  7930. $result = $this->Form->input('non_existing_field_in_contact_model');
  7931. $expected = array(
  7932. 'div' => array('class' => 'input text'),
  7933. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7934. 'Non Existing Field In Contact Model',
  7935. '/label',
  7936. 'input' => array(
  7937. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  7938. 'id' => 'ContactNonExistingFieldInContactModel'
  7939. ),
  7940. '/div'
  7941. );
  7942. $this->assertTags($result, $expected);
  7943. $result = $this->Form->input('Address.street');
  7944. $expected = array(
  7945. 'div' => array('class' => 'input text'),
  7946. 'label' => array('for' => 'AddressStreet'),
  7947. 'Street',
  7948. '/label',
  7949. 'input' => array(
  7950. 'type' => 'text', 'name' => 'Address[street]',
  7951. 'id' => 'AddressStreet'
  7952. ),
  7953. '/div'
  7954. );
  7955. $this->assertTags($result, $expected);
  7956. $result = $this->Form->input('Address.non_existing_field_in_model');
  7957. $expected = array(
  7958. 'div' => array('class' => 'input text'),
  7959. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7960. 'Non Existing Field In Model',
  7961. '/label',
  7962. 'input' => array(
  7963. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  7964. 'id' => 'AddressNonExistingFieldInModel'
  7965. ),
  7966. '/div'
  7967. );
  7968. $this->assertTags($result, $expected);
  7969. $result = $this->Form->input('name', array('div' => false));
  7970. $expected = array(
  7971. 'label' => array('for' => 'ContactName'),
  7972. 'Name',
  7973. '/label',
  7974. 'input' => array(
  7975. 'type' => 'text', 'name' => 'Contact[name]',
  7976. 'id' => 'ContactName', 'maxlength' => '255'
  7977. )
  7978. );
  7979. $this->assertTags($result, $expected);
  7980. extract($this->dateRegex);
  7981. $now = strtotime('now');
  7982. $result = $this->Form->input('Contact.published', array('div' => false));
  7983. $expected = array(
  7984. 'label' => array('for' => 'ContactPublishedMonth'),
  7985. 'Published',
  7986. '/label',
  7987. array('select' => array(
  7988. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  7989. )),
  7990. $monthsRegex,
  7991. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7992. date('F', $now),
  7993. '/option',
  7994. '*/select',
  7995. '-',
  7996. array('select' => array(
  7997. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  7998. )),
  7999. $daysRegex,
  8000. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8001. date('j', $now),
  8002. '/option',
  8003. '*/select',
  8004. '-',
  8005. array('select' => array(
  8006. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  8007. )),
  8008. $yearsRegex,
  8009. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8010. date('Y', $now),
  8011. '*/select'
  8012. );
  8013. $this->assertTags($result, $expected);
  8014. $result = $this->Form->input('Contact.updated', array('div' => false));
  8015. $expected = array(
  8016. 'label' => array('for' => 'ContactUpdatedMonth'),
  8017. 'Updated',
  8018. '/label',
  8019. array('select' => array(
  8020. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  8021. )),
  8022. $monthsRegex,
  8023. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8024. date('F', $now),
  8025. '/option',
  8026. '*/select',
  8027. '-',
  8028. array('select' => array(
  8029. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  8030. )),
  8031. $daysRegex,
  8032. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8033. date('j', $now),
  8034. '/option',
  8035. '*/select',
  8036. '-',
  8037. array('select' => array(
  8038. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  8039. )),
  8040. $yearsRegex,
  8041. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8042. date('Y', $now),
  8043. '*/select'
  8044. );
  8045. $this->assertTags($result, $expected);
  8046. $result = $this->Form->input('UserForm.stuff');
  8047. $expected = array(
  8048. 'div' => array('class' => 'input text'),
  8049. 'label' => array('for' => 'UserFormStuff'),
  8050. 'Stuff',
  8051. '/label',
  8052. 'input' => array(
  8053. 'type' => 'text', 'name' => 'UserForm[stuff]',
  8054. 'id' => 'UserFormStuff', 'maxlength' => 10
  8055. ),
  8056. '/div'
  8057. );
  8058. $this->assertTags($result, $expected);
  8059. }
  8060. /**
  8061. * testForMagicInputNonExistingNorValidated method
  8062. *
  8063. * @return void
  8064. */
  8065. public function testForMagicInputNonExistingNorValidated() {
  8066. $encoding = strtolower(Configure::read('App.encoding'));
  8067. $result = $this->Form->create('Contact');
  8068. $expected = array(
  8069. 'form' => array(
  8070. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8071. 'accept-charset' => $encoding
  8072. ),
  8073. 'div' => array('style' => 'display:none;'),
  8074. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8075. '/div'
  8076. );
  8077. $this->assertTags($result, $expected);
  8078. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8079. $expected = array(
  8080. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8081. 'Non Existing Nor Validated',
  8082. '/label',
  8083. 'input' => array(
  8084. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  8085. 'id' => 'ContactNonExistingNorValidated'
  8086. )
  8087. );
  8088. $this->assertTags($result, $expected);
  8089. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8090. 'div' => false, 'value' => 'my value'
  8091. ));
  8092. $expected = array(
  8093. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8094. 'Non Existing Nor Validated',
  8095. '/label',
  8096. 'input' => array(
  8097. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  8098. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8099. )
  8100. );
  8101. $this->assertTags($result, $expected);
  8102. $this->Form->request->data = array(
  8103. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8104. ));
  8105. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8106. $expected = array(
  8107. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8108. 'Non Existing Nor Validated',
  8109. '/label',
  8110. 'input' => array(
  8111. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  8112. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8113. )
  8114. );
  8115. $this->assertTags($result, $expected);
  8116. }
  8117. /**
  8118. * testFormMagicInputLabel method
  8119. *
  8120. * @return void
  8121. */
  8122. public function testFormMagicInputLabel() {
  8123. $encoding = strtolower(Configure::read('App.encoding'));
  8124. $result = $this->Form->create('Contact');
  8125. $expected = array(
  8126. 'form' => array(
  8127. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8128. 'accept-charset' => $encoding
  8129. ),
  8130. 'div' => array('style' => 'display:none;'),
  8131. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8132. '/div'
  8133. );
  8134. $this->assertTags($result, $expected);
  8135. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8136. $this->assertTags($result, array('input' => array(
  8137. 'name' => 'Contact[name]', 'type' => 'text',
  8138. 'id' => 'ContactName', 'maxlength' => '255')
  8139. ));
  8140. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8141. $expected = array(
  8142. 'label' => array('for' => 'ContactName'),
  8143. 'My label',
  8144. '/label',
  8145. 'input' => array(
  8146. 'type' => 'text', 'name' => 'Contact[name]',
  8147. 'id' => 'ContactName', 'maxlength' => '255'
  8148. )
  8149. );
  8150. $this->assertTags($result, $expected);
  8151. $result = $this->Form->input('Contact.name', array(
  8152. 'div' => false, 'label' => array('class' => 'mandatory')
  8153. ));
  8154. $expected = array(
  8155. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8156. 'Name',
  8157. '/label',
  8158. 'input' => array(
  8159. 'type' => 'text', 'name' => 'Contact[name]',
  8160. 'id' => 'ContactName', 'maxlength' => '255'
  8161. )
  8162. );
  8163. $this->assertTags($result, $expected);
  8164. $result = $this->Form->input('Contact.name', array(
  8165. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8166. ));
  8167. $expected = array(
  8168. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8169. 'My label',
  8170. '/label',
  8171. 'input' => array(
  8172. 'type' => 'text', 'name' => 'Contact[name]',
  8173. 'id' => 'ContactName', 'maxlength' => '255'
  8174. )
  8175. );
  8176. $this->assertTags($result, $expected);
  8177. $result = $this->Form->input('Contact.name', array(
  8178. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8179. ));
  8180. $expected = array(
  8181. 'label' => array('for' => 'my_id'),
  8182. 'Name',
  8183. '/label',
  8184. 'input' => array(
  8185. 'type' => 'text', 'name' => 'Contact[name]',
  8186. 'id' => 'my_id', 'maxlength' => '255'
  8187. )
  8188. );
  8189. $this->assertTags($result, $expected);
  8190. $result = $this->Form->input('1.id');
  8191. $this->assertTags($result, array('input' => array(
  8192. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  8193. 'id' => 'Contact1Id'
  8194. )));
  8195. $result = $this->Form->input("1.name");
  8196. $expected = array(
  8197. 'div' => array('class' => 'input text'),
  8198. 'label' => array('for' => 'Contact1Name'),
  8199. 'Name',
  8200. '/label',
  8201. 'input' => array(
  8202. 'type' => 'text', 'name' => 'Contact[1][name]',
  8203. 'id' => 'Contact1Name', 'maxlength' => '255'
  8204. ),
  8205. '/div'
  8206. );
  8207. $this->assertTags($result, $expected);
  8208. $result = $this->Form->input('Contact.1.id');
  8209. $this->assertTags($result, array(
  8210. 'input' => array(
  8211. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  8212. 'id' => 'Contact1Id'
  8213. )
  8214. ));
  8215. $result = $this->Form->input("Model.1.name");
  8216. $expected = array(
  8217. 'div' => array('class' => 'input text'),
  8218. 'label' => array('for' => 'Model1Name'),
  8219. 'Name',
  8220. '/label',
  8221. 'input' => array(
  8222. 'type' => 'text', 'name' => 'Model[1][name]',
  8223. 'id' => 'Model1Name'
  8224. ),
  8225. '/div'
  8226. );
  8227. $this->assertTags($result, $expected);
  8228. }
  8229. /**
  8230. * testFormEnd method
  8231. *
  8232. * @return void
  8233. */
  8234. public function testFormEnd() {
  8235. $this->assertEquals('</form>', $this->Form->end());
  8236. $result = $this->Form->end('');
  8237. $expected = array(
  8238. 'div' => array('class' => 'submit'),
  8239. 'input' => array('type' => 'submit', 'value' => ''),
  8240. '/div',
  8241. '/form'
  8242. );
  8243. $this->assertTags($result, $expected);
  8244. $result = $this->Form->end(array('label' => ''));
  8245. $expected = array(
  8246. 'div' => array('class' => 'submit'),
  8247. 'input' => array('type' => 'submit', 'value' => ''),
  8248. '/div',
  8249. '/form'
  8250. );
  8251. $this->assertTags($result, $expected);
  8252. $result = $this->Form->end('save');
  8253. $expected = array(
  8254. 'div' => array('class' => 'submit'),
  8255. 'input' => array('type' => 'submit', 'value' => 'save'),
  8256. '/div',
  8257. '/form'
  8258. );
  8259. $this->assertTags($result, $expected);
  8260. $result = $this->Form->end(array('label' => 'save'));
  8261. $expected = array(
  8262. 'div' => array('class' => 'submit'),
  8263. 'input' => array('type' => 'submit', 'value' => 'save'),
  8264. '/div',
  8265. '/form'
  8266. );
  8267. $this->assertTags($result, $expected);
  8268. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8269. $expected = array(
  8270. 'div' => array('class' => 'submit'),
  8271. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8272. '/div',
  8273. '/form'
  8274. );
  8275. $this->assertTags($result, $expected);
  8276. $result = $this->Form->end(array('name' => 'Whatever'));
  8277. $expected = array(
  8278. 'div' => array('class' => 'submit'),
  8279. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8280. '/div',
  8281. '/form'
  8282. );
  8283. $this->assertTags($result, $expected);
  8284. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8285. $expected = array(
  8286. 'div' => array('class' => 'good'),
  8287. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8288. '/div',
  8289. '/form'
  8290. );
  8291. $this->assertTags($result, $expected);
  8292. $result = $this->Form->end(array(
  8293. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8294. ));
  8295. $expected = array(
  8296. 'div' => array('class' => 'good'),
  8297. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8298. '/div',
  8299. '/form'
  8300. );
  8301. $this->assertTags($result, $expected);
  8302. }
  8303. /**
  8304. * testMultipleFormWithIdFields method
  8305. *
  8306. * @return void
  8307. */
  8308. public function testMultipleFormWithIdFields() {
  8309. $this->Form->create('UserForm');
  8310. $result = $this->Form->input('id');
  8311. $this->assertTags($result, array('input' => array(
  8312. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  8313. )));
  8314. $result = $this->Form->input('ValidateItem.id');
  8315. $this->assertTags($result, array('input' => array(
  8316. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  8317. 'id' => 'ValidateItemId'
  8318. )));
  8319. $result = $this->Form->input('ValidateUser.id');
  8320. $this->assertTags($result, array('input' => array(
  8321. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  8322. 'id' => 'ValidateUserId'
  8323. )));
  8324. }
  8325. /**
  8326. * testDbLessModel method
  8327. *
  8328. * @return void
  8329. */
  8330. public function testDbLessModel() {
  8331. $this->Form->create('TestMail');
  8332. $result = $this->Form->input('name');
  8333. $expected = array(
  8334. 'div' => array('class' => 'input text'),
  8335. 'label' => array('for' => 'TestMailName'),
  8336. 'Name',
  8337. '/label',
  8338. 'input' => array(
  8339. 'name' => 'TestMail[name]', 'type' => 'text',
  8340. 'id' => 'TestMailName'
  8341. ),
  8342. '/div'
  8343. );
  8344. $this->assertTags($result, $expected);
  8345. ClassRegistry::init('TestMail');
  8346. $this->Form->create('TestMail');
  8347. $result = $this->Form->input('name');
  8348. $expected = array(
  8349. 'div' => array('class' => 'input text'),
  8350. 'label' => array('for' => 'TestMailName'),
  8351. 'Name',
  8352. '/label',
  8353. 'input' => array(
  8354. 'name' => 'TestMail[name]', 'type' => 'text',
  8355. 'id' => 'TestMailName'
  8356. ),
  8357. '/div'
  8358. );
  8359. $this->assertTags($result, $expected);
  8360. }
  8361. /**
  8362. * testBrokenness method
  8363. *
  8364. * @return void
  8365. */
  8366. public function testBrokenness() {
  8367. /*
  8368. * #4 This test has two parents and four children. By default (as of r7117) both
  8369. * parents are show but the first parent is missing a child. This is the inconsistency
  8370. * in the default behaviour - one parent has all children, the other does not - dependent
  8371. * on the data values.
  8372. */
  8373. $result = $this->Form->select('Model.field', array(
  8374. 'Fred' => array(
  8375. 'freds_son_1' => 'Fred',
  8376. 'freds_son_2' => 'Freddie'
  8377. ),
  8378. 'Bert' => array(
  8379. 'berts_son_1' => 'Albert',
  8380. 'berts_son_2' => 'Bertie')
  8381. ),
  8382. array('showParents' => true, 'empty' => false)
  8383. );
  8384. $expected = array(
  8385. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8386. array('optgroup' => array('label' => 'Fred')),
  8387. array('option' => array('value' => 'freds_son_1')),
  8388. 'Fred',
  8389. '/option',
  8390. array('option' => array('value' => 'freds_son_2')),
  8391. 'Freddie',
  8392. '/option',
  8393. '/optgroup',
  8394. array('optgroup' => array('label' => 'Bert')),
  8395. array('option' => array('value' => 'berts_son_1')),
  8396. 'Albert',
  8397. '/option',
  8398. array('option' => array('value' => 'berts_son_2')),
  8399. 'Bertie',
  8400. '/option',
  8401. '/optgroup',
  8402. '/select'
  8403. );
  8404. $this->assertTags($result, $expected);
  8405. /*
  8406. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8407. * changed, so we should expect the same select list data, just with a different name
  8408. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8409. * This is where data corruption can occur, because when a select value is missing from
  8410. * a list a form will substitute the first value in the list - without the user knowing.
  8411. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8412. * affect the availability of 3 => 'Three' as a valid option.
  8413. */
  8414. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8415. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8416. ));
  8417. $result = $this->Form->select(
  8418. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8419. );
  8420. $expected = array(
  8421. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8422. array('option' => array('value' => 1)),
  8423. 'One',
  8424. '/option',
  8425. array('option' => array('value' => 2)),
  8426. 'Two',
  8427. '/option',
  8428. array('optgroup' => array('label' => 'Three')),
  8429. array('option' => array('value' => 3)),
  8430. 'Three',
  8431. '/option',
  8432. array('option' => array('value' => 4)),
  8433. 'Four',
  8434. '/option',
  8435. array('option' => array('value' => 5)),
  8436. 'Five',
  8437. '/option',
  8438. '/optgroup',
  8439. '/select'
  8440. );
  8441. $this->assertTags($result, $expected);
  8442. }
  8443. /**
  8444. * Test the generation of fields for a multi record form.
  8445. *
  8446. * @return void
  8447. */
  8448. public function testMultiRecordForm() {
  8449. $this->Form->create('ValidateProfile');
  8450. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8451. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8452. $expected = array(
  8453. 'div' => array('class' => 'input textarea'),
  8454. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8455. 'Name',
  8456. '/label',
  8457. 'textarea' => array(
  8458. 'id' => 'ValidateProfile1ValidateItem2Name',
  8459. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  8460. 'cols' => 30,
  8461. 'rows' => 6
  8462. ),
  8463. 'Value',
  8464. '/textarea',
  8465. '/div'
  8466. );
  8467. $this->assertTags($result, $expected);
  8468. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8469. $expected = array(
  8470. 'div' => array('class' => 'input date'),
  8471. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8472. 'Created',
  8473. '/label',
  8474. array('select' => array(
  8475. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  8476. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8477. )
  8478. ),
  8479. array('option' => array('value' => '')), '/option',
  8480. $this->dateRegex['monthsRegex'],
  8481. '/select', '-',
  8482. array('select' => array(
  8483. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  8484. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8485. )
  8486. ),
  8487. array('option' => array('value' => '')), '/option',
  8488. $this->dateRegex['daysRegex'],
  8489. '/select', '-',
  8490. array('select' => array(
  8491. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  8492. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8493. )
  8494. ),
  8495. array('option' => array('value' => '')), '/option',
  8496. $this->dateRegex['yearsRegex'],
  8497. '/select',
  8498. '/div'
  8499. );
  8500. $this->assertTags($result, $expected);
  8501. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8502. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8503. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8504. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8505. $expected = array(
  8506. 'div' => array('class' => 'input select error'),
  8507. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8508. 'Profile',
  8509. '/label',
  8510. 'select' => array(
  8511. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  8512. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8513. 'class' => 'form-error'
  8514. ),
  8515. '/select',
  8516. array('div' => array('class' => 'error-message')),
  8517. 'Error',
  8518. '/div',
  8519. '/div'
  8520. );
  8521. $this->assertTags($result, $expected);
  8522. }
  8523. /**
  8524. * test the correct display of multi-record form validation errors.
  8525. *
  8526. * @return void
  8527. */
  8528. public function testMultiRecordFormValidationErrors() {
  8529. $this->Form->create('ValidateProfile');
  8530. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8531. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8532. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8533. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8534. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8535. $result = $this->Form->error('ValidateProfile.2.city');
  8536. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8537. $result = $this->Form->error('2.city');
  8538. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8539. }
  8540. /**
  8541. * test the correct display of multi-record form validation errors.
  8542. *
  8543. * @return void
  8544. */
  8545. public function testSaveManyRecordFormValidationErrors() {
  8546. $this->Form->create('ValidateUser');
  8547. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8548. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8549. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8550. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8551. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8552. $result = $this->Form->error('ValidateUser.0.city');
  8553. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8554. }
  8555. /**
  8556. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8557. *
  8558. * @return void
  8559. */
  8560. public function testInputTemplate() {
  8561. $result = $this->Form->input('Contact.email', array(
  8562. 'type' => 'text', 'format' => array('input')
  8563. ));
  8564. $expected = array(
  8565. 'div' => array('class' => 'input text'),
  8566. 'input' => array(
  8567. 'type' => 'text', 'name' => 'Contact[email]',
  8568. 'id' => 'ContactEmail'
  8569. ),
  8570. '/div'
  8571. );
  8572. $this->assertTags($result, $expected);
  8573. $result = $this->Form->input('Contact.email', array(
  8574. 'type' => 'text', 'format' => array('input', 'label'),
  8575. 'label' => '<em>Email (required)</em>'
  8576. ));
  8577. $expected = array(
  8578. 'div' => array('class' => 'input text'),
  8579. array('input' => array(
  8580. 'type' => 'text', 'name' => 'Contact[email]',
  8581. 'id' => 'ContactEmail'
  8582. )),
  8583. 'label' => array('for' => 'ContactEmail'),
  8584. 'em' => array(),
  8585. 'Email (required)',
  8586. '/em',
  8587. '/label',
  8588. '/div'
  8589. );
  8590. $this->assertTags($result, $expected);
  8591. $result = $this->Form->input('Contact.email', array(
  8592. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8593. 'between' => '<div>Something in the middle</div>',
  8594. 'after' => '<span>Some text at the end</span>'
  8595. ));
  8596. $expected = array(
  8597. 'div' => array('class' => 'input text'),
  8598. array('input' => array(
  8599. 'type' => 'text', 'name' => 'Contact[email]',
  8600. 'id' => 'ContactEmail'
  8601. )),
  8602. array('div' => array()),
  8603. 'Something in the middle',
  8604. '/div',
  8605. 'label' => array('for' => 'ContactEmail'),
  8606. 'Email',
  8607. '/label',
  8608. 'span' => array(),
  8609. 'Some text at the end',
  8610. '/span',
  8611. '/div'
  8612. );
  8613. $this->assertTags($result, $expected);
  8614. $result = $this->Form->input('Contact.method', array(
  8615. 'type' => 'radio',
  8616. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8617. 'between' => 'I am between',
  8618. ));
  8619. $expected = array(
  8620. 'div' => array('class' => 'input radio'),
  8621. 'fieldset' => array(),
  8622. 'legend' => array(),
  8623. 'Method',
  8624. '/legend',
  8625. 'I am between',
  8626. 'input' => array(
  8627. 'type' => 'hidden', 'name' => 'Contact[method]',
  8628. 'value' => '', 'id' => 'ContactMethod_'
  8629. ),
  8630. array('input' => array(
  8631. 'type' => 'radio', 'name' => 'Contact[method]',
  8632. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8633. )),
  8634. array('label' => array('for' => 'ContactMethodEmail')),
  8635. 'Email',
  8636. '/label',
  8637. array('input' => array(
  8638. 'type' => 'radio', 'name' => 'Contact[method]',
  8639. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8640. )),
  8641. array('label' => array('for' => 'ContactMethodPigeon')),
  8642. 'Pigeon',
  8643. '/label',
  8644. '/fieldset',
  8645. '/div',
  8646. );
  8647. $this->assertTags($result, $expected);
  8648. }
  8649. /**
  8650. * test that some html5 inputs + FormHelper::__call() work
  8651. *
  8652. * @return void
  8653. */
  8654. public function testHtml5Inputs() {
  8655. $result = $this->Form->email('User.email');
  8656. $expected = array(
  8657. 'input' => array('type' => 'email', 'name' => 'User[email]', 'id' => 'UserEmail')
  8658. );
  8659. $this->assertTags($result, $expected);
  8660. $result = $this->Form->search('User.query');
  8661. $expected = array(
  8662. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery')
  8663. );
  8664. $this->assertTags($result, $expected);
  8665. $result = $this->Form->search('User.query', array('value' => 'test'));
  8666. $expected = array(
  8667. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8668. );
  8669. $this->assertTags($result, $expected);
  8670. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8671. $expected = array(
  8672. 'input' => array('type' => 'text', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8673. );
  8674. $this->assertTags($result, $expected);
  8675. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8676. $expected = array(
  8677. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8678. );
  8679. $this->assertTags($result, $expected);
  8680. }
  8681. /**
  8682. *
  8683. * @expectedException Cake\Error\Exception
  8684. * @return void
  8685. */
  8686. public function testHtml5InputException() {
  8687. $this->Form->email();
  8688. }
  8689. /**
  8690. * Tests that a model can be loaded from the model names passed in the request object
  8691. *
  8692. * @return void
  8693. */
  8694. public function testIntrospectModelFromRequest() {
  8695. $this->loadFixtures('Post');
  8696. App::build(array(
  8697. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  8698. ));
  8699. Plugin::load('TestPlugin');
  8700. $this->Form->request['models'] = array(
  8701. 'TestPluginPost' => array(
  8702. 'plugin' => 'TestPlugin',
  8703. 'className' => 'TestPluginPost'
  8704. )
  8705. );
  8706. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8707. $this->Form->create('TestPluginPost');
  8708. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8709. $this->assertInstanceOf('TestPlugin\Model\TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8710. Plugin::unload();
  8711. }
  8712. /**
  8713. * Tests that it is possible to set the validation errors directly in the helper for a field
  8714. *
  8715. * @return void
  8716. */
  8717. public function testCustomValidationErrors() {
  8718. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8719. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8720. $this->assertEquals('Badness!', $result);
  8721. }
  8722. /**
  8723. * Tests that the 'on' key validates as expected on create
  8724. *
  8725. * @return void
  8726. */
  8727. public function testRequiredOnCreate() {
  8728. $this->Form->create('Contact');
  8729. $result = $this->Form->input('Contact.imrequiredonupdate');
  8730. $expected = array(
  8731. 'div' => array('class' => 'input text'),
  8732. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8733. 'Imrequiredonupdate',
  8734. '/label',
  8735. 'input' => array(
  8736. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8737. 'id' => 'ContactImrequiredonupdate'
  8738. ),
  8739. '/div'
  8740. );
  8741. $this->assertTags($result, $expected);
  8742. $result = $this->Form->input('Contact.imrequiredoncreate');
  8743. $expected = array(
  8744. 'div' => array('class' => 'input text required'),
  8745. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8746. 'Imrequiredoncreate',
  8747. '/label',
  8748. 'input' => array(
  8749. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8750. 'id' => 'ContactImrequiredoncreate',
  8751. 'required' => 'required'
  8752. ),
  8753. '/div'
  8754. );
  8755. $this->assertTags($result, $expected);
  8756. $result = $this->Form->input('Contact.imrequiredonboth');
  8757. $expected = array(
  8758. 'div' => array('class' => 'input text required'),
  8759. 'label' => array('for' => 'ContactImrequiredonboth'),
  8760. 'Imrequiredonboth',
  8761. '/label',
  8762. 'input' => array(
  8763. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8764. 'id' => 'ContactImrequiredonboth',
  8765. 'required' => 'required'
  8766. ),
  8767. '/div'
  8768. );
  8769. $this->assertTags($result, $expected);
  8770. $this->Form->inputDefaults(array('required' => false));
  8771. $result = $this->Form->input('Contact.imrequired');
  8772. $expected = array(
  8773. 'div' => array('class' => 'input text'),
  8774. 'label' => array('for' => 'ContactImrequired'),
  8775. 'Imrequired',
  8776. '/label',
  8777. 'input' => array(
  8778. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8779. 'id' => 'ContactImrequired'
  8780. ),
  8781. '/div'
  8782. );
  8783. $this->assertTags($result, $expected);
  8784. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8785. $this->assertTags($result, $expected);
  8786. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8787. $expected = array(
  8788. 'div' => array('class' => 'input text required'),
  8789. 'label' => array('for' => 'ContactImrequired'),
  8790. 'Imrequired',
  8791. '/label',
  8792. 'input' => array(
  8793. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8794. 'id' => 'ContactImrequired'
  8795. ),
  8796. '/div'
  8797. );
  8798. $this->assertTags($result, $expected);
  8799. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8800. $this->assertTags($result, $expected);
  8801. }
  8802. /**
  8803. * Tests that the 'on' key validates as expected on update
  8804. *
  8805. * @return void
  8806. */
  8807. public function testRequiredOnUpdate() {
  8808. $this->Form->request->data['Contact']['id'] = 1;
  8809. $this->Form->create('Contact');
  8810. $result = $this->Form->input('Contact.imrequiredonupdate');
  8811. $expected = array(
  8812. 'div' => array('class' => 'input text required'),
  8813. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8814. 'Imrequiredonupdate',
  8815. '/label',
  8816. 'input' => array(
  8817. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8818. 'id' => 'ContactImrequiredonupdate',
  8819. 'required' => 'required'
  8820. ),
  8821. '/div'
  8822. );
  8823. $this->assertTags($result, $expected);
  8824. $result = $this->Form->input('Contact.imrequiredoncreate');
  8825. $expected = array(
  8826. 'div' => array('class' => 'input text'),
  8827. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8828. 'Imrequiredoncreate',
  8829. '/label',
  8830. 'input' => array(
  8831. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8832. 'id' => 'ContactImrequiredoncreate'
  8833. ),
  8834. '/div'
  8835. );
  8836. $this->assertTags($result, $expected);
  8837. $result = $this->Form->input('Contact.imrequiredonboth');
  8838. $expected = array(
  8839. 'div' => array('class' => 'input text required'),
  8840. 'label' => array('for' => 'ContactImrequiredonboth'),
  8841. 'Imrequiredonboth',
  8842. '/label',
  8843. 'input' => array(
  8844. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8845. 'id' => 'ContactImrequiredonboth',
  8846. 'required' => 'required'
  8847. ),
  8848. '/div'
  8849. );
  8850. $this->assertTags($result, $expected);
  8851. $result = $this->Form->input('Contact.imrequired');
  8852. $expected = array(
  8853. 'div' => array('class' => 'input text required'),
  8854. 'label' => array('for' => 'ContactImrequired'),
  8855. 'Imrequired',
  8856. '/label',
  8857. 'input' => array(
  8858. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8859. 'id' => 'ContactImrequired',
  8860. 'required' => 'required'
  8861. ),
  8862. '/div'
  8863. );
  8864. $this->assertTags($result, $expected);
  8865. }
  8866. /**
  8867. * Test inputDefaults setter and getter
  8868. *
  8869. * @return void
  8870. */
  8871. public function testInputDefaults() {
  8872. $this->Form->create('Contact');
  8873. $this->Form->inputDefaults(array(
  8874. 'label' => false,
  8875. 'div' => array(
  8876. 'style' => 'color: #000;'
  8877. )
  8878. ));
  8879. $result = $this->Form->input('Contact.field1');
  8880. $expected = array(
  8881. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8882. 'input' => array(
  8883. 'type' => 'text', 'name' => 'Contact[field1]',
  8884. 'id' => 'ContactField1'
  8885. ),
  8886. '/div'
  8887. );
  8888. $this->assertTags($result, $expected);
  8889. $this->Form->inputDefaults(array(
  8890. 'div' => false,
  8891. 'label' => 'Label',
  8892. ));
  8893. $result = $this->Form->input('Contact.field1');
  8894. $expected = array(
  8895. 'label' => array('for' => 'ContactField1'),
  8896. 'Label',
  8897. '/label',
  8898. 'input' => array(
  8899. 'type' => 'text', 'name' => 'Contact[field1]',
  8900. 'id' => 'ContactField1'
  8901. ),
  8902. );
  8903. $this->assertTags($result, $expected);
  8904. $this->Form->inputDefaults(array(
  8905. 'label' => false,
  8906. ), true);
  8907. $result = $this->Form->input('Contact.field1');
  8908. $expected = array(
  8909. 'input' => array(
  8910. 'type' => 'text', 'name' => 'Contact[field1]',
  8911. 'id' => 'ContactField1'
  8912. ),
  8913. );
  8914. $this->assertTags($result, $expected);
  8915. $result = $this->Form->inputDefaults();
  8916. $expected = array(
  8917. 'div' => false,
  8918. 'label' => false,
  8919. );
  8920. $this->assertEquals($expected, $result);
  8921. }
  8922. }