FormHelperTest.php 313 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  319. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  320. );
  321. /**
  322. * beforeValidate method
  323. *
  324. * @return void
  325. */
  326. public function beforeValidate($options = array()) {
  327. $this->invalidate('email');
  328. return false;
  329. }
  330. }
  331. /**
  332. * ValidateProfile class
  333. *
  334. * @package Cake.Test.Case.View.Helper
  335. */
  336. class ValidateProfile extends CakeTestModel {
  337. /**
  338. * useTable property
  339. *
  340. * @var boolean
  341. */
  342. public $useTable = false;
  343. /**
  344. * schema property
  345. *
  346. * @var array
  347. */
  348. protected $_schema = array(
  349. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  350. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  351. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  352. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  353. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  354. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  355. );
  356. /**
  357. * hasOne property
  358. *
  359. * @var array
  360. */
  361. public $hasOne = array('ValidateItem' => array(
  362. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  363. ));
  364. /**
  365. * belongsTo property
  366. *
  367. * @var array
  368. */
  369. public $belongsTo = array('ValidateUser' => array(
  370. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  371. ));
  372. /**
  373. * beforeValidate method
  374. *
  375. * @return void
  376. */
  377. public function beforeValidate($options = array()) {
  378. $this->invalidate('full_name');
  379. $this->invalidate('city');
  380. return false;
  381. }
  382. }
  383. /**
  384. * ValidateItem class
  385. *
  386. * @package Cake.Test.Case.View.Helper
  387. */
  388. class ValidateItem extends CakeTestModel {
  389. /**
  390. * useTable property
  391. *
  392. * @var boolean
  393. */
  394. public $useTable = false;
  395. /**
  396. * schema property
  397. *
  398. * @var array
  399. */
  400. protected $_schema = array(
  401. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  402. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  403. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  404. 'description' => array(
  405. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  406. ),
  407. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  408. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  409. );
  410. /**
  411. * belongsTo property
  412. *
  413. * @var array
  414. */
  415. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  416. /**
  417. * beforeValidate method
  418. *
  419. * @return void
  420. */
  421. public function beforeValidate($options = array()) {
  422. $this->invalidate('description');
  423. return false;
  424. }
  425. }
  426. /**
  427. * TestMail class
  428. *
  429. * @package Cake.Test.Case.View.Helper
  430. */
  431. class TestMail extends CakeTestModel {
  432. /**
  433. * useTable property
  434. *
  435. * @var boolean
  436. */
  437. public $useTable = false;
  438. }
  439. /**
  440. * FormHelperTest class
  441. *
  442. * @package Cake.Test.Case.View.Helper
  443. * @property FormHelper $Form
  444. */
  445. class FormHelperTest extends CakeTestCase {
  446. /**
  447. * Fixtures to be used
  448. *
  449. * @var array
  450. */
  451. public $fixtures = array('core.post');
  452. /**
  453. * Do not load the fixtures by default
  454. *
  455. * @var boolean
  456. */
  457. public $autoFixtures = false;
  458. /**
  459. * setUp method
  460. *
  461. * @return void
  462. */
  463. public function setUp() {
  464. parent::setUp();
  465. Configure::write('Config.language', 'eng');
  466. Configure::write('App.base', '');
  467. Configure::delete('Asset');
  468. $this->Controller = new ContactTestController();
  469. $this->View = new View($this->Controller);
  470. $this->Form = new FormHelper($this->View);
  471. $this->Form->Html = new HtmlHelper($this->View);
  472. $this->Form->request = new CakeRequest('contacts/add', false);
  473. $this->Form->request->here = '/contacts/add';
  474. $this->Form->request['action'] = 'add';
  475. $this->Form->request->webroot = '';
  476. $this->Form->request->base = '';
  477. ClassRegistry::addObject('Contact', new Contact());
  478. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  479. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  480. ClassRegistry::addObject('User', new UserForm());
  481. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  482. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  483. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  484. $this->oldSalt = Configure::read('Security.salt');
  485. $this->dateRegex = array(
  486. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  487. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  488. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  491. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  492. );
  493. Configure::write('Security.salt', 'foo!');
  494. }
  495. /**
  496. * tearDown method
  497. *
  498. * @return void
  499. */
  500. public function tearDown() {
  501. parent::tearDown();
  502. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  503. Configure::write('Security.salt', $this->oldSalt);
  504. }
  505. /**
  506. * testFormCreateWithSecurity method
  507. *
  508. * Test form->create() with security key.
  509. *
  510. * @return void
  511. */
  512. public function testCreateWithSecurity() {
  513. $this->Form->request['_Token'] = array('key' => 'testKey');
  514. $encoding = strtolower(Configure::read('App.encoding'));
  515. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  516. $expected = array(
  517. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  518. 'div' => array('style' => 'display:none;'),
  519. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  520. array('input' => array(
  521. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  522. )),
  523. '/div'
  524. );
  525. $this->assertTags($result, $expected);
  526. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  527. $expected['form']['id'] = 'MyForm';
  528. $this->assertTags($result, $expected);
  529. }
  530. /**
  531. * testFormCreateGetNoSecurity method
  532. *
  533. * Test form->create() with no security key as its a get form
  534. *
  535. * @return void
  536. */
  537. public function testCreateEndGetNoSecurity() {
  538. $this->Form->request['_Token'] = array('key' => 'testKey');
  539. $encoding = strtolower(Configure::read('App.encoding'));
  540. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  541. $this->assertNotContains('Token', $result);
  542. $result = $this->Form->end('Save');
  543. $this->assertNotContains('Token', $result);
  544. }
  545. /**
  546. * test that create() clears the fields property so it starts fresh
  547. *
  548. * @return void
  549. */
  550. public function testCreateClearingFields() {
  551. $this->Form->fields = array('model_id');
  552. $this->Form->create('Contact');
  553. $this->assertEquals(array(), $this->Form->fields);
  554. }
  555. /**
  556. * Tests form hash generation with model-less data
  557. *
  558. * @return void
  559. */
  560. public function testValidateHashNoModel() {
  561. $this->Form->request['_Token'] = array('key' => 'foo');
  562. $result = $this->Form->secure(array('anything'));
  563. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  564. }
  565. /**
  566. * Tests that models with identical field names get resolved properly
  567. *
  568. * @return void
  569. */
  570. public function testDuplicateFieldNameResolution() {
  571. $result = $this->Form->create('ValidateUser');
  572. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  573. $result = $this->Form->input('ValidateItem.name');
  574. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  575. $result = $this->Form->input('ValidateUser.name');
  576. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  577. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  578. $this->assertRegExp('/type="text"/', $result);
  579. $result = $this->Form->input('ValidateItem.name');
  580. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  581. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  582. $this->assertRegExp('/<textarea/', $result);
  583. $result = $this->Form->input('name');
  584. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  585. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  586. $this->assertRegExp('/type="text"/', $result);
  587. }
  588. /**
  589. * Tests that hidden fields generated for checkboxes don't get locked
  590. *
  591. * @return void
  592. */
  593. public function testNoCheckboxLocking() {
  594. $this->Form->request['_Token'] = array('key' => 'foo');
  595. $this->assertSame(array(), $this->Form->fields);
  596. $this->Form->checkbox('check', array('value' => '1'));
  597. $this->assertSame($this->Form->fields, array('check'));
  598. }
  599. /**
  600. * testFormSecurityFields method
  601. *
  602. * Test generation of secure form hash generation.
  603. *
  604. * @return void
  605. */
  606. public function testFormSecurityFields() {
  607. $key = 'testKey';
  608. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  609. $this->Form->request['_Token'] = array('key' => $key);
  610. $result = $this->Form->secure($fields);
  611. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  612. $hash .= ':' . 'Model.valid';
  613. $hash = urlencode($hash);
  614. $expected = array(
  615. 'div' => array('style' => 'display:none;'),
  616. array('input' => array(
  617. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  618. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  619. )),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  622. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  623. )),
  624. '/div'
  625. );
  626. $this->assertTags($result, $expected);
  627. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  628. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  629. $result = $this->Form->secure($fields);
  630. $expected = array(
  631. 'div' => array('class' => 'hidden'),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  634. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  635. )),
  636. array('input' => array(
  637. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  638. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  639. )),
  640. '/div'
  641. );
  642. $this->assertTags($result, $expected);
  643. }
  644. /**
  645. * Tests correct generation of number fields for double and float fields
  646. *
  647. * @return void
  648. */
  649. public function testTextFieldGenerationForFloats() {
  650. $model = ClassRegistry::getObject('Contact');
  651. $model->setSchema(array('foo' => array(
  652. 'type' => 'float',
  653. 'null' => false,
  654. 'default' => null,
  655. 'length' => 10
  656. )));
  657. $this->Form->create('Contact');
  658. $result = $this->Form->input('foo');
  659. $expected = array(
  660. 'div' => array('class' => 'input number'),
  661. 'label' => array('for' => 'ContactFoo'),
  662. 'Foo',
  663. '/label',
  664. array('input' => array(
  665. 'type' => 'number',
  666. 'name' => 'data[Contact][foo]',
  667. 'id' => 'ContactFoo',
  668. 'step' => 'any'
  669. )),
  670. '/div'
  671. );
  672. $this->assertTags($result, $expected);
  673. $result = $this->Form->input('foo', array('step' => 0.5));
  674. $expected = array(
  675. 'div' => array('class' => 'input number'),
  676. 'label' => array('for' => 'ContactFoo'),
  677. 'Foo',
  678. '/label',
  679. array('input' => array(
  680. 'type' => 'number',
  681. 'name' => 'data[Contact][foo]',
  682. 'id' => 'ContactFoo',
  683. 'step' => '0.5'
  684. )),
  685. '/div'
  686. );
  687. $this->assertTags($result, $expected);
  688. }
  689. /**
  690. * Tests correct generation of number fields for integer fields
  691. *
  692. * @return void
  693. */
  694. public function testTextFieldTypeNumberGenerationForIntegers() {
  695. $model = ClassRegistry::getObject('Contact');
  696. $model->setSchema(array('foo' => array(
  697. 'type' => 'integer',
  698. 'null' => false,
  699. 'default' => null,
  700. 'length' => null
  701. )));
  702. $this->Form->create('Contact');
  703. $result = $this->Form->input('foo');
  704. $expected = array(
  705. 'div' => array('class' => 'input number'),
  706. 'label' => array('for' => 'ContactFoo'),
  707. 'Foo',
  708. '/label',
  709. array('input' => array(
  710. 'type' => 'number', 'name' => 'data[Contact][foo]',
  711. 'id' => 'ContactFoo'
  712. )),
  713. '/div'
  714. );
  715. $this->assertTags($result, $expected);
  716. }
  717. /**
  718. * testFormSecurityMultipleFields method
  719. *
  720. * Test secure() with multiple row form. Ensure hash is correct.
  721. *
  722. * @return void
  723. */
  724. public function testFormSecurityMultipleFields() {
  725. $key = 'testKey';
  726. $fields = array(
  727. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  728. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  729. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  730. );
  731. $this->Form->request['_Token'] = array('key' => $key);
  732. $result = $this->Form->secure($fields);
  733. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  734. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  735. $expected = array(
  736. 'div' => array('style' => 'display:none;'),
  737. array('input' => array(
  738. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  739. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  740. )),
  741. array('input' => array(
  742. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  743. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  744. )),
  745. '/div'
  746. );
  747. $this->assertTags($result, $expected);
  748. }
  749. /**
  750. * testFormSecurityMultipleSubmitButtons
  751. *
  752. * test form submit generation and ensure that _Token is only created on end()
  753. *
  754. * @return void
  755. */
  756. public function testFormSecurityMultipleSubmitButtons() {
  757. $key = 'testKey';
  758. $this->Form->request['_Token'] = array('key' => $key);
  759. $this->Form->create('Addresses');
  760. $this->Form->input('Address.title');
  761. $this->Form->input('Address.first_name');
  762. $result = $this->Form->submit('Save', array('name' => 'save'));
  763. $expected = array(
  764. 'div' => array('class' => 'submit'),
  765. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  766. '/div',
  767. );
  768. $this->assertTags($result, $expected);
  769. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  770. $expected = array(
  771. 'div' => array('class' => 'submit'),
  772. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  773. '/div',
  774. );
  775. $this->assertTags($result, $expected);
  776. $result = $this->Form->end(null);
  777. $expected = array(
  778. 'div' => array('style' => 'display:none;'),
  779. array('input' => array(
  780. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  781. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  782. )),
  783. array('input' => array(
  784. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  785. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  786. )),
  787. '/div'
  788. );
  789. $this->assertTags($result, $expected);
  790. }
  791. /**
  792. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  793. *
  794. * @return void
  795. */
  796. public function testSecurityButtonNestedNamed() {
  797. $key = 'testKey';
  798. $this->Form->request['_Token'] = array('key' => $key);
  799. $this->Form->create('Addresses');
  800. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  801. $result = $this->Form->unlockField();
  802. $this->assertEquals(array('Address.button'), $result);
  803. }
  804. /**
  805. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  806. *
  807. * @return void
  808. */
  809. public function testSecuritySubmitNestedNamed() {
  810. $key = 'testKey';
  811. $this->Form->request['_Token'] = array('key' => $key);
  812. $this->Form->create('Addresses');
  813. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  814. $result = $this->Form->unlockField();
  815. $this->assertEquals(array('Address.button'), $result);
  816. }
  817. /**
  818. * Test that the correct fields are unlocked for image submits with no names.
  819. *
  820. * @return void
  821. */
  822. public function testSecuritySubmitImageNoName() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('User');
  826. $result = $this->Form->submit('save.png');
  827. $expected = array(
  828. 'div' => array('class' => 'submit'),
  829. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  834. }
  835. /**
  836. * Test that the correct fields are unlocked for image submits with names.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitImageName() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('User');
  844. $result = $this->Form->submit('save.png', array('name' => 'test'));
  845. $expected = array(
  846. 'div' => array('class' => 'submit'),
  847. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  848. '/div'
  849. );
  850. $this->assertTags($result, $expected);
  851. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  852. }
  853. /**
  854. * testFormSecurityMultipleInputFields method
  855. *
  856. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  857. *
  858. * @return void
  859. */
  860. public function testFormSecurityMultipleInputFields() {
  861. $key = 'testKey';
  862. $this->Form->request['_Token'] = array('key' => $key);
  863. $this->Form->create('Addresses');
  864. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  865. $this->Form->input('Addresses.0.title');
  866. $this->Form->input('Addresses.0.first_name');
  867. $this->Form->input('Addresses.0.last_name');
  868. $this->Form->input('Addresses.0.address');
  869. $this->Form->input('Addresses.0.city');
  870. $this->Form->input('Addresses.0.phone');
  871. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  872. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  873. $this->Form->input('Addresses.1.title');
  874. $this->Form->input('Addresses.1.first_name');
  875. $this->Form->input('Addresses.1.last_name');
  876. $this->Form->input('Addresses.1.address');
  877. $this->Form->input('Addresses.1.city');
  878. $this->Form->input('Addresses.1.phone');
  879. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  880. $result = $this->Form->secure($this->Form->fields);
  881. $hash = 'a3b9b2ba1cb688838f92818a5970e17dd7943a78%3AAddresses.0.id%7CAddresses.1.id';
  882. $expected = array(
  883. 'div' => array('style' => 'display:none;'),
  884. array('input' => array(
  885. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  886. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  887. )),
  888. array('input' => array(
  889. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  890. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  891. )),
  892. '/div'
  893. );
  894. $this->assertTags($result, $expected);
  895. }
  896. /**
  897. * Test form security with Model.field.0 style inputs
  898. *
  899. * @return void
  900. */
  901. public function testFormSecurityArrayFields() {
  902. $key = 'testKey';
  903. $this->Form->request->params['_Token']['key'] = $key;
  904. $this->Form->create('Address');
  905. $this->Form->input('Address.primary.1');
  906. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  907. $this->Form->input('Address.secondary.1.0');
  908. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  909. }
  910. /**
  911. * testFormSecurityMultipleInputDisabledFields method
  912. *
  913. * test secure form generation with multiple records and disabled fields.
  914. *
  915. * @return void
  916. */
  917. public function testFormSecurityMultipleInputDisabledFields() {
  918. $key = 'testKey';
  919. $this->Form->request->params['_Token'] = array(
  920. 'key' => $key,
  921. 'unlockedFields' => array('first_name', 'address')
  922. );
  923. $this->Form->create();
  924. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  925. $this->Form->input('Addresses.0.title');
  926. $this->Form->input('Addresses.0.first_name');
  927. $this->Form->input('Addresses.0.last_name');
  928. $this->Form->input('Addresses.0.address');
  929. $this->Form->input('Addresses.0.city');
  930. $this->Form->input('Addresses.0.phone');
  931. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  932. $this->Form->input('Addresses.1.title');
  933. $this->Form->input('Addresses.1.first_name');
  934. $this->Form->input('Addresses.1.last_name');
  935. $this->Form->input('Addresses.1.address');
  936. $this->Form->input('Addresses.1.city');
  937. $this->Form->input('Addresses.1.phone');
  938. $result = $this->Form->secure($this->Form->fields);
  939. $hash = '5c9cadf9da008cc444d3960b481391a425a5d979%3AAddresses.0.id%7CAddresses.1.id';
  940. $expected = array(
  941. 'div' => array('style' => 'display:none;'),
  942. array('input' => array(
  943. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  944. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  945. )),
  946. array('input' => array(
  947. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  948. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  949. )),
  950. '/div'
  951. );
  952. $this->assertTags($result, $expected);
  953. }
  954. /**
  955. * testFormSecurityInputDisabledFields method
  956. *
  957. * Test single record form with disabled fields.
  958. *
  959. * @return void
  960. */
  961. public function testFormSecurityInputUnlockedFields() {
  962. $key = 'testKey';
  963. $this->Form->request['_Token'] = array(
  964. 'key' => $key,
  965. 'unlockedFields' => array('first_name', 'address')
  966. );
  967. $this->Form->create();
  968. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  969. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  970. $this->Form->input('Addresses.title');
  971. $this->Form->input('Addresses.first_name');
  972. $this->Form->input('Addresses.last_name');
  973. $this->Form->input('Addresses.address');
  974. $this->Form->input('Addresses.city');
  975. $this->Form->input('Addresses.phone');
  976. $result = $this->Form->fields;
  977. $expected = array(
  978. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  979. 'Addresses.city', 'Addresses.phone'
  980. );
  981. $this->assertEquals($expected, $result);
  982. $result = $this->Form->secure($expected);
  983. $hash = '40289bd07811587887ff56585a8526ff9da59d7a%3AAddresses.id';
  984. $expected = array(
  985. 'div' => array('style' => 'display:none;'),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  988. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  989. )),
  990. array('input' => array(
  991. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  992. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  993. )),
  994. '/div'
  995. );
  996. $this->assertTags($result, $expected);
  997. }
  998. /**
  999. * test securing inputs with custom name attributes.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testFormSecureWithCustomNameAttribute() {
  1004. $this->Form->request->params['_Token']['key'] = 'testKey';
  1005. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1006. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1007. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1008. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1009. }
  1010. /**
  1011. * testFormSecuredInput method
  1012. *
  1013. * Test generation of entire secure form, assertions made on input() output.
  1014. *
  1015. * @return void
  1016. */
  1017. public function testFormSecuredInput() {
  1018. $this->Form->request['_Token'] = array('key' => 'testKey');
  1019. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1020. $encoding = strtolower(Configure::read('App.encoding'));
  1021. $expected = array(
  1022. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1023. 'div' => array('style' => 'display:none;'),
  1024. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1025. array('input' => array(
  1026. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1027. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1028. )),
  1029. '/div'
  1030. );
  1031. $this->assertTags($result, $expected);
  1032. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1033. $expected = array(
  1034. 'div' => array('class' => 'input text'),
  1035. 'label' => array('for' => 'UserFormPublished'),
  1036. 'Published',
  1037. '/label',
  1038. array('input' => array(
  1039. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1040. 'id' => 'UserFormPublished'
  1041. )),
  1042. '/div'
  1043. );
  1044. $this->assertTags($result, $expected);
  1045. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1046. $expected = array(
  1047. 'div' => array('class' => 'input text'),
  1048. 'label' => array('for' => 'UserFormOther'),
  1049. 'Other',
  1050. '/label',
  1051. array('input' => array(
  1052. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1053. 'id' => 'UserFormOther'
  1054. )),
  1055. '/div'
  1056. );
  1057. $this->assertTags($result, $expected);
  1058. $result = $this->Form->hidden('UserForm.stuff');
  1059. $expected = array(
  1060. 'input' => array(
  1061. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1062. 'id' => 'UserFormStuff'
  1063. ));
  1064. $this->assertTags($result, $expected);
  1065. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1066. $expected = array('input' => array(
  1067. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1068. 'value' => '0', 'id' => 'UserFormHidden'
  1069. ));
  1070. $this->assertTags($result, $expected);
  1071. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1072. $expected = array(
  1073. 'div' => array('class' => 'input checkbox'),
  1074. array('input' => array(
  1075. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1076. 'value' => '0', 'id' => 'UserFormSomething_'
  1077. )),
  1078. array('input' => array(
  1079. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1080. 'value' => '1', 'id' => 'UserFormSomething'
  1081. )),
  1082. 'label' => array('for' => 'UserFormSomething'),
  1083. 'Something',
  1084. '/label',
  1085. '/div'
  1086. );
  1087. $this->assertTags($result, $expected);
  1088. $result = $this->Form->fields;
  1089. $expected = array(
  1090. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1091. 'UserForm.hidden' => '0', 'UserForm.something'
  1092. );
  1093. $this->assertEquals($expected, $result);
  1094. $hash = '6014b4e1c4f39eb62389712111dbe6435bec66cb%3AUserForm.hidden%7CUserForm.stuff';
  1095. $result = $this->Form->secure($this->Form->fields);
  1096. $expected = array(
  1097. 'div' => array('style' => 'display:none;'),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1100. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1101. )),
  1102. array('input' => array(
  1103. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1104. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1105. )),
  1106. '/div'
  1107. );
  1108. $this->assertTags($result, $expected);
  1109. }
  1110. /**
  1111. * Test secured inputs with custom names.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testSecuredInputCustomName() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $this->assertEquals(array(), $this->Form->fields);
  1118. $this->Form->input('text_input', array(
  1119. 'name' => 'data[Option][General.default_role]',
  1120. ));
  1121. $expected = array('Option.General.default_role');
  1122. $this->assertEquals($expected, $this->Form->fields);
  1123. $this->Form->input('select_box', array(
  1124. 'name' => 'data[Option][General.select_role]',
  1125. 'type' => 'select',
  1126. 'options' => array(1, 2),
  1127. ));
  1128. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1129. $this->assertEquals($expected, $this->Form->fields);
  1130. }
  1131. /**
  1132. * Tests that the correct keys are added to the field hash index
  1133. *
  1134. * @return void
  1135. */
  1136. public function testSecuredFileInput() {
  1137. $this->Form->request['_Token'] = array('key' => 'testKey');
  1138. $this->assertEquals(array(), $this->Form->fields);
  1139. $this->Form->file('Attachment.file');
  1140. $expected = array(
  1141. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1142. 'Attachment.file.error', 'Attachment.file.size'
  1143. );
  1144. $this->assertEquals($expected, $this->Form->fields);
  1145. }
  1146. /**
  1147. * test that multiple selects keys are added to field hash
  1148. *
  1149. * @return void
  1150. */
  1151. public function testSecuredMultipleSelect() {
  1152. $this->Form->request['_Token'] = array('key' => 'testKey');
  1153. $this->assertEquals(array(), $this->Form->fields);
  1154. $options = array('1' => 'one', '2' => 'two');
  1155. $this->Form->select('Model.select', $options);
  1156. $expected = array('Model.select');
  1157. $this->assertEquals($expected, $this->Form->fields);
  1158. $this->Form->fields = array();
  1159. $this->Form->select('Model.select', $options, array('multiple' => true));
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. }
  1162. /**
  1163. * testFormSecuredRadio method
  1164. *
  1165. * @return void
  1166. */
  1167. public function testSecuredRadio() {
  1168. $this->Form->request['_Token'] = array('key' => 'testKey');
  1169. $this->assertEquals(array(), $this->Form->fields);
  1170. $options = array('1' => 'option1', '2' => 'option2');
  1171. $this->Form->radio('Test.test', $options);
  1172. $expected = array('Test.test');
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * Test that when disabled is in a list based attribute array it works.
  1177. *
  1178. * @return void
  1179. */
  1180. public function testSecuredAndDisabledNotAssoc() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1183. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1184. $this->Form->text('Model.text', array('disabled'));
  1185. $this->Form->textarea('Model.textarea', array('disabled'));
  1186. $this->Form->password('Model.password', array('disabled'));
  1187. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1188. $expected = array(
  1189. 'Model.radio' => ''
  1190. );
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1195. * hashing.
  1196. *
  1197. * @return void
  1198. */
  1199. public function testSecuredAndDisabled() {
  1200. $this->Form->request['_Token'] = array('key' => 'testKey');
  1201. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1202. $this->Form->text('Model.text', array('disabled' => true));
  1203. $this->Form->password('Model.text', array('disabled' => true));
  1204. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1205. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1206. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1207. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1208. $this->Form->month('Model.month', array('disabled' => true));
  1209. $this->Form->day('Model.day', array('disabled' => true));
  1210. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1211. $this->Form->minute('Model.minute', array('disabled' => true));
  1212. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1213. $expected = array(
  1214. 'Model.radio' => ''
  1215. );
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. }
  1218. /**
  1219. * testDisableSecurityUsingForm method
  1220. *
  1221. * @return void
  1222. */
  1223. public function testDisableSecurityUsingForm() {
  1224. $this->Form->request['_Token'] = array(
  1225. 'key' => 'testKey',
  1226. 'disabledFields' => array()
  1227. );
  1228. $this->Form->create();
  1229. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1230. $this->Form->input('Addresses.title');
  1231. $this->Form->input('Addresses.first_name', array('secure' => false));
  1232. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1233. $this->Form->input('Addresses.zip', array(
  1234. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1235. ));
  1236. $result = $this->Form->fields;
  1237. $expected = array(
  1238. 'Addresses.id' => '123456', 'Addresses.title',
  1239. );
  1240. $this->assertEquals($expected, $result);
  1241. }
  1242. /**
  1243. * test disableField
  1244. *
  1245. * @return void
  1246. */
  1247. public function testUnlockFieldAddsToList() {
  1248. $this->Form->request['_Token'] = array(
  1249. 'key' => 'testKey',
  1250. 'unlockedFields' => array()
  1251. );
  1252. $this->Form->create('Contact');
  1253. $this->Form->unlockField('Contact.name');
  1254. $this->Form->text('Contact.name');
  1255. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1256. $this->assertEquals(array(), $this->Form->fields);
  1257. }
  1258. /**
  1259. * test unlockField removing from fields array.
  1260. *
  1261. * @return void
  1262. */
  1263. public function testUnlockFieldRemovingFromFields() {
  1264. $this->Form->request['_Token'] = array(
  1265. 'key' => 'testKey',
  1266. 'unlockedFields' => array()
  1267. );
  1268. $this->Form->create('Contact');
  1269. $this->Form->hidden('Contact.id', array('value' => 1));
  1270. $this->Form->text('Contact.name');
  1271. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1272. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1273. $this->Form->unlockField('Contact.name');
  1274. $this->Form->unlockField('Contact.id');
  1275. $this->assertEquals(array(), $this->Form->fields);
  1276. }
  1277. /**
  1278. * testTagIsInvalid method
  1279. *
  1280. * @return void
  1281. */
  1282. public function testTagIsInvalid() {
  1283. $Contact = ClassRegistry::getObject('Contact');
  1284. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1285. $this->Form->setEntity('Contact.0.email');
  1286. $result = $this->Form->tagIsInvalid();
  1287. $this->assertEquals($expected, $result);
  1288. $this->Form->setEntity('Contact.1.email');
  1289. $result = $this->Form->tagIsInvalid();
  1290. $this->assertFalse($result);
  1291. $this->Form->setEntity('Contact.0.name');
  1292. $result = $this->Form->tagIsInvalid();
  1293. $this->assertFalse($result);
  1294. }
  1295. /**
  1296. * Test tagIsInvalid with validation errors from a saveMany
  1297. *
  1298. * @return void
  1299. */
  1300. public function testTagIsInvalidSaveMany() {
  1301. $Contact = ClassRegistry::getObject('Contact');
  1302. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1303. $this->Form->create('Contact');
  1304. $this->Form->setEntity('0.email');
  1305. $result = $this->Form->tagIsInvalid();
  1306. $this->assertEquals($expected, $result);
  1307. $this->Form->setEntity('0.Contact.email');
  1308. $result = $this->Form->tagIsInvalid();
  1309. $this->assertEquals($expected, $result);
  1310. }
  1311. /**
  1312. * Test validation errors.
  1313. *
  1314. * @return void
  1315. */
  1316. public function testPasswordValidation() {
  1317. $Contact = ClassRegistry::getObject('Contact');
  1318. $Contact->validationErrors['password'] = array('Please provide a password');
  1319. $result = $this->Form->input('Contact.password');
  1320. $expected = array(
  1321. 'div' => array('class' => 'input password error'),
  1322. 'label' => array('for' => 'ContactPassword'),
  1323. 'Password',
  1324. '/label',
  1325. 'input' => array(
  1326. 'type' => 'password', 'name' => 'data[Contact][password]',
  1327. 'id' => 'ContactPassword', 'class' => 'form-error'
  1328. ),
  1329. array('div' => array('class' => 'error-message')),
  1330. 'Please provide a password',
  1331. '/div',
  1332. '/div'
  1333. );
  1334. $this->assertTags($result, $expected);
  1335. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1336. $expected = array(
  1337. 'div' => array('class' => 'input password error'),
  1338. 'label' => array('for' => 'ContactPassword'),
  1339. 'Password',
  1340. '/label',
  1341. 'input' => array(
  1342. 'type' => 'password', 'name' => 'data[Contact][password]',
  1343. 'id' => 'ContactPassword', 'class' => 'form-error'
  1344. ),
  1345. '/div'
  1346. );
  1347. $this->assertTags($result, $expected);
  1348. }
  1349. /**
  1350. * Test validation errors, when validation message is an empty string.
  1351. *
  1352. * @return void
  1353. */
  1354. public function testEmptyErrorValidation() {
  1355. $this->Form->validationErrors['Contact']['password'] = '';
  1356. $result = $this->Form->input('Contact.password');
  1357. $expected = array(
  1358. 'div' => array('class' => 'input password error'),
  1359. 'label' => array('for' => 'ContactPassword'),
  1360. 'Password',
  1361. '/label',
  1362. 'input' => array(
  1363. 'type' => 'password', 'name' => 'data[Contact][password]',
  1364. 'id' => 'ContactPassword', 'class' => 'form-error'
  1365. ),
  1366. array('div' => array('class' => 'error-message')),
  1367. array(),
  1368. '/div',
  1369. '/div'
  1370. );
  1371. $this->assertTags($result, $expected);
  1372. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1373. $expected = array(
  1374. 'div' => array('class' => 'input password error'),
  1375. 'label' => array('for' => 'ContactPassword'),
  1376. 'Password',
  1377. '/label',
  1378. 'input' => array(
  1379. 'type' => 'password', 'name' => 'data[Contact][password]',
  1380. 'id' => 'ContactPassword', 'class' => 'form-error'
  1381. ),
  1382. '/div'
  1383. );
  1384. $this->assertTags($result, $expected);
  1385. }
  1386. /**
  1387. * Test validation errors, when calling input() overriding validation message by an empty string.
  1388. *
  1389. * @return void
  1390. */
  1391. public function testEmptyInputErrorValidation() {
  1392. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1393. $result = $this->Form->input('Contact.password', array('error' => ''));
  1394. $expected = array(
  1395. 'div' => array('class' => 'input password error'),
  1396. 'label' => array('for' => 'ContactPassword'),
  1397. 'Password',
  1398. '/label',
  1399. 'input' => array(
  1400. 'type' => 'password', 'name' => 'data[Contact][password]',
  1401. 'id' => 'ContactPassword', 'class' => 'form-error'
  1402. ),
  1403. array('div' => array('class' => 'error-message')),
  1404. array(),
  1405. '/div',
  1406. '/div'
  1407. );
  1408. $this->assertTags($result, $expected);
  1409. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1410. $expected = array(
  1411. 'div' => array('class' => 'input password error'),
  1412. 'label' => array('for' => 'ContactPassword'),
  1413. 'Password',
  1414. '/label',
  1415. 'input' => array(
  1416. 'type' => 'password', 'name' => 'data[Contact][password]',
  1417. 'id' => 'ContactPassword', 'class' => 'form-error'
  1418. ),
  1419. '/div'
  1420. );
  1421. $this->assertTags($result, $expected);
  1422. }
  1423. /**
  1424. * testFormValidationAssociated method
  1425. *
  1426. * test display of form errors in conjunction with model::validates.
  1427. *
  1428. * @return void
  1429. */
  1430. public function testFormValidationAssociated() {
  1431. $this->UserForm = ClassRegistry::getObject('UserForm');
  1432. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1433. $data = array(
  1434. 'UserForm' => array('name' => 'user'),
  1435. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1436. );
  1437. $result = $this->UserForm->OpenidUrl->create($data);
  1438. $this->assertFalse(empty($result));
  1439. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1440. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1441. $encoding = strtolower(Configure::read('App.encoding'));
  1442. $expected = array(
  1443. 'form' => array(
  1444. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1445. 'accept-charset' => $encoding
  1446. ),
  1447. 'div' => array('style' => 'display:none;'),
  1448. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1449. '/div'
  1450. );
  1451. $this->assertTags($result, $expected);
  1452. $result = $this->Form->error(
  1453. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1454. );
  1455. $this->assertEquals('Error, not registered', $result);
  1456. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1457. }
  1458. /**
  1459. * testFormValidationAssociatedFirstLevel method
  1460. *
  1461. * test form error display with associated model.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormValidationAssociatedFirstLevel() {
  1466. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1467. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1468. $data = array(
  1469. 'ValidateUser' => array('name' => 'mariano'),
  1470. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1471. );
  1472. $result = $this->ValidateUser->create($data);
  1473. $this->assertFalse(empty($result));
  1474. $this->assertFalse($this->ValidateUser->validates());
  1475. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1476. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1477. $encoding = strtolower(Configure::read('App.encoding'));
  1478. $expected = array(
  1479. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1480. 'div' => array('style' => 'display:none;'),
  1481. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1482. '/div'
  1483. );
  1484. $this->assertTags($result, $expected);
  1485. $result = $this->Form->error(
  1486. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1487. );
  1488. $this->assertEquals('Invalid email', $result);
  1489. $result = $this->Form->error(
  1490. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1491. );
  1492. $this->assertEquals('Invalid name', $result);
  1493. $result = $this->Form->error(
  1494. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1495. );
  1496. $this->assertEquals('Invalid city', $result);
  1497. unset($this->ValidateUser->ValidateProfile);
  1498. unset($this->ValidateUser);
  1499. }
  1500. /**
  1501. * testFormValidationAssociatedSecondLevel method
  1502. *
  1503. * test form error display with associated model.
  1504. *
  1505. * @return void
  1506. */
  1507. public function testFormValidationAssociatedSecondLevel() {
  1508. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1509. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1510. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1511. $data = array(
  1512. 'ValidateUser' => array('name' => 'mariano'),
  1513. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1514. 'ValidateItem' => array('name' => 'Item')
  1515. );
  1516. $result = $this->ValidateUser->create($data);
  1517. $this->assertFalse(empty($result));
  1518. $this->assertFalse($this->ValidateUser->validates());
  1519. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1520. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1521. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1522. $encoding = strtolower(Configure::read('App.encoding'));
  1523. $expected = array(
  1524. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1525. 'div' => array('style' => 'display:none;'),
  1526. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1527. '/div'
  1528. );
  1529. $this->assertTags($result, $expected);
  1530. $result = $this->Form->error(
  1531. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1532. );
  1533. $this->assertEquals('Invalid email', $result);
  1534. $result = $this->Form->error(
  1535. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1536. );
  1537. $this->assertEquals('Invalid name', $result);
  1538. $result = $this->Form->error(
  1539. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1540. );
  1541. $result = $this->Form->error(
  1542. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1543. );
  1544. $this->assertEquals('Invalid description', $result);
  1545. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1546. unset($this->ValidateUser->ValidateProfile);
  1547. unset($this->ValidateUser);
  1548. }
  1549. /**
  1550. * testFormValidationMultiRecord method
  1551. *
  1552. * test form error display with multiple records.
  1553. *
  1554. * @return void
  1555. */
  1556. public function testFormValidationMultiRecord() {
  1557. $Contact = ClassRegistry::getObject('Contact');
  1558. $Contact->validationErrors[2] = array(
  1559. 'name' => array('This field cannot be left blank')
  1560. );
  1561. $result = $this->Form->input('Contact.2.name');
  1562. $expected = array(
  1563. 'div' => array('class' => 'input text error'),
  1564. 'label' => array('for' => 'Contact2Name'),
  1565. 'Name',
  1566. '/label',
  1567. 'input' => array(
  1568. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1569. 'class' => 'form-error', 'maxlength' => 255
  1570. ),
  1571. array('div' => array('class' => 'error-message')),
  1572. 'This field cannot be left blank',
  1573. '/div',
  1574. '/div'
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. }
  1578. /**
  1579. * testMultipleInputValidation method
  1580. *
  1581. * test multiple record form validation error display.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testMultipleInputValidation() {
  1586. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1587. $Address->validationErrors[0] = array(
  1588. 'title' => array('This field cannot be empty'),
  1589. 'first_name' => array('This field cannot be empty')
  1590. );
  1591. $Address->validationErrors[1] = array(
  1592. 'last_name' => array('You must have a last name')
  1593. );
  1594. $this->Form->create();
  1595. $result = $this->Form->input('Address.0.title');
  1596. $expected = array(
  1597. 'div' => array('class'),
  1598. 'label' => array('for'),
  1599. 'preg:/[^<]+/',
  1600. '/label',
  1601. 'input' => array(
  1602. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1603. ),
  1604. array('div' => array('class' => 'error-message')),
  1605. 'This field cannot be empty',
  1606. '/div',
  1607. '/div'
  1608. );
  1609. $this->assertTags($result, $expected);
  1610. $result = $this->Form->input('Address.0.first_name');
  1611. $expected = array(
  1612. 'div' => array('class'),
  1613. 'label' => array('for'),
  1614. 'preg:/[^<]+/',
  1615. '/label',
  1616. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1617. array('div' => array('class' => 'error-message')),
  1618. 'This field cannot be empty',
  1619. '/div',
  1620. '/div'
  1621. );
  1622. $this->assertTags($result, $expected);
  1623. $result = $this->Form->input('Address.0.last_name');
  1624. $expected = array(
  1625. 'div' => array('class'),
  1626. 'label' => array('for'),
  1627. 'preg:/[^<]+/',
  1628. '/label',
  1629. 'input' => array('type' => 'text', 'name', 'id'),
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->input('Address.1.last_name');
  1634. $expected = array(
  1635. 'div' => array('class'),
  1636. 'label' => array('for'),
  1637. 'preg:/[^<]+/',
  1638. '/label',
  1639. 'input' => array(
  1640. 'type' => 'text', 'name', 'id',
  1641. 'class' => 'form-error'
  1642. ),
  1643. array('div' => array('class' => 'error-message')),
  1644. 'You must have a last name',
  1645. '/div',
  1646. '/div'
  1647. );
  1648. $this->assertTags($result, $expected);
  1649. }
  1650. /**
  1651. * testInput method
  1652. *
  1653. * Test various incarnations of input().
  1654. *
  1655. * @return void
  1656. */
  1657. public function testInput() {
  1658. $result = $this->Form->input('ValidateUser.balance');
  1659. $expected = array(
  1660. 'div' => array('class'),
  1661. 'label' => array('for'),
  1662. 'Balance',
  1663. '/label',
  1664. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1665. '/div',
  1666. );
  1667. $this->assertTags($result, $expected);
  1668. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1669. $expected = array(
  1670. 'div' => array('class' => 'input email'),
  1671. 'label' => array('for' => 'custom'),
  1672. 'Email',
  1673. '/label',
  1674. array('input' => array(
  1675. 'type' => 'email', 'name' => 'data[Contact][email]',
  1676. 'id' => 'custom', 'maxlength' => 255
  1677. )),
  1678. '/div'
  1679. );
  1680. $this->assertTags($result, $expected);
  1681. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1682. $expected = array(
  1683. '<div',
  1684. 'label' => array('for' => 'ContactEmail'),
  1685. 'Email',
  1686. '/label',
  1687. array('input' => array(
  1688. 'type' => 'email', 'name' => 'data[Contact][email]',
  1689. 'id' => 'ContactEmail', 'maxlength' => 255
  1690. )),
  1691. '/div'
  1692. );
  1693. $this->assertTags($result, $expected);
  1694. $result = $this->Form->hidden('Contact.idontexist');
  1695. $expected = array('input' => array(
  1696. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1697. 'id' => 'ContactIdontexist'
  1698. ));
  1699. $this->assertTags($result, $expected);
  1700. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1701. $expected = array(
  1702. 'div' => array('class' => 'input text'),
  1703. 'label' => array('for' => 'ContactEmail'),
  1704. 'Email',
  1705. '/label',
  1706. array('input' => array(
  1707. 'type' => 'text', 'name' => 'data[Contact][email]',
  1708. 'id' => 'ContactEmail'
  1709. )),
  1710. '/div'
  1711. );
  1712. $this->assertTags($result, $expected);
  1713. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1714. $expected = array(
  1715. 'div' => array('class' => 'input text'),
  1716. 'label' => array('for' => 'Contact5Email'),
  1717. 'Email',
  1718. '/label',
  1719. array('input' => array(
  1720. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1721. 'id' => 'Contact5Email'
  1722. )),
  1723. '/div'
  1724. );
  1725. $this->assertTags($result, $expected);
  1726. $result = $this->Form->input('Contact.password');
  1727. $expected = array(
  1728. 'div' => array('class' => 'input password'),
  1729. 'label' => array('for' => 'ContactPassword'),
  1730. 'Password',
  1731. '/label',
  1732. array('input' => array(
  1733. 'type' => 'password', 'name' => 'data[Contact][password]',
  1734. 'id' => 'ContactPassword'
  1735. )),
  1736. '/div'
  1737. );
  1738. $this->assertTags($result, $expected);
  1739. $result = $this->Form->input('Contact.email', array(
  1740. 'type' => 'file', 'class' => 'textbox'
  1741. ));
  1742. $expected = array(
  1743. 'div' => array('class' => 'input file'),
  1744. 'label' => array('for' => 'ContactEmail'),
  1745. 'Email',
  1746. '/label',
  1747. array('input' => array(
  1748. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1749. 'id' => 'ContactEmail'
  1750. )),
  1751. '/div'
  1752. );
  1753. $this->assertTags($result, $expected);
  1754. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1755. $result = $this->Form->input('Contact.phone');
  1756. $expected = array(
  1757. 'div' => array('class' => 'input tel'),
  1758. 'label' => array('for' => 'ContactPhone'),
  1759. 'Phone',
  1760. '/label',
  1761. array('input' => array(
  1762. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1763. 'value' => 'Hello &amp; World &gt; weird chars',
  1764. 'id' => 'ContactPhone', 'maxlength' => 255
  1765. )),
  1766. '/div'
  1767. );
  1768. $this->assertTags($result, $expected);
  1769. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1770. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1771. $expected = array(
  1772. 'div' => array('class' => 'input text'),
  1773. 'label' => array('for' => 'myId'),
  1774. 'Field',
  1775. '/label',
  1776. 'input' => array(
  1777. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1778. 'value' => 'My value', 'id' => 'myId'
  1779. ),
  1780. '/div'
  1781. );
  1782. $this->assertTags($result, $expected);
  1783. unset($this->Form->request->data);
  1784. $Contact = ClassRegistry::getObject('Contact');
  1785. $Contact->validationErrors['field'] = array('Badness!');
  1786. $result = $this->Form->input('Contact.field');
  1787. $expected = array(
  1788. 'div' => array('class' => 'input text error'),
  1789. 'label' => array('for' => 'ContactField'),
  1790. 'Field',
  1791. '/label',
  1792. 'input' => array(
  1793. 'type' => 'text', 'name' => 'data[Contact][field]',
  1794. 'id' => 'ContactField', 'class' => 'form-error'
  1795. ),
  1796. array('div' => array('class' => 'error-message')),
  1797. 'Badness!',
  1798. '/div',
  1799. '/div'
  1800. );
  1801. $this->assertTags($result, $expected);
  1802. $result = $this->Form->input('Contact.field', array(
  1803. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1804. ));
  1805. $expected = array(
  1806. 'label' => array('for' => 'ContactField'),
  1807. 'Field',
  1808. '/label',
  1809. 'input' => array(
  1810. 'type' => 'text', 'name' => 'data[Contact][field]',
  1811. 'id' => 'ContactField', 'class' => 'form-error'
  1812. ),
  1813. array('span' => array('class' => 'error-message')),
  1814. 'Badness!',
  1815. '/span'
  1816. );
  1817. $this->assertTags($result, $expected);
  1818. $result = $this->Form->input('Contact.field', array(
  1819. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1820. ));
  1821. $expected = array(
  1822. 'div' => array('class' => 'input text error'),
  1823. 'label' => array('for' => 'ContactField'),
  1824. 'Field',
  1825. '/label',
  1826. 'input' => array(
  1827. 'type' => 'text', 'name' => 'data[Contact][field]',
  1828. 'id' => 'ContactField', 'class' => 'form-error'
  1829. ),
  1830. array('div' => array('class' => 'error')),
  1831. 'Badness!',
  1832. '/div'
  1833. );
  1834. $this->assertTags($result, $expected);
  1835. $result = $this->Form->input('Contact.field', array(
  1836. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1837. ));
  1838. $expected = array(
  1839. 'span' => array('class' => 'input text error'),
  1840. 'label' => array('for' => 'ContactField'),
  1841. 'Field',
  1842. '/label',
  1843. 'input' => array(
  1844. 'type' => 'text', 'name' => 'data[Contact][field]',
  1845. 'id' => 'ContactField', 'class' => 'form-error'
  1846. ),
  1847. 'Badness!',
  1848. '/span'
  1849. );
  1850. $this->assertTags($result, $expected);
  1851. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1852. $expected = array(
  1853. 'div' => array('class' => 'input text error'),
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. 'A message to you, Rudy',
  1862. array('div' => array('class' => 'error-message')),
  1863. 'Badness!',
  1864. '/div',
  1865. '/div'
  1866. );
  1867. $this->assertTags($result, $expected);
  1868. $this->Form->setEntity(null);
  1869. $this->Form->setEntity('Contact.field');
  1870. $result = $this->Form->input('Contact.field', array(
  1871. 'after' => 'A message to you, Rudy', 'error' => false
  1872. ));
  1873. $expected = array(
  1874. 'div' => array('class' => 'input text'),
  1875. 'label' => array('for' => 'ContactField'),
  1876. 'Field',
  1877. '/label',
  1878. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1879. 'A message to you, Rudy',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1884. $expected = array(
  1885. 'div' => array('class' => 'input text'),
  1886. 'label' => array('for' => 'ObjectField'),
  1887. 'Field',
  1888. '/label',
  1889. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1890. 'A message to you, Rudy',
  1891. '/div'
  1892. );
  1893. $this->assertTags($result, $expected);
  1894. $Contact->validationErrors['field'] = array('minLength');
  1895. $result = $this->Form->input('Contact.field', array(
  1896. 'error' => array(
  1897. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1898. 'maxLength' => 'login too large'
  1899. )
  1900. ));
  1901. $expected = array(
  1902. 'div' => array('class' => 'input text error'),
  1903. 'label' => array('for' => 'ContactField'),
  1904. 'Field',
  1905. '/label',
  1906. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1907. array('div' => array('class' => 'error-message')),
  1908. 'Le login doit contenir au moins 2 caractères',
  1909. '/div',
  1910. '/div'
  1911. );
  1912. $this->assertTags($result, $expected);
  1913. $Contact->validationErrors['field'] = array('maxLength');
  1914. $result = $this->Form->input('Contact.field', array(
  1915. 'error' => array(
  1916. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1917. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1918. 'maxLength' => 'login too large',
  1919. )
  1920. ));
  1921. $expected = array(
  1922. 'div' => array('class' => 'input text error'),
  1923. 'label' => array('for' => 'ContactField'),
  1924. 'Field',
  1925. '/label',
  1926. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1927. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1928. 'login too large',
  1929. '/span',
  1930. '/div'
  1931. );
  1932. $this->assertTags($result, $expected);
  1933. }
  1934. /**
  1935. * Test that inputs with 0 can be created.
  1936. *
  1937. * @return void
  1938. */
  1939. public function testInputZero() {
  1940. $this->Form->create('User');
  1941. $result = $this->Form->input('0');
  1942. $expected = array(
  1943. 'div' => array('class' => 'input text'),
  1944. 'label' => array('for' => 'User0'), '/label',
  1945. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. }
  1950. /**
  1951. * test input() with checkbox creation
  1952. *
  1953. * @return void
  1954. */
  1955. public function testInputCheckbox() {
  1956. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1957. $expected = array(
  1958. 'div' => array('class' => 'input checkbox'),
  1959. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1960. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1961. '/div'
  1962. );
  1963. $this->assertTags($result, $expected);
  1964. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1965. $expected = array(
  1966. 'div' => array('class' => 'input checkbox'),
  1967. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1968. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1969. '/div'
  1970. );
  1971. $this->assertTags($result, $expected);
  1972. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1973. $expected = array(
  1974. 'div' => array('class' => 'input checkbox'),
  1975. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1976. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. $result = $this->Form->input('User.disabled', array(
  1981. 'label' => 'Disabled',
  1982. 'type' => 'checkbox',
  1983. 'data-foo' => 'disabled'
  1984. ));
  1985. $expected = array(
  1986. 'div' => array('class' => 'input checkbox'),
  1987. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1988. array('input' => array(
  1989. 'type' => 'checkbox',
  1990. 'name' => 'data[User][disabled]',
  1991. 'value' => '1',
  1992. 'id' => 'UserDisabled',
  1993. 'data-foo' => 'disabled'
  1994. )),
  1995. 'label' => array('for' => 'UserDisabled'),
  1996. 'Disabled',
  1997. '/label',
  1998. '/div'
  1999. );
  2000. $this->assertTags($result, $expected);
  2001. }
  2002. /**
  2003. * test form->input() with time types.
  2004. *
  2005. * @return void
  2006. */
  2007. public function testInputTime() {
  2008. extract($this->dateRegex);
  2009. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2010. $result = explode(':', $result);
  2011. $this->assertRegExp('/option value="23"/', $result[0]);
  2012. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2013. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2014. $result = explode(':', $result);
  2015. $this->assertRegExp('/option value="23"/', $result[0]);
  2016. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2017. $result = $this->Form->input('Model.field', array(
  2018. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2019. ));
  2020. $result = explode(':', $result);
  2021. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2022. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2023. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2024. $result = $this->Form->input('Model.field', array(
  2025. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2026. ));
  2027. $result = explode(':', $result);
  2028. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2029. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2030. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2031. $result = $this->Form->input('prueba', array(
  2032. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2033. 'maxYear' => date('Y') + 1, 'interval' => 15
  2034. ));
  2035. $result = explode(':', $result);
  2036. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2037. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2038. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2039. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2040. $result = $this->Form->input('Random.start_time', array(
  2041. 'type' => 'time',
  2042. 'selected' => '18:15'
  2043. ));
  2044. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2045. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2046. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2047. $result = $this->Form->input('published', array('type' => 'time'));
  2048. $now = strtotime('now');
  2049. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2050. $now = strtotime('2013-03-09 00:42:21');
  2051. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2052. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2053. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2054. }
  2055. /**
  2056. * Test interval + selected near the hour roll over.
  2057. *
  2058. * @return void
  2059. */
  2060. public function testTimeSelectedWithInterval() {
  2061. $result = $this->Form->input('Model.start_time', array(
  2062. 'type' => 'time',
  2063. 'interval' => 15,
  2064. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2065. ));
  2066. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2067. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2068. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2069. $result = $this->Form->input('Model.start_time', array(
  2070. 'type' => 'time',
  2071. 'interval' => 15,
  2072. 'selected' => '2012-10-23 15:57:00'
  2073. ));
  2074. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2075. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2076. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2077. $result = $this->Form->input('Model.start_time', array(
  2078. 'timeFormat' => 24,
  2079. 'type' => 'time',
  2080. 'interval' => 15,
  2081. 'selected' => '15:57'
  2082. ));
  2083. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2084. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2085. $result = $this->Form->input('Model.start_time', array(
  2086. 'timeFormat' => 24,
  2087. 'type' => 'time',
  2088. 'interval' => 15,
  2089. 'selected' => '23:57'
  2090. ));
  2091. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2092. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2093. $result = $this->Form->input('Model.created', array(
  2094. 'timeFormat' => 24,
  2095. 'type' => 'datetime',
  2096. 'interval' => 15,
  2097. 'selected' => '2012-09-30 23:56'
  2098. ));
  2099. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2100. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2101. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2102. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2103. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2104. }
  2105. /**
  2106. * Test time with selected values around 12:xx:xx
  2107. *
  2108. * @return void
  2109. */
  2110. public function testTimeSelectedWithIntervalTwelve() {
  2111. $result = $this->Form->input('Model.start_time', array(
  2112. 'type' => 'time',
  2113. 'timeFormat' => 12,
  2114. 'interval' => 15,
  2115. 'selected' => '00:00:00'
  2116. ));
  2117. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2118. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2119. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2120. $result = $this->Form->input('Model.start_time', array(
  2121. 'type' => 'time',
  2122. 'timeFormat' => 12,
  2123. 'interval' => 15,
  2124. 'selected' => '12:00:00'
  2125. ));
  2126. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2127. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2128. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2129. $result = $this->Form->input('Model.start_time', array(
  2130. 'type' => 'time',
  2131. 'timeFormat' => 12,
  2132. 'interval' => 15,
  2133. 'selected' => '12:15:00'
  2134. ));
  2135. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2136. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2137. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2138. }
  2139. /**
  2140. * Test interval & timeFormat = 12
  2141. *
  2142. * @return void
  2143. */
  2144. public function testInputTimeWithIntervalAnd12HourFormat() {
  2145. $result = $this->Form->input('Model.start_time', array(
  2146. 'type' => 'time',
  2147. 'timeFormat' => 12,
  2148. 'interval' => 5,
  2149. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2150. ));
  2151. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2152. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2153. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2154. $result = $this->Form->input('Model.start_time', array(
  2155. 'type' => 'time',
  2156. 'timeFormat' => '12',
  2157. 'interval' => 5,
  2158. 'selected' => '2013-04-19 16:30:00'
  2159. ));
  2160. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2161. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2162. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2163. $result = $this->Form->input('Model.start_time', array(
  2164. 'type' => 'time',
  2165. 'timeFormat' => '12',
  2166. 'interval' => 10,
  2167. 'selected' => '2013-05-19 00:33:00'
  2168. ));
  2169. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2170. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2171. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2172. $result = $this->Form->input('Model.start_time', array(
  2173. 'type' => 'time',
  2174. 'timeFormat' => '12',
  2175. 'interval' => 10,
  2176. 'selected' => '2013-05-19 13:33:00'
  2177. ));
  2178. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2179. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2180. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2181. $result = $this->Form->input('Model.start_time', array(
  2182. 'type' => 'time',
  2183. 'timeFormat' => '12',
  2184. 'interval' => 10,
  2185. 'selected' => '2013-05-19 01:33:00'
  2186. ));
  2187. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2188. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2189. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2190. }
  2191. /**
  2192. * test form->input() with datetime, date and time types
  2193. *
  2194. * @return void
  2195. */
  2196. public function testInputDatetime() {
  2197. extract($this->dateRegex);
  2198. $result = $this->Form->input('prueba', array(
  2199. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2200. 'maxYear' => date('Y') + 1, 'interval' => 15
  2201. ));
  2202. $result = explode(':', $result);
  2203. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2204. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2205. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2206. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2207. //related to ticket #5013
  2208. $result = $this->Form->input('Contact.date', array(
  2209. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2210. ));
  2211. $this->assertRegExp('/class="customClass"/', $result);
  2212. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2213. $result = $this->Form->input('Contact.date', array(
  2214. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2215. ));
  2216. $this->assertRegExp('/id="customIdDay"/', $result);
  2217. $this->assertRegExp('/id="customIdMonth"/', $result);
  2218. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2219. $result = $this->Form->input('Model.field', array(
  2220. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2221. ));
  2222. $this->assertRegExp('/id="customIDDay"/', $result);
  2223. $this->assertRegExp('/id="customIDHour"/', $result);
  2224. $result = explode('</select><select', $result);
  2225. $result = explode(':', $result[1]);
  2226. $this->assertRegExp('/option value="23"/', $result[0]);
  2227. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2228. $result = $this->Form->input('Model.field', array(
  2229. 'type' => 'datetime', 'timeFormat' => 12
  2230. ));
  2231. $result = explode('</select><select', $result);
  2232. $result = explode(':', $result[1]);
  2233. $this->assertRegExp('/option value="12"/', $result[0]);
  2234. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2235. $this->Form->request->data = array('Contact' => array('created' => null));
  2236. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2237. $expected = array(
  2238. 'div' => array('class' => 'input date'),
  2239. 'label' => array('for' => 'ContactCreatedMonth'),
  2240. 'Created',
  2241. '/label',
  2242. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2243. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2244. $monthsRegex,
  2245. '/select', '-',
  2246. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2247. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2248. $daysRegex,
  2249. '/select', '-',
  2250. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2251. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2252. $yearsRegex,
  2253. '/select',
  2254. '/div'
  2255. );
  2256. $this->assertTags($result, $expected);
  2257. $this->Form->request->data = array('Contact' => array('created' => null));
  2258. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2259. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2260. $this->Form->request->data = array('Contact' => array('created' => null));
  2261. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2262. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2263. $result = $this->Form->input('Contact.created', array(
  2264. 'type' => 'date',
  2265. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2266. 'timeFormat' => 'NONE'
  2267. ));
  2268. $this->assertRegExp('/for\="created-month"/', $result);
  2269. }
  2270. /**
  2271. * Test generating checkboxes in a loop.
  2272. *
  2273. * @return void
  2274. */
  2275. public function testInputCheckboxesInLoop() {
  2276. for ($i = 1; $i < 5; $i++) {
  2277. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2278. $expected = array(
  2279. 'div' => array('class' => 'input checkbox'),
  2280. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2281. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2282. 'label' => array('for' => "Contact{$i}Email"),
  2283. 'Email',
  2284. '/label',
  2285. '/div'
  2286. );
  2287. $this->assertTags($result, $expected);
  2288. }
  2289. }
  2290. /**
  2291. * Test generating checkboxes with disabled elements.
  2292. *
  2293. * @return void
  2294. */
  2295. public function testInputCheckboxWithDisabledElements() {
  2296. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2297. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2298. $expected = array(
  2299. array('div' => array('class' => 'input select')),
  2300. array('label' => array('for' => "ContactMultiple")),
  2301. 'Multiple',
  2302. '/label',
  2303. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2304. array('div' => array('class' => 'checkbox')),
  2305. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2306. array('label' => array('for' => "ContactMultiple1")),
  2307. 'One',
  2308. '/label',
  2309. '/div',
  2310. array('div' => array('class' => 'checkbox')),
  2311. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2312. array('label' => array('for' => "ContactMultiple2")),
  2313. 'Two',
  2314. '/label',
  2315. '/div',
  2316. array('div' => array('class' => 'checkbox')),
  2317. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2318. array('label' => array('for' => "ContactMultiple3")),
  2319. 'Three',
  2320. '/label',
  2321. '/div',
  2322. '/div'
  2323. );
  2324. $this->assertTags($result, $expected);
  2325. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2326. $this->assertTags($result, $expected);
  2327. $disabled = array('2', 3);
  2328. $expected = array(
  2329. array('div' => array('class' => 'input select')),
  2330. array('label' => array('for' => "ContactMultiple")),
  2331. 'Multiple',
  2332. '/label',
  2333. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2334. array('div' => array('class' => 'checkbox')),
  2335. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2336. array('label' => array('for' => "ContactMultiple1")),
  2337. 'One',
  2338. '/label',
  2339. '/div',
  2340. array('div' => array('class' => 'checkbox')),
  2341. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2342. array('label' => array('for' => "ContactMultiple2")),
  2343. 'Two',
  2344. '/label',
  2345. '/div',
  2346. array('div' => array('class' => 'checkbox')),
  2347. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2348. array('label' => array('for' => "ContactMultiple3")),
  2349. 'Three',
  2350. '/label',
  2351. '/div',
  2352. '/div'
  2353. );
  2354. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2355. $this->assertTags($result, $expected);
  2356. // make sure 50 does only disable 50, and not 50f5c0cf
  2357. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2358. $disabled = array(50);
  2359. $expected = array(
  2360. array('div' => array('class' => 'input select')),
  2361. array('label' => array('for' => "ContactMultiple")),
  2362. 'Multiple',
  2363. '/label',
  2364. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2365. array('div' => array('class' => 'checkbox')),
  2366. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2367. array('label' => array('for' => "ContactMultiple50")),
  2368. 'Fifty',
  2369. '/label',
  2370. '/div',
  2371. array('div' => array('class' => 'checkbox')),
  2372. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2373. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2374. 'Stringy',
  2375. '/label',
  2376. '/div',
  2377. '/div'
  2378. );
  2379. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2380. $this->assertTags($result, $expected);
  2381. }
  2382. /**
  2383. * test input name with leading integer, ensure attributes are generated correctly.
  2384. *
  2385. * @return void
  2386. */
  2387. public function testInputWithLeadingInteger() {
  2388. $result = $this->Form->text('0.Node.title');
  2389. $expected = array(
  2390. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2391. );
  2392. $this->assertTags($result, $expected);
  2393. }
  2394. /**
  2395. * test form->input() with select type inputs.
  2396. *
  2397. * @return void
  2398. */
  2399. public function testInputSelectType() {
  2400. $result = $this->Form->input('email', array(
  2401. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2402. );
  2403. $expected = array(
  2404. 'div' => array('class' => 'input select'),
  2405. 'label' => array('for' => 'email'),
  2406. 'Email',
  2407. '/label',
  2408. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2409. array('option' => array('value' => '')),
  2410. '/option',
  2411. array('option' => array('value' => 'è')),
  2412. 'Firést',
  2413. '/option',
  2414. array('option' => array('value' => 'é')),
  2415. 'Secoènd',
  2416. '/option',
  2417. '/select',
  2418. '/div'
  2419. );
  2420. $this->assertTags($result, $expected);
  2421. $result = $this->Form->input('email', array(
  2422. 'options' => array('First', 'Second'), 'empty' => true)
  2423. );
  2424. $expected = array(
  2425. 'div' => array('class' => 'input select'),
  2426. 'label' => array('for' => 'email'),
  2427. 'Email',
  2428. '/label',
  2429. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2430. array('option' => array('value' => '')),
  2431. '/option',
  2432. array('option' => array('value' => '0')),
  2433. 'First',
  2434. '/option',
  2435. array('option' => array('value' => '1')),
  2436. 'Second',
  2437. '/option',
  2438. '/select',
  2439. '/div'
  2440. );
  2441. $this->assertTags($result, $expected);
  2442. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2443. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2444. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2445. $expected = array(
  2446. 'div' => array('class' => 'input select'),
  2447. 'label' => array('for' => 'ModelUserId'),
  2448. 'User',
  2449. '/label',
  2450. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2451. array('option' => array('value' => '')),
  2452. '/option',
  2453. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2454. 'good',
  2455. '/option',
  2456. array('option' => array('value' => 'other')),
  2457. 'bad',
  2458. '/option',
  2459. '/select',
  2460. '/div'
  2461. );
  2462. $this->assertTags($result, $expected);
  2463. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2464. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2465. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2466. $expected = array(
  2467. 'div' => array('class' => 'input select'),
  2468. 'label' => array('for' => 'ThingUserId'),
  2469. 'User',
  2470. '/label',
  2471. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2472. array('option' => array('value' => '')),
  2473. 'Some Empty',
  2474. '/option',
  2475. array('option' => array('value' => 'value')),
  2476. 'good',
  2477. '/option',
  2478. array('option' => array('value' => 'other')),
  2479. 'bad',
  2480. '/option',
  2481. '/select',
  2482. '/div'
  2483. );
  2484. $this->assertTags($result, $expected);
  2485. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2486. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2487. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2488. $expected = array(
  2489. 'div' => array('class' => 'input select'),
  2490. 'label' => array('for' => 'ThingUserId'),
  2491. 'User',
  2492. '/label',
  2493. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2494. array('option' => array('value' => '')),
  2495. 'Some Empty',
  2496. '/option',
  2497. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2498. 'good',
  2499. '/option',
  2500. array('option' => array('value' => 'other')),
  2501. 'bad',
  2502. '/option',
  2503. '/select',
  2504. '/div'
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2508. $this->Form->request->data = array('User' => array('User' => array('value')));
  2509. $result = $this->Form->input('User.User', array('empty' => true));
  2510. $expected = array(
  2511. 'div' => array('class' => 'input select'),
  2512. 'label' => array('for' => 'UserUser'),
  2513. 'User',
  2514. '/label',
  2515. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2516. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2517. array('option' => array('value' => '')),
  2518. '/option',
  2519. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2520. 'good',
  2521. '/option',
  2522. array('option' => array('value' => 'other')),
  2523. 'bad',
  2524. '/option',
  2525. '/select',
  2526. '/div'
  2527. );
  2528. $this->assertTags($result, $expected);
  2529. $this->Form->data = array();
  2530. $result = $this->Form->input('Publisher.id', array(
  2531. 'label' => 'Publisher',
  2532. 'type' => 'select',
  2533. 'multiple' => 'checkbox',
  2534. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2535. ));
  2536. $expected = array(
  2537. array('div' => array('class' => 'input select')),
  2538. array('label' => array('for' => 'PublisherId')),
  2539. 'Publisher',
  2540. '/label',
  2541. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2542. array('div' => array('class' => 'checkbox')),
  2543. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2544. array('label' => array('for' => 'PublisherIdValue1')),
  2545. 'Label 1',
  2546. '/label',
  2547. '/div',
  2548. array('div' => array('class' => 'checkbox')),
  2549. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2550. array('label' => array('for' => 'PublisherIdValue2')),
  2551. 'Label 2',
  2552. '/label',
  2553. '/div',
  2554. '/div'
  2555. );
  2556. $this->assertTags($result, $expected);
  2557. }
  2558. /**
  2559. * test that input() and a non standard primary key makes a hidden input by default.
  2560. *
  2561. * @return void
  2562. */
  2563. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2564. $this->Form->create('User');
  2565. $this->Form->fieldset = array(
  2566. 'User' => array(
  2567. 'fields' => array(
  2568. 'model_id' => array('type' => 'integer')
  2569. ),
  2570. 'validates' => array(),
  2571. 'key' => 'model_id'
  2572. )
  2573. );
  2574. $result = $this->Form->input('model_id');
  2575. $expected = array(
  2576. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2577. );
  2578. $this->assertTags($result, $expected);
  2579. }
  2580. /**
  2581. * test that overriding the magic select type widget is possible
  2582. *
  2583. * @return void
  2584. */
  2585. public function testInputOverridingMagicSelectType() {
  2586. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2587. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2588. $expected = array(
  2589. 'div' => array('class' => 'input text'),
  2590. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2591. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2592. '/div'
  2593. );
  2594. $this->assertTags($result, $expected);
  2595. //Check that magic types still work for plural/singular vars
  2596. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2597. $result = $this->Form->input('Model.type');
  2598. $expected = array(
  2599. 'div' => array('class' => 'input select'),
  2600. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2601. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2602. array('option' => array('value' => 'value')), 'good', '/option',
  2603. array('option' => array('value' => 'other')), 'bad', '/option',
  2604. '/select',
  2605. '/div'
  2606. );
  2607. $this->assertTags($result, $expected);
  2608. }
  2609. /**
  2610. * Test that inferred types do not override developer input
  2611. *
  2612. * @return void
  2613. */
  2614. public function testInputMagicTypeDoesNotOverride() {
  2615. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2616. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2617. $expected = array(
  2618. 'div' => array('class' => 'input checkbox'),
  2619. array('input' => array(
  2620. 'type' => 'hidden',
  2621. 'name' => 'data[Model][user]',
  2622. 'id' => 'ModelUser_',
  2623. 'value' => 0,
  2624. )),
  2625. array('input' => array(
  2626. 'name' => 'data[Model][user]',
  2627. 'type' => 'checkbox',
  2628. 'id' => 'ModelUser',
  2629. 'value' => 1
  2630. )),
  2631. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2632. '/div'
  2633. );
  2634. $this->assertTags($result, $expected);
  2635. }
  2636. /**
  2637. * Test that magic input() selects are created for type=number
  2638. *
  2639. * @return void
  2640. */
  2641. public function testInputMagicSelectForTypeNumber() {
  2642. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2643. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2644. $result = $this->Form->input('ValidateUser.balance');
  2645. $expected = array(
  2646. 'div' => array('class' => 'input select'),
  2647. 'label' => array('for' => 'ValidateUserBalance'),
  2648. 'Balance',
  2649. '/label',
  2650. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2651. array('option' => array('value' => '0')),
  2652. 'nothing',
  2653. '/option',
  2654. array('option' => array('value' => '1', 'selected' => 'selected')),
  2655. 'some',
  2656. '/option',
  2657. array('option' => array('value' => '100')),
  2658. 'a lot',
  2659. '/option',
  2660. '/select',
  2661. '/div'
  2662. );
  2663. $this->assertTags($result, $expected);
  2664. }
  2665. /**
  2666. * Test that magic input() selects can easily be converted into radio types without error.
  2667. *
  2668. * @return void
  2669. */
  2670. public function testInputMagicSelectChangeToRadio() {
  2671. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2672. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2673. $this->assertRegExp('/input type="radio"/', $result);
  2674. }
  2675. /**
  2676. * fields with the same name as the model should work.
  2677. *
  2678. * @return void
  2679. */
  2680. public function testInputWithMatchingFieldAndModelName() {
  2681. $this->Form->create('User');
  2682. $this->Form->fieldset = array(
  2683. 'User' => array(
  2684. 'fields' => array(
  2685. 'User' => array('type' => 'text')
  2686. ),
  2687. 'validates' => array(),
  2688. 'key' => 'id'
  2689. )
  2690. );
  2691. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2692. $result = $this->Form->input('User', array('type' => 'text'));
  2693. $expected = array(
  2694. 'div' => array('class' => 'input text'),
  2695. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2696. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2697. '/div'
  2698. );
  2699. $this->assertTags($result, $expected);
  2700. }
  2701. /**
  2702. * testFormInputs method
  2703. *
  2704. * test correct results from form::inputs().
  2705. *
  2706. * @return void
  2707. */
  2708. public function testFormInputs() {
  2709. $this->Form->create('Contact');
  2710. $result = $this->Form->inputs('The Legend');
  2711. $expected = array(
  2712. '<fieldset',
  2713. '<legend',
  2714. 'The Legend',
  2715. '/legend',
  2716. '*/fieldset',
  2717. );
  2718. $this->assertTags($result, $expected);
  2719. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2720. $expected = array(
  2721. 'fieldset' => array('class' => 'classy-stuff'),
  2722. '<legend',
  2723. 'Field of Dreams',
  2724. '/legend',
  2725. '*/fieldset'
  2726. );
  2727. $this->assertTags($result, $expected);
  2728. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2729. $this->assertTags($result, $expected);
  2730. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2731. $this->assertTags($result, $expected);
  2732. $this->Form->create('Contact');
  2733. $this->Form->request['prefix'] = 'admin';
  2734. $this->Form->request['action'] = 'admin_edit';
  2735. $result = $this->Form->inputs();
  2736. $expected = array(
  2737. '<fieldset',
  2738. '<legend',
  2739. 'Edit Contact',
  2740. '/legend',
  2741. '*/fieldset',
  2742. );
  2743. $this->assertTags($result, $expected);
  2744. $this->Form->create('Contact');
  2745. $result = $this->Form->inputs(false);
  2746. $expected = array(
  2747. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2748. array('div' => array('class' => 'input text')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input email')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input tel')),
  2753. '*/div',
  2754. array('div' => array('class' => 'input password')),
  2755. '*/div',
  2756. array('div' => array('class' => 'input date')),
  2757. '*/div',
  2758. array('div' => array('class' => 'input date')),
  2759. '*/div',
  2760. array('div' => array('class' => 'input datetime')),
  2761. '*/div',
  2762. array('div' => array('class' => 'input number')),
  2763. '*/div',
  2764. array('div' => array('class' => 'input select')),
  2765. '*/div',
  2766. );
  2767. $this->assertTags($result, $expected);
  2768. $this->Form->create('Contact');
  2769. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2770. $expected = array(
  2771. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2772. array('div' => array('class' => 'input text')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input email')),
  2775. '*/div',
  2776. array('div' => array('class' => 'input tel')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input password')),
  2779. '*/div',
  2780. array('div' => array('class' => 'input date')),
  2781. '*/div',
  2782. array('div' => array('class' => 'input date')),
  2783. '*/div',
  2784. array('div' => array('class' => 'input datetime')),
  2785. '*/div',
  2786. array('div' => array('class' => 'input number')),
  2787. '*/div',
  2788. array('div' => array('class' => 'input select')),
  2789. '*/div',
  2790. );
  2791. $this->assertTags($result, $expected);
  2792. $this->Form->create('Contact');
  2793. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2794. $this->assertTags($result, $expected);
  2795. $this->Form->create('Contact');
  2796. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2797. $expected = array(
  2798. 'fieldset' => array(),
  2799. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2800. array('div' => array('class' => 'input text')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input email')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input tel')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input password')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input date')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input date')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input datetime')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input number')),
  2815. '*/div',
  2816. array('div' => array('class' => 'input select')),
  2817. '*/div',
  2818. '/fieldset'
  2819. );
  2820. $this->assertTags($result, $expected);
  2821. $this->Form->create('Contact');
  2822. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2823. $expected = array(
  2824. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2825. array('div' => array('class' => 'input text')),
  2826. '*/div',
  2827. array('div' => array('class' => 'input email')),
  2828. '*/div',
  2829. array('div' => array('class' => 'input tel')),
  2830. '*/div',
  2831. array('div' => array('class' => 'input password')),
  2832. '*/div',
  2833. array('div' => array('class' => 'input date')),
  2834. '*/div',
  2835. array('div' => array('class' => 'input date')),
  2836. '*/div',
  2837. array('div' => array('class' => 'input datetime')),
  2838. '*/div',
  2839. array('div' => array('class' => 'input number')),
  2840. '*/div',
  2841. array('div' => array('class' => 'input select')),
  2842. '*/div',
  2843. );
  2844. $this->assertTags($result, $expected);
  2845. $this->Form->create('Contact');
  2846. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2847. $this->assertTags($result, $expected);
  2848. $this->Form->create('Contact');
  2849. $result = $this->Form->inputs('Hello');
  2850. $expected = array(
  2851. 'fieldset' => array(),
  2852. 'legend' => array(),
  2853. 'Hello',
  2854. '/legend',
  2855. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2856. array('div' => array('class' => 'input text')),
  2857. '*/div',
  2858. array('div' => array('class' => 'input email')),
  2859. '*/div',
  2860. array('div' => array('class' => 'input tel')),
  2861. '*/div',
  2862. array('div' => array('class' => 'input password')),
  2863. '*/div',
  2864. array('div' => array('class' => 'input date')),
  2865. '*/div',
  2866. array('div' => array('class' => 'input date')),
  2867. '*/div',
  2868. array('div' => array('class' => 'input datetime')),
  2869. '*/div',
  2870. array('div' => array('class' => 'input number')),
  2871. '*/div',
  2872. array('div' => array('class' => 'input select')),
  2873. '*/div',
  2874. '/fieldset'
  2875. );
  2876. $this->assertTags($result, $expected);
  2877. $this->Form->create('Contact');
  2878. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2879. $expected = array(
  2880. 'fieldset' => array(),
  2881. 'legend' => array(),
  2882. 'Hello',
  2883. '/legend',
  2884. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2885. array('div' => array('class' => 'input text')),
  2886. '*/div',
  2887. array('div' => array('class' => 'input email')),
  2888. '*/div',
  2889. array('div' => array('class' => 'input tel')),
  2890. '*/div',
  2891. array('div' => array('class' => 'input password')),
  2892. '*/div',
  2893. array('div' => array('class' => 'input date')),
  2894. '*/div',
  2895. array('div' => array('class' => 'input date')),
  2896. '*/div',
  2897. array('div' => array('class' => 'input datetime')),
  2898. '*/div',
  2899. array('div' => array('class' => 'input number')),
  2900. '*/div',
  2901. array('div' => array('class' => 'input select')),
  2902. '*/div',
  2903. '/fieldset'
  2904. );
  2905. $this->assertTags($result, $expected);
  2906. $this->Form->create('Contact');
  2907. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2908. $this->assertTags($result, $expected);
  2909. $this->Form->end();
  2910. $this->Form->create(false);
  2911. $expected = array(
  2912. 'fieldset' => array(),
  2913. array('div' => array('class' => 'input text')),
  2914. 'label' => array('for' => 'foo'),
  2915. 'Foo',
  2916. '/label',
  2917. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2918. '*/div',
  2919. '/fieldset'
  2920. );
  2921. $result = $this->Form->inputs(
  2922. array('foo' => array('type' => 'text')),
  2923. array(),
  2924. array('legend' => false)
  2925. );
  2926. $this->assertTags($result, $expected);
  2927. }
  2928. /**
  2929. * Tests inputs() works with plugin models
  2930. *
  2931. * @return void
  2932. */
  2933. public function testInputsPluginModel() {
  2934. $this->loadFixtures('Post');
  2935. App::build(array(
  2936. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2937. ));
  2938. CakePlugin::load('TestPlugin');
  2939. $this->Form->request['models'] = array(
  2940. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2941. );
  2942. $this->Form->create('TestPlugin.TestPluginPost');
  2943. $result = $this->Form->inputs();
  2944. $this->assertContains('data[TestPluginPost][id]', $result);
  2945. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2946. $this->assertContains('data[TestPluginPost][title]', $result);
  2947. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2948. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2949. $this->assertEquals('TestPluginPost', $this->Form->model());
  2950. }
  2951. /**
  2952. * testSelectAsCheckbox method
  2953. *
  2954. * test multi-select widget with checkbox formatting.
  2955. *
  2956. * @return void
  2957. */
  2958. public function testSelectAsCheckbox() {
  2959. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2960. $expected = array(
  2961. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2962. array('div' => array('class' => 'checkbox')),
  2963. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2964. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2965. 'first',
  2966. '/label',
  2967. '/div',
  2968. array('div' => array('class' => 'checkbox')),
  2969. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2970. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2971. 'second',
  2972. '/label',
  2973. '/div',
  2974. array('div' => array('class' => 'checkbox')),
  2975. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2976. array('label' => array('for' => 'ModelMultiField2')),
  2977. 'third',
  2978. '/label',
  2979. '/div',
  2980. );
  2981. $this->assertTags($result, $expected);
  2982. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2983. $expected = array(
  2984. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2985. array('div' => array('class' => 'checkbox')),
  2986. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2987. array('label' => array('for' => 'ModelMultiField12')),
  2988. 'half',
  2989. '/label',
  2990. '/div',
  2991. );
  2992. $this->assertTags($result, $expected);
  2993. }
  2994. /**
  2995. * testLabel method
  2996. *
  2997. * test label generation.
  2998. *
  2999. * @return void
  3000. */
  3001. public function testLabel() {
  3002. $this->Form->text('Person.name');
  3003. $result = $this->Form->label();
  3004. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3005. $this->Form->text('Person.name');
  3006. $result = $this->Form->label();
  3007. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3008. $result = $this->Form->label('Person.first_name');
  3009. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3010. $result = $this->Form->label('Person.first_name', 'Your first name');
  3011. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3012. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3013. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3014. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3015. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3016. $result = $this->Form->label('Person.first_name', '');
  3017. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3018. $result = $this->Form->label('Person.2.name', '');
  3019. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3020. }
  3021. /**
  3022. * testTextbox method
  3023. *
  3024. * test textbox element generation
  3025. *
  3026. * @return void
  3027. */
  3028. public function testTextbox() {
  3029. $result = $this->Form->text('Model.field');
  3030. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3031. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3032. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3033. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3034. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3035. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3036. $result = $this->Form->text('Model.text');
  3037. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3038. $Contact = ClassRegistry::getObject('Contact');
  3039. $Contact->validationErrors['text'] = array(true);
  3040. $this->Form->request->data['Contact']['text'] = 'test';
  3041. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3042. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3043. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3044. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3045. $expected = array(
  3046. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3047. );
  3048. $this->assertTags($result, $expected);
  3049. }
  3050. /**
  3051. * testDefaultValue method
  3052. *
  3053. * Test default value setting
  3054. *
  3055. * @return void
  3056. */
  3057. public function testDefaultValue() {
  3058. $this->Form->request->data['Model']['field'] = 'test';
  3059. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3060. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3061. unset($this->Form->request->data['Model']['field']);
  3062. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3063. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3064. }
  3065. /**
  3066. * testCheckboxDefaultValue method
  3067. *
  3068. * Test default value setting on checkbox() method
  3069. *
  3070. * @return void
  3071. */
  3072. public function testCheckboxDefaultValue() {
  3073. $this->Form->request->data['Model']['field'] = false;
  3074. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3075. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3076. unset($this->Form->request->data['Model']['field']);
  3077. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3078. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3079. $this->Form->request->data['Model']['field'] = true;
  3080. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3081. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3082. unset($this->Form->request->data['Model']['field']);
  3083. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3084. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3085. }
  3086. /**
  3087. * testError method
  3088. *
  3089. * Test field error generation
  3090. *
  3091. * @return void
  3092. */
  3093. public function testError() {
  3094. $Contact = ClassRegistry::getObject('Contact');
  3095. $Contact->validationErrors['field'] = array(1);
  3096. $result = $this->Form->error('Contact.field');
  3097. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3098. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3099. $this->assertEquals('Error in field Field', $result);
  3100. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3101. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3102. $this->assertEquals('This field contains invalid input', $result);
  3103. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3104. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3105. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3106. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3107. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3108. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3109. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3110. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3111. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3112. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3113. $this->assertEquals('<strong>Badness!</strong>', $result);
  3114. $Contact->validationErrors['field'] = array("email");
  3115. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3116. $expected = array(
  3117. 'div' => array('class' => 'field-error'),
  3118. 'No good!',
  3119. '/div'
  3120. );
  3121. $this->assertTags($result, $expected);
  3122. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3123. $result = $this->Form->error('Contact.field', array(
  3124. 'notEmpty' => 'Cannot be empty',
  3125. 'email' => 'No good!'
  3126. ));
  3127. $expected = array(
  3128. 'div' => array('class' => 'error-message'),
  3129. 'ul' => array(),
  3130. '<li', 'Cannot be empty', '/li',
  3131. '<li', 'No good!', '/li',
  3132. '<li', 'Something else', '/li',
  3133. '/ul',
  3134. '/div'
  3135. );
  3136. $this->assertTags($result, $expected);
  3137. // Testing error messages list options
  3138. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3139. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3140. $expected = array(
  3141. 'div' => array('class' => 'error-message'),
  3142. 'ol' => array(),
  3143. '<li', 'notEmpty', '/li',
  3144. '<li', 'email', '/li',
  3145. '/ol',
  3146. '/div'
  3147. );
  3148. $this->assertTags($result, $expected);
  3149. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3150. $expected = array(
  3151. 'div' => array('class' => 'error-message'),
  3152. 'ol' => array(),
  3153. '<li', 'notEmpty', '/li',
  3154. '<li', 'email', '/li',
  3155. '/ol',
  3156. '/div'
  3157. );
  3158. $this->assertTags($result, $expected);
  3159. $result = $this->Form->error('Contact.field', null, array(
  3160. 'listOptions' => array(
  3161. 'class' => 'ul-class',
  3162. 'itemOptions' => array(
  3163. 'class' => 'li-class'
  3164. )
  3165. )
  3166. ));
  3167. $expected = array(
  3168. 'div' => array('class' => 'error-message'),
  3169. 'ul' => array('class' => 'ul-class'),
  3170. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3171. array('li' => array('class' => 'li-class')), 'email', '/li',
  3172. '/ul',
  3173. '/div'
  3174. );
  3175. $this->assertTags($result, $expected);
  3176. }
  3177. /**
  3178. * test error options when using form->input();
  3179. *
  3180. * @return void
  3181. */
  3182. public function testInputErrorEscape() {
  3183. $this->Form->create('ValidateProfile');
  3184. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3185. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3186. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3187. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3188. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3189. $this->assertRegExp('/required<br>/', $result);
  3190. }
  3191. /**
  3192. * testPassword method
  3193. *
  3194. * Test password element generation
  3195. *
  3196. * @return void
  3197. */
  3198. public function testPassword() {
  3199. $Contact = ClassRegistry::getObject('Contact');
  3200. $result = $this->Form->password('Contact.field');
  3201. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3202. $Contact->validationErrors['passwd'] = 1;
  3203. $this->Form->request->data['Contact']['passwd'] = 'test';
  3204. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3205. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3206. }
  3207. /**
  3208. * testRadio method
  3209. *
  3210. * Test radio element set generation
  3211. *
  3212. * @return void
  3213. */
  3214. public function testRadio() {
  3215. $result = $this->Form->radio('Model.field', array('option A'));
  3216. $expected = array(
  3217. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3218. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3219. 'label' => array('for' => 'ModelField0'),
  3220. 'option A',
  3221. '/label'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3225. $expected = array(
  3226. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3227. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3228. 'label' => array('for' => 'ModelField12'),
  3229. 'half',
  3230. '/label'
  3231. );
  3232. $this->assertTags($result, $expected);
  3233. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3234. $expected = array(
  3235. 'fieldset' => array(),
  3236. 'legend' => array(),
  3237. 'Field',
  3238. '/legend',
  3239. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3240. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3241. array('label' => array('for' => 'ModelField0')),
  3242. 'option A',
  3243. '/label',
  3244. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3245. array('label' => array('for' => 'ModelField1')),
  3246. 'option B',
  3247. '/label',
  3248. '/fieldset'
  3249. );
  3250. $this->assertTags($result, $expected);
  3251. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3252. $expected = array(
  3253. 'fieldset' => array(),
  3254. 'legend' => array(),
  3255. 'Field',
  3256. '/legend',
  3257. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3258. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3259. array('label' => array('for' => 'ModelField0')),
  3260. 'option A',
  3261. '/label',
  3262. 'br' => array(),
  3263. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3264. array('label' => array('for' => 'ModelField1')),
  3265. 'option B',
  3266. '/label',
  3267. '/fieldset'
  3268. );
  3269. $this->assertTags($result, $expected);
  3270. $result = $this->Form->radio(
  3271. 'Employee.gender',
  3272. array('male' => 'Male', 'female' => 'Female'),
  3273. array('form' => 'my-form')
  3274. );
  3275. $expected = array(
  3276. 'fieldset' => array(),
  3277. 'legend' => array(),
  3278. 'Gender',
  3279. '/legend',
  3280. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'),
  3281. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
  3282. array('label' => array('for' => 'EmployeeGenderMale')),
  3283. 'Male',
  3284. '/label',
  3285. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')),
  3286. array('label' => array('for' => 'EmployeeGenderFemale')),
  3287. 'Female',
  3288. '/label',
  3289. '/fieldset',
  3290. );
  3291. $this->assertTags($result, $expected);
  3292. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3293. $expected = array(
  3294. 'fieldset' => array(),
  3295. 'legend' => array(),
  3296. 'Gender',
  3297. '/legend',
  3298. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3299. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3300. array('label' => array('for' => 'OfficerGenderMale')),
  3301. 'Male',
  3302. '/label',
  3303. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3304. array('label' => array('for' => 'OfficerGenderFemale')),
  3305. 'Female',
  3306. '/label',
  3307. '/fieldset',
  3308. );
  3309. $this->assertTags($result, $expected);
  3310. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3311. $expected = array(
  3312. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3313. array('input' => array(
  3314. 'type' => 'radio',
  3315. 'name' => 'data[Contact][1][imrequired]',
  3316. 'value' => '0',
  3317. 'id' => 'Contact1Imrequired0',
  3318. 'required' => 'required'
  3319. )),
  3320. 'label' => array('for' => 'Contact1Imrequired0'),
  3321. 'option A',
  3322. '/label'
  3323. );
  3324. $this->assertTags($result, $expected);
  3325. $result = $this->Form->radio('Model.1.field', array('option A'));
  3326. $expected = array(
  3327. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3328. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3329. 'label' => array('for' => 'Model1Field0'),
  3330. 'option A',
  3331. '/label'
  3332. );
  3333. $this->assertTags($result, $expected);
  3334. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3335. $expected = array(
  3336. 'fieldset' => array(),
  3337. 'legend' => array(),
  3338. 'Field',
  3339. '/legend',
  3340. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3341. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3342. array('label' => array('for' => 'ModelField0')),
  3343. 'option A',
  3344. '/label',
  3345. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3346. array('label' => array('for' => 'ModelField1')),
  3347. 'option B',
  3348. '/label',
  3349. '/fieldset'
  3350. );
  3351. $this->assertTags($result, $expected);
  3352. $result = $this->Form->radio(
  3353. 'Model.field',
  3354. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3355. );
  3356. $expected = array(
  3357. 'fieldset' => array(),
  3358. 'legend' => array(),
  3359. 'Field',
  3360. '/legend',
  3361. 'input' => array(
  3362. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3363. 'id' => 'ModelField_', 'value' => '',
  3364. ),
  3365. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3366. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3367. array('label' => array('for' => 'ModelFieldAB')),
  3368. 'first',
  3369. '/label',
  3370. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3371. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3372. array('label' => array('for' => 'ModelFieldAB1')),
  3373. 'second',
  3374. '/label',
  3375. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3376. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3377. array('label' => array('for' => 'ModelFieldAB2')),
  3378. 'third',
  3379. '/label',
  3380. '/fieldset'
  3381. );
  3382. $this->assertTags($result, $expected);
  3383. }
  3384. /**
  3385. * testRadioDifferentModel
  3386. * Refs #2911
  3387. *
  3388. * @return void
  3389. */
  3390. public function testRadioDifferentModel() {
  3391. $this->Form->create('User');
  3392. $result = $this->Form->radio(
  3393. 'Model.field',
  3394. array('v1' => 'option A', 'v2' => 'option B'),
  3395. array('label' => true, 'legend' => false, 'value' => false)
  3396. );
  3397. $expected = array(
  3398. array('input' => array(
  3399. 'type' => 'radio', 'name' => 'data[Model][field]',
  3400. 'value' => 'v1', 'id' => 'ModelFieldV1'
  3401. )),
  3402. array('label' => array('for' => 'ModelFieldV1')),
  3403. 'option A',
  3404. '/label',
  3405. array('input' => array(
  3406. 'type' => 'radio', 'name' => 'data[Model][field]',
  3407. 'value' => 'v2', 'id' => 'ModelFieldV2'
  3408. )),
  3409. array('label' => array('for' => 'ModelFieldV2')),
  3410. 'option B',
  3411. '/label'
  3412. );
  3413. $this->assertTags($result, $expected);
  3414. }
  3415. /**
  3416. * Test radio inputs with between as string or array. Also ensure
  3417. * that an array with less between elements works.
  3418. *
  3419. * @return void
  3420. */
  3421. public function testRadioBetween() {
  3422. $result = $this->Form->radio(
  3423. 'Model.field',
  3424. array('option A', 'option B'),
  3425. array('between' => 'I am between')
  3426. );
  3427. $expected = array(
  3428. 'fieldset' => array(),
  3429. 'legend' => array(),
  3430. 'Field',
  3431. '/legend',
  3432. 'I am between',
  3433. 'input' => array(
  3434. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3435. 'value' => '', 'id' => 'ModelField_'
  3436. ),
  3437. array('input' => array(
  3438. 'type' => 'radio', 'name' => 'data[Model][field]',
  3439. 'value' => '0', 'id' => 'ModelField0'
  3440. )),
  3441. array('label' => array('for' => 'ModelField0')),
  3442. 'option A',
  3443. '/label',
  3444. array('input' => array(
  3445. 'type' => 'radio', 'name' => 'data[Model][field]',
  3446. 'value' => '1', 'id' => 'ModelField1'
  3447. )),
  3448. array('label' => array('for' => 'ModelField1')),
  3449. 'option B',
  3450. '/label',
  3451. '/fieldset'
  3452. );
  3453. $this->assertTags($result, $expected);
  3454. $result = $this->Form->radio(
  3455. 'Model.field',
  3456. array('option A', 'option B', 'option C'),
  3457. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3458. );
  3459. $expected = array(
  3460. 'fieldset' => array(),
  3461. 'legend' => array(),
  3462. 'Field',
  3463. '/legend',
  3464. 'input' => array(
  3465. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3466. 'value' => '', 'id' => 'ModelField_'
  3467. ),
  3468. array('input' => array(
  3469. 'type' => 'radio', 'name' => 'data[Model][field]',
  3470. 'value' => '0', 'id' => 'ModelField0'
  3471. )),
  3472. array('label' => array('for' => 'ModelField0')),
  3473. 'option A',
  3474. '/label',
  3475. 'between A',
  3476. '--separator--',
  3477. array('input' => array(
  3478. 'type' => 'radio', 'name' => 'data[Model][field]',
  3479. 'value' => '1', 'id' => 'ModelField1'
  3480. )),
  3481. array('label' => array('for' => 'ModelField1')),
  3482. 'option B',
  3483. '/label',
  3484. 'between B',
  3485. '--separator--',
  3486. array('input' => array(
  3487. 'type' => 'radio', 'name' => 'data[Model][field]',
  3488. 'value' => '2', 'id' => 'ModelField2'
  3489. )),
  3490. array('label' => array('for' => 'ModelField2')),
  3491. 'option C',
  3492. '/label',
  3493. 'between C',
  3494. '/fieldset'
  3495. );
  3496. $this->assertTags($result, $expected);
  3497. $result = $this->Form->input('Model.field', array(
  3498. 'options' => array('1' => 'first', '2' => 'second'),
  3499. 'type' => 'radio',
  3500. 'before' => '--before--',
  3501. 'after' => '--after--',
  3502. 'separator' => '--separator--',
  3503. 'between' => array('--between first--', '--between second--')
  3504. ));
  3505. $expected = array(
  3506. 'div' => array('class' => 'input radio'),
  3507. '--before--',
  3508. 'fieldset' => array(),
  3509. 'legend' => array(),
  3510. 'Field',
  3511. '/legend',
  3512. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3513. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3514. array('label' => array('for' => 'ModelField1')),
  3515. 'first',
  3516. '/label',
  3517. '--between first--',
  3518. '--separator--',
  3519. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3520. array('label' => array('for' => 'ModelField2')),
  3521. 'second',
  3522. '/label',
  3523. '--between second--',
  3524. '/fieldset',
  3525. '--after--',
  3526. '/div'
  3527. );
  3528. $this->assertTags($result, $expected);
  3529. $result = $this->Form->input('Model.field', array(
  3530. 'options' => array('1' => 'first', '2' => 'second'),
  3531. 'type' => 'radio',
  3532. 'before' => '--before--',
  3533. 'after' => '--after--',
  3534. 'separator' => '--separator--',
  3535. 'between' => array('--between first--')
  3536. ));
  3537. $expected = array(
  3538. 'div' => array('class' => 'input radio'),
  3539. '--before--',
  3540. 'fieldset' => array(),
  3541. 'legend' => array(),
  3542. 'Field',
  3543. '/legend',
  3544. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3545. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3546. array('label' => array('for' => 'ModelField1')),
  3547. 'first',
  3548. '/label',
  3549. '--between first--',
  3550. '--separator--',
  3551. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3552. array('label' => array('for' => 'ModelField2')),
  3553. 'second',
  3554. '/label',
  3555. '/fieldset',
  3556. '--after--',
  3557. '/div'
  3558. );
  3559. $this->assertTags($result, $expected);
  3560. }
  3561. /**
  3562. * Test that radios with a 0 value are selected under the correct conditions.
  3563. * Also ensure that values that are booleanish are handled correctly.
  3564. *
  3565. * @return void
  3566. */
  3567. public function testRadioOptionWithBooleanishValues() {
  3568. $expected = array(
  3569. 'fieldset' => array(),
  3570. 'legend' => array(),
  3571. 'Field',
  3572. '/legend',
  3573. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3574. array('label' => array('for' => 'ModelField1')),
  3575. 'Yes',
  3576. '/label',
  3577. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3578. array('label' => array('for' => 'ModelField0')),
  3579. 'No',
  3580. '/label',
  3581. '/fieldset'
  3582. );
  3583. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3584. $this->assertTags($result, $expected);
  3585. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3586. $this->assertTags($result, $expected);
  3587. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3588. $this->assertTags($result, $expected);
  3589. $expected = array(
  3590. 'fieldset' => array(),
  3591. 'legend' => array(),
  3592. 'Field',
  3593. '/legend',
  3594. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3595. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3596. array('label' => array('for' => 'ModelField1')),
  3597. 'Yes',
  3598. '/label',
  3599. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3600. array('label' => array('for' => 'ModelField0')),
  3601. 'No',
  3602. '/label',
  3603. '/fieldset'
  3604. );
  3605. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3606. $this->assertTags($result, $expected);
  3607. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3608. $this->assertTags($result, $expected);
  3609. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3610. $this->assertTags($result, $expected);
  3611. $expected = array(
  3612. 'fieldset' => array(),
  3613. 'legend' => array(),
  3614. 'Field',
  3615. '/legend',
  3616. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3617. array('label' => array('for' => 'ModelField1')),
  3618. 'Yes',
  3619. '/label',
  3620. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3621. array('label' => array('for' => 'ModelField0')),
  3622. 'No',
  3623. '/label',
  3624. '/fieldset'
  3625. );
  3626. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3627. $this->assertTags($result, $expected);
  3628. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3629. $this->assertTags($result, $expected);
  3630. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3631. $this->assertTags($result, $expected);
  3632. }
  3633. /**
  3634. * test disabled radio options
  3635. *
  3636. * @return void
  3637. */
  3638. public function testRadioDisabled() {
  3639. $result = $this->Form->radio(
  3640. 'Model.field',
  3641. array('option A', 'option B'),
  3642. array('disabled' => array(0), 'value' => '0')
  3643. );
  3644. $expected = array(
  3645. 'fieldset' => array(),
  3646. 'legend' => array(),
  3647. 'Field',
  3648. '/legend',
  3649. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3650. array('label' => array('for' => 'ModelField0')),
  3651. 'option A',
  3652. '/label',
  3653. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3654. array('label' => array('for' => 'ModelField1')),
  3655. 'option B',
  3656. '/label',
  3657. '/fieldset'
  3658. );
  3659. $this->assertTags($result, $expected);
  3660. $result = $this->Form->radio(
  3661. 'Model.field',
  3662. array('option A', 'option B'),
  3663. array('disabled' => true, 'value' => '0')
  3664. );
  3665. $expected = array(
  3666. 'fieldset' => array(),
  3667. 'legend' => array(),
  3668. 'Field',
  3669. '/legend',
  3670. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3671. array('label' => array('for' => 'ModelField0')),
  3672. 'option A',
  3673. '/label',
  3674. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3675. array('label' => array('for' => 'ModelField1')),
  3676. 'option B',
  3677. '/label',
  3678. '/fieldset'
  3679. );
  3680. $this->assertTags($result, $expected);
  3681. $result = $this->Form->radio(
  3682. 'Model.field',
  3683. array('option A', 'option B'),
  3684. array('disabled' => 'disabled', 'value' => '0')
  3685. );
  3686. $expected = array(
  3687. 'fieldset' => array(),
  3688. 'legend' => array(),
  3689. 'Field',
  3690. '/legend',
  3691. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3692. array('label' => array('for' => 'ModelField0')),
  3693. 'option A',
  3694. '/label',
  3695. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3696. array('label' => array('for' => 'ModelField1')),
  3697. 'option B',
  3698. '/label',
  3699. '/fieldset'
  3700. );
  3701. $this->assertTags($result, $expected);
  3702. $result = $this->Form->input('Model.field', array(
  3703. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3704. 'type' => 'radio',
  3705. 'disabled' => array(2, '3x'),
  3706. ));
  3707. $expected = array(
  3708. 'div' => array('class' => 'input radio'),
  3709. 'fieldset' => array(),
  3710. 'legend' => array(),
  3711. 'Field',
  3712. '/legend',
  3713. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3714. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3715. array('label' => array('for' => 'ModelField1')),
  3716. 'first',
  3717. '/label',
  3718. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3719. array('label' => array('for' => 'ModelField2')),
  3720. 'second',
  3721. '/label',
  3722. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3723. array('label' => array('for' => 'ModelField2x')),
  3724. '2x',
  3725. '/label',
  3726. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3727. array('label' => array('for' => 'ModelField3')),
  3728. 'third',
  3729. '/label',
  3730. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3731. array('label' => array('for' => 'ModelField3x')),
  3732. '3x',
  3733. '/label',
  3734. '/fieldset',
  3735. '/div'
  3736. );
  3737. $this->assertTags($result, $expected);
  3738. $result = $this->Form->input('Model.field', array(
  3739. 'type' => 'radio',
  3740. 'options' => array(
  3741. 1 => 'A',
  3742. 2 => 'B',
  3743. 3 => 'C'
  3744. ),
  3745. 'disabled' => array(1)
  3746. ));
  3747. $expected = array(
  3748. 'div' => array('class' => 'input radio'),
  3749. 'fieldset' => array(),
  3750. 'legend' => array(),
  3751. 'Field',
  3752. '/legend',
  3753. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3754. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3755. array('label' => array('for' => 'ModelField1')),
  3756. 'A',
  3757. '/label',
  3758. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3759. array('label' => array('for' => 'ModelField2')),
  3760. 'B',
  3761. '/label',
  3762. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3763. array('label' => array('for' => 'ModelField3')),
  3764. 'C',
  3765. '/label',
  3766. '/fieldset',
  3767. '/div'
  3768. );
  3769. $this->assertTags($result, $expected);
  3770. }
  3771. /**
  3772. * test disabling the hidden input for radio buttons
  3773. *
  3774. * @return void
  3775. */
  3776. public function testRadioHiddenInputDisabling() {
  3777. $result = $this->Form->input('Model.1.field', array(
  3778. 'type' => 'radio',
  3779. 'options' => array('option A'),
  3780. 'hiddenField' => false
  3781. )
  3782. );
  3783. $expected = array(
  3784. 'div' => array('class' => 'input radio'),
  3785. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3786. 'label' => array('for' => 'Model1Field0'),
  3787. 'option A',
  3788. '/label',
  3789. '/div'
  3790. );
  3791. $this->assertTags($result, $expected);
  3792. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3793. $expected = array(
  3794. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3795. 'label' => array('for' => 'Model1Field0'),
  3796. 'option A',
  3797. '/label'
  3798. );
  3799. $this->assertTags($result, $expected);
  3800. }
  3801. /**
  3802. * test adding an empty option for radio buttons
  3803. *
  3804. * @return void
  3805. */
  3806. public function testRadioAddEmptyOption() {
  3807. $result = $this->Form->input('Model.1.field', array(
  3808. 'type' => 'radio',
  3809. 'options' => array('option A'),
  3810. 'empty' => true,
  3811. 'hiddenField' => false
  3812. ));
  3813. $expected = array(
  3814. 'div' => array('class' => 'input radio'),
  3815. 'fieldset' => array(),
  3816. 'legend' => array(),
  3817. 'Field',
  3818. '/legend',
  3819. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3820. array('label' => array('for' => 'Model1Field')),
  3821. __('empty'),
  3822. '/label',
  3823. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3824. array('label' => array('for' => 'Model1Field0')),
  3825. 'option A',
  3826. '/label',
  3827. '/fieldset',
  3828. '/div'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. $result = $this->Form->input('Model.1.field', array(
  3832. 'type' => 'radio',
  3833. 'options' => array('option A'),
  3834. 'empty' => 'CustomEmptyLabel',
  3835. 'hiddenField' => false
  3836. ));
  3837. $expected = array(
  3838. 'div' => array('class' => 'input radio'),
  3839. 'fieldset' => array(),
  3840. 'legend' => array(),
  3841. 'Field',
  3842. '/legend',
  3843. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3844. array('label' => array('for' => 'Model1Field')),
  3845. 'CustomEmptyLabel',
  3846. '/label',
  3847. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3848. array('label' => array('for' => 'Model1Field0')),
  3849. 'option A',
  3850. '/label',
  3851. '/fieldset',
  3852. '/div'
  3853. );
  3854. $this->assertTags($result, $expected);
  3855. $result = $this->Form->input('Model.1.field', array(
  3856. 'type' => 'radio',
  3857. 'options' => array('option A'),
  3858. 'empty' => false,
  3859. 'hiddenField' => false
  3860. ));
  3861. $this->assertTextNotContains('"Model1Field"', $result);
  3862. }
  3863. /**
  3864. * Test that radio() accepts an array for label
  3865. *
  3866. * @return void
  3867. */
  3868. public function testRadioLabelArray() {
  3869. $result = $this->Form->input('Model.field', array(
  3870. 'type' => 'radio',
  3871. 'legend' => false,
  3872. 'label' => array(
  3873. 'class' => 'checkbox float-left',
  3874. ),
  3875. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3876. ));
  3877. $this->assertTextContains(
  3878. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3879. $result
  3880. );
  3881. }
  3882. /**
  3883. * Test that label id's match the input element id's when radio is called after create().
  3884. *
  3885. * @return void
  3886. */
  3887. public function testRadioWithCreate() {
  3888. $this->Form->create('Model');
  3889. $result = $this->Form->radio('recipient',
  3890. array('1' => '1', '2' => '2', '3' => '3'),
  3891. array('legend' => false, 'value' => '1')
  3892. );
  3893. $this->assertTextNotContains(
  3894. '<label for="ModelModelRecipient1">1</label>',
  3895. $result
  3896. );
  3897. $this->assertTextContains(
  3898. '<label for="ModelRecipient1">1</label>',
  3899. $result
  3900. );
  3901. }
  3902. /**
  3903. * testDomIdSuffix method
  3904. *
  3905. * @return void
  3906. */
  3907. public function testDomIdSuffix() {
  3908. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3909. $this->assertEquals('1StringWith1DollarSigns', $result);
  3910. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3911. $this->assertEquals('AbcXFooYBar', $result);
  3912. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
  3913. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3914. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
  3915. $this->assertEquals('AbcX=FooY=Bar', $result);
  3916. }
  3917. /**
  3918. * testDomIdSuffixCollisionResolvement()
  3919. *
  3920. * @return void
  3921. */
  3922. public function testDomIdSuffixCollisionResolvement() {
  3923. $result = $this->Form->domIdSuffix('a>b');
  3924. $this->assertEquals('AB', $result);
  3925. $result = $this->Form->domIdSuffix('a<b');
  3926. $this->assertEquals('AB1', $result);
  3927. $result = $this->Form->domIdSuffix('a\'b');
  3928. $this->assertEquals('AB2', $result);
  3929. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3930. $this->assertEquals('1StringWith1Dollar', $result);
  3931. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3932. $this->assertEquals('1StringWith1Dollar1', $result);
  3933. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3934. $this->assertEquals('1StringWith1Dollar2', $result);
  3935. }
  3936. /**
  3937. * testSelect method
  3938. *
  3939. * Test select element generation.
  3940. *
  3941. * @return void
  3942. */
  3943. public function testSelect() {
  3944. $result = $this->Form->select('Model.field', array());
  3945. $expected = array(
  3946. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3947. array('option' => array('value' => '')),
  3948. '/option',
  3949. '/select'
  3950. );
  3951. $this->assertTags($result, $expected);
  3952. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3953. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3954. $expected = array(
  3955. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3956. array('option' => array('value' => '')),
  3957. '/option',
  3958. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3959. 'good',
  3960. '/option',
  3961. array('option' => array('value' => 'other')),
  3962. 'bad',
  3963. '/option',
  3964. '/select'
  3965. );
  3966. $this->assertTags($result, $expected);
  3967. $this->Form->request->data = array();
  3968. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3969. $expected = array(
  3970. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3971. array('option' => array('value' => '')),
  3972. '/option',
  3973. array('option' => array('value' => 'value')),
  3974. 'good',
  3975. '/option',
  3976. array('option' => array('value' => 'other')),
  3977. 'bad',
  3978. '/option',
  3979. '/select'
  3980. );
  3981. $this->assertTags($result, $expected);
  3982. $result = $this->Form->select(
  3983. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3984. array('empty' => false)
  3985. );
  3986. $expected = array(
  3987. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3988. array('option' => array('value' => 'first')),
  3989. 'first &quot;html&quot; &lt;chars&gt;',
  3990. '/option',
  3991. array('option' => array('value' => 'second')),
  3992. 'value',
  3993. '/option',
  3994. '/select'
  3995. );
  3996. $this->assertTags($result, $expected);
  3997. $result = $this->Form->select(
  3998. 'Model.field',
  3999. array('first' => 'first "html" <chars>', 'second' => 'value'),
  4000. array('escape' => false, 'empty' => false)
  4001. );
  4002. $expected = array(
  4003. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4004. array('option' => array('value' => 'first')),
  4005. 'first "html" <chars>',
  4006. '/option',
  4007. array('option' => array('value' => 'second')),
  4008. 'value',
  4009. '/option',
  4010. '/select'
  4011. );
  4012. $this->assertTags($result, $expected);
  4013. $options = array(
  4014. array('value' => 'first', 'name' => 'First'),
  4015. array('value' => 'first', 'name' => 'Another First'),
  4016. );
  4017. $result = $this->Form->select(
  4018. 'Model.field',
  4019. $options,
  4020. array('escape' => false, 'empty' => false)
  4021. );
  4022. $expected = array(
  4023. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4024. array('option' => array('value' => 'first')),
  4025. 'First',
  4026. '/option',
  4027. array('option' => array('value' => 'first')),
  4028. 'Another First',
  4029. '/option',
  4030. '/select'
  4031. );
  4032. $this->assertTags($result, $expected);
  4033. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  4034. $result = $this->Form->select(
  4035. 'Model.contact_id',
  4036. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4037. array('escape' => false, 'empty' => 'pick something')
  4038. );
  4039. $expected = array(
  4040. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4041. array('option' => array('value' => '')), 'pick something', '/option',
  4042. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4043. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4044. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4045. '/select'
  4046. );
  4047. $this->assertTags($result, $expected);
  4048. $this->Form->request->data['Model']['field'] = 0;
  4049. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4050. $expected = array(
  4051. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4052. array('option' => array('value' => '')), '/option',
  4053. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4054. array('option' => array('value' => '1')), 'Yes', '/option',
  4055. '/select'
  4056. );
  4057. $this->assertTags($result, $expected);
  4058. $this->Form->request->data['Model']['field'] = 50;
  4059. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4060. $expected = array(
  4061. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4062. array('option' => array('value' => '')), '/option',
  4063. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4064. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4065. '/select'
  4066. );
  4067. $this->assertTags($result, $expected);
  4068. $result = $this->Form->select('Contact.required_one', array('option A'));
  4069. $expected = array(
  4070. 'select' => array(
  4071. 'name' => 'data[Contact][required_one]',
  4072. 'id' => 'ContactRequiredOne',
  4073. 'required' => 'required'
  4074. ),
  4075. array('option' => array('value' => '')), '/option',
  4076. array('option' => array('value' => '0')), 'option A', '/option',
  4077. '/select'
  4078. );
  4079. $this->assertTags($result, $expected);
  4080. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4081. $expected = array(
  4082. 'select' => array(
  4083. 'name' => 'data[Contact][required_one]',
  4084. 'id' => 'ContactRequiredOne',
  4085. 'disabled' => 'disabled'
  4086. ),
  4087. array('option' => array('value' => '')), '/option',
  4088. array('option' => array('value' => '0')), 'option A', '/option',
  4089. '/select'
  4090. );
  4091. $this->assertTags($result, $expected);
  4092. }
  4093. /**
  4094. * test that select() with optiongroups listens to the escape param.
  4095. *
  4096. * @return void
  4097. */
  4098. public function testSelectOptionGroupEscaping() {
  4099. $options = array(
  4100. '>< Key' => array(
  4101. 1 => 'One',
  4102. 2 => 'Two'
  4103. )
  4104. );
  4105. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4106. $expected = array(
  4107. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4108. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4109. array('option' => array('value' => '1')), 'One', '/option',
  4110. array('option' => array('value' => '2')), 'Two', '/option',
  4111. '/optgroup',
  4112. '/select'
  4113. );
  4114. $this->assertTags($result, $expected);
  4115. $options = array(
  4116. '>< Key' => array(
  4117. 1 => 'One',
  4118. 2 => 'Two'
  4119. )
  4120. );
  4121. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4122. $expected = array(
  4123. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4124. 'optgroup' => array('label' => '>< Key'),
  4125. array('option' => array('value' => '1')), 'One', '/option',
  4126. array('option' => array('value' => '2')), 'Two', '/option',
  4127. '/optgroup',
  4128. '/select'
  4129. );
  4130. $this->assertTags($result, $expected);
  4131. }
  4132. /**
  4133. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4134. *
  4135. * @return void
  4136. */
  4137. public function testSelectWithNullAttributes() {
  4138. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4139. $expected = array(
  4140. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4141. array('option' => array('value' => '0')),
  4142. 'first',
  4143. '/option',
  4144. array('option' => array('value' => '1')),
  4145. 'second',
  4146. '/option',
  4147. '/select'
  4148. );
  4149. $this->assertTags($result, $expected);
  4150. }
  4151. /**
  4152. * testNestedSelect method
  4153. *
  4154. * test select element generation with optgroups
  4155. *
  4156. * @return void
  4157. */
  4158. public function testNestedSelect() {
  4159. $result = $this->Form->select(
  4160. 'Model.field',
  4161. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4162. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4163. )), array('empty' => false)
  4164. );
  4165. $expected = array(
  4166. 'select' => array('name' => 'data[Model][field]',
  4167. 'id' => 'ModelField'),
  4168. array('option' => array('value' => 1)),
  4169. 'One',
  4170. '/option',
  4171. array('option' => array('value' => 2)),
  4172. 'Two',
  4173. '/option',
  4174. array('optgroup' => array('label' => 'Three')),
  4175. array('option' => array('value' => 4)),
  4176. 'Four',
  4177. '/option',
  4178. array('option' => array('value' => 5)),
  4179. 'Five',
  4180. '/option',
  4181. '/optgroup',
  4182. '/select'
  4183. );
  4184. $this->assertTags($result, $expected);
  4185. $result = $this->Form->select(
  4186. 'Model.field',
  4187. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4188. array('showParents' => true, 'empty' => false)
  4189. );
  4190. $expected = array(
  4191. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4192. array('option' => array('value' => 1)),
  4193. 'One',
  4194. '/option',
  4195. array('option' => array('value' => 2)),
  4196. 'Two',
  4197. '/option',
  4198. array('optgroup' => array('label' => 'Three')),
  4199. array('option' => array('value' => 3)),
  4200. 'Three',
  4201. '/option',
  4202. array('option' => array('value' => 4)),
  4203. 'Four',
  4204. '/option',
  4205. '/optgroup',
  4206. '/select'
  4207. );
  4208. $this->assertTags($result, $expected);
  4209. }
  4210. /**
  4211. * testSelectMultiple method
  4212. *
  4213. * test generation of multiple select elements
  4214. *
  4215. * @return void
  4216. */
  4217. public function testSelectMultiple() {
  4218. $options = array('first', 'second', 'third');
  4219. $result = $this->Form->select(
  4220. 'Model.multi_field', $options, array('multiple' => true)
  4221. );
  4222. $expected = array(
  4223. 'input' => array(
  4224. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4225. ),
  4226. 'select' => array(
  4227. 'name' => 'data[Model][multi_field][]',
  4228. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4229. ),
  4230. array('option' => array('value' => '0')),
  4231. 'first',
  4232. '/option',
  4233. array('option' => array('value' => '1')),
  4234. 'second',
  4235. '/option',
  4236. array('option' => array('value' => '2')),
  4237. 'third',
  4238. '/option',
  4239. '/select'
  4240. );
  4241. $this->assertTags($result, $expected);
  4242. $result = $this->Form->select(
  4243. 'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
  4244. );
  4245. $expected = array(
  4246. 'input' => array(
  4247. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_',
  4248. 'form' => 'my-form',
  4249. ),
  4250. 'select' => array(
  4251. 'name' => 'data[Model][multi_field][]',
  4252. 'id' => 'ModelMultiField',
  4253. 'multiple' => 'multiple',
  4254. 'form' => 'my-form',
  4255. ),
  4256. array('option' => array('value' => '0')),
  4257. 'first',
  4258. '/option',
  4259. array('option' => array('value' => '1')),
  4260. 'second',
  4261. '/option',
  4262. array('option' => array('value' => '2')),
  4263. 'third',
  4264. '/option',
  4265. '/select'
  4266. );
  4267. $this->assertTags($result, $expected);
  4268. $result = $this->Form->select(
  4269. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4270. );
  4271. $expected = array(
  4272. 'input' => array(
  4273. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4274. ),
  4275. 'select' => array(
  4276. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4277. 'multiple' => 'multiple'
  4278. ),
  4279. array('option' => array('value' => '0', 'selected' => 'selected')),
  4280. 'first',
  4281. '/option',
  4282. array('option' => array('value' => '1', 'selected' => 'selected')),
  4283. 'second',
  4284. '/option',
  4285. array('option' => array('value' => '2')),
  4286. 'third',
  4287. '/option',
  4288. '/select'
  4289. );
  4290. $this->assertTags($result, $expected);
  4291. $result = $this->Form->select(
  4292. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4293. );
  4294. $expected = array(
  4295. 'select' => array(
  4296. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4297. ),
  4298. array('option' => array('value' => '0', 'selected' => 'selected')),
  4299. 'first',
  4300. '/option',
  4301. array('option' => array('value' => '1', 'selected' => 'selected')),
  4302. 'second',
  4303. '/option',
  4304. array('option' => array('value' => '2')),
  4305. 'third',
  4306. '/option',
  4307. '/select'
  4308. );
  4309. $this->assertTags($result, $expected);
  4310. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4311. $selected = array('2', '3x');
  4312. $result = $this->Form->select(
  4313. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4314. );
  4315. $expected = array(
  4316. 'input' => array(
  4317. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4318. ),
  4319. 'select' => array(
  4320. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4321. ),
  4322. array('option' => array('value' => '1')),
  4323. 'One',
  4324. '/option',
  4325. array('option' => array('value' => '2', 'selected' => 'selected')),
  4326. 'Two',
  4327. '/option',
  4328. array('option' => array('value' => '3')),
  4329. 'Three',
  4330. '/option',
  4331. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4332. 'Stringy',
  4333. '/option',
  4334. '/select'
  4335. );
  4336. $this->assertTags($result, $expected);
  4337. $result = $this->Form->select('Contact.required_one', array(
  4338. '1' => 'option A',
  4339. '2' => 'option B'
  4340. ), array('multiple' => true));
  4341. $expected = array(
  4342. 'input' => array(
  4343. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4344. ),
  4345. 'select' => array(
  4346. 'name' => 'data[Contact][required_one][]',
  4347. 'id' => 'ContactRequiredOne',
  4348. 'required' => 'required',
  4349. 'multiple' => 'multiple'
  4350. ),
  4351. array('option' => array('value' => '1')), 'option A', '/option',
  4352. array('option' => array('value' => '2')), 'option B', '/option',
  4353. '/select'
  4354. );
  4355. $this->assertTags($result, $expected);
  4356. $result = $this->Form->select(
  4357. 'Model.multi_field',
  4358. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4359. array('multiple' => true)
  4360. );
  4361. $expected = array(
  4362. 'input' => array(
  4363. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4364. 'id' => 'ModelMultiField_'
  4365. ),
  4366. array('select' => array('name' => 'data[Model][multi_field][]',
  4367. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4368. )),
  4369. array('option' => array('value' => 'a&gt;b')),
  4370. 'first',
  4371. '/option',
  4372. array('option' => array('value' => 'a&lt;b')),
  4373. 'second',
  4374. '/option',
  4375. array('option' => array(
  4376. 'value' => 'a&quot;b'
  4377. )),
  4378. 'third',
  4379. '/option',
  4380. '/select'
  4381. );
  4382. $this->assertTags($result, $expected);
  4383. }
  4384. /**
  4385. * Test generating multiple select with disabled elements.
  4386. *
  4387. * @return void
  4388. */
  4389. public function testSelectMultipleWithDisabledElements() {
  4390. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4391. $disabled = array(2, 3);
  4392. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4393. $expected = array(
  4394. 'input' => array(
  4395. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4396. ),
  4397. 'select' => array(
  4398. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4399. ),
  4400. array('option' => array('value' => '1')),
  4401. 'One',
  4402. '/option',
  4403. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4404. 'Two',
  4405. '/option',
  4406. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4407. 'Three',
  4408. '/option',
  4409. array('option' => array('value' => '3x')),
  4410. 'Stringy',
  4411. '/option',
  4412. '/select'
  4413. );
  4414. $this->assertTags($result, $expected);
  4415. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4416. $disabled = array('2', '3x');
  4417. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4418. $expected = array(
  4419. array('div' => array('class' => 'input select')),
  4420. array('label' => array('for' => 'ContactMultiple')),
  4421. 'Multiple',
  4422. '/label',
  4423. 'input' => array(
  4424. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4425. ),
  4426. 'select' => array(
  4427. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4428. ),
  4429. array('option' => array('value' => '1')),
  4430. 'One',
  4431. '/option',
  4432. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4433. 'Two',
  4434. '/option',
  4435. array('option' => array('value' => '3')),
  4436. 'Three',
  4437. '/option',
  4438. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4439. 'Stringy',
  4440. '/option',
  4441. '/select',
  4442. '/div'
  4443. );
  4444. $this->assertTags($result, $expected);
  4445. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4446. $disabled = true;
  4447. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4448. $expected = array(
  4449. array('div' => array('class' => 'input select')),
  4450. array('label' => array('for' => 'ContactMultiple')),
  4451. 'Multiple',
  4452. '/label',
  4453. 'input' => array(
  4454. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4455. ),
  4456. 'select' => array(
  4457. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4458. ),
  4459. array('option' => array('value' => '1')),
  4460. 'One',
  4461. '/option',
  4462. array('option' => array('value' => '2')),
  4463. 'Two',
  4464. '/option',
  4465. array('option' => array('value' => '3')),
  4466. 'Three',
  4467. '/option',
  4468. array('option' => array('value' => '3x')),
  4469. 'Stringy',
  4470. '/option',
  4471. '/select',
  4472. '/div'
  4473. );
  4474. $this->assertTags($result, $expected);
  4475. }
  4476. /**
  4477. * Test generating select with disabled elements.
  4478. *
  4479. * @return void
  4480. */
  4481. public function testSelectWithDisabledElements() {
  4482. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4483. $disabled = array(2, 3);
  4484. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4485. $expected = array(
  4486. 'select' => array(
  4487. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4488. ),
  4489. array('option' => array('value' => '')),
  4490. '/option',
  4491. array('option' => array('value' => '1')),
  4492. 'One',
  4493. '/option',
  4494. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4495. 'Two',
  4496. '/option',
  4497. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4498. 'Three',
  4499. '/option',
  4500. array('option' => array('value' => '3x')),
  4501. 'Stringy',
  4502. '/option',
  4503. '/select'
  4504. );
  4505. $this->assertTags($result, $expected);
  4506. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4507. $disabled = array('2', '3x');
  4508. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4509. $expected = array(
  4510. array('div' => array('class' => 'input select')),
  4511. array('label' => array('for' => 'ModelField')),
  4512. 'Field',
  4513. '/label',
  4514. 'select' => array(
  4515. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4516. ),
  4517. array('option' => array('value' => '1')),
  4518. 'One',
  4519. '/option',
  4520. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4521. 'Two',
  4522. '/option',
  4523. array('option' => array('value' => '3')),
  4524. 'Three',
  4525. '/option',
  4526. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4527. 'Stringy',
  4528. '/option',
  4529. '/select',
  4530. '/div'
  4531. );
  4532. $this->assertTags($result, $expected);
  4533. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4534. $disabled = true;
  4535. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4536. $expected = array(
  4537. array('div' => array('class' => 'input select')),
  4538. array('label' => array('for' => 'ModelField')),
  4539. 'Field',
  4540. '/label',
  4541. 'select' => array(
  4542. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4543. ),
  4544. array('option' => array('value' => '1')),
  4545. 'One',
  4546. '/option',
  4547. array('option' => array('value' => '2')),
  4548. 'Two',
  4549. '/option',
  4550. array('option' => array('value' => '3')),
  4551. 'Three',
  4552. '/option',
  4553. array('option' => array('value' => '3x')),
  4554. 'Stringy',
  4555. '/option',
  4556. '/select',
  4557. '/div'
  4558. );
  4559. $this->assertTags($result, $expected);
  4560. }
  4561. /**
  4562. * test generation of habtm select boxes.
  4563. *
  4564. * @return void
  4565. */
  4566. public function testHabtmSelectBox() {
  4567. $this->View->viewVars['contactTags'] = array(
  4568. 1 => 'blue',
  4569. 2 => 'red',
  4570. 3 => 'green'
  4571. );
  4572. $this->Form->request->data = array(
  4573. 'Contact' => array(),
  4574. 'ContactTag' => array(
  4575. array(
  4576. 'id' => '1',
  4577. 'name' => 'blue'
  4578. ),
  4579. array(
  4580. 'id' => 3,
  4581. 'name' => 'green'
  4582. )
  4583. )
  4584. );
  4585. $this->Form->create('Contact');
  4586. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4587. $expected = array(
  4588. 'input' => array(
  4589. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4590. ),
  4591. 'select' => array(
  4592. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4593. 'multiple' => 'multiple'
  4594. ),
  4595. array('option' => array('value' => '1', 'selected' => 'selected')),
  4596. 'blue',
  4597. '/option',
  4598. array('option' => array('value' => '2')),
  4599. 'red',
  4600. '/option',
  4601. array('option' => array('value' => '3', 'selected' => 'selected')),
  4602. 'green',
  4603. '/option',
  4604. '/select'
  4605. );
  4606. $this->assertTags($result, $expected);
  4607. // make sure only 50 is selected, and not 50f5c0cf
  4608. $this->View->viewVars['contactTags'] = array(
  4609. '1' => 'blue',
  4610. '50f5c0cf' => 'red',
  4611. '50' => 'green'
  4612. );
  4613. $this->Form->request->data = array(
  4614. 'Contact' => array(),
  4615. 'ContactTag' => array(
  4616. array(
  4617. 'id' => 1,
  4618. 'name' => 'blue'
  4619. ),
  4620. array(
  4621. 'id' => 50,
  4622. 'name' => 'green'
  4623. )
  4624. )
  4625. );
  4626. $this->Form->create('Contact');
  4627. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4628. $expected = array(
  4629. 'input' => array(
  4630. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4631. ),
  4632. 'select' => array(
  4633. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4634. 'multiple' => 'multiple'
  4635. ),
  4636. array('option' => array('value' => '1', 'selected' => 'selected')),
  4637. 'blue',
  4638. '/option',
  4639. array('option' => array('value' => '50f5c0cf')),
  4640. 'red',
  4641. '/option',
  4642. array('option' => array('value' => '50', 'selected' => 'selected')),
  4643. 'green',
  4644. '/option',
  4645. '/select'
  4646. );
  4647. $this->assertTags($result, $expected);
  4648. }
  4649. /**
  4650. * test generation of multi select elements in checkbox format
  4651. *
  4652. * @return void
  4653. */
  4654. public function testSelectMultipleCheckboxes() {
  4655. $result = $this->Form->select(
  4656. 'Model.multi_field',
  4657. array('first', 'second', 'third'),
  4658. array('multiple' => 'checkbox')
  4659. );
  4660. $expected = array(
  4661. 'input' => array(
  4662. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4663. ),
  4664. array('div' => array('class' => 'checkbox')),
  4665. array('input' => array(
  4666. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4667. 'value' => '0', 'id' => 'ModelMultiField0'
  4668. )),
  4669. array('label' => array('for' => 'ModelMultiField0')),
  4670. 'first',
  4671. '/label',
  4672. '/div',
  4673. array('div' => array('class' => 'checkbox')),
  4674. array('input' => array(
  4675. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4676. 'value' => '1', 'id' => 'ModelMultiField1'
  4677. )),
  4678. array('label' => array('for' => 'ModelMultiField1')),
  4679. 'second',
  4680. '/label',
  4681. '/div',
  4682. array('div' => array('class' => 'checkbox')),
  4683. array('input' => array(
  4684. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4685. 'value' => '2', 'id' => 'ModelMultiField2'
  4686. )),
  4687. array('label' => array('for' => 'ModelMultiField2')),
  4688. 'third',
  4689. '/label',
  4690. '/div'
  4691. );
  4692. $this->assertTags($result, $expected);
  4693. $result = $this->Form->select(
  4694. 'Model.multi_field',
  4695. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4696. array('multiple' => 'checkbox')
  4697. );
  4698. $expected = array(
  4699. 'input' => array(
  4700. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4701. ),
  4702. array('div' => array('class' => 'checkbox')),
  4703. array('input' => array(
  4704. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4705. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4706. )),
  4707. array('label' => array('for' => 'ModelMultiFieldA')),
  4708. 'first',
  4709. '/label',
  4710. '/div',
  4711. array('div' => array('class' => 'checkbox')),
  4712. array('input' => array(
  4713. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4714. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4715. )),
  4716. array('label' => array('for' => 'ModelMultiFieldB')),
  4717. 'second',
  4718. '/label',
  4719. '/div',
  4720. array('div' => array('class' => 'checkbox')),
  4721. array('input' => array(
  4722. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4723. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4724. )),
  4725. array('label' => array('for' => 'ModelMultiFieldC')),
  4726. 'third',
  4727. '/label',
  4728. '/div'
  4729. );
  4730. $this->assertTags($result, $expected);
  4731. $result = $this->Form->select(
  4732. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4733. );
  4734. $expected = array(
  4735. 'input' => array(
  4736. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4737. ),
  4738. array('div' => array('class' => 'checkbox')),
  4739. array('input' => array(
  4740. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4741. 'value' => '1', 'id' => 'ModelMultiField1'
  4742. )),
  4743. array('label' => array('for' => 'ModelMultiField1')),
  4744. 'first',
  4745. '/label',
  4746. '/div'
  4747. );
  4748. $this->assertTags($result, $expected);
  4749. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4750. $result = $this->Form->select(
  4751. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4752. );
  4753. $expected = array(
  4754. 'input' => array(
  4755. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4756. ),
  4757. array('div' => array('class' => 'checkbox')),
  4758. array('input' => array(
  4759. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4760. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4761. )),
  4762. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4763. 'first',
  4764. '/label',
  4765. '/div',
  4766. array('div' => array('class' => 'checkbox')),
  4767. array('input' => array(
  4768. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4769. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4770. )),
  4771. array('label' => array('for' => 'ModelTagsArray')),
  4772. 'Array',
  4773. '/label',
  4774. '/div'
  4775. );
  4776. $this->assertTags($result, $expected);
  4777. $result = $this->Form->select(
  4778. 'Model.multi_field',
  4779. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4780. array('multiple' => 'checkbox')
  4781. );
  4782. $expected = array(
  4783. 'input' => array(
  4784. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4785. ),
  4786. array('div' => array('class' => 'checkbox')),
  4787. array('input' => array(
  4788. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4789. 'value' => 'a+', 'id' => 'ModelMultiFieldA2'
  4790. )),
  4791. array('label' => array('for' => 'ModelMultiFieldA2')),
  4792. 'first',
  4793. '/label',
  4794. '/div',
  4795. array('div' => array('class' => 'checkbox')),
  4796. array('input' => array(
  4797. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4798. 'value' => 'a++', 'id' => 'ModelMultiFieldA1'
  4799. )),
  4800. array('label' => array('for' => 'ModelMultiFieldA1')),
  4801. 'second',
  4802. '/label',
  4803. '/div',
  4804. array('div' => array('class' => 'checkbox')),
  4805. array('input' => array(
  4806. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4807. 'value' => 'a+++', 'id' => 'ModelMultiFieldA'
  4808. )),
  4809. array('label' => array('for' => 'ModelMultiFieldA')),
  4810. 'third',
  4811. '/label',
  4812. '/div'
  4813. );
  4814. $this->assertTags($result, $expected);
  4815. $result = $this->Form->select(
  4816. 'Model.multi_field',
  4817. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4818. array('multiple' => 'checkbox')
  4819. );
  4820. $expected = array(
  4821. 'input' => array(
  4822. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4823. ),
  4824. array('div' => array('class' => 'checkbox')),
  4825. array('input' => array(
  4826. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4827. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4828. )),
  4829. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4830. 'first',
  4831. '/label',
  4832. '/div',
  4833. array('div' => array('class' => 'checkbox')),
  4834. array('input' => array(
  4835. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4836. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4837. )),
  4838. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4839. 'second',
  4840. '/label',
  4841. '/div',
  4842. array('div' => array('class' => 'checkbox')),
  4843. array('input' => array(
  4844. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4845. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4846. )),
  4847. array('label' => array('for' => 'ModelMultiFieldAB')),
  4848. 'third',
  4849. '/label',
  4850. '/div'
  4851. );
  4852. $this->assertTags($result, $expected);
  4853. }
  4854. /**
  4855. * test multiple checkboxes with div styles.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testSelectMultipleCheckboxDiv() {
  4860. $result = $this->Form->select(
  4861. 'Model.tags',
  4862. array('first', 'second'),
  4863. array('multiple' => 'checkbox', 'class' => 'my-class')
  4864. );
  4865. $expected = array(
  4866. 'input' => array(
  4867. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4868. ),
  4869. array('div' => array('class' => 'my-class')),
  4870. array('input' => array(
  4871. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4872. 'value' => '0', 'id' => 'ModelTags0'
  4873. )),
  4874. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4875. '/div',
  4876. array('div' => array('class' => 'my-class')),
  4877. array('input' => array(
  4878. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4879. 'value' => '1', 'id' => 'ModelTags1'
  4880. )),
  4881. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4882. '/div'
  4883. );
  4884. $this->assertTags($result, $expected);
  4885. $result = $this->Form->input('Model.tags', array(
  4886. 'options' => array('first', 'second'),
  4887. 'multiple' => 'checkbox',
  4888. 'class' => 'my-class',
  4889. 'div' => false,
  4890. 'label' => false
  4891. ));
  4892. $this->assertTags($result, $expected);
  4893. $Contact = ClassRegistry::getObject('Contact');
  4894. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4895. $result = $this->Form->input('Contact.tags', array(
  4896. 'options' => array('one'),
  4897. 'multiple' => 'checkbox',
  4898. 'label' => false,
  4899. 'div' => false
  4900. ));
  4901. $expected = array(
  4902. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4903. array('div' => array('class' => 'checkbox form-error')),
  4904. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4905. array('label' => array('for' => 'ContactTags0')),
  4906. 'one',
  4907. '/label',
  4908. '/div'
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. $result = $this->Form->input('Contact.tags', array(
  4912. 'options' => array('one'),
  4913. 'multiple' => 'checkbox',
  4914. 'class' => 'mycheckbox',
  4915. 'label' => false,
  4916. 'div' => false
  4917. ));
  4918. $expected = array(
  4919. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4920. array('div' => array('class' => 'mycheckbox form-error')),
  4921. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4922. array('label' => array('for' => 'ContactTags0')),
  4923. 'one',
  4924. '/label',
  4925. '/div'
  4926. );
  4927. $this->assertTags($result, $expected);
  4928. }
  4929. /**
  4930. * Checks the security hash array generated for multiple-input checkbox elements
  4931. *
  4932. * @return void
  4933. */
  4934. public function testSelectMultipleCheckboxSecurity() {
  4935. $this->Form->request['_Token'] = array('key' => 'testKey');
  4936. $this->assertEquals(array(), $this->Form->fields);
  4937. $result = $this->Form->select(
  4938. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4939. array('multiple' => 'checkbox')
  4940. );
  4941. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4942. $result = $this->Form->secure($this->Form->fields);
  4943. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4944. $this->assertRegExp('/"' . $key . '"/', $result);
  4945. }
  4946. /**
  4947. * Multiple select elements should always be secured as they always participate
  4948. * in the POST data.
  4949. *
  4950. * @return void
  4951. */
  4952. public function testSelectMultipleSecureWithNoOptions() {
  4953. $this->Form->request['_Token'] = array('key' => 'testkey');
  4954. $this->assertEquals(array(), $this->Form->fields);
  4955. $this->Form->select(
  4956. 'Model.select',
  4957. array(),
  4958. array('multiple' => true)
  4959. );
  4960. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4961. }
  4962. /**
  4963. * When a select box has no options it should not be added to the fields list
  4964. * as it always fail post validation.
  4965. *
  4966. * @return void
  4967. */
  4968. public function testSelectNoSecureWithNoOptions() {
  4969. $this->Form->request['_Token'] = array('key' => 'testkey');
  4970. $this->assertEquals(array(), $this->Form->fields);
  4971. $this->Form->select(
  4972. 'Model.select',
  4973. array()
  4974. );
  4975. $this->assertEquals(array(), $this->Form->fields);
  4976. $this->Form->select(
  4977. 'Model.select',
  4978. array(),
  4979. array('empty' => true)
  4980. );
  4981. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4982. }
  4983. /**
  4984. * testInputMultipleCheckboxes method
  4985. *
  4986. * test input() resulting in multi select elements being generated.
  4987. *
  4988. * @return void
  4989. */
  4990. public function testInputMultipleCheckboxes() {
  4991. $result = $this->Form->input('Model.multi_field', array(
  4992. 'options' => array('first', 'second', 'third'),
  4993. 'multiple' => 'checkbox'
  4994. ));
  4995. $expected = array(
  4996. array('div' => array('class' => 'input select')),
  4997. array('label' => array('for' => 'ModelMultiField')),
  4998. 'Multi Field',
  4999. '/label',
  5000. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5001. array('div' => array('class' => 'checkbox')),
  5002. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5003. array('label' => array('for' => 'ModelMultiField0')),
  5004. 'first',
  5005. '/label',
  5006. '/div',
  5007. array('div' => array('class' => 'checkbox')),
  5008. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5009. array('label' => array('for' => 'ModelMultiField1')),
  5010. 'second',
  5011. '/label',
  5012. '/div',
  5013. array('div' => array('class' => 'checkbox')),
  5014. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5015. array('label' => array('for' => 'ModelMultiField2')),
  5016. 'third',
  5017. '/label',
  5018. '/div',
  5019. '/div'
  5020. );
  5021. $this->assertTags($result, $expected);
  5022. $result = $this->Form->input('Model.multi_field', array(
  5023. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  5024. 'multiple' => 'checkbox'
  5025. ));
  5026. $expected = array(
  5027. array('div' => array('class' => 'input select')),
  5028. array('label' => array('for' => 'ModelMultiField')),
  5029. 'Multi Field',
  5030. '/label',
  5031. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5032. array('div' => array('class' => 'checkbox')),
  5033. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  5034. array('label' => array('for' => 'ModelMultiFieldA')),
  5035. 'first',
  5036. '/label',
  5037. '/div',
  5038. array('div' => array('class' => 'checkbox')),
  5039. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5040. array('label' => array('for' => 'ModelMultiFieldB')),
  5041. 'second',
  5042. '/label',
  5043. '/div',
  5044. array('div' => array('class' => 'checkbox')),
  5045. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5046. array('label' => array('for' => 'ModelMultiFieldC')),
  5047. 'third',
  5048. '/label',
  5049. '/div',
  5050. '/div'
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. $result = $this->Form->input('Model.multi_field', array(
  5054. 'options' => array('1' => 'first'),
  5055. 'multiple' => 'checkbox',
  5056. 'label' => false,
  5057. 'div' => false
  5058. ));
  5059. $expected = array(
  5060. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5061. array('div' => array('class' => 'checkbox')),
  5062. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5063. array('label' => array('for' => 'ModelMultiField1')),
  5064. 'first',
  5065. '/label',
  5066. '/div'
  5067. );
  5068. $this->assertTags($result, $expected);
  5069. $result = $this->Form->input('Model.multi_field', array(
  5070. 'options' => array('2' => 'second'),
  5071. 'multiple' => 'checkbox',
  5072. 'label' => false,
  5073. 'div' => false
  5074. ));
  5075. $expected = array(
  5076. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5077. array('div' => array('class' => 'checkbox')),
  5078. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5079. array('label' => array('for' => 'ModelMultiField2')),
  5080. 'second',
  5081. '/label',
  5082. '/div'
  5083. );
  5084. $this->assertTags($result, $expected);
  5085. }
  5086. /**
  5087. * testSelectHiddenFieldOmission method
  5088. *
  5089. * test that select() with 'hiddenField' => false omits the hidden field
  5090. *
  5091. * @return void
  5092. */
  5093. public function testSelectHiddenFieldOmission() {
  5094. $result = $this->Form->select('Model.multi_field',
  5095. array('first', 'second'),
  5096. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5097. );
  5098. $expected = array(
  5099. array('div' => array('class' => 'checkbox')),
  5100. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5101. array('label' => array('for' => 'ModelMultiField0')),
  5102. 'first',
  5103. '/label',
  5104. '/div',
  5105. array('div' => array('class' => 'checkbox')),
  5106. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5107. array('label' => array('for' => 'ModelMultiField1')),
  5108. 'second',
  5109. '/label',
  5110. '/div'
  5111. );
  5112. $this->assertTags($result, $expected);
  5113. $result = $this->Form->input('Model.multi_field', array(
  5114. 'options' => array('first', 'second'),
  5115. 'multiple' => 'checkbox',
  5116. 'hiddenField' => false
  5117. ));
  5118. $expected = array(
  5119. array('div' => array('class' => 'input select')),
  5120. array('label' => array('for' => 'ModelMultiField')),
  5121. 'Multi Field',
  5122. '/label',
  5123. array('div' => array('class' => 'checkbox')),
  5124. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5125. array('label' => array('for' => 'ModelMultiField0')),
  5126. 'first',
  5127. '/label',
  5128. '/div',
  5129. array('div' => array('class' => 'checkbox')),
  5130. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5131. array('label' => array('for' => 'ModelMultiField1')),
  5132. 'second',
  5133. '/label',
  5134. '/div',
  5135. '/div'
  5136. );
  5137. $this->assertTags($result, $expected);
  5138. }
  5139. /**
  5140. * test that select() with multiple = checkbox works with overriding name attribute.
  5141. *
  5142. * @return void
  5143. */
  5144. public function testSelectCheckboxMultipleOverrideName() {
  5145. $result = $this->Form->input('category', array(
  5146. 'type' => 'select',
  5147. 'multiple' => 'checkbox',
  5148. 'name' => 'data[fish]',
  5149. 'options' => array('1', '2'),
  5150. 'div' => false,
  5151. 'label' => false,
  5152. ));
  5153. $expected = array(
  5154. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5155. array('div' => array('class' => 'checkbox')),
  5156. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5157. array('label' => array('for' => 'Category0')), '1', '/label',
  5158. '/div',
  5159. array('div' => array('class' => 'checkbox')),
  5160. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5161. array('label' => array('for' => 'Category1')), '2', '/label',
  5162. '/div'
  5163. );
  5164. $this->assertTags($result, $expected);
  5165. }
  5166. /**
  5167. * Test that 'id' overrides all the checkbox id's as well.
  5168. *
  5169. * @return void
  5170. */
  5171. public function testSelectCheckboxMultipleId() {
  5172. $result = $this->Form->select(
  5173. 'Model.multi_field',
  5174. array('first', 'second', 'third'),
  5175. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5176. );
  5177. $expected = array(
  5178. 'input' => array(
  5179. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5180. ),
  5181. array('div' => array('class' => 'checkbox')),
  5182. array('input' => array(
  5183. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5184. 'value' => '0', 'id' => 'CustomId0'
  5185. )),
  5186. array('label' => array('for' => 'CustomId0')),
  5187. 'first',
  5188. '/label',
  5189. '/div',
  5190. array('div' => array('class' => 'checkbox')),
  5191. array('input' => array(
  5192. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5193. 'value' => '1', 'id' => 'CustomId1'
  5194. )),
  5195. array('label' => array('for' => 'CustomId1')),
  5196. 'second',
  5197. '/label',
  5198. '/div',
  5199. array('div' => array('class' => 'checkbox')),
  5200. array('input' => array(
  5201. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5202. 'value' => '2', 'id' => 'CustomId2'
  5203. )),
  5204. array('label' => array('for' => 'CustomId2')),
  5205. 'third',
  5206. '/label',
  5207. '/div'
  5208. );
  5209. $this->assertTags($result, $expected);
  5210. }
  5211. /**
  5212. * testCheckbox method
  5213. *
  5214. * Test generation of checkboxes
  5215. *
  5216. * @return void
  5217. */
  5218. public function testCheckbox() {
  5219. $result = $this->Form->checkbox('Model.field');
  5220. $expected = array(
  5221. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5222. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $result = $this->Form->checkbox('Model.field', array(
  5226. 'id' => 'theID',
  5227. 'value' => 'myvalue',
  5228. 'form' => 'my-form',
  5229. ));
  5230. $expected = array(
  5231. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'),
  5232. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
  5233. );
  5234. $this->assertTags($result, $expected);
  5235. $Contact = ClassRegistry::getObject('Contact');
  5236. $Contact->validationErrors['field'] = 1;
  5237. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5238. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5239. $expected = array(
  5240. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5241. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5242. );
  5243. $this->assertTags($result, $expected);
  5244. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5245. $expected = array(
  5246. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5247. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5248. );
  5249. $this->assertTags($result, $expected);
  5250. $this->Form->request->data['Contact']['field'] = '';
  5251. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5252. $expected = array(
  5253. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5254. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5255. );
  5256. $this->assertTags($result, $expected);
  5257. $Contact->validationErrors = array();
  5258. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5259. $expected = array(
  5260. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5261. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5262. );
  5263. $this->assertTags($result, $expected);
  5264. $this->Form->request->data['Contact']['published'] = 1;
  5265. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5266. $expected = array(
  5267. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5268. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5269. );
  5270. $this->assertTags($result, $expected);
  5271. $this->Form->request->data['Contact']['published'] = 0;
  5272. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5273. $expected = array(
  5274. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5275. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5276. );
  5277. $this->assertTags($result, $expected);
  5278. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5279. $expected = array(
  5280. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5281. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5282. );
  5283. $this->assertTags($result, $expected);
  5284. $result = $this->Form->checkbox('CustomField.1.value');
  5285. $expected = array(
  5286. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5287. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5288. );
  5289. $this->assertTags($result, $expected);
  5290. }
  5291. /**
  5292. * test checkbox() with a custom name attribute
  5293. *
  5294. * @return void
  5295. */
  5296. public function testCheckboxCustomNameAttribute() {
  5297. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5298. $expected = array(
  5299. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5300. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5301. );
  5302. $this->assertTags($result, $expected);
  5303. }
  5304. /**
  5305. * test the checked option for checkboxes.
  5306. *
  5307. * @return void
  5308. */
  5309. public function testCheckboxCheckedOption() {
  5310. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5311. $expected = array(
  5312. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5313. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5314. );
  5315. $this->assertTags($result, $expected);
  5316. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5317. $expected = array(
  5318. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5319. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5320. );
  5321. $this->assertTags($result, $expected);
  5322. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5323. $expected = array(
  5324. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5325. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5326. );
  5327. $this->assertTags($result, $expected);
  5328. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5329. $expected = array(
  5330. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5331. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5332. );
  5333. $this->assertTags($result, $expected);
  5334. $this->Form->request->data['Model']['field'] = 1;
  5335. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5336. $expected = array(
  5337. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5338. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. }
  5342. /**
  5343. * Test that disabled attribute works on both the checkbox and hidden input.
  5344. *
  5345. * @return void
  5346. */
  5347. public function testCheckboxDisabling() {
  5348. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5349. $expected = array(
  5350. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5351. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5352. );
  5353. $this->assertTags($result, $expected);
  5354. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5355. $expected = array(
  5356. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5357. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5358. );
  5359. $this->assertTags($result, $expected);
  5360. }
  5361. /**
  5362. * Test that the hidden input for checkboxes can be omitted or set to a
  5363. * specific value.
  5364. *
  5365. * @return void
  5366. */
  5367. public function testCheckboxHiddenField() {
  5368. $result = $this->Form->input('UserForm.something', array(
  5369. 'type' => 'checkbox',
  5370. 'hiddenField' => false
  5371. ));
  5372. $expected = array(
  5373. 'div' => array('class' => 'input checkbox'),
  5374. array('input' => array(
  5375. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5376. 'value' => '1', 'id' => 'UserFormSomething'
  5377. )),
  5378. 'label' => array('for' => 'UserFormSomething'),
  5379. 'Something',
  5380. '/label',
  5381. '/div'
  5382. );
  5383. $this->assertTags($result, $expected);
  5384. $result = $this->Form->input('UserForm.something', array(
  5385. 'type' => 'checkbox',
  5386. 'value' => 'Y',
  5387. 'hiddenField' => 'N',
  5388. ));
  5389. $expected = array(
  5390. 'div' => array('class' => 'input checkbox'),
  5391. array('input' => array(
  5392. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5393. 'value' => 'N', 'id' => 'UserFormSomething_'
  5394. )),
  5395. array('input' => array(
  5396. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5397. 'value' => 'Y', 'id' => 'UserFormSomething'
  5398. )),
  5399. 'label' => array('for' => 'UserFormSomething'),
  5400. 'Something',
  5401. '/label',
  5402. '/div'
  5403. );
  5404. $this->assertTags($result, $expected);
  5405. }
  5406. /**
  5407. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  5408. *
  5409. * @return void
  5410. */
  5411. public function testCheckboxZeroValue() {
  5412. $result = $this->Form->input('User.get_spam', array(
  5413. 'type' => 'checkbox',
  5414. 'value' => '0',
  5415. 'hiddenField' => '1',
  5416. ));
  5417. $expected = array(
  5418. 'div' => array('class' => 'input checkbox'),
  5419. array('input' => array(
  5420. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  5421. 'value' => '1', 'id' => 'UserGetSpam_'
  5422. )),
  5423. array('input' => array(
  5424. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  5425. 'value' => '0', 'id' => 'UserGetSpam'
  5426. )),
  5427. 'label' => array('for' => 'UserGetSpam'),
  5428. 'Get Spam',
  5429. '/label',
  5430. '/div'
  5431. );
  5432. $this->assertTags($result, $expected);
  5433. }
  5434. /**
  5435. * testDateTime method
  5436. *
  5437. * Test generation of date/time select elements
  5438. *
  5439. * @return void
  5440. */
  5441. public function testDateTime() {
  5442. extract($this->dateRegex);
  5443. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5444. $now = strtotime('now');
  5445. $expected = array(
  5446. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5447. $daysRegex,
  5448. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5449. date('j', $now),
  5450. '/option',
  5451. '*/select',
  5452. '-',
  5453. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5454. $monthsRegex,
  5455. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5456. date('F', $now),
  5457. '/option',
  5458. '*/select',
  5459. '-',
  5460. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5461. $yearsRegex,
  5462. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5463. date('Y', $now),
  5464. '/option',
  5465. '*/select',
  5466. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5467. $hoursRegex,
  5468. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5469. date('g', $now),
  5470. '/option',
  5471. '*/select',
  5472. ':',
  5473. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5474. $minutesRegex,
  5475. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5476. date('i', $now),
  5477. '/option',
  5478. '*/select',
  5479. ' ',
  5480. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5481. $meridianRegex,
  5482. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5483. date('a', $now),
  5484. '/option',
  5485. '*/select'
  5486. );
  5487. $this->assertTags($result, $expected);
  5488. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5489. $expected = array(
  5490. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5491. $daysRegex,
  5492. array('option' => array('value' => '')),
  5493. '/option',
  5494. '*/select',
  5495. '-',
  5496. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5497. $monthsRegex,
  5498. array('option' => array('value' => '')),
  5499. '/option',
  5500. '*/select',
  5501. '-',
  5502. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5503. $yearsRegex,
  5504. array('option' => array('value' => '')),
  5505. '/option',
  5506. '*/select',
  5507. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5508. $hoursRegex,
  5509. array('option' => array('value' => '')),
  5510. '/option',
  5511. '*/select',
  5512. ':',
  5513. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5514. $minutesRegex,
  5515. array('option' => array('value' => '')),
  5516. '/option',
  5517. '*/select',
  5518. ' ',
  5519. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5520. $meridianRegex,
  5521. array('option' => array('value' => '')),
  5522. '/option',
  5523. '*/select'
  5524. );
  5525. $this->assertTags($result, $expected);
  5526. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5527. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5528. $this->assertTags($result, $expected);
  5529. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5530. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5531. $this->assertTags($result, $expected);
  5532. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5533. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5534. $expected = array(
  5535. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5536. $daysRegex,
  5537. array('option' => array('value' => '')),
  5538. '/option',
  5539. '*/select',
  5540. '-',
  5541. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5542. $monthsRegex,
  5543. array('option' => array('value' => '')),
  5544. '/option',
  5545. '*/select',
  5546. '-',
  5547. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5548. $yearsRegex,
  5549. array('option' => array('value' => '')),
  5550. '/option',
  5551. '*/select',
  5552. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5553. $hoursRegex,
  5554. array('option' => array('value' => '')),
  5555. '/option',
  5556. '*/select',
  5557. ':',
  5558. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5559. $minutesRegex,
  5560. array('option' => array('value' => '')),
  5561. '/option',
  5562. array('option' => array('value' => '00')),
  5563. '00',
  5564. '/option',
  5565. array('option' => array('value' => '05')),
  5566. '05',
  5567. '/option',
  5568. array('option' => array('value' => '10')),
  5569. '10',
  5570. '/option',
  5571. '*/select',
  5572. ' ',
  5573. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5574. $meridianRegex,
  5575. array('option' => array('value' => '')),
  5576. '/option',
  5577. '*/select'
  5578. );
  5579. $this->assertTags($result, $expected);
  5580. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5581. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5582. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5583. $this->Form->request->data['Contact']['data'] = null;
  5584. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5585. $expected = array(
  5586. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5587. $daysRegex,
  5588. array('option' => array('value' => '')),
  5589. '/option',
  5590. '*/select',
  5591. '-',
  5592. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5593. $monthsRegex,
  5594. array('option' => array('value' => '')),
  5595. '/option',
  5596. '*/select',
  5597. '-',
  5598. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5599. $yearsRegex,
  5600. array('option' => array('value' => '')),
  5601. '/option',
  5602. '*/select',
  5603. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5604. $hoursRegex,
  5605. array('option' => array('value' => '')),
  5606. '/option',
  5607. '*/select',
  5608. ':',
  5609. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5610. $minutesRegex,
  5611. array('option' => array('value' => '')),
  5612. '/option',
  5613. '*/select',
  5614. ' ',
  5615. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5616. $meridianRegex,
  5617. array('option' => array('value' => '')),
  5618. '/option',
  5619. '*/select'
  5620. );
  5621. $this->assertTags($result, $expected);
  5622. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5623. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5624. $now = strtotime($this->Form->data['Model']['field']);
  5625. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5626. $expected = array(
  5627. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5628. $daysRegex,
  5629. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5630. date('j', $now),
  5631. '/option',
  5632. '*/select',
  5633. '-',
  5634. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5635. $monthsRegex,
  5636. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5637. date('F', $now),
  5638. '/option',
  5639. '*/select',
  5640. '-',
  5641. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5642. $yearsRegex,
  5643. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5644. date('Y', $now),
  5645. '/option',
  5646. '*/select',
  5647. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5648. $hoursRegex,
  5649. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5650. date('g', $now),
  5651. '/option',
  5652. '*/select',
  5653. ':',
  5654. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5655. $minutesRegex,
  5656. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5657. date('i', $now),
  5658. '/option',
  5659. '*/select',
  5660. ' ',
  5661. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5662. $meridianRegex,
  5663. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5664. date('a', $now),
  5665. '/option',
  5666. '*/select'
  5667. );
  5668. $this->assertTags($result, $expected);
  5669. $selected = strtotime('2008-10-26 12:33:00');
  5670. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5671. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5672. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5673. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5674. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5675. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5676. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5677. $this->Form->create('Contact');
  5678. $result = $this->Form->input('published');
  5679. $now = strtotime('now');
  5680. $expected = array(
  5681. 'div' => array('class' => 'input date'),
  5682. 'label' => array('for' => 'ContactPublishedMonth'),
  5683. 'Published',
  5684. '/label',
  5685. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5686. $monthsRegex,
  5687. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5688. date('F', $now),
  5689. '/option',
  5690. '*/select',
  5691. '-',
  5692. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5693. $daysRegex,
  5694. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5695. date('j', $now),
  5696. '/option',
  5697. '*/select',
  5698. '-',
  5699. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5700. $yearsRegex,
  5701. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5702. date('Y', $now),
  5703. '/option',
  5704. '*/select',
  5705. '/div'
  5706. );
  5707. $this->assertTags($result, $expected);
  5708. $result = $this->Form->input('published2', array('type' => 'date'));
  5709. $now = strtotime('now');
  5710. $expected = array(
  5711. 'div' => array('class' => 'input date'),
  5712. 'label' => array('for' => 'ContactPublished2Month'),
  5713. 'Published2',
  5714. '/label',
  5715. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5716. $monthsRegex,
  5717. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5718. date('F', $now),
  5719. '/option',
  5720. '*/select',
  5721. '-',
  5722. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5723. $daysRegex,
  5724. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5725. date('j', $now),
  5726. '/option',
  5727. '*/select',
  5728. '-',
  5729. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5730. $yearsRegex,
  5731. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5732. date('Y', $now),
  5733. '/option',
  5734. '*/select',
  5735. '/div'
  5736. );
  5737. $this->assertTags($result, $expected);
  5738. $this->Form->create('Contact');
  5739. $result = $this->Form->input('published', array('monthNames' => false));
  5740. $now = strtotime('now');
  5741. $expected = array(
  5742. 'div' => array('class' => 'input date'),
  5743. 'label' => array('for' => 'ContactPublishedMonth'),
  5744. 'Published',
  5745. '/label',
  5746. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5747. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5748. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5749. date('m', $now),
  5750. '/option',
  5751. '*/select',
  5752. '-',
  5753. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5754. $daysRegex,
  5755. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5756. date('j', $now),
  5757. '/option',
  5758. '*/select',
  5759. '-',
  5760. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5761. $yearsRegex,
  5762. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5763. date('Y', $now),
  5764. '/option',
  5765. '*/select',
  5766. '/div'
  5767. );
  5768. $this->assertTags($result, $expected);
  5769. $result = $this->Form->input('published', array(
  5770. 'timeFormat' => 24,
  5771. 'interval' => 5,
  5772. 'selected' => strtotime('2009-09-03 13:37:00'),
  5773. 'type' => 'datetime'
  5774. ));
  5775. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5776. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5777. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5778. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5779. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5780. }
  5781. /**
  5782. * Test dateTime with rounding
  5783. *
  5784. * @return void
  5785. */
  5786. public function testDateTimeRounding() {
  5787. $this->Form->request->data['Contact'] = array(
  5788. 'date' => array(
  5789. 'day' => '13',
  5790. 'month' => '12',
  5791. 'year' => '2010',
  5792. 'hour' => '04',
  5793. 'min' => '19',
  5794. 'meridian' => 'AM'
  5795. )
  5796. );
  5797. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5798. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5799. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5800. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5801. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5802. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5803. }
  5804. /**
  5805. * Test that empty values don't trigger errors.
  5806. *
  5807. * @return void
  5808. */
  5809. public function testDateTimeNoErrorsOnEmptyData() {
  5810. $this->Form->request->data['Contact'] = array(
  5811. 'date' => array(
  5812. 'day' => '',
  5813. 'month' => '',
  5814. 'year' => '',
  5815. 'hour' => '',
  5816. 'min' => '',
  5817. 'meridian' => ''
  5818. )
  5819. );
  5820. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5821. $this->assertNotEmpty($result);
  5822. }
  5823. /**
  5824. * test that datetime() and default values work.
  5825. *
  5826. * @return void
  5827. */
  5828. public function testDatetimeWithDefault() {
  5829. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5830. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5831. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5832. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5833. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5834. 'default' => '2009-06-01 11:15:30'
  5835. ));
  5836. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5837. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5838. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5839. }
  5840. /**
  5841. * test that bogus non-date time data doesn't cause errors.
  5842. *
  5843. * @return void
  5844. */
  5845. public function testDateTimeWithBogusData() {
  5846. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5847. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5848. }
  5849. /**
  5850. * testDateTime all zeros
  5851. *
  5852. * @return void
  5853. */
  5854. public function testDateTimeAllZeros() {
  5855. $result = $this->Form->dateTime('Contact.date',
  5856. 'DMY',
  5857. false,
  5858. array(
  5859. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5860. 'value' => '0000-00-00'
  5861. )
  5862. );
  5863. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5864. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5865. }
  5866. /**
  5867. * testDateTimeEmptyAsArray
  5868. *
  5869. * @return void
  5870. */
  5871. public function testDateTimeEmptyAsArray() {
  5872. $result = $this->Form->dateTime('Contact.date',
  5873. 'DMY',
  5874. '12',
  5875. array(
  5876. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5877. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5878. )
  5879. )
  5880. );
  5881. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5882. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5883. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5884. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5885. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5886. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5887. $result = $this->Form->dateTime('Contact.date',
  5888. 'DMY',
  5889. '12',
  5890. array(
  5891. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5892. )
  5893. );
  5894. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5895. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5896. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5897. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5898. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5899. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5900. }
  5901. /**
  5902. * testFormDateTimeMulti method
  5903. *
  5904. * test multiple datetime element generation
  5905. *
  5906. * @return void
  5907. */
  5908. public function testFormDateTimeMulti() {
  5909. extract($this->dateRegex);
  5910. $result = $this->Form->dateTime('Contact.1.updated');
  5911. $expected = array(
  5912. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5913. $daysRegex,
  5914. array('option' => array('value' => '')),
  5915. '/option',
  5916. '*/select',
  5917. '-',
  5918. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5919. $monthsRegex,
  5920. array('option' => array('value' => '')),
  5921. '/option',
  5922. '*/select',
  5923. '-',
  5924. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5925. $yearsRegex,
  5926. array('option' => array('value' => '')),
  5927. '/option',
  5928. '*/select',
  5929. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5930. $hoursRegex,
  5931. array('option' => array('value' => '')),
  5932. '/option',
  5933. '*/select',
  5934. ':',
  5935. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5936. $minutesRegex,
  5937. array('option' => array('value' => '')),
  5938. '/option',
  5939. '*/select',
  5940. ' ',
  5941. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5942. $meridianRegex,
  5943. array('option' => array('value' => '')),
  5944. '/option',
  5945. '*/select'
  5946. );
  5947. $this->assertTags($result, $expected);
  5948. $result = $this->Form->dateTime('Contact.2.updated');
  5949. $expected = array(
  5950. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5951. $daysRegex,
  5952. array('option' => array('value' => '')),
  5953. '/option',
  5954. '*/select',
  5955. '-',
  5956. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5957. $monthsRegex,
  5958. array('option' => array('value' => '')),
  5959. '/option',
  5960. '*/select',
  5961. '-',
  5962. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5963. $yearsRegex,
  5964. array('option' => array('value' => '')),
  5965. '/option',
  5966. '*/select',
  5967. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5968. $hoursRegex,
  5969. array('option' => array('value' => '')),
  5970. '/option',
  5971. '*/select',
  5972. ':',
  5973. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5974. $minutesRegex,
  5975. array('option' => array('value' => '')),
  5976. '/option',
  5977. '*/select',
  5978. ' ',
  5979. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5980. $meridianRegex,
  5981. array('option' => array('value' => '')),
  5982. '/option',
  5983. '*/select'
  5984. );
  5985. $this->assertTags($result, $expected);
  5986. }
  5987. /**
  5988. * When changing the date format, the label should always focus the first select box when
  5989. * clicked.
  5990. *
  5991. * @return void
  5992. */
  5993. public function testDateTimeLabelIdMatchesFirstInput() {
  5994. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5995. $this->assertContains('label for="ModelDateMonth"', $result);
  5996. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5997. $this->assertContains('label for="ModelDateDay"', $result);
  5998. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5999. $this->assertContains('label for="ModelDateYear"', $result);
  6000. }
  6001. /**
  6002. * testMonth method
  6003. *
  6004. * @return void
  6005. */
  6006. public function testMonth() {
  6007. $result = $this->Form->month('Model.field');
  6008. $expected = array(
  6009. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6010. array('option' => array('value' => '')),
  6011. '/option',
  6012. array('option' => array('value' => '01')),
  6013. date('F', strtotime('2008-01-01 00:00:00')),
  6014. '/option',
  6015. array('option' => array('value' => '02')),
  6016. date('F', strtotime('2008-02-01 00:00:00')),
  6017. '/option',
  6018. '*/select',
  6019. );
  6020. $this->assertTags($result, $expected);
  6021. $result = $this->Form->month('Model.field', array('empty' => true));
  6022. $expected = array(
  6023. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6024. array('option' => array('value' => '')),
  6025. '/option',
  6026. array('option' => array('value' => '01')),
  6027. date('F', strtotime('2008-01-01 00:00:00')),
  6028. '/option',
  6029. array('option' => array('value' => '02')),
  6030. date('F', strtotime('2008-02-01 00:00:00')),
  6031. '/option',
  6032. '*/select',
  6033. );
  6034. $this->assertTags($result, $expected);
  6035. $result = $this->Form->month('Model.field', array('monthNames' => false));
  6036. $expected = array(
  6037. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6038. array('option' => array('value' => '')),
  6039. '/option',
  6040. array('option' => array('value' => '01')),
  6041. '01',
  6042. '/option',
  6043. array('option' => array('value' => '02')),
  6044. '02',
  6045. '/option',
  6046. '*/select',
  6047. );
  6048. $this->assertTags($result, $expected);
  6049. $monthNames = array(
  6050. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6051. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  6052. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  6053. $expected = array(
  6054. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6055. array('option' => array('value' => '')),
  6056. '/option',
  6057. array('option' => array('value' => '01')),
  6058. 'Jan',
  6059. '/option',
  6060. array('option' => array('value' => '02')),
  6061. 'Feb',
  6062. '/option',
  6063. '*/select',
  6064. );
  6065. $this->assertTags($result, $expected);
  6066. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6067. $result = $this->Form->month('Project.release');
  6068. $expected = array(
  6069. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  6070. array('option' => array('value' => '')),
  6071. '/option',
  6072. array('option' => array('value' => '01')),
  6073. 'January',
  6074. '/option',
  6075. array('option' => array('value' => '02', 'selected' => 'selected')),
  6076. 'February',
  6077. '/option',
  6078. '*/select',
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $this->Form->request->data['Model']['field'] = '12a';
  6082. $result = $this->Form->month('Model.field');
  6083. $expected = array(
  6084. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6085. array('option' => array('value' => '')),
  6086. '/option',
  6087. array('option' => array('value' => '01')),
  6088. date('F', strtotime('2008-01-01 00:00:00')),
  6089. '/option',
  6090. array('option' => array('value' => '02')),
  6091. date('F', strtotime('2008-02-01 00:00:00')),
  6092. '/option',
  6093. '*/select',
  6094. );
  6095. $this->assertTags($result, $expected);
  6096. }
  6097. /**
  6098. * testDay method
  6099. *
  6100. * @return void
  6101. */
  6102. public function testDay() {
  6103. extract($this->dateRegex);
  6104. $result = $this->Form->day('Model.field', array('value' => false));
  6105. $expected = array(
  6106. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6107. array('option' => array('value' => '')),
  6108. '/option',
  6109. array('option' => array('value' => '01')),
  6110. '1',
  6111. '/option',
  6112. array('option' => array('value' => '02')),
  6113. '2',
  6114. '/option',
  6115. $daysRegex,
  6116. '/select',
  6117. );
  6118. $this->assertTags($result, $expected);
  6119. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6120. $result = $this->Form->day('Model.field');
  6121. $expected = array(
  6122. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6123. array('option' => array('value' => '')),
  6124. '/option',
  6125. array('option' => array('value' => '01')),
  6126. '1',
  6127. '/option',
  6128. array('option' => array('value' => '02')),
  6129. '2',
  6130. '/option',
  6131. $daysRegex,
  6132. array('option' => array('value' => '10', 'selected' => 'selected')),
  6133. '10',
  6134. '/option',
  6135. $daysRegex,
  6136. '/select',
  6137. );
  6138. $this->assertTags($result, $expected);
  6139. $this->Form->request->data['Model']['field'] = '';
  6140. $result = $this->Form->day('Model.field', array('value' => '10'));
  6141. $expected = array(
  6142. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6143. array('option' => array('value' => '')),
  6144. '/option',
  6145. array('option' => array('value' => '01')),
  6146. '1',
  6147. '/option',
  6148. array('option' => array('value' => '02')),
  6149. '2',
  6150. '/option',
  6151. $daysRegex,
  6152. array('option' => array('value' => '10', 'selected' => 'selected')),
  6153. '10',
  6154. '/option',
  6155. $daysRegex,
  6156. '/select',
  6157. );
  6158. $this->assertTags($result, $expected);
  6159. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6160. $result = $this->Form->day('Model.field', array('value' => true));
  6161. $expected = array(
  6162. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6163. array('option' => array('value' => '')),
  6164. '/option',
  6165. array('option' => array('value' => '01')),
  6166. '1',
  6167. '/option',
  6168. array('option' => array('value' => '02')),
  6169. '2',
  6170. '/option',
  6171. $daysRegex,
  6172. array('option' => array('value' => '10', 'selected' => 'selected')),
  6173. '10',
  6174. '/option',
  6175. $daysRegex,
  6176. '/select',
  6177. );
  6178. $this->assertTags($result, $expected);
  6179. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6180. $result = $this->Form->day('Project.release');
  6181. $expected = array(
  6182. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6183. array('option' => array('value' => '')),
  6184. '/option',
  6185. array('option' => array('value' => '01')),
  6186. '1',
  6187. '/option',
  6188. array('option' => array('value' => '02')),
  6189. '2',
  6190. '/option',
  6191. $daysRegex,
  6192. array('option' => array('value' => '10', 'selected' => 'selected')),
  6193. '10',
  6194. '/option',
  6195. $daysRegex,
  6196. '/select',
  6197. );
  6198. $this->assertTags($result, $expected);
  6199. $this->Form->request->data['Model']['field'] = '12e';
  6200. $result = $this->Form->day('Model.field');
  6201. $expected = array(
  6202. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6203. array('option' => array('value' => '')),
  6204. '/option',
  6205. array('option' => array('value' => '01')),
  6206. '1',
  6207. '/option',
  6208. array('option' => array('value' => '02')),
  6209. '2',
  6210. '/option',
  6211. $daysRegex,
  6212. '/select',
  6213. );
  6214. $this->assertTags($result, $expected);
  6215. }
  6216. /**
  6217. * testMinute method
  6218. *
  6219. * @return void
  6220. */
  6221. public function testMinute() {
  6222. extract($this->dateRegex);
  6223. $result = $this->Form->minute('Model.field');
  6224. $expected = array(
  6225. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6226. array('option' => array('value' => '')),
  6227. '/option',
  6228. array('option' => array('value' => '00')),
  6229. '00',
  6230. '/option',
  6231. array('option' => array('value' => '01')),
  6232. '01',
  6233. '/option',
  6234. array('option' => array('value' => '02')),
  6235. '02',
  6236. '/option',
  6237. $minutesRegex,
  6238. '/select',
  6239. );
  6240. $this->assertTags($result, $expected);
  6241. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6242. $result = $this->Form->minute('Model.field');
  6243. $expected = array(
  6244. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6245. array('option' => array('value' => '')),
  6246. '/option',
  6247. array('option' => array('value' => '00')),
  6248. '00',
  6249. '/option',
  6250. array('option' => array('value' => '01')),
  6251. '01',
  6252. '/option',
  6253. array('option' => array('value' => '02')),
  6254. '02',
  6255. '/option',
  6256. $minutesRegex,
  6257. array('option' => array('value' => '12', 'selected' => 'selected')),
  6258. '12',
  6259. '/option',
  6260. $minutesRegex,
  6261. '/select',
  6262. );
  6263. $this->assertTags($result, $expected);
  6264. $this->Form->request->data['Model']['field'] = '';
  6265. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6266. $expected = array(
  6267. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6268. array('option' => array('value' => '')),
  6269. '/option',
  6270. array('option' => array('value' => '00')),
  6271. '00',
  6272. '/option',
  6273. array('option' => array('value' => '05')),
  6274. '05',
  6275. '/option',
  6276. array('option' => array('value' => '10')),
  6277. '10',
  6278. '/option',
  6279. $minutesRegex,
  6280. '/select',
  6281. );
  6282. $this->assertTags($result, $expected);
  6283. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6284. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6285. $expected = array(
  6286. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6287. array('option' => array('value' => '')),
  6288. '/option',
  6289. array('option' => array('value' => '00')),
  6290. '00',
  6291. '/option',
  6292. array('option' => array('value' => '05')),
  6293. '05',
  6294. '/option',
  6295. array('option' => array('value' => '10', 'selected' => 'selected')),
  6296. '10',
  6297. '/option',
  6298. $minutesRegex,
  6299. '/select',
  6300. );
  6301. $this->assertTags($result, $expected);
  6302. $result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
  6303. $expected = array(
  6304. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6305. array('option' => array('value' => '')),
  6306. '/option',
  6307. array('option' => array('value' => '00')),
  6308. '00',
  6309. '/option',
  6310. array('option' => array('value' => '01')),
  6311. '01',
  6312. '/option',
  6313. array('option' => array('value' => '02')),
  6314. '02',
  6315. '/option',
  6316. $minutesRegex,
  6317. '/select',
  6318. );
  6319. $this->assertTags($result, $expected);
  6320. }
  6321. /**
  6322. * testHour method
  6323. *
  6324. * @return void
  6325. */
  6326. public function testHour() {
  6327. extract($this->dateRegex);
  6328. $result = $this->Form->hour('Model.field', false);
  6329. $expected = array(
  6330. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6331. array('option' => array('value' => '')),
  6332. '/option',
  6333. array('option' => array('value' => '01')),
  6334. '1',
  6335. '/option',
  6336. array('option' => array('value' => '02')),
  6337. '2',
  6338. '/option',
  6339. $hoursRegex,
  6340. '/select',
  6341. );
  6342. $this->assertTags($result, $expected);
  6343. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6344. $result = $this->Form->hour('Model.field', false);
  6345. $expected = array(
  6346. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6347. array('option' => array('value' => '')),
  6348. '/option',
  6349. array('option' => array('value' => '01')),
  6350. '1',
  6351. '/option',
  6352. array('option' => array('value' => '02')),
  6353. '2',
  6354. '/option',
  6355. $hoursRegex,
  6356. array('option' => array('value' => '12', 'selected' => 'selected')),
  6357. '12',
  6358. '/option',
  6359. '/select',
  6360. );
  6361. $this->assertTags($result, $expected);
  6362. $this->Form->request->data['Model']['field'] = '';
  6363. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6364. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6365. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6366. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6367. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6368. $result = $this->Form->hour('Model.field', true);
  6369. $expected = array(
  6370. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6371. array('option' => array('value' => '')),
  6372. '/option',
  6373. array('option' => array('value' => '00', 'selected' => 'selected')),
  6374. '0',
  6375. '/option',
  6376. array('option' => array('value' => '01')),
  6377. '1',
  6378. '/option',
  6379. array('option' => array('value' => '02')),
  6380. '2',
  6381. '/option',
  6382. $hoursRegex,
  6383. '/select',
  6384. );
  6385. $this->assertTags($result, $expected);
  6386. unset($this->Form->request->data['Model']['field']);
  6387. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6388. $thisHour = date('H');
  6389. $optValue = date('G');
  6390. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6391. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6392. $result = $this->Form->hour('Model.field', true);
  6393. $expected = array(
  6394. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6395. array('option' => array('value' => '')),
  6396. '/option',
  6397. array('option' => array('value' => '00')),
  6398. '0',
  6399. '/option',
  6400. array('option' => array('value' => '01', 'selected' => 'selected')),
  6401. '1',
  6402. '/option',
  6403. array('option' => array('value' => '02')),
  6404. '2',
  6405. '/option',
  6406. $hoursRegex,
  6407. '/select',
  6408. );
  6409. $this->assertTags($result, $expected);
  6410. $this->Form->request->data['Model']['field'] = '18a';
  6411. $result = $this->Form->hour('Model.field', false);
  6412. $expected = array(
  6413. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6414. array('option' => array('value' => '')),
  6415. '/option',
  6416. array('option' => array('value' => '01')),
  6417. '1',
  6418. '/option',
  6419. array('option' => array('value' => '02')),
  6420. '2',
  6421. '/option',
  6422. $hoursRegex,
  6423. '/select',
  6424. );
  6425. $this->assertTags($result, $expected);
  6426. }
  6427. /**
  6428. * testYear method
  6429. *
  6430. * @return void
  6431. */
  6432. public function testYear() {
  6433. $result = $this->Form->year('Model.field', 2006, 2007);
  6434. $expected = array(
  6435. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6436. array('option' => array('value' => '')),
  6437. '/option',
  6438. array('option' => array('value' => '2007')),
  6439. '2007',
  6440. '/option',
  6441. array('option' => array('value' => '2006')),
  6442. '2006',
  6443. '/option',
  6444. '/select',
  6445. );
  6446. $this->assertTags($result, $expected);
  6447. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6448. $expected = array(
  6449. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6450. array('option' => array('value' => '')),
  6451. '/option',
  6452. array('option' => array('value' => '2006')),
  6453. '2006',
  6454. '/option',
  6455. array('option' => array('value' => '2007')),
  6456. '2007',
  6457. '/option',
  6458. '/select',
  6459. );
  6460. $this->assertTags($result, $expected);
  6461. $this->request->data['Contact']['published'] = '';
  6462. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6463. $expected = array(
  6464. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6465. array('option' => array('value' => '')),
  6466. '/option',
  6467. array('option' => array('value' => '2007')),
  6468. '2007',
  6469. '/option',
  6470. array('option' => array('value' => '2006')),
  6471. '2006',
  6472. '/option',
  6473. '/select',
  6474. );
  6475. $this->assertTags($result, $expected);
  6476. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6477. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6478. $expected = array(
  6479. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6480. array('option' => array('value' => '2007')),
  6481. '2007',
  6482. '/option',
  6483. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6484. '2006',
  6485. '/option',
  6486. '/select',
  6487. );
  6488. $this->assertTags($result, $expected);
  6489. $this->Form->request->data['Contact']['published'] = '';
  6490. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6491. $expected = array(
  6492. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6493. array('option' => array('value' => '')),
  6494. '/option',
  6495. array('option' => array('value' => '2007')),
  6496. '2007',
  6497. '/option',
  6498. array('option' => array('value' => '2006')),
  6499. '2006',
  6500. '/option',
  6501. '/select',
  6502. );
  6503. $this->assertTags($result, $expected);
  6504. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6505. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6506. $expected = array(
  6507. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6508. array('option' => array('value' => '2007')),
  6509. '2007',
  6510. '/option',
  6511. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6512. '2006',
  6513. '/option',
  6514. '/select',
  6515. );
  6516. $this->assertTags($result, $expected);
  6517. $this->Form->request->data['Contact']['published'] = '';
  6518. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6519. $expected = array(
  6520. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6521. array('option' => array('value' => '')),
  6522. '/option',
  6523. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6524. '2007',
  6525. '/option',
  6526. array('option' => array('value' => '2006')),
  6527. '2006',
  6528. '/option',
  6529. '/select',
  6530. );
  6531. $this->assertTags($result, $expected);
  6532. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6533. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6534. $expected = array(
  6535. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6536. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6537. '2007',
  6538. '/option',
  6539. array('option' => array('value' => '2006')),
  6540. '2006',
  6541. '/option',
  6542. '/select',
  6543. );
  6544. $this->assertTags($result, $expected);
  6545. $this->Form->request->data['Contact']['published'] = '';
  6546. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6547. $expected = array(
  6548. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6549. array('option' => array('value' => '2008')),
  6550. '2008',
  6551. '/option',
  6552. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6553. '2007',
  6554. '/option',
  6555. array('option' => array('value' => '2006')),
  6556. '2006',
  6557. '/option',
  6558. '/select',
  6559. );
  6560. $this->assertTags($result, $expected);
  6561. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6562. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6563. $expected = array(
  6564. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6565. array('option' => array('value' => '2008')),
  6566. '2008',
  6567. '/option',
  6568. array('option' => array('value' => '2007')),
  6569. '2007',
  6570. '/option',
  6571. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6572. '2006',
  6573. '/option',
  6574. '/select',
  6575. );
  6576. $this->assertTags($result, $expected);
  6577. $this->Form->request->data = array();
  6578. $this->Form->create('Contact');
  6579. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6580. $expected = array(
  6581. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6582. array('option' => array('value' => '2008')),
  6583. '2008',
  6584. '/option',
  6585. array('option' => array('value' => '2007')),
  6586. '2007',
  6587. '/option',
  6588. array('option' => array('value' => '2006')),
  6589. '2006',
  6590. '/option',
  6591. '/select',
  6592. );
  6593. $this->assertTags($result, $expected);
  6594. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6595. $this->assertContains('data[Contact][published][year]', $result);
  6596. $this->Form->request->data['Contact']['published'] = '2014ee';
  6597. $result = $this->Form->year('Contact.published', 2010, 2011);
  6598. $expected = array(
  6599. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6600. array('option' => array('value' => '')),
  6601. '/option',
  6602. array('option' => array('value' => '2011')),
  6603. '2011',
  6604. '/option',
  6605. array('option' => array('value' => '2010')),
  6606. '2010',
  6607. '/option',
  6608. '/select',
  6609. );
  6610. $this->assertTags($result, $expected);
  6611. }
  6612. /**
  6613. * testYearAutoExpandRange method
  6614. *
  6615. * @return void
  6616. */
  6617. public function testYearAutoExpandRange() {
  6618. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6619. $result = $this->Form->year('User.birthday');
  6620. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6621. $result = $matches[1];
  6622. $expected = range(date('Y') + 20, 1930);
  6623. $this->assertEquals($expected, $result);
  6624. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6625. $result = $this->Form->year('Project.release');
  6626. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6627. $result = $matches[1];
  6628. $expected = range(2050, date('Y') - 20);
  6629. $this->assertEquals($expected, $result);
  6630. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6631. $result = $this->Form->year('Project.release', 1890, 1900);
  6632. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6633. $result = $matches[1];
  6634. $expected = range(1900, 1881);
  6635. $this->assertEquals($expected, $result);
  6636. }
  6637. /**
  6638. * testInputDate method
  6639. *
  6640. * Test various inputs with type date and different dateFormat values.
  6641. * Failing to provide a dateFormat key should not error.
  6642. * It should simply not pre-select any value then.
  6643. *
  6644. * @return void
  6645. */
  6646. public function testInputDate() {
  6647. $this->Form->request->data = array(
  6648. 'User' => array(
  6649. 'month_year' => array('month' => date('m')),
  6650. 'just_year' => array('month' => date('m')),
  6651. 'just_month' => array('year' => date('Y')),
  6652. 'just_day' => array('month' => date('m')),
  6653. )
  6654. );
  6655. $this->Form->create('User');
  6656. $result = $this->Form->input('month_year',
  6657. array(
  6658. 'label' => false,
  6659. 'div' => false,
  6660. 'type' => 'date',
  6661. 'dateFormat' => 'MY',
  6662. 'minYear' => 2006,
  6663. 'maxYear' => 2008
  6664. )
  6665. );
  6666. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6667. $this->assertNotContains('value="2008" selected="selected"', $result);
  6668. $result = $this->Form->input('just_year',
  6669. array(
  6670. 'type' => 'date',
  6671. 'label' => false,
  6672. 'dateFormat' => 'Y',
  6673. 'minYear' => date('Y'),
  6674. 'maxYear' => date('Y', strtotime('+20 years'))
  6675. )
  6676. );
  6677. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6678. $result = $this->Form->input('just_month',
  6679. array(
  6680. 'type' => 'date',
  6681. 'label' => false,
  6682. 'dateFormat' => 'M',
  6683. 'empty' => false,
  6684. )
  6685. );
  6686. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6687. $result = $this->Form->input('just_day',
  6688. array(
  6689. 'type' => 'date',
  6690. 'label' => false,
  6691. 'dateFormat' => 'D',
  6692. 'empty' => false,
  6693. )
  6694. );
  6695. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6696. }
  6697. /**
  6698. * testInputDateMaxYear method
  6699. *
  6700. * Let's say we want to only allow users born from 2006 to 2008 to register
  6701. * This being the first singup page, we still don't have any data
  6702. *
  6703. * @return void
  6704. */
  6705. public function testInputDateMaxYear() {
  6706. $this->Form->request->data = array();
  6707. $this->Form->create('User');
  6708. $result = $this->Form->input('birthday',
  6709. array(
  6710. 'label' => false,
  6711. 'div' => false,
  6712. 'type' => 'date',
  6713. 'dateFormat' => 'DMY',
  6714. 'minYear' => 2006,
  6715. 'maxYear' => 2008
  6716. )
  6717. );
  6718. $this->assertContains('value="2008" selected="selected"', $result);
  6719. }
  6720. /**
  6721. * testTextArea method
  6722. *
  6723. * @return void
  6724. */
  6725. public function testTextArea() {
  6726. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6727. $result = $this->Form->textarea('Model.field');
  6728. $expected = array(
  6729. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6730. 'some test data',
  6731. '/textarea',
  6732. );
  6733. $this->assertTags($result, $expected);
  6734. $result = $this->Form->textarea('Model.tmp');
  6735. $expected = array(
  6736. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6737. '/textarea',
  6738. );
  6739. $this->assertTags($result, $expected);
  6740. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6741. $result = $this->Form->textarea('Model.field');
  6742. $expected = array(
  6743. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6744. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6745. '/textarea',
  6746. );
  6747. $this->assertTags($result, $expected);
  6748. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6749. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6750. $expected = array(
  6751. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6752. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6753. '/textarea',
  6754. );
  6755. $this->assertTags($result, $expected);
  6756. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6757. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6758. $expected = array(
  6759. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6760. '/textarea'
  6761. );
  6762. $this->assertTags($result, $expected);
  6763. }
  6764. /**
  6765. * testTextAreaWithStupidCharacters method
  6766. *
  6767. * test text area with non-ascii characters
  6768. *
  6769. * @return void
  6770. */
  6771. public function testTextAreaWithStupidCharacters() {
  6772. $this->loadFixtures('Post');
  6773. $result = $this->Form->input('Post.content', array(
  6774. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6775. ));
  6776. $expected = array(
  6777. 'div' => array('class' => 'input textarea'),
  6778. 'label' => array('for' => 'PostContent'),
  6779. 'Current Text',
  6780. '/label',
  6781. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6782. 'GREAT®',
  6783. '/textarea',
  6784. '/div'
  6785. );
  6786. $this->assertTags($result, $expected);
  6787. }
  6788. /**
  6789. * testHiddenField method
  6790. *
  6791. * @return void
  6792. */
  6793. public function testHiddenField() {
  6794. $Contact = ClassRegistry::getObject('Contact');
  6795. $Contact->validationErrors['field'] = 1;
  6796. $this->Form->request->data['Contact']['field'] = 'test';
  6797. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6798. $this->assertTags($result, array(
  6799. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6800. );
  6801. }
  6802. /**
  6803. * testFileUploadField method
  6804. *
  6805. * @return void
  6806. */
  6807. public function testFileUploadField() {
  6808. $result = $this->Form->file('Model.upload');
  6809. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6810. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6811. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6812. $expected = array(
  6813. 'div' => array('class' => 'input file'),
  6814. 'label' => array('for' => 'ModelUpload'),
  6815. 'Upload',
  6816. '/label',
  6817. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6818. '/div'
  6819. );
  6820. $this->assertTags($result, $expected);
  6821. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6822. $result = $this->Form->file('Model.upload');
  6823. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6824. }
  6825. /**
  6826. * test File upload input on a model not used in create();
  6827. *
  6828. * @return void
  6829. */
  6830. public function testFileUploadOnOtherModel() {
  6831. $this->Form->create('ValidateUser', array('type' => 'file'));
  6832. $result = $this->Form->file('ValidateProfile.city');
  6833. $expected = array(
  6834. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6835. );
  6836. $this->assertTags($result, $expected);
  6837. }
  6838. /**
  6839. * testButton method
  6840. *
  6841. * @return void
  6842. */
  6843. public function testButton() {
  6844. $result = $this->Form->button('Hi');
  6845. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6846. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6847. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6848. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6849. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6850. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6851. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6852. $result = $this->Form->button('No type', array('type' => false));
  6853. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6854. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6855. $this->assertNotRegExp('/\&039/', $result);
  6856. }
  6857. /**
  6858. * Test that button() makes unlocked fields by default.
  6859. *
  6860. * @return void
  6861. */
  6862. public function testButtonUnlockedByDefault() {
  6863. $this->Form->request->params['_Token']['key'] = 'secured';
  6864. $this->Form->button('Save', array('name' => 'save'));
  6865. $this->Form->button('Clear');
  6866. $result = $this->Form->unlockField();
  6867. $this->assertEquals(array('save'), $result);
  6868. }
  6869. /**
  6870. * testPostButton method
  6871. *
  6872. * @return void
  6873. */
  6874. public function testPostButton() {
  6875. $result = $this->Form->postButton('Hi', '/controller/action');
  6876. $this->assertTags($result, array(
  6877. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6878. 'div' => array('style' => 'display:none;'),
  6879. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6880. '/div',
  6881. 'button' => array('type' => 'submit'),
  6882. 'Hi',
  6883. '/button',
  6884. '/form'
  6885. ));
  6886. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6887. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6888. }
  6889. /**
  6890. * Test using postButton with N dimensional data.
  6891. *
  6892. * @return void
  6893. */
  6894. public function testPostButtonNestedData() {
  6895. $data = array(
  6896. 'one' => array(
  6897. 'two' => array(
  6898. 3, 4, 5
  6899. )
  6900. )
  6901. );
  6902. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  6903. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  6904. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  6905. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  6906. }
  6907. /**
  6908. * Test that postButton adds _Token fields.
  6909. *
  6910. * @return void
  6911. */
  6912. public function testSecurePostButton() {
  6913. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6914. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6915. $expected = array(
  6916. 'form' => array(
  6917. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6918. ),
  6919. array('div' => array('style' => 'display:none;')),
  6920. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6921. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6922. '/div',
  6923. 'button' => array('type' => 'submit'),
  6924. 'Delete',
  6925. '/button',
  6926. array('div' => array('style' => 'display:none;')),
  6927. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6928. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6929. '/div',
  6930. '/form',
  6931. );
  6932. $this->assertTags($result, $expected);
  6933. }
  6934. /**
  6935. * testPostLink method
  6936. *
  6937. * @return void
  6938. */
  6939. public function testPostLink() {
  6940. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6941. $this->assertTags($result, array(
  6942. 'form' => array(
  6943. 'method' => 'post', 'action' => '/posts/delete/1',
  6944. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6945. ),
  6946. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6947. '/form',
  6948. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6949. 'Delete',
  6950. '/a'
  6951. ));
  6952. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6953. $this->assertTags($result, array(
  6954. 'form' => array(
  6955. 'method' => 'post', 'action' => '/posts/delete/1',
  6956. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6957. ),
  6958. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6959. '/form',
  6960. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6961. 'Delete',
  6962. '/a'
  6963. ));
  6964. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6965. $this->assertTags($result, array(
  6966. 'form' => array(
  6967. 'method' => 'post', 'action' => '/posts/delete/1',
  6968. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6969. ),
  6970. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6971. '/form',
  6972. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6973. 'Delete',
  6974. '/a'
  6975. ));
  6976. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6977. $this->assertTags($result, array(
  6978. 'form' => array(
  6979. 'method' => 'post', 'action' => '/posts/delete/1',
  6980. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6981. ),
  6982. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6983. '/form',
  6984. '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;/'),
  6985. 'Delete',
  6986. '/a'
  6987. ));
  6988. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6989. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6990. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6991. $this->assertTags($result, array(
  6992. 'form' => array(
  6993. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6994. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6995. ),
  6996. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6997. '/form',
  6998. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6999. 'Delete',
  7000. '/a'
  7001. ));
  7002. $result = $this->Form->postLink(
  7003. '',
  7004. array('controller' => 'items', 'action' => 'delete', 10),
  7005. array('class' => 'btn btn-danger', 'escape' => false),
  7006. 'Confirm thing'
  7007. );
  7008. $this->assertTags($result, array(
  7009. 'form' => array(
  7010. 'method' => 'post', 'action' => '/items/delete/10',
  7011. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7012. ),
  7013. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7014. '/form',
  7015. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7016. '/a'
  7017. ));
  7018. }
  7019. /**
  7020. * Test that security hashes for postLink include the url.
  7021. *
  7022. * @return void
  7023. */
  7024. public function testPostLinkSecurityHash() {
  7025. $hash = Security::hash(
  7026. '/posts/delete/1' .
  7027. serialize(array()) .
  7028. '' .
  7029. Configure::read('Security.salt')
  7030. );
  7031. $hash .= '%3A';
  7032. $this->Form->request->params['_Token']['key'] = 'test';
  7033. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7034. $this->assertTags($result, array(
  7035. 'form' => array(
  7036. 'method' => 'post', 'action' => '/posts/delete/1',
  7037. 'name', 'id', 'style' => 'display:none;'
  7038. ),
  7039. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7040. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'test', 'id')),
  7041. 'div' => array('style' => 'display:none;'),
  7042. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => $hash, 'id')),
  7043. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id')),
  7044. '/div',
  7045. '/form',
  7046. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7047. 'Delete',
  7048. '/a'
  7049. ));
  7050. }
  7051. /**
  7052. * Test using postLink with N dimensional data.
  7053. *
  7054. * @return void
  7055. */
  7056. public function testPostLinkNestedData() {
  7057. $data = array(
  7058. 'one' => array(
  7059. 'two' => array(
  7060. 3, 4, 5
  7061. )
  7062. )
  7063. );
  7064. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  7065. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  7066. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  7067. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  7068. }
  7069. /**
  7070. * test creating postLinks after a GET form.
  7071. *
  7072. * @return void
  7073. */
  7074. public function testPostLinkAfterGetForm() {
  7075. $this->Form->request->params['_Token']['key'] = 'testkey';
  7076. $this->Form->create('User', array('type' => 'get'));
  7077. $this->Form->end();
  7078. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7079. $this->assertTags($result, array(
  7080. 'form' => array(
  7081. 'method' => 'post', 'action' => '/posts/delete/1',
  7082. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7083. ),
  7084. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7085. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7086. 'div' => array('style' => 'display:none;'),
  7087. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7088. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7089. '/div',
  7090. '/form',
  7091. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7092. 'Delete',
  7093. '/a'
  7094. ));
  7095. }
  7096. /**
  7097. * Test that postLink adds _Token fields.
  7098. *
  7099. * @return void
  7100. */
  7101. public function testSecurePostLink() {
  7102. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  7103. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7104. $expected = array(
  7105. 'form' => array(
  7106. 'method' => 'post', 'action' => '/posts/delete/1',
  7107. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7108. ),
  7109. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7110. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7111. 'div' => array('style' => 'display:none;'),
  7112. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7113. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7114. '/div',
  7115. '/form',
  7116. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7117. 'Delete',
  7118. '/a'
  7119. );
  7120. $this->assertTags($result, $expected);
  7121. }
  7122. /**
  7123. * testSubmitButton method
  7124. *
  7125. * @return void
  7126. */
  7127. public function testSubmitButton() {
  7128. $result = $this->Form->submit('');
  7129. $expected = array(
  7130. 'div' => array('class' => 'submit'),
  7131. 'input' => array('type' => 'submit', 'value' => ''),
  7132. '/div'
  7133. );
  7134. $this->assertTags($result, $expected);
  7135. $result = $this->Form->submit('Test Submit');
  7136. $expected = array(
  7137. 'div' => array('class' => 'submit'),
  7138. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7139. '/div'
  7140. );
  7141. $this->assertTags($result, $expected);
  7142. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  7143. $expected = array(
  7144. 'span' => array('class' => 'submit'),
  7145. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7146. '/span'
  7147. );
  7148. $this->assertTags($result, $expected);
  7149. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  7150. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  7151. $this->assertTags($result, $expected);
  7152. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  7153. $expected = array(
  7154. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  7155. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7156. '/div'
  7157. );
  7158. $this->assertTags($result, $expected);
  7159. $result = $this->Form->submit('Next >');
  7160. $expected = array(
  7161. 'div' => array('class' => 'submit'),
  7162. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  7163. '/div'
  7164. );
  7165. $this->assertTags($result, $expected);
  7166. $result = $this->Form->submit('Next >', array('escape' => false));
  7167. $expected = array(
  7168. 'div' => array('class' => 'submit'),
  7169. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  7170. '/div'
  7171. );
  7172. $this->assertTags($result, $expected);
  7173. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7174. $expected = array(
  7175. 'div' => array('class' => 'submit'),
  7176. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7177. '/div'
  7178. );
  7179. $this->assertTags($result, $expected);
  7180. $before = '--before--';
  7181. $after = '--after--';
  7182. $result = $this->Form->submit('Test', array('before' => $before));
  7183. $expected = array(
  7184. 'div' => array('class' => 'submit'),
  7185. '--before--',
  7186. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7187. '/div'
  7188. );
  7189. $this->assertTags($result, $expected);
  7190. $result = $this->Form->submit('Test', array('after' => $after));
  7191. $expected = array(
  7192. 'div' => array('class' => 'submit'),
  7193. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7194. '--after--',
  7195. '/div'
  7196. );
  7197. $this->assertTags($result, $expected);
  7198. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7199. $expected = array(
  7200. 'div' => array('class' => 'submit'),
  7201. '--before--',
  7202. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7203. '--after--',
  7204. '/div'
  7205. );
  7206. $this->assertTags($result, $expected);
  7207. }
  7208. /**
  7209. * test image submit types.
  7210. *
  7211. * @return void
  7212. */
  7213. public function testSubmitImage() {
  7214. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7215. $expected = array(
  7216. 'div' => array('class' => 'submit'),
  7217. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7218. '/div'
  7219. );
  7220. $this->assertTags($result, $expected);
  7221. $result = $this->Form->submit('/relative/cake.power.gif');
  7222. $expected = array(
  7223. 'div' => array('class' => 'submit'),
  7224. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7225. '/div'
  7226. );
  7227. $this->assertTags($result, $expected);
  7228. $result = $this->Form->submit('cake.power.gif');
  7229. $expected = array(
  7230. 'div' => array('class' => 'submit'),
  7231. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7232. '/div'
  7233. );
  7234. $this->assertTags($result, $expected);
  7235. $result = $this->Form->submit('Not.an.image');
  7236. $expected = array(
  7237. 'div' => array('class' => 'submit'),
  7238. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7239. '/div'
  7240. );
  7241. $this->assertTags($result, $expected);
  7242. $after = '--after--';
  7243. $before = '--before--';
  7244. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7245. $expected = array(
  7246. 'div' => array('class' => 'submit'),
  7247. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7248. '--after--',
  7249. '/div'
  7250. );
  7251. $this->assertTags($result, $expected);
  7252. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7253. $expected = array(
  7254. 'div' => array('class' => 'submit'),
  7255. '--before--',
  7256. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7257. '/div'
  7258. );
  7259. $this->assertTags($result, $expected);
  7260. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7261. $expected = array(
  7262. 'div' => array('class' => 'submit'),
  7263. '--before--',
  7264. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7265. '--after--',
  7266. '/div'
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7270. $expected = array(
  7271. 'div' => array('class' => 'submit'),
  7272. '--before--',
  7273. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7274. '--after--',
  7275. '/div'
  7276. );
  7277. $this->assertTags($result, $expected);
  7278. }
  7279. /**
  7280. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7281. * be submitted at a time.
  7282. *
  7283. * @return void
  7284. */
  7285. public function testSubmitUnlockedByDefault() {
  7286. $this->Form->request->params['_Token']['key'] = 'secured';
  7287. $this->Form->submit('Go go');
  7288. $this->Form->submit('Save', array('name' => 'save'));
  7289. $result = $this->Form->unlockField();
  7290. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7291. }
  7292. /**
  7293. * Test submit image with timestamps.
  7294. *
  7295. * @return void
  7296. */
  7297. public function testSubmitImageTimestamp() {
  7298. Configure::write('Asset.timestamp', 'force');
  7299. $result = $this->Form->submit('cake.power.gif');
  7300. $expected = array(
  7301. 'div' => array('class' => 'submit'),
  7302. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7303. '/div'
  7304. );
  7305. $this->assertTags($result, $expected);
  7306. }
  7307. /**
  7308. * test the create() method
  7309. *
  7310. * @return void
  7311. */
  7312. public function testCreate() {
  7313. $result = $this->Form->create('Contact');
  7314. $encoding = strtolower(Configure::read('App.encoding'));
  7315. $expected = array(
  7316. 'form' => array(
  7317. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7318. 'accept-charset' => $encoding
  7319. ),
  7320. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7321. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7322. '/div'
  7323. );
  7324. $this->assertTags($result, $expected);
  7325. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7326. $expected = array('form' => array(
  7327. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7328. 'accept-charset' => $encoding
  7329. ));
  7330. $this->assertTags($result, $expected);
  7331. $result = $this->Form->create('Contact', array('type' => 'get'));
  7332. $expected = array('form' => array(
  7333. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7334. 'accept-charset' => $encoding
  7335. ));
  7336. $this->assertTags($result, $expected);
  7337. $result = $this->Form->create('Contact', array('type' => 'put'));
  7338. $expected = array(
  7339. 'form' => array(
  7340. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7341. 'accept-charset' => $encoding
  7342. ),
  7343. 'div' => array('style' => 'display:none;'),
  7344. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7345. '/div'
  7346. );
  7347. $this->assertTags($result, $expected);
  7348. $result = $this->Form->create('Contact', array('type' => 'file'));
  7349. $expected = array(
  7350. 'form' => array(
  7351. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7352. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7353. ),
  7354. 'div' => array('style' => 'display:none;'),
  7355. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7356. '/div'
  7357. );
  7358. $this->assertTags($result, $expected);
  7359. $this->Form->request->data['Contact']['id'] = 1;
  7360. $this->Form->request->here = '/contacts/edit/1';
  7361. $this->Form->request['action'] = 'edit';
  7362. $result = $this->Form->create('Contact');
  7363. $expected = array(
  7364. 'form' => array(
  7365. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7366. 'accept-charset' => $encoding
  7367. ),
  7368. 'div' => array('style' => 'display:none;'),
  7369. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7370. '/div'
  7371. );
  7372. $this->assertTags($result, $expected);
  7373. $this->Form->request->data['Contact']['id'] = 1;
  7374. $this->Form->request->here = '/contacts/edit/1';
  7375. $this->Form->request['action'] = 'edit';
  7376. $result = $this->Form->create('Contact', array('type' => 'file'));
  7377. $expected = array(
  7378. 'form' => array(
  7379. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7380. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7381. ),
  7382. 'div' => array('style' => 'display:none;'),
  7383. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7384. '/div'
  7385. );
  7386. $this->assertTags($result, $expected);
  7387. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7388. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7389. $expected = array(
  7390. 'form' => array(
  7391. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7392. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7393. ),
  7394. 'div' => array('style' => 'display:none;'),
  7395. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7396. '/div'
  7397. );
  7398. $this->assertTags($result, $expected);
  7399. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7400. $expected = array(
  7401. 'form' => array(
  7402. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7403. 'accept-charset' => $encoding
  7404. ),
  7405. 'div' => array('style' => 'display:none;'),
  7406. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7407. '/div'
  7408. );
  7409. $this->assertTags($result, $expected);
  7410. $this->Form->request['action'] = 'add';
  7411. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7412. $expected = array(
  7413. 'form' => array(
  7414. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7415. 'accept-charset' => $encoding
  7416. ),
  7417. 'div' => array('style' => 'display:none;'),
  7418. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7419. '/div'
  7420. );
  7421. $this->assertTags($result, $expected);
  7422. $result = $this->Form->create('User', array('action' => 'login'));
  7423. $expected = array(
  7424. 'form' => array(
  7425. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7426. 'accept-charset' => $encoding
  7427. ),
  7428. 'div' => array('style' => 'display:none;'),
  7429. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7430. '/div'
  7431. );
  7432. $this->assertTags($result, $expected);
  7433. $result = $this->Form->create('User', array('url' => '/users/login'));
  7434. $expected = array(
  7435. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7436. 'div' => array('style' => 'display:none;'),
  7437. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7438. '/div'
  7439. );
  7440. $this->assertTags($result, $expected);
  7441. $this->Form->request['controller'] = 'pages';
  7442. $result = $this->Form->create('User', array('action' => 'signup'));
  7443. $expected = array(
  7444. 'form' => array(
  7445. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7446. 'accept-charset' => $encoding
  7447. ),
  7448. 'div' => array('style' => 'display:none;'),
  7449. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7450. '/div'
  7451. );
  7452. $this->assertTags($result, $expected);
  7453. $this->Form->request->data = array();
  7454. $this->Form->request['controller'] = 'contacts';
  7455. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7456. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7457. $expected = array(
  7458. 'form' => array(
  7459. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7460. 'accept-charset' => 'utf-8'
  7461. ),
  7462. 'div' => array('style' => 'display:none;'),
  7463. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7464. '/div'
  7465. );
  7466. $this->assertTags($result, $expected);
  7467. }
  7468. /**
  7469. * Test the onsubmit option for create()
  7470. *
  7471. * @return void
  7472. */
  7473. public function testCreateOnSubmit() {
  7474. $this->Form->request->data = array();
  7475. $this->Form->request['controller'] = 'contacts';
  7476. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7477. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7478. $expected = array(
  7479. 'form' => array(
  7480. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7481. 'accept-charset' => 'utf-8'
  7482. ),
  7483. 'div' => array('style' => 'display:none;'),
  7484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7485. '/div'
  7486. );
  7487. $this->assertTags($result, $expected);
  7488. $this->Form->request->data = array();
  7489. $this->Form->request['controller'] = 'contacts';
  7490. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7491. $result = $this->Form->create(array(
  7492. 'url' => array('action' => 'index', 'param'),
  7493. 'default' => false,
  7494. 'onsubmit' => 'someFunction();'
  7495. ));
  7496. $expected = array(
  7497. 'form' => array(
  7498. 'id' => 'ContactAddForm', 'method' => 'post',
  7499. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7500. 'action' => '/contacts/index/param',
  7501. 'accept-charset' => 'utf-8'
  7502. ),
  7503. 'div' => array('style' => 'display:none;'),
  7504. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7505. '/div'
  7506. );
  7507. $this->assertTags($result, $expected);
  7508. }
  7509. /**
  7510. * test create() with automatic url generation
  7511. *
  7512. * @return void
  7513. */
  7514. public function testCreateAutoUrl() {
  7515. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7516. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7517. $this->Form->request->base = '/base_url';
  7518. $result = $this->Form->create('Contact');
  7519. $expected = array(
  7520. 'form' => array(
  7521. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7522. 'accept-charset' => 'utf-8'
  7523. ),
  7524. 'div' => array('style' => 'display:none;'),
  7525. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7526. '/div'
  7527. );
  7528. $this->assertTags($result, $expected);
  7529. $this->Form->request['action'] = 'delete';
  7530. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7531. $this->Form->request->base = '/base_url';
  7532. $result = $this->Form->create('Contact');
  7533. $expected = array(
  7534. 'form' => array(
  7535. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7536. 'accept-charset' => 'utf-8'
  7537. ),
  7538. 'div' => array('style' => 'display:none;'),
  7539. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7540. '/div'
  7541. );
  7542. $this->assertTags($result, $expected);
  7543. }
  7544. /**
  7545. * test create() with a custom route
  7546. *
  7547. * @return void
  7548. */
  7549. public function testCreateCustomRoute() {
  7550. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7551. $encoding = strtolower(Configure::read('App.encoding'));
  7552. $result = $this->Form->create('User', array('action' => 'login'));
  7553. $expected = array(
  7554. 'form' => array(
  7555. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  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 that inputDefaults are stored and used.
  7566. *
  7567. * @return void
  7568. */
  7569. public function testCreateWithInputDefaults() {
  7570. $this->Form->create('User', array(
  7571. 'inputDefaults' => array(
  7572. 'div' => false,
  7573. 'label' => false,
  7574. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7575. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7576. )
  7577. ));
  7578. $result = $this->Form->input('username');
  7579. $expected = array(
  7580. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7581. );
  7582. $this->assertTags($result, $expected);
  7583. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7584. $expected = array(
  7585. 'div' => array('class' => 'input text'),
  7586. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7587. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7588. '/div'
  7589. );
  7590. $this->assertTags($result, $expected);
  7591. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7592. $expected = array(
  7593. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7594. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7595. );
  7596. $this->assertTags($result, $expected);
  7597. $this->Form->create('User', array(
  7598. 'inputDefaults' => array(
  7599. 'div' => false,
  7600. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7601. )
  7602. ));
  7603. $result = $this->Form->input('username', array('div' => true));
  7604. $expected = array(
  7605. 'div' => array('class' => 'input text'),
  7606. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7607. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7608. '/div'
  7609. );
  7610. $this->assertTags($result, $expected);
  7611. }
  7612. /**
  7613. * test automatic accept-charset overriding
  7614. *
  7615. * @return void
  7616. */
  7617. public function testCreateWithAcceptCharset() {
  7618. $result = $this->Form->create('UserForm', array(
  7619. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7620. )
  7621. );
  7622. $expected = array(
  7623. 'form' => array(
  7624. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7625. 'accept-charset' => 'iso-8859-1'
  7626. ),
  7627. 'div' => array('style' => 'display:none;'),
  7628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7629. '/div'
  7630. );
  7631. $this->assertTags($result, $expected);
  7632. }
  7633. /**
  7634. * Test base form URL when url param is passed with multiple parameters (&)
  7635. *
  7636. * @return void
  7637. */
  7638. public function testCreateQuerystringrequest() {
  7639. $encoding = strtolower(Configure::read('App.encoding'));
  7640. $result = $this->Form->create('Contact', array(
  7641. 'type' => 'post',
  7642. 'escape' => false,
  7643. 'url' => array(
  7644. 'controller' => 'controller',
  7645. 'action' => 'action',
  7646. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7647. )
  7648. ));
  7649. $expected = array(
  7650. 'form' => array(
  7651. 'id' => 'ContactAddForm',
  7652. 'method' => 'post',
  7653. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7654. 'accept-charset' => $encoding
  7655. ),
  7656. 'div' => array('style' => 'display:none;'),
  7657. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7658. '/div'
  7659. );
  7660. $this->assertTags($result, $expected);
  7661. $result = $this->Form->create('Contact', array(
  7662. 'type' => 'post',
  7663. 'url' => array(
  7664. 'controller' => 'controller',
  7665. 'action' => 'action',
  7666. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7667. )
  7668. ));
  7669. $expected = array(
  7670. 'form' => array(
  7671. 'id' => 'ContactAddForm',
  7672. 'method' => 'post',
  7673. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7674. 'accept-charset' => $encoding
  7675. ),
  7676. 'div' => array('style' => 'display:none;'),
  7677. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7678. '/div'
  7679. );
  7680. $this->assertTags($result, $expected);
  7681. }
  7682. /**
  7683. * test that create() doesn't cause errors by multiple id's being in the primary key
  7684. * as could happen with multiple select or checkboxes.
  7685. *
  7686. * @return void
  7687. */
  7688. public function testCreateWithMultipleIdInData() {
  7689. $encoding = strtolower(Configure::read('App.encoding'));
  7690. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7691. $result = $this->Form->create('Contact');
  7692. $expected = array(
  7693. 'form' => array(
  7694. 'id' => 'ContactAddForm',
  7695. 'method' => 'post',
  7696. 'action' => '/contacts/add',
  7697. 'accept-charset' => $encoding
  7698. ),
  7699. 'div' => array('style' => 'display:none;'),
  7700. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7701. '/div'
  7702. );
  7703. $this->assertTags($result, $expected);
  7704. }
  7705. /**
  7706. * test that create() doesn't add in extra passed params.
  7707. *
  7708. * @return void
  7709. */
  7710. public function testCreatePassedArgs() {
  7711. $encoding = strtolower(Configure::read('App.encoding'));
  7712. $this->Form->request->data['Contact']['id'] = 1;
  7713. $result = $this->Form->create('Contact', array(
  7714. 'type' => 'post',
  7715. 'escape' => false,
  7716. 'url' => array(
  7717. 'action' => 'edit',
  7718. 'myparam'
  7719. )
  7720. ));
  7721. $expected = array(
  7722. 'form' => array(
  7723. 'id' => 'ContactAddForm',
  7724. 'method' => 'post',
  7725. 'action' => '/contacts/edit/myparam',
  7726. 'accept-charset' => $encoding
  7727. ),
  7728. 'div' => array('style' => 'display:none;'),
  7729. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7730. '/div'
  7731. );
  7732. $this->assertTags($result, $expected);
  7733. }
  7734. /**
  7735. * test that create() works without raising errors with a Mock Model
  7736. *
  7737. * @return void
  7738. */
  7739. public function testCreateNoErrorsWithMockModel() {
  7740. $encoding = strtolower(Configure::read('App.encoding'));
  7741. $ContactMock = $this->getMockBuilder('Contact')
  7742. ->disableOriginalConstructor()
  7743. ->getMock();
  7744. ClassRegistry::removeObject('Contact');
  7745. ClassRegistry::addObject('Contact', $ContactMock);
  7746. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7747. $expected = array('form' => array(
  7748. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7749. 'accept-charset' => $encoding
  7750. ));
  7751. $this->assertTags($result, $expected);
  7752. }
  7753. /**
  7754. * test creating a get form, and get form inputs.
  7755. *
  7756. * @return void
  7757. */
  7758. public function testGetFormCreate() {
  7759. $encoding = strtolower(Configure::read('App.encoding'));
  7760. $result = $this->Form->create('Contact', array('type' => 'get'));
  7761. $this->assertTags($result, array('form' => array(
  7762. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7763. 'accept-charset' => $encoding
  7764. )));
  7765. $result = $this->Form->text('Contact.name');
  7766. $this->assertTags($result, array('input' => array(
  7767. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7768. )));
  7769. $result = $this->Form->password('password');
  7770. $this->assertTags($result, array('input' => array(
  7771. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7772. )));
  7773. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7774. $result = $this->Form->text('user_form');
  7775. $this->assertTags($result, array('input' => array(
  7776. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7777. )));
  7778. }
  7779. /**
  7780. * test get form, and inputs when the model param is false
  7781. *
  7782. * @return void
  7783. */
  7784. public function testGetFormWithFalseModel() {
  7785. $encoding = strtolower(Configure::read('App.encoding'));
  7786. $this->Form->request['controller'] = 'contact_test';
  7787. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7788. $expected = array('form' => array(
  7789. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7790. 'accept-charset' => $encoding
  7791. ));
  7792. $this->assertTags($result, $expected);
  7793. $result = $this->Form->text('reason');
  7794. $expected = array(
  7795. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7796. );
  7797. $this->assertTags($result, $expected);
  7798. }
  7799. /**
  7800. * test that datetime() works with GET style forms.
  7801. *
  7802. * @return void
  7803. */
  7804. public function testDateTimeWithGetForms() {
  7805. extract($this->dateRegex);
  7806. $this->Form->create('Contact', array('type' => 'get'));
  7807. $result = $this->Form->datetime('created');
  7808. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7809. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7810. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7811. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7812. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7813. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7814. }
  7815. /**
  7816. * testEditFormWithData method
  7817. *
  7818. * test auto populating form elements from submitted data.
  7819. *
  7820. * @return void
  7821. */
  7822. public function testEditFormWithData() {
  7823. $this->Form->request->data = array('Person' => array(
  7824. 'id' => 1,
  7825. 'first_name' => 'Nate',
  7826. 'last_name' => 'Abele',
  7827. 'email' => 'nate@example.com'
  7828. ));
  7829. $this->Form->request->addParams(array(
  7830. 'models' => array('Person'),
  7831. 'controller' => 'people',
  7832. 'action' => 'add'
  7833. ));
  7834. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7835. $this->Form->create();
  7836. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7837. $expected = array(
  7838. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7839. 'select' => array(
  7840. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7841. ),
  7842. array('option' => array('value' => 1)), 'Nate', '/option',
  7843. array('option' => array('value' => 2)), 'Garrett', '/option',
  7844. array('option' => array('value' => 3)), 'Larry', '/option',
  7845. '/select'
  7846. );
  7847. $this->assertTags($result, $expected);
  7848. }
  7849. /**
  7850. * Test that required fields are created for various types of validation.
  7851. *
  7852. * @return void
  7853. */
  7854. public function testFormInputRequiredDetection() {
  7855. $this->Form->create('Contact');
  7856. $result = $this->Form->input('Contact.non_existing');
  7857. $expected = array(
  7858. 'div' => array('class' => 'input text'),
  7859. 'label' => array('for' => 'ContactNonExisting'),
  7860. 'Non Existing',
  7861. '/label',
  7862. 'input' => array(
  7863. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7864. 'id' => 'ContactNonExisting'
  7865. ),
  7866. '/div'
  7867. );
  7868. $this->assertTags($result, $expected);
  7869. $result = $this->Form->input('Contact.imrequired');
  7870. $expected = array(
  7871. 'div' => array('class' => 'input text required'),
  7872. 'label' => array('for' => 'ContactImrequired'),
  7873. 'Imrequired',
  7874. '/label',
  7875. 'input' => array(
  7876. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7877. 'id' => 'ContactImrequired',
  7878. 'required' => 'required'
  7879. ),
  7880. '/div'
  7881. );
  7882. $this->assertTags($result, $expected);
  7883. $result = $this->Form->input('Contact.imalsorequired');
  7884. $expected = array(
  7885. 'div' => array('class' => 'input text required'),
  7886. 'label' => array('for' => 'ContactImalsorequired'),
  7887. 'Imalsorequired',
  7888. '/label',
  7889. 'input' => array(
  7890. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7891. 'id' => 'ContactImalsorequired',
  7892. 'required' => 'required'
  7893. ),
  7894. '/div'
  7895. );
  7896. $this->assertTags($result, $expected);
  7897. $result = $this->Form->input('Contact.imrequiredtoo');
  7898. $expected = array(
  7899. 'div' => array('class' => 'input text required'),
  7900. 'label' => array('for' => 'ContactImrequiredtoo'),
  7901. 'Imrequiredtoo',
  7902. '/label',
  7903. 'input' => array(
  7904. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7905. 'id' => 'ContactImrequiredtoo',
  7906. 'required' => 'required'
  7907. ),
  7908. '/div'
  7909. );
  7910. $this->assertTags($result, $expected);
  7911. $result = $this->Form->input('Contact.required_one');
  7912. $expected = array(
  7913. 'div' => array('class' => 'input text required'),
  7914. 'label' => array('for' => 'ContactRequiredOne'),
  7915. 'Required One',
  7916. '/label',
  7917. 'input' => array(
  7918. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7919. 'id' => 'ContactRequiredOne',
  7920. 'required' => 'required'
  7921. ),
  7922. '/div'
  7923. );
  7924. $this->assertTags($result, $expected);
  7925. $result = $this->Form->input('Contact.string_required');
  7926. $expected = array(
  7927. 'div' => array('class' => 'input text required'),
  7928. 'label' => array('for' => 'ContactStringRequired'),
  7929. 'String Required',
  7930. '/label',
  7931. 'input' => array(
  7932. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7933. 'id' => 'ContactStringRequired',
  7934. 'required' => 'required'
  7935. ),
  7936. '/div'
  7937. );
  7938. $this->assertTags($result, $expected);
  7939. $result = $this->Form->input('Contact.imnotrequired');
  7940. $expected = array(
  7941. 'div' => array('class' => 'input text'),
  7942. 'label' => array('for' => 'ContactImnotrequired'),
  7943. 'Imnotrequired',
  7944. '/label',
  7945. 'input' => array(
  7946. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7947. 'id' => 'ContactImnotrequired'
  7948. ),
  7949. '/div'
  7950. );
  7951. $this->assertTags($result, $expected);
  7952. $result = $this->Form->input('Contact.imalsonotrequired');
  7953. $expected = array(
  7954. 'div' => array('class' => 'input text'),
  7955. 'label' => array('for' => 'ContactImalsonotrequired'),
  7956. 'Imalsonotrequired',
  7957. '/label',
  7958. 'input' => array(
  7959. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7960. 'id' => 'ContactImalsonotrequired'
  7961. ),
  7962. '/div'
  7963. );
  7964. $this->assertTags($result, $expected);
  7965. $result = $this->Form->input('Contact.imalsonotrequired2');
  7966. $expected = array(
  7967. 'div' => array('class' => 'input text'),
  7968. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7969. 'Imalsonotrequired2',
  7970. '/label',
  7971. 'input' => array(
  7972. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7973. 'id' => 'ContactImalsonotrequired2'
  7974. ),
  7975. '/div'
  7976. );
  7977. $this->assertTags($result, $expected);
  7978. $result = $this->Form->input('Contact.imnotrequiredeither');
  7979. $expected = array(
  7980. 'div' => array('class' => 'input text'),
  7981. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7982. 'Imnotrequiredeither',
  7983. '/label',
  7984. 'input' => array(
  7985. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7986. 'id' => 'ContactImnotrequiredeither'
  7987. ),
  7988. '/div'
  7989. );
  7990. $this->assertTags($result, $expected);
  7991. $result = $this->Form->input('Contact.iamrequiredalways');
  7992. $expected = array(
  7993. 'div' => array('class' => 'input text required'),
  7994. 'label' => array('for' => 'ContactIamrequiredalways'),
  7995. 'Iamrequiredalways',
  7996. '/label',
  7997. 'input' => array(
  7998. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7999. 'id' => 'ContactIamrequiredalways',
  8000. 'required' => 'required'
  8001. ),
  8002. '/div'
  8003. );
  8004. $this->assertTags($result, $expected);
  8005. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  8006. $expected = array(
  8007. 'div' => array('class' => 'input checkbox required'),
  8008. array('input' => array(
  8009. 'type' => 'hidden',
  8010. 'name' => 'data[Contact][boolean_field]',
  8011. 'id' => 'ContactBooleanField_',
  8012. 'value' => '0'
  8013. )),
  8014. array('input' => array(
  8015. 'type' => 'checkbox',
  8016. 'name' => 'data[Contact][boolean_field]',
  8017. 'value' => '1',
  8018. 'id' => 'ContactBooleanField'
  8019. )),
  8020. 'label' => array('for' => 'ContactBooleanField'),
  8021. 'Boolean Field',
  8022. '/label',
  8023. '/div'
  8024. );
  8025. $this->assertTags($result, $expected);
  8026. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  8027. $expected = array(
  8028. 'div' => array('class' => 'input checkbox required'),
  8029. array('input' => array(
  8030. 'type' => 'hidden',
  8031. 'name' => 'data[Contact][boolean_field]',
  8032. 'id' => 'ContactBooleanField_',
  8033. 'value' => '0'
  8034. )),
  8035. array('input' => array(
  8036. 'type' => 'checkbox',
  8037. 'name' => 'data[Contact][boolean_field]',
  8038. 'value' => '1',
  8039. 'id' => 'ContactBooleanField',
  8040. 'required' => 'required'
  8041. )),
  8042. 'label' => array('for' => 'ContactBooleanField'),
  8043. 'Boolean Field',
  8044. '/label',
  8045. '/div'
  8046. );
  8047. $this->assertTags($result, $expected);
  8048. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  8049. $expected = array(
  8050. 'div' => array('class' => 'input file required'),
  8051. 'label' => array('for' => 'ContactIamrequiredalways'),
  8052. 'Iamrequiredalways',
  8053. '/label',
  8054. 'input' => array(
  8055. 'type' => 'file',
  8056. 'name' => 'data[Contact][iamrequiredalways]',
  8057. 'id' => 'ContactIamrequiredalways',
  8058. 'required' => 'required'
  8059. ),
  8060. '/div'
  8061. );
  8062. $this->assertTags($result, $expected);
  8063. }
  8064. /**
  8065. * Test that required fields are created when only using ModelValidator::add().
  8066. *
  8067. * @return void
  8068. */
  8069. public function testFormInputRequiredDetectionModelValidator() {
  8070. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  8071. $this->Form->create('ContactTag');
  8072. $result = $this->Form->input('ContactTag.iwillberequired');
  8073. $expected = array(
  8074. 'div' => array('class' => 'input text required'),
  8075. 'label' => array('for' => 'ContactTagIwillberequired'),
  8076. 'Iwillberequired',
  8077. '/label',
  8078. 'input' => array(
  8079. 'name' => 'data[ContactTag][iwillberequired]',
  8080. 'type' => 'text',
  8081. 'id' => 'ContactTagIwillberequired',
  8082. 'required' => 'required'
  8083. ),
  8084. '/div'
  8085. );
  8086. $this->assertTags($result, $expected);
  8087. }
  8088. /**
  8089. * testFormMagicInput method
  8090. *
  8091. * @return void
  8092. */
  8093. public function testFormMagicInput() {
  8094. $encoding = strtolower(Configure::read('App.encoding'));
  8095. $result = $this->Form->create('Contact');
  8096. $expected = array(
  8097. 'form' => array(
  8098. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8099. 'accept-charset' => $encoding
  8100. ),
  8101. 'div' => array('style' => 'display:none;'),
  8102. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8103. '/div'
  8104. );
  8105. $this->assertTags($result, $expected);
  8106. $result = $this->Form->input('name');
  8107. $expected = array(
  8108. 'div' => array('class' => 'input text'),
  8109. 'label' => array('for' => 'ContactName'),
  8110. 'Name',
  8111. '/label',
  8112. 'input' => array(
  8113. 'type' => 'text', 'name' => 'data[Contact][name]',
  8114. 'id' => 'ContactName', 'maxlength' => '255'
  8115. ),
  8116. '/div'
  8117. );
  8118. $this->assertTags($result, $expected);
  8119. $result = $this->Form->input('non_existing_field_in_contact_model');
  8120. $expected = array(
  8121. 'div' => array('class' => 'input text'),
  8122. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  8123. 'Non Existing Field In Contact Model',
  8124. '/label',
  8125. 'input' => array(
  8126. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  8127. 'id' => 'ContactNonExistingFieldInContactModel'
  8128. ),
  8129. '/div'
  8130. );
  8131. $this->assertTags($result, $expected);
  8132. $result = $this->Form->input('Address.street');
  8133. $expected = array(
  8134. 'div' => array('class' => 'input text'),
  8135. 'label' => array('for' => 'AddressStreet'),
  8136. 'Street',
  8137. '/label',
  8138. 'input' => array(
  8139. 'type' => 'text', 'name' => 'data[Address][street]',
  8140. 'id' => 'AddressStreet'
  8141. ),
  8142. '/div'
  8143. );
  8144. $this->assertTags($result, $expected);
  8145. $result = $this->Form->input('Address.non_existing_field_in_model');
  8146. $expected = array(
  8147. 'div' => array('class' => 'input text'),
  8148. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  8149. 'Non Existing Field In Model',
  8150. '/label',
  8151. 'input' => array(
  8152. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  8153. 'id' => 'AddressNonExistingFieldInModel'
  8154. ),
  8155. '/div'
  8156. );
  8157. $this->assertTags($result, $expected);
  8158. $result = $this->Form->input('name', array('div' => false));
  8159. $expected = array(
  8160. 'label' => array('for' => 'ContactName'),
  8161. 'Name',
  8162. '/label',
  8163. 'input' => array(
  8164. 'type' => 'text', 'name' => 'data[Contact][name]',
  8165. 'id' => 'ContactName', 'maxlength' => '255'
  8166. )
  8167. );
  8168. $this->assertTags($result, $expected);
  8169. extract($this->dateRegex);
  8170. $now = strtotime('now');
  8171. $result = $this->Form->input('Contact.published', array('div' => false));
  8172. $expected = array(
  8173. 'label' => array('for' => 'ContactPublishedMonth'),
  8174. 'Published',
  8175. '/label',
  8176. array('select' => array(
  8177. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  8178. )),
  8179. $monthsRegex,
  8180. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8181. date('F', $now),
  8182. '/option',
  8183. '*/select',
  8184. '-',
  8185. array('select' => array(
  8186. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  8187. )),
  8188. $daysRegex,
  8189. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8190. date('j', $now),
  8191. '/option',
  8192. '*/select',
  8193. '-',
  8194. array('select' => array(
  8195. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  8196. )),
  8197. $yearsRegex,
  8198. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8199. date('Y', $now),
  8200. '*/select'
  8201. );
  8202. $this->assertTags($result, $expected);
  8203. $result = $this->Form->input('Contact.updated', array('div' => false));
  8204. $expected = array(
  8205. 'label' => array('for' => 'ContactUpdatedMonth'),
  8206. 'Updated',
  8207. '/label',
  8208. array('select' => array(
  8209. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  8210. )),
  8211. $monthsRegex,
  8212. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8213. date('F', $now),
  8214. '/option',
  8215. '*/select',
  8216. '-',
  8217. array('select' => array(
  8218. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  8219. )),
  8220. $daysRegex,
  8221. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8222. date('j', $now),
  8223. '/option',
  8224. '*/select',
  8225. '-',
  8226. array('select' => array(
  8227. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  8228. )),
  8229. $yearsRegex,
  8230. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8231. date('Y', $now),
  8232. '*/select'
  8233. );
  8234. $this->assertTags($result, $expected);
  8235. $result = $this->Form->input('UserForm.stuff');
  8236. $expected = array(
  8237. 'div' => array('class' => 'input text'),
  8238. 'label' => array('for' => 'UserFormStuff'),
  8239. 'Stuff',
  8240. '/label',
  8241. 'input' => array(
  8242. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8243. 'id' => 'UserFormStuff', 'maxlength' => 10
  8244. ),
  8245. '/div'
  8246. );
  8247. $this->assertTags($result, $expected);
  8248. }
  8249. /**
  8250. * testForMagicInputNonExistingNorValidated method
  8251. *
  8252. * @return void
  8253. */
  8254. public function testForMagicInputNonExistingNorValidated() {
  8255. $encoding = strtolower(Configure::read('App.encoding'));
  8256. $result = $this->Form->create('Contact');
  8257. $expected = array(
  8258. 'form' => array(
  8259. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8260. 'accept-charset' => $encoding
  8261. ),
  8262. 'div' => array('style' => 'display:none;'),
  8263. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8264. '/div'
  8265. );
  8266. $this->assertTags($result, $expected);
  8267. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8268. $expected = array(
  8269. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8270. 'Non Existing Nor Validated',
  8271. '/label',
  8272. 'input' => array(
  8273. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8274. 'id' => 'ContactNonExistingNorValidated'
  8275. )
  8276. );
  8277. $this->assertTags($result, $expected);
  8278. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8279. 'div' => false, 'value' => 'my value'
  8280. ));
  8281. $expected = array(
  8282. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8283. 'Non Existing Nor Validated',
  8284. '/label',
  8285. 'input' => array(
  8286. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8287. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8288. )
  8289. );
  8290. $this->assertTags($result, $expected);
  8291. $this->Form->request->data = array(
  8292. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8293. ));
  8294. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8295. $expected = array(
  8296. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8297. 'Non Existing Nor Validated',
  8298. '/label',
  8299. 'input' => array(
  8300. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8301. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8302. )
  8303. );
  8304. $this->assertTags($result, $expected);
  8305. }
  8306. /**
  8307. * testFormMagicInputLabel method
  8308. *
  8309. * @return void
  8310. */
  8311. public function testFormMagicInputLabel() {
  8312. $encoding = strtolower(Configure::read('App.encoding'));
  8313. $result = $this->Form->create('Contact');
  8314. $expected = array(
  8315. 'form' => array(
  8316. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8317. 'accept-charset' => $encoding
  8318. ),
  8319. 'div' => array('style' => 'display:none;'),
  8320. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8321. '/div'
  8322. );
  8323. $this->assertTags($result, $expected);
  8324. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8325. $this->assertTags($result, array('input' => array(
  8326. 'name' => 'data[Contact][name]', 'type' => 'text',
  8327. 'id' => 'ContactName', 'maxlength' => '255')
  8328. ));
  8329. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8330. $expected = array(
  8331. 'label' => array('for' => 'ContactName'),
  8332. 'My label',
  8333. '/label',
  8334. 'input' => array(
  8335. 'type' => 'text', 'name' => 'data[Contact][name]',
  8336. 'id' => 'ContactName', 'maxlength' => '255'
  8337. )
  8338. );
  8339. $this->assertTags($result, $expected);
  8340. $result = $this->Form->input('Contact.name', array(
  8341. 'div' => false, 'label' => array('class' => 'mandatory')
  8342. ));
  8343. $expected = array(
  8344. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8345. 'Name',
  8346. '/label',
  8347. 'input' => array(
  8348. 'type' => 'text', 'name' => 'data[Contact][name]',
  8349. 'id' => 'ContactName', 'maxlength' => '255'
  8350. )
  8351. );
  8352. $this->assertTags($result, $expected);
  8353. $result = $this->Form->input('Contact.name', array(
  8354. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8355. ));
  8356. $expected = array(
  8357. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8358. 'My label',
  8359. '/label',
  8360. 'input' => array(
  8361. 'type' => 'text', 'name' => 'data[Contact][name]',
  8362. 'id' => 'ContactName', 'maxlength' => '255'
  8363. )
  8364. );
  8365. $this->assertTags($result, $expected);
  8366. $result = $this->Form->input('Contact.name', array(
  8367. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8368. ));
  8369. $expected = array(
  8370. 'label' => array('for' => 'my_id'),
  8371. 'Name',
  8372. '/label',
  8373. 'input' => array(
  8374. 'type' => 'text', 'name' => 'data[Contact][name]',
  8375. 'id' => 'my_id', 'maxlength' => '255'
  8376. )
  8377. );
  8378. $this->assertTags($result, $expected);
  8379. $result = $this->Form->input('1.id');
  8380. $this->assertTags($result, array('input' => array(
  8381. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8382. 'id' => 'Contact1Id'
  8383. )));
  8384. $result = $this->Form->input("1.name");
  8385. $expected = array(
  8386. 'div' => array('class' => 'input text'),
  8387. 'label' => array('for' => 'Contact1Name'),
  8388. 'Name',
  8389. '/label',
  8390. 'input' => array(
  8391. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8392. 'id' => 'Contact1Name', 'maxlength' => '255'
  8393. ),
  8394. '/div'
  8395. );
  8396. $this->assertTags($result, $expected);
  8397. $result = $this->Form->input('Contact.1.id');
  8398. $this->assertTags($result, array(
  8399. 'input' => array(
  8400. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8401. 'id' => 'Contact1Id'
  8402. )
  8403. ));
  8404. $result = $this->Form->input("Model.1.name");
  8405. $expected = array(
  8406. 'div' => array('class' => 'input text'),
  8407. 'label' => array('for' => 'Model1Name'),
  8408. 'Name',
  8409. '/label',
  8410. 'input' => array(
  8411. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8412. 'id' => 'Model1Name'
  8413. ),
  8414. '/div'
  8415. );
  8416. $this->assertTags($result, $expected);
  8417. }
  8418. /**
  8419. * testFormEnd method
  8420. *
  8421. * @return void
  8422. */
  8423. public function testFormEnd() {
  8424. $this->assertEquals('</form>', $this->Form->end());
  8425. $result = $this->Form->end('');
  8426. $expected = array(
  8427. 'div' => array('class' => 'submit'),
  8428. 'input' => array('type' => 'submit', 'value' => ''),
  8429. '/div',
  8430. '/form'
  8431. );
  8432. $this->assertTags($result, $expected);
  8433. $result = $this->Form->end(array('label' => ''));
  8434. $expected = array(
  8435. 'div' => array('class' => 'submit'),
  8436. 'input' => array('type' => 'submit', 'value' => ''),
  8437. '/div',
  8438. '/form'
  8439. );
  8440. $this->assertTags($result, $expected);
  8441. $result = $this->Form->end('save');
  8442. $expected = array(
  8443. 'div' => array('class' => 'submit'),
  8444. 'input' => array('type' => 'submit', 'value' => 'save'),
  8445. '/div',
  8446. '/form'
  8447. );
  8448. $this->assertTags($result, $expected);
  8449. $result = $this->Form->end(array('label' => 'save'));
  8450. $expected = array(
  8451. 'div' => array('class' => 'submit'),
  8452. 'input' => array('type' => 'submit', 'value' => 'save'),
  8453. '/div',
  8454. '/form'
  8455. );
  8456. $this->assertTags($result, $expected);
  8457. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8458. $expected = array(
  8459. 'div' => array('class' => 'submit'),
  8460. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8461. '/div',
  8462. '/form'
  8463. );
  8464. $this->assertTags($result, $expected);
  8465. $result = $this->Form->end(array('name' => 'Whatever'));
  8466. $expected = array(
  8467. 'div' => array('class' => 'submit'),
  8468. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8469. '/div',
  8470. '/form'
  8471. );
  8472. $this->assertTags($result, $expected);
  8473. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8474. $expected = array(
  8475. 'div' => array('class' => 'good'),
  8476. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8477. '/div',
  8478. '/form'
  8479. );
  8480. $this->assertTags($result, $expected);
  8481. $result = $this->Form->end(array(
  8482. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8483. ));
  8484. $expected = array(
  8485. 'div' => array('class' => 'good'),
  8486. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8487. '/div',
  8488. '/form'
  8489. );
  8490. $this->assertTags($result, $expected);
  8491. }
  8492. /**
  8493. * testMultipleFormWithIdFields method
  8494. *
  8495. * @return void
  8496. */
  8497. public function testMultipleFormWithIdFields() {
  8498. $this->Form->create('UserForm');
  8499. $result = $this->Form->input('id');
  8500. $this->assertTags($result, array('input' => array(
  8501. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8502. )));
  8503. $result = $this->Form->input('ValidateItem.id');
  8504. $this->assertTags($result, array('input' => array(
  8505. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8506. 'id' => 'ValidateItemId'
  8507. )));
  8508. $result = $this->Form->input('ValidateUser.id');
  8509. $this->assertTags($result, array('input' => array(
  8510. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8511. 'id' => 'ValidateUserId'
  8512. )));
  8513. }
  8514. /**
  8515. * testDbLessModel method
  8516. *
  8517. * @return void
  8518. */
  8519. public function testDbLessModel() {
  8520. $this->Form->create('TestMail');
  8521. $result = $this->Form->input('name');
  8522. $expected = array(
  8523. 'div' => array('class' => 'input text'),
  8524. 'label' => array('for' => 'TestMailName'),
  8525. 'Name',
  8526. '/label',
  8527. 'input' => array(
  8528. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8529. 'id' => 'TestMailName'
  8530. ),
  8531. '/div'
  8532. );
  8533. $this->assertTags($result, $expected);
  8534. ClassRegistry::init('TestMail');
  8535. $this->Form->create('TestMail');
  8536. $result = $this->Form->input('name');
  8537. $expected = array(
  8538. 'div' => array('class' => 'input text'),
  8539. 'label' => array('for' => 'TestMailName'),
  8540. 'Name',
  8541. '/label',
  8542. 'input' => array(
  8543. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8544. 'id' => 'TestMailName'
  8545. ),
  8546. '/div'
  8547. );
  8548. $this->assertTags($result, $expected);
  8549. }
  8550. /**
  8551. * testBrokenness method
  8552. *
  8553. * @return void
  8554. */
  8555. public function testBrokenness() {
  8556. /*
  8557. * #4 This test has two parents and four children. By default (as of r7117) both
  8558. * parents are show but the first parent is missing a child. This is the inconsistency
  8559. * in the default behaviour - one parent has all children, the other does not - dependent
  8560. * on the data values.
  8561. */
  8562. $result = $this->Form->select('Model.field', array(
  8563. 'Fred' => array(
  8564. 'freds_son_1' => 'Fred',
  8565. 'freds_son_2' => 'Freddie'
  8566. ),
  8567. 'Bert' => array(
  8568. 'berts_son_1' => 'Albert',
  8569. 'berts_son_2' => 'Bertie')
  8570. ),
  8571. array('showParents' => true, 'empty' => false)
  8572. );
  8573. $expected = array(
  8574. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8575. array('optgroup' => array('label' => 'Fred')),
  8576. array('option' => array('value' => 'freds_son_1')),
  8577. 'Fred',
  8578. '/option',
  8579. array('option' => array('value' => 'freds_son_2')),
  8580. 'Freddie',
  8581. '/option',
  8582. '/optgroup',
  8583. array('optgroup' => array('label' => 'Bert')),
  8584. array('option' => array('value' => 'berts_son_1')),
  8585. 'Albert',
  8586. '/option',
  8587. array('option' => array('value' => 'berts_son_2')),
  8588. 'Bertie',
  8589. '/option',
  8590. '/optgroup',
  8591. '/select'
  8592. );
  8593. $this->assertTags($result, $expected);
  8594. /*
  8595. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8596. * changed, so we should expect the same select list data, just with a different name
  8597. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8598. * This is where data corruption can occur, because when a select value is missing from
  8599. * a list a form will substitute the first value in the list - without the user knowing.
  8600. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8601. * affect the availability of 3 => 'Three' as a valid option.
  8602. */
  8603. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8604. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8605. ));
  8606. $result = $this->Form->select(
  8607. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8608. );
  8609. $expected = array(
  8610. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8611. array('option' => array('value' => 1)),
  8612. 'One',
  8613. '/option',
  8614. array('option' => array('value' => 2)),
  8615. 'Two',
  8616. '/option',
  8617. array('optgroup' => array('label' => 'Three')),
  8618. array('option' => array('value' => 3)),
  8619. 'Three',
  8620. '/option',
  8621. array('option' => array('value' => 4)),
  8622. 'Four',
  8623. '/option',
  8624. array('option' => array('value' => 5)),
  8625. 'Five',
  8626. '/option',
  8627. '/optgroup',
  8628. '/select'
  8629. );
  8630. $this->assertTags($result, $expected);
  8631. }
  8632. /**
  8633. * Test the generation of fields for a multi record form.
  8634. *
  8635. * @return void
  8636. */
  8637. public function testMultiRecordForm() {
  8638. $this->Form->create('ValidateProfile');
  8639. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8640. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8641. $expected = array(
  8642. 'div' => array('class' => 'input textarea'),
  8643. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8644. 'Name',
  8645. '/label',
  8646. 'textarea' => array(
  8647. 'id' => 'ValidateProfile1ValidateItem2Name',
  8648. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8649. 'cols' => 30,
  8650. 'rows' => 6
  8651. ),
  8652. 'Value',
  8653. '/textarea',
  8654. '/div'
  8655. );
  8656. $this->assertTags($result, $expected);
  8657. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8658. $expected = array(
  8659. 'div' => array('class' => 'input date'),
  8660. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8661. 'Created',
  8662. '/label',
  8663. array('select' => array(
  8664. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8665. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8666. )
  8667. ),
  8668. array('option' => array('value' => '')), '/option',
  8669. $this->dateRegex['monthsRegex'],
  8670. '/select', '-',
  8671. array('select' => array(
  8672. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8673. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8674. )
  8675. ),
  8676. array('option' => array('value' => '')), '/option',
  8677. $this->dateRegex['daysRegex'],
  8678. '/select', '-',
  8679. array('select' => array(
  8680. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8681. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8682. )
  8683. ),
  8684. array('option' => array('value' => '')), '/option',
  8685. $this->dateRegex['yearsRegex'],
  8686. '/select',
  8687. '/div'
  8688. );
  8689. $this->assertTags($result, $expected);
  8690. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8691. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8692. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8693. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8694. $expected = array(
  8695. 'div' => array('class' => 'input select error'),
  8696. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8697. 'Profile',
  8698. '/label',
  8699. 'select' => array(
  8700. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8701. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8702. 'class' => 'form-error'
  8703. ),
  8704. '/select',
  8705. array('div' => array('class' => 'error-message')),
  8706. 'Error',
  8707. '/div',
  8708. '/div'
  8709. );
  8710. $this->assertTags($result, $expected);
  8711. }
  8712. /**
  8713. * test the correct display of multi-record form validation errors.
  8714. *
  8715. * @return void
  8716. */
  8717. public function testMultiRecordFormValidationErrors() {
  8718. $this->Form->create('ValidateProfile');
  8719. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8720. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8721. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8722. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8723. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8724. $result = $this->Form->error('ValidateProfile.2.city');
  8725. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8726. $result = $this->Form->error('2.city');
  8727. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8728. }
  8729. /**
  8730. * test the correct display of multi-record form validation errors.
  8731. *
  8732. * @return void
  8733. */
  8734. public function testSaveManyRecordFormValidationErrors() {
  8735. $this->Form->create('ValidateUser');
  8736. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8737. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8738. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8739. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8740. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8741. $result = $this->Form->error('ValidateUser.0.city');
  8742. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8743. }
  8744. /**
  8745. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8746. *
  8747. * @return void
  8748. */
  8749. public function testInputTemplate() {
  8750. $result = $this->Form->input('Contact.email', array(
  8751. 'type' => 'text', 'format' => array('input')
  8752. ));
  8753. $expected = array(
  8754. 'div' => array('class' => 'input text'),
  8755. 'input' => array(
  8756. 'type' => 'text', 'name' => 'data[Contact][email]',
  8757. 'id' => 'ContactEmail'
  8758. ),
  8759. '/div'
  8760. );
  8761. $this->assertTags($result, $expected);
  8762. $result = $this->Form->input('Contact.email', array(
  8763. 'type' => 'text', 'format' => array('input', 'label'),
  8764. 'label' => '<em>Email (required)</em>'
  8765. ));
  8766. $expected = array(
  8767. 'div' => array('class' => 'input text'),
  8768. array('input' => array(
  8769. 'type' => 'text', 'name' => 'data[Contact][email]',
  8770. 'id' => 'ContactEmail'
  8771. )),
  8772. 'label' => array('for' => 'ContactEmail'),
  8773. 'em' => array(),
  8774. 'Email (required)',
  8775. '/em',
  8776. '/label',
  8777. '/div'
  8778. );
  8779. $this->assertTags($result, $expected);
  8780. $result = $this->Form->input('Contact.email', array(
  8781. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8782. 'between' => '<div>Something in the middle</div>',
  8783. 'after' => '<span>Some text at the end</span>'
  8784. ));
  8785. $expected = array(
  8786. 'div' => array('class' => 'input text'),
  8787. array('input' => array(
  8788. 'type' => 'text', 'name' => 'data[Contact][email]',
  8789. 'id' => 'ContactEmail'
  8790. )),
  8791. array('div' => array()),
  8792. 'Something in the middle',
  8793. '/div',
  8794. 'label' => array('for' => 'ContactEmail'),
  8795. 'Email',
  8796. '/label',
  8797. 'span' => array(),
  8798. 'Some text at the end',
  8799. '/span',
  8800. '/div'
  8801. );
  8802. $this->assertTags($result, $expected);
  8803. $result = $this->Form->input('Contact.method', array(
  8804. 'type' => 'radio',
  8805. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8806. 'between' => 'I am between',
  8807. ));
  8808. $expected = array(
  8809. 'div' => array('class' => 'input radio'),
  8810. 'fieldset' => array(),
  8811. 'legend' => array(),
  8812. 'Method',
  8813. '/legend',
  8814. 'I am between',
  8815. 'input' => array(
  8816. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8817. 'value' => '', 'id' => 'ContactMethod_'
  8818. ),
  8819. array('input' => array(
  8820. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8821. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8822. )),
  8823. array('label' => array('for' => 'ContactMethodEmail')),
  8824. 'Email',
  8825. '/label',
  8826. array('input' => array(
  8827. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8828. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8829. )),
  8830. array('label' => array('for' => 'ContactMethodPigeon')),
  8831. 'Pigeon',
  8832. '/label',
  8833. '/fieldset',
  8834. '/div',
  8835. );
  8836. $this->assertTags($result, $expected);
  8837. }
  8838. /**
  8839. * test that some html5 inputs + FormHelper::__call() work
  8840. *
  8841. * @return void
  8842. */
  8843. public function testHtml5Inputs() {
  8844. $result = $this->Form->email('User.email');
  8845. $expected = array(
  8846. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8847. );
  8848. $this->assertTags($result, $expected);
  8849. $result = $this->Form->search('User.query');
  8850. $expected = array(
  8851. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8852. );
  8853. $this->assertTags($result, $expected);
  8854. $result = $this->Form->search('User.query', array('value' => 'test'));
  8855. $expected = array(
  8856. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8857. );
  8858. $this->assertTags($result, $expected);
  8859. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8860. $expected = array(
  8861. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8862. );
  8863. $this->assertTags($result, $expected);
  8864. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8865. $expected = array(
  8866. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8867. );
  8868. $this->assertTags($result, $expected);
  8869. }
  8870. /**
  8871. *
  8872. * @expectedException CakeException
  8873. * @return void
  8874. */
  8875. public function testHtml5InputException() {
  8876. $this->Form->email();
  8877. }
  8878. /**
  8879. * Tests that a model can be loaded from the model names passed in the request object
  8880. *
  8881. * @return void
  8882. */
  8883. public function testIntrospectModelFromRequest() {
  8884. $this->loadFixtures('Post');
  8885. App::build(array(
  8886. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8887. ));
  8888. CakePlugin::load('TestPlugin');
  8889. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8890. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8891. $this->Form->create('TestPluginPost');
  8892. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8893. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8894. CakePlugin::unload();
  8895. App::build();
  8896. }
  8897. /**
  8898. * Tests that it is possible to set the validation errors directly in the helper for a field
  8899. *
  8900. * @return void
  8901. */
  8902. public function testCustomValidationErrors() {
  8903. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8904. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8905. $this->assertEquals('Badness!', $result);
  8906. }
  8907. /**
  8908. * Tests that the 'on' key validates as expected on create
  8909. *
  8910. * @return void
  8911. */
  8912. public function testRequiredOnCreate() {
  8913. $this->Form->create('Contact');
  8914. $result = $this->Form->input('Contact.imrequiredonupdate');
  8915. $expected = array(
  8916. 'div' => array('class' => 'input text'),
  8917. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8918. 'Imrequiredonupdate',
  8919. '/label',
  8920. 'input' => array(
  8921. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8922. 'id' => 'ContactImrequiredonupdate'
  8923. ),
  8924. '/div'
  8925. );
  8926. $this->assertTags($result, $expected);
  8927. $result = $this->Form->input('Contact.imrequiredoncreate');
  8928. $expected = array(
  8929. 'div' => array('class' => 'input text required'),
  8930. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8931. 'Imrequiredoncreate',
  8932. '/label',
  8933. 'input' => array(
  8934. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8935. 'id' => 'ContactImrequiredoncreate',
  8936. 'required' => 'required'
  8937. ),
  8938. '/div'
  8939. );
  8940. $this->assertTags($result, $expected);
  8941. $result = $this->Form->input('Contact.imrequiredonboth');
  8942. $expected = array(
  8943. 'div' => array('class' => 'input text required'),
  8944. 'label' => array('for' => 'ContactImrequiredonboth'),
  8945. 'Imrequiredonboth',
  8946. '/label',
  8947. 'input' => array(
  8948. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8949. 'id' => 'ContactImrequiredonboth',
  8950. 'required' => 'required'
  8951. ),
  8952. '/div'
  8953. );
  8954. $this->assertTags($result, $expected);
  8955. $this->Form->inputDefaults(array('required' => false));
  8956. $result = $this->Form->input('Contact.imrequired');
  8957. $expected = array(
  8958. 'div' => array('class' => 'input text'),
  8959. 'label' => array('for' => 'ContactImrequired'),
  8960. 'Imrequired',
  8961. '/label',
  8962. 'input' => array(
  8963. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8964. 'id' => 'ContactImrequired'
  8965. ),
  8966. '/div'
  8967. );
  8968. $this->assertTags($result, $expected);
  8969. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8970. $this->assertTags($result, $expected);
  8971. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8972. $expected = array(
  8973. 'div' => array('class' => 'input text required'),
  8974. 'label' => array('for' => 'ContactImrequired'),
  8975. 'Imrequired',
  8976. '/label',
  8977. 'input' => array(
  8978. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8979. 'id' => 'ContactImrequired'
  8980. ),
  8981. '/div'
  8982. );
  8983. $this->assertTags($result, $expected);
  8984. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8985. $this->assertTags($result, $expected);
  8986. }
  8987. /**
  8988. * Tests that the 'on' key validates as expected on update
  8989. *
  8990. * @return void
  8991. */
  8992. public function testRequiredOnUpdate() {
  8993. $this->Form->request->data['Contact']['id'] = 1;
  8994. $this->Form->create('Contact');
  8995. $result = $this->Form->input('Contact.imrequiredonupdate');
  8996. $expected = array(
  8997. 'div' => array('class' => 'input text required'),
  8998. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8999. 'Imrequiredonupdate',
  9000. '/label',
  9001. 'input' => array(
  9002. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  9003. 'id' => 'ContactImrequiredonupdate',
  9004. 'required' => 'required'
  9005. ),
  9006. '/div'
  9007. );
  9008. $this->assertTags($result, $expected);
  9009. $result = $this->Form->input('Contact.imrequiredoncreate');
  9010. $expected = array(
  9011. 'div' => array('class' => 'input text'),
  9012. 'label' => array('for' => 'ContactImrequiredoncreate'),
  9013. 'Imrequiredoncreate',
  9014. '/label',
  9015. 'input' => array(
  9016. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  9017. 'id' => 'ContactImrequiredoncreate'
  9018. ),
  9019. '/div'
  9020. );
  9021. $this->assertTags($result, $expected);
  9022. $result = $this->Form->input('Contact.imrequiredonboth');
  9023. $expected = array(
  9024. 'div' => array('class' => 'input text required'),
  9025. 'label' => array('for' => 'ContactImrequiredonboth'),
  9026. 'Imrequiredonboth',
  9027. '/label',
  9028. 'input' => array(
  9029. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  9030. 'id' => 'ContactImrequiredonboth',
  9031. 'required' => 'required'
  9032. ),
  9033. '/div'
  9034. );
  9035. $this->assertTags($result, $expected);
  9036. $result = $this->Form->input('Contact.imrequired');
  9037. $expected = array(
  9038. 'div' => array('class' => 'input text required'),
  9039. 'label' => array('for' => 'ContactImrequired'),
  9040. 'Imrequired',
  9041. '/label',
  9042. 'input' => array(
  9043. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9044. 'id' => 'ContactImrequired',
  9045. 'required' => 'required'
  9046. ),
  9047. '/div'
  9048. );
  9049. $this->assertTags($result, $expected);
  9050. }
  9051. /**
  9052. * Test inputDefaults setter and getter
  9053. *
  9054. * @return void
  9055. */
  9056. public function testInputDefaults() {
  9057. $this->Form->create('Contact');
  9058. $this->Form->inputDefaults(array(
  9059. 'label' => false,
  9060. 'div' => array(
  9061. 'style' => 'color: #000;'
  9062. )
  9063. ));
  9064. $result = $this->Form->input('Contact.field1');
  9065. $expected = array(
  9066. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  9067. 'input' => array(
  9068. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9069. 'id' => 'ContactField1'
  9070. ),
  9071. '/div'
  9072. );
  9073. $this->assertTags($result, $expected);
  9074. $this->Form->inputDefaults(array(
  9075. 'div' => false,
  9076. 'label' => 'Label',
  9077. ));
  9078. $result = $this->Form->input('Contact.field1');
  9079. $expected = array(
  9080. 'label' => array('for' => 'ContactField1'),
  9081. 'Label',
  9082. '/label',
  9083. 'input' => array(
  9084. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9085. 'id' => 'ContactField1'
  9086. ),
  9087. );
  9088. $this->assertTags($result, $expected);
  9089. $this->Form->inputDefaults(array(
  9090. 'label' => false,
  9091. ), true);
  9092. $result = $this->Form->input('Contact.field1');
  9093. $expected = array(
  9094. 'input' => array(
  9095. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9096. 'id' => 'ContactField1'
  9097. ),
  9098. );
  9099. $this->assertTags($result, $expected);
  9100. $result = $this->Form->inputDefaults();
  9101. $expected = array(
  9102. 'div' => false,
  9103. 'label' => false,
  9104. );
  9105. $this->assertEquals($expected, $result);
  9106. }
  9107. }