FormHelperTest.php 255 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  111. ),
  112. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  113. );
  114. /**
  115. * schema method
  116. *
  117. * @return void
  118. */
  119. public function setSchema($schema) {
  120. $this->_schema = $schema;
  121. }
  122. /**
  123. * hasAndBelongsToMany property
  124. *
  125. * @var array
  126. */
  127. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  128. /**
  129. * hasAndBelongsToMany property
  130. *
  131. * @var array
  132. */
  133. public $belongsTo = array('User' => array('className' => 'UserForm'));
  134. }
  135. /**
  136. * ContactTagsContact class
  137. *
  138. * @package cake
  139. * @package Cake.Test.Case.View.Helper
  140. */
  141. class ContactTagsContact extends CakeTestModel {
  142. /**
  143. * useTable property
  144. *
  145. * @var bool false
  146. */
  147. public $useTable = false;
  148. /**
  149. * name property
  150. *
  151. * @var string 'Contact'
  152. */
  153. public $name = 'ContactTagsContact';
  154. /**
  155. * Default schema
  156. *
  157. * @var array
  158. */
  159. protected $_schema = array(
  160. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  161. 'contact_tag_id' => array(
  162. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  163. )
  164. );
  165. /**
  166. * schema method
  167. *
  168. * @return void
  169. */
  170. public function setSchema($schema) {
  171. $this->_schema = $schema;
  172. }
  173. }
  174. /**
  175. * ContactNonStandardPk class
  176. *
  177. * @package cake
  178. * @package Cake.Test.Case.View.Helper
  179. */
  180. class ContactNonStandardPk extends Contact {
  181. /**
  182. * primaryKey property
  183. *
  184. * @var string 'pk'
  185. */
  186. public $primaryKey = 'pk';
  187. /**
  188. * name property
  189. *
  190. * @var string 'ContactNonStandardPk'
  191. */
  192. public $name = 'ContactNonStandardPk';
  193. /**
  194. * schema method
  195. *
  196. * @return void
  197. */
  198. public function schema($field = false) {
  199. $this->_schema = parent::schema();
  200. $this->_schema['pk'] = $this->_schema['id'];
  201. unset($this->_schema['id']);
  202. return $this->_schema;
  203. }
  204. }
  205. /**
  206. * ContactTag class
  207. *
  208. * @package cake
  209. * @package Cake.Test.Case.View.Helper
  210. */
  211. class ContactTag extends Model {
  212. /**
  213. * useTable property
  214. *
  215. * @var bool false
  216. */
  217. public $useTable = false;
  218. /**
  219. * schema definition
  220. *
  221. * @var array
  222. */
  223. protected $_schema = array(
  224. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  225. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  226. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  227. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * UserForm class
  232. *
  233. * @package cake
  234. * @package Cake.Test.Case.View.Helper
  235. */
  236. class UserForm extends CakeTestModel {
  237. /**
  238. * useTable property
  239. *
  240. * @var bool false
  241. */
  242. public $useTable = false;
  243. /**
  244. * primaryKey property
  245. *
  246. * @var string 'id'
  247. */
  248. public $primaryKey = 'id';
  249. /**
  250. * name property
  251. *
  252. * @var string 'UserForm'
  253. */
  254. public $name = 'UserForm';
  255. /**
  256. * hasMany property
  257. *
  258. * @var array
  259. */
  260. public $hasMany = array(
  261. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  262. ));
  263. /**
  264. * schema definition
  265. *
  266. * @var array
  267. */
  268. protected $_schema = array(
  269. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  270. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  271. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  272. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  273. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  274. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  275. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  276. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  277. );
  278. }
  279. /**
  280. * OpenidUrl class
  281. *
  282. * @package cake
  283. * @package Cake.Test.Case.View.Helper
  284. */
  285. class OpenidUrl extends CakeTestModel {
  286. /**
  287. * useTable property
  288. *
  289. * @var bool false
  290. */
  291. public $useTable = false;
  292. /**
  293. * primaryKey property
  294. *
  295. * @var string 'id'
  296. */
  297. public $primaryKey = 'id';
  298. /**
  299. * name property
  300. *
  301. * @var string 'OpenidUrl'
  302. */
  303. public $name = 'OpenidUrl';
  304. /**
  305. * belongsTo property
  306. *
  307. * @var array
  308. */
  309. public $belongsTo = array('UserForm' => array(
  310. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  311. ));
  312. /**
  313. * validate property
  314. *
  315. * @var array
  316. */
  317. public $validate = array('openid_not_registered' => array());
  318. /**
  319. * schema method
  320. *
  321. * @var array
  322. */
  323. protected $_schema = array(
  324. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  325. 'user_form_id' => array(
  326. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  327. ),
  328. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  329. );
  330. /**
  331. * beforeValidate method
  332. *
  333. * @return void
  334. */
  335. public function beforeValidate($options = array()) {
  336. $this->invalidate('openid_not_registered');
  337. return true;
  338. }
  339. }
  340. /**
  341. * ValidateUser class
  342. *
  343. * @package cake
  344. * @package Cake.Test.Case.View.Helper
  345. */
  346. class ValidateUser extends CakeTestModel {
  347. /**
  348. * primaryKey property
  349. *
  350. * @var string 'id'
  351. */
  352. public $primaryKey = 'id';
  353. /**
  354. * useTable property
  355. *
  356. * @var bool false
  357. */
  358. public $useTable = false;
  359. /**
  360. * name property
  361. *
  362. * @var string 'ValidateUser'
  363. */
  364. public $name = 'ValidateUser';
  365. /**
  366. * hasOne property
  367. *
  368. * @var array
  369. */
  370. public $hasOne = array('ValidateProfile' => array(
  371. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  372. ));
  373. /**
  374. * schema method
  375. *
  376. * @var array
  377. */
  378. protected $_schema = array(
  379. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  380. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  381. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  383. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  384. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  385. );
  386. /**
  387. * beforeValidate method
  388. *
  389. * @return void
  390. */
  391. public function beforeValidate($options = array()) {
  392. $this->invalidate('email');
  393. return false;
  394. }
  395. }
  396. /**
  397. * ValidateProfile class
  398. *
  399. * @package cake
  400. * @package Cake.Test.Case.View.Helper
  401. */
  402. class ValidateProfile extends CakeTestModel {
  403. /**
  404. * primaryKey property
  405. *
  406. * @var string 'id'
  407. */
  408. public $primaryKey = 'id';
  409. /**
  410. * useTable property
  411. *
  412. * @var bool false
  413. */
  414. public $useTable = false;
  415. /**
  416. * schema property
  417. *
  418. * @var array
  419. */
  420. protected $_schema = array(
  421. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  422. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  424. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  426. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  427. );
  428. /**
  429. * name property
  430. *
  431. * @var string 'ValidateProfile'
  432. */
  433. public $name = 'ValidateProfile';
  434. /**
  435. * hasOne property
  436. *
  437. * @var array
  438. */
  439. public $hasOne = array('ValidateItem' => array(
  440. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  441. ));
  442. /**
  443. * belongsTo property
  444. *
  445. * @var array
  446. */
  447. public $belongsTo = array('ValidateUser' => array(
  448. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  449. ));
  450. /**
  451. * beforeValidate method
  452. *
  453. * @return void
  454. */
  455. public function beforeValidate($options = array()) {
  456. $this->invalidate('full_name');
  457. $this->invalidate('city');
  458. return false;
  459. }
  460. }
  461. /**
  462. * ValidateItem class
  463. *
  464. * @package cake
  465. * @package Cake.Test.Case.View.Helper
  466. */
  467. class ValidateItem extends CakeTestModel {
  468. /**
  469. * primaryKey property
  470. *
  471. * @var string 'id'
  472. */
  473. public $primaryKey = 'id';
  474. /**
  475. * useTable property
  476. *
  477. * @var bool false
  478. */
  479. public $useTable = false;
  480. /**
  481. * name property
  482. *
  483. * @var string 'ValidateItem'
  484. */
  485. public $name = 'ValidateItem';
  486. /**
  487. * schema property
  488. *
  489. * @var array
  490. */
  491. protected $_schema = array(
  492. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  493. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  495. 'description' => array(
  496. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  497. ),
  498. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  499. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  500. );
  501. /**
  502. * belongsTo property
  503. *
  504. * @var array
  505. */
  506. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  507. /**
  508. * beforeValidate method
  509. *
  510. * @return void
  511. */
  512. public function beforeValidate($options = array()) {
  513. $this->invalidate('description');
  514. return false;
  515. }
  516. }
  517. /**
  518. * TestMail class
  519. *
  520. * @package cake
  521. * @package Cake.Test.Case.View.Helper
  522. */
  523. class TestMail extends CakeTestModel {
  524. /**
  525. * primaryKey property
  526. *
  527. * @var string 'id'
  528. */
  529. public $primaryKey = 'id';
  530. /**
  531. * useTable property
  532. *
  533. * @var bool false
  534. */
  535. public $useTable = false;
  536. /**
  537. * name property
  538. *
  539. * @var string 'TestMail'
  540. */
  541. public $name = 'TestMail';
  542. }
  543. /**
  544. * FormHelperTest class
  545. *
  546. * @package cake
  547. * @subpackage Cake.Test.Case.View.Helper
  548. * @property FormHelper $Form
  549. */
  550. class FormHelperTest extends CakeTestCase {
  551. /**
  552. * Fixtures to be used
  553. *
  554. * @var array
  555. */
  556. public $fixtures = array('core.post');
  557. /**
  558. * Do not load the fixtures by default
  559. *
  560. * @var boolean
  561. */
  562. public $autoFixtures = false;
  563. /**
  564. * setUp method
  565. *
  566. * @return void
  567. */
  568. public function setUp() {
  569. parent::setUp();
  570. Configure::write('App.base', '');
  571. $this->Controller = new ContactTestController();
  572. $this->View = new View($this->Controller);
  573. $this->Form = new FormHelper($this->View);
  574. $this->Form->Html = new HtmlHelper($this->View);
  575. $this->Form->request = new CakeRequest('contacts/add', false);
  576. $this->Form->request->here = '/contacts/add';
  577. $this->Form->request['action'] = 'add';
  578. $this->Form->request->webroot = '';
  579. $this->Form->request->base = '';
  580. ClassRegistry::addObject('Contact', new Contact());
  581. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  582. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  583. ClassRegistry::addObject('User', new UserForm());
  584. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  585. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  586. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  587. $this->oldSalt = Configure::read('Security.salt');
  588. $this->dateRegex = array(
  589. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  590. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  591. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  592. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  594. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  595. );
  596. Configure::write('Security.salt', 'foo!');
  597. }
  598. /**
  599. * tearDown method
  600. *
  601. * @return void
  602. */
  603. public function tearDown() {
  604. parent::tearDown();
  605. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  606. Configure::write('Security.salt', $this->oldSalt);
  607. }
  608. /**
  609. * testFormCreateWithSecurity method
  610. *
  611. * Test form->create() with security key.
  612. *
  613. * @return void
  614. */
  615. public function testCreateWithSecurity() {
  616. $this->Form->request['_Token'] = array('key' => 'testKey');
  617. $encoding = strtolower(Configure::read('App.encoding'));
  618. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  619. $expected = array(
  620. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  621. 'div' => array('style' => 'display:none;'),
  622. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  625. )),
  626. '/div'
  627. );
  628. $this->assertTags($result, $expected);
  629. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  630. $expected['form']['id'] = 'MyForm';
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * test that create() clears the fields property so it starts fresh
  635. *
  636. * @return void
  637. */
  638. public function testCreateClearingFields() {
  639. $this->Form->fields = array('model_id');
  640. $this->Form->create('Contact');
  641. $this->assertEquals(array(), $this->Form->fields);
  642. }
  643. /**
  644. * Tests form hash generation with model-less data
  645. *
  646. * @return void
  647. */
  648. public function testValidateHashNoModel() {
  649. $this->Form->request['_Token'] = array('key' => 'foo');
  650. $result = $this->Form->secure(array('anything'));
  651. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  652. }
  653. /**
  654. * Tests that models with identical field names get resolved properly
  655. *
  656. * @return void
  657. */
  658. public function testDuplicateFieldNameResolution() {
  659. $result = $this->Form->create('ValidateUser');
  660. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  661. $result = $this->Form->input('ValidateItem.name');
  662. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  663. $result = $this->Form->input('ValidateUser.name');
  664. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  665. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  666. $this->assertRegExp('/type="text"/', $result);
  667. $result = $this->Form->input('ValidateItem.name');
  668. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  669. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  670. $this->assertRegExp('/<textarea/', $result);
  671. $result = $this->Form->input('name');
  672. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  673. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  674. $this->assertRegExp('/type="text"/', $result);
  675. }
  676. /**
  677. * Tests that hidden fields generated for checkboxes don't get locked
  678. *
  679. * @return void
  680. */
  681. public function testNoCheckboxLocking() {
  682. $this->Form->request['_Token'] = array('key' => 'foo');
  683. $this->assertSame(array(), $this->Form->fields);
  684. $this->Form->checkbox('check', array('value' => '1'));
  685. $this->assertSame($this->Form->fields, array('check'));
  686. }
  687. /**
  688. * testFormSecurityFields method
  689. *
  690. * Test generation of secure form hash generation.
  691. *
  692. * @return void
  693. */
  694. public function testFormSecurityFields() {
  695. $key = 'testKey';
  696. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  697. $this->Form->request['_Token'] = array('key' => $key);
  698. $result = $this->Form->secure($fields);
  699. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  700. $expected .= ':' . 'Model.valid';
  701. $expected = array(
  702. 'div' => array('style' => 'display:none;'),
  703. array('input' => array(
  704. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  705. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  706. )),
  707. array('input' => array(
  708. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  709. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  710. )),
  711. '/div'
  712. );
  713. $this->assertTags($result, $expected);
  714. }
  715. /**
  716. * Tests correct generation of number fields for double and float fields
  717. *
  718. * @return void
  719. */
  720. public function testTextFieldGenerationForFloats() {
  721. $model = ClassRegistry::getObject('Contact');
  722. $model->setSchema(array('foo' => array(
  723. 'type' => 'float',
  724. 'null' => false,
  725. 'default' => null,
  726. 'length' => null
  727. )));
  728. $this->Form->create('Contact');
  729. $result = $this->Form->input('foo');
  730. $expected = array(
  731. 'div' => array('class' => 'input number'),
  732. 'label' => array('for' => 'ContactFoo'),
  733. 'Foo',
  734. '/label',
  735. array('input' => array(
  736. 'type' => 'number',
  737. 'name' => 'data[Contact][foo]',
  738. 'id' => 'ContactFoo',
  739. 'step' => 'any'
  740. )),
  741. '/div'
  742. );
  743. $this->assertTags($result, $expected);
  744. $result = $this->Form->input('foo', array('step' => 0.5));
  745. $expected = array(
  746. 'div' => array('class' => 'input number'),
  747. 'label' => array('for' => 'ContactFoo'),
  748. 'Foo',
  749. '/label',
  750. array('input' => array(
  751. 'type' => 'number',
  752. 'name' => 'data[Contact][foo]',
  753. 'id' => 'ContactFoo',
  754. 'step' => '0.5'
  755. )),
  756. '/div'
  757. );
  758. $this->assertTags($result, $expected);
  759. }
  760. /**
  761. * Tests correct generation of number fields for integer fields
  762. *
  763. * @access public
  764. * @return void
  765. */
  766. public function testTextFieldTypeNumberGenerationForIntegers() {
  767. $model = ClassRegistry::getObject('Contact');
  768. $model->setSchema(array('foo' => array(
  769. 'type' => 'integer',
  770. 'null' => false,
  771. 'default' => null,
  772. 'length' => null
  773. )));
  774. $this->Form->create('Contact');
  775. $result = $this->Form->input('foo');
  776. $expected = array(
  777. 'div' => array('class' => 'input number'),
  778. 'label' => array('for' => 'ContactFoo'),
  779. 'Foo',
  780. '/label',
  781. array('input' => array(
  782. 'type' => 'number', 'name' => 'data[Contact][foo]',
  783. 'id' => 'ContactFoo'
  784. )),
  785. '/div'
  786. );
  787. $this->assertTags($result, $expected);
  788. }
  789. /**
  790. * testFormSecurityMultipleFields method
  791. *
  792. * Test secure() with multiple row form. Ensure hash is correct.
  793. *
  794. * @return void
  795. */
  796. public function testFormSecurityMultipleFields() {
  797. $key = 'testKey';
  798. $fields = array(
  799. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  800. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  801. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  802. );
  803. $this->Form->request['_Token'] = array('key' => $key);
  804. $result = $this->Form->secure($fields);
  805. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  806. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  807. $expected = array(
  808. 'div' => array('style' => 'display:none;'),
  809. array('input' => array(
  810. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  811. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  812. )),
  813. array('input' => array(
  814. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  815. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  816. )),
  817. '/div'
  818. );
  819. $this->assertTags($result, $expected);
  820. }
  821. /**
  822. * testFormSecurityMultipleSubmitButtons
  823. *
  824. * test form submit generation and ensure that _Token is only created on end()
  825. *
  826. * @return void
  827. */
  828. public function testFormSecurityMultipleSubmitButtons() {
  829. $key = 'testKey';
  830. $this->Form->request['_Token'] = array('key' => $key);
  831. $this->Form->create('Addresses');
  832. $this->Form->input('Address.title');
  833. $this->Form->input('Address.first_name');
  834. $result = $this->Form->submit('Save', array('name' => 'save'));
  835. $expected = array(
  836. 'div' => array('class' => 'submit'),
  837. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  838. '/div',
  839. );
  840. $this->assertTags($result, $expected);
  841. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  842. $expected = array(
  843. 'div' => array('class' => 'submit'),
  844. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  845. '/div',
  846. );
  847. $this->assertTags($result, $expected);
  848. $result = $this->Form->end(null);
  849. $expected = array(
  850. 'div' => array('style' => 'display:none;'),
  851. array('input' => array(
  852. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  853. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  854. )),
  855. array('input' => array(
  856. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  857. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  858. )),
  859. '/div'
  860. );
  861. $this->assertTags($result, $expected);
  862. }
  863. /**
  864. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  865. *
  866. * @return void
  867. */
  868. public function testSecurityButtonNestedNamed() {
  869. $key = 'testKey';
  870. $this->Form->request['_Token'] = array('key' => $key);
  871. $this->Form->create('Addresses');
  872. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  873. $result = $this->Form->unlockField();
  874. $this->assertEquals(array('Address.button'), $result);
  875. }
  876. /**
  877. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  878. *
  879. * @return void
  880. */
  881. public function testSecuritySubmitNestedNamed() {
  882. $key = 'testKey';
  883. $this->Form->request['_Token'] = array('key' => $key);
  884. $this->Form->create('Addresses');
  885. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  886. $result = $this->Form->unlockField();
  887. $this->assertEquals(array('Address.button'), $result);
  888. }
  889. /**
  890. * Test that the correct fields are unlocked for image submits with no names.
  891. *
  892. * @return void
  893. */
  894. public function testSecuritySubmitImageNoName() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('User');
  898. $result = $this->Form->submit('save.png');
  899. $expected = array(
  900. 'div' => array('class' => 'submit'),
  901. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  902. '/div'
  903. );
  904. $this->assertTags($result, $expected);
  905. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  906. }
  907. /**
  908. * Test that the correct fields are unlocked for image submits with names.
  909. *
  910. * @return void
  911. */
  912. public function testSecuritySubmitImageName() {
  913. $key = 'testKey';
  914. $this->Form->request['_Token'] = array('key' => $key);
  915. $this->Form->create('User');
  916. $result = $this->Form->submit('save.png', array('name' => 'test'));
  917. $expected = array(
  918. 'div' => array('class' => 'submit'),
  919. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  920. '/div'
  921. );
  922. $this->assertTags($result, $expected);
  923. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  924. }
  925. /**
  926. * testFormSecurityMultipleInputFields method
  927. *
  928. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  929. *
  930. * @return void
  931. */
  932. public function testFormSecurityMultipleInputFields() {
  933. $key = 'testKey';
  934. $this->Form->request['_Token'] = array('key' => $key);
  935. $this->Form->create('Addresses');
  936. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  937. $this->Form->input('Addresses.0.title');
  938. $this->Form->input('Addresses.0.first_name');
  939. $this->Form->input('Addresses.0.last_name');
  940. $this->Form->input('Addresses.0.address');
  941. $this->Form->input('Addresses.0.city');
  942. $this->Form->input('Addresses.0.phone');
  943. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  944. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  945. $this->Form->input('Addresses.1.title');
  946. $this->Form->input('Addresses.1.first_name');
  947. $this->Form->input('Addresses.1.last_name');
  948. $this->Form->input('Addresses.1.address');
  949. $this->Form->input('Addresses.1.city');
  950. $this->Form->input('Addresses.1.phone');
  951. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  952. $result = $this->Form->secure($this->Form->fields);
  953. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  954. $expected = array(
  955. 'div' => array('style' => 'display:none;'),
  956. array('input' => array(
  957. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  958. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  959. )),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  962. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  963. )),
  964. '/div'
  965. );
  966. $this->assertTags($result, $expected);
  967. }
  968. /**
  969. * Test form security with Model.field.0 style inputs
  970. *
  971. * @return void
  972. */
  973. public function testFormSecurityArrayFields() {
  974. $key = 'testKey';
  975. $this->Form->request->params['_Token']['key'] = $key;
  976. $this->Form->create('Address');
  977. $this->Form->input('Address.primary.1');
  978. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  979. $this->Form->input('Address.secondary.1.0');
  980. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  981. }
  982. /**
  983. * testFormSecurityMultipleInputDisabledFields method
  984. *
  985. * test secure form generation with multiple records and disabled fields.
  986. *
  987. * @return void
  988. */
  989. public function testFormSecurityMultipleInputDisabledFields() {
  990. $key = 'testKey';
  991. $this->Form->request->params['_Token'] = array(
  992. 'key' => $key,
  993. 'unlockedFields' => array('first_name', 'address')
  994. );
  995. $this->Form->create();
  996. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  997. $this->Form->input('Addresses.0.title');
  998. $this->Form->input('Addresses.0.first_name');
  999. $this->Form->input('Addresses.0.last_name');
  1000. $this->Form->input('Addresses.0.address');
  1001. $this->Form->input('Addresses.0.city');
  1002. $this->Form->input('Addresses.0.phone');
  1003. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1004. $this->Form->input('Addresses.1.title');
  1005. $this->Form->input('Addresses.1.first_name');
  1006. $this->Form->input('Addresses.1.last_name');
  1007. $this->Form->input('Addresses.1.address');
  1008. $this->Form->input('Addresses.1.city');
  1009. $this->Form->input('Addresses.1.phone');
  1010. $result = $this->Form->secure($this->Form->fields);
  1011. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1012. $expected = array(
  1013. 'div' => array('style' => 'display:none;'),
  1014. array('input' => array(
  1015. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1016. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1017. )),
  1018. array('input' => array(
  1019. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1020. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1021. )),
  1022. '/div'
  1023. );
  1024. $this->assertTags($result, $expected);
  1025. }
  1026. /**
  1027. * testFormSecurityInputDisabledFields method
  1028. *
  1029. * Test single record form with disabled fields.
  1030. *
  1031. * @return void
  1032. */
  1033. public function testFormSecurityInputUnlockedFields() {
  1034. $key = 'testKey';
  1035. $this->Form->request['_Token'] = array(
  1036. 'key' => $key,
  1037. 'unlockedFields' => array('first_name', 'address')
  1038. );
  1039. $this->Form->create();
  1040. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1041. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1042. $this->Form->input('Addresses.title');
  1043. $this->Form->input('Addresses.first_name');
  1044. $this->Form->input('Addresses.last_name');
  1045. $this->Form->input('Addresses.address');
  1046. $this->Form->input('Addresses.city');
  1047. $this->Form->input('Addresses.phone');
  1048. $result = $this->Form->fields;
  1049. $expected = array(
  1050. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1051. 'Addresses.city', 'Addresses.phone'
  1052. );
  1053. $this->assertEquals($expected, $result);
  1054. $result = $this->Form->secure($expected);
  1055. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1056. $expected = array(
  1057. 'div' => array('style' => 'display:none;'),
  1058. array('input' => array(
  1059. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1060. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1061. )),
  1062. array('input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1064. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1065. )),
  1066. '/div'
  1067. );
  1068. $this->assertTags($result, $expected);
  1069. }
  1070. /**
  1071. * test securing inputs with custom name attributes.
  1072. *
  1073. * @return void
  1074. */
  1075. public function testFormSecureWithCustomNameAttribute() {
  1076. $this->Form->request->params['_Token']['key'] = 'testKey';
  1077. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1078. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1079. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1080. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1081. }
  1082. /**
  1083. * testFormSecuredInput method
  1084. *
  1085. * Test generation of entire secure form, assertions made on input() output.
  1086. *
  1087. * @return void
  1088. */
  1089. public function testFormSecuredInput() {
  1090. $this->Form->request['_Token'] = array('key' => 'testKey');
  1091. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1092. $encoding = strtolower(Configure::read('App.encoding'));
  1093. $expected = array(
  1094. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1095. 'div' => array('style' => 'display:none;'),
  1096. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1097. array('input' => array(
  1098. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1099. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1100. )),
  1101. '/div'
  1102. );
  1103. $this->assertTags($result, $expected);
  1104. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1105. $expected = array(
  1106. 'div' => array('class' => 'input text'),
  1107. 'label' => array('for' => 'UserFormPublished'),
  1108. 'Published',
  1109. '/label',
  1110. array('input' => array(
  1111. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1112. 'id' => 'UserFormPublished'
  1113. )),
  1114. '/div'
  1115. );
  1116. $this->assertTags($result, $expected);
  1117. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1118. $expected = array(
  1119. 'div' => array('class' => 'input text'),
  1120. 'label' => array('for' => 'UserFormOther'),
  1121. 'Other',
  1122. '/label',
  1123. array('input' => array(
  1124. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1125. 'id' => 'UserFormOther'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->hidden('UserForm.stuff');
  1131. $expected = array('input' => array(
  1132. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1133. 'id' => 'UserFormStuff'
  1134. ));
  1135. $this->assertTags($result, $expected);
  1136. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1137. $expected = array('input' => array(
  1138. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1139. 'value' => '0', 'id' => 'UserFormHidden'
  1140. ));
  1141. $this->assertTags($result, $expected);
  1142. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1143. $expected = array(
  1144. 'div' => array('class' => 'input checkbox'),
  1145. array('input' => array(
  1146. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1147. 'value' => '0', 'id' => 'UserFormSomething_'
  1148. )),
  1149. array('input' => array(
  1150. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1151. 'value' => '1', 'id' => 'UserFormSomething'
  1152. )),
  1153. 'label' => array('for' => 'UserFormSomething'),
  1154. 'Something',
  1155. '/label',
  1156. '/div'
  1157. );
  1158. $this->assertTags($result, $expected);
  1159. $result = $this->Form->fields;
  1160. $expected = array(
  1161. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1162. 'UserForm.hidden' => '0', 'UserForm.something'
  1163. );
  1164. $this->assertEquals($expected, $result);
  1165. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1166. $result = $this->Form->secure($this->Form->fields);
  1167. $expected = array(
  1168. 'div' => array('style' => 'display:none;'),
  1169. array('input' => array(
  1170. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1171. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1172. )),
  1173. array('input' => array(
  1174. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1175. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1176. )),
  1177. '/div'
  1178. );
  1179. $this->assertTags($result, $expected);
  1180. }
  1181. /**
  1182. * Tests that the correct keys are added to the field hash index
  1183. *
  1184. * @return void
  1185. */
  1186. public function testFormSecuredFileInput() {
  1187. $this->Form->request['_Token'] = array('key' => 'testKey');
  1188. $this->assertEquals(array(), $this->Form->fields);
  1189. $result = $this->Form->file('Attachment.file');
  1190. $expected = array(
  1191. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1192. 'Attachment.file.error', 'Attachment.file.size'
  1193. );
  1194. $this->assertEquals($expected, $this->Form->fields);
  1195. }
  1196. /**
  1197. * test that multiple selects keys are added to field hash
  1198. *
  1199. * @return void
  1200. */
  1201. public function testFormSecuredMultipleSelect() {
  1202. $this->Form->request['_Token'] = array('key' => 'testKey');
  1203. $this->assertEquals(array(), $this->Form->fields);
  1204. $options = array('1' => 'one', '2' => 'two');
  1205. $this->Form->select('Model.select', $options);
  1206. $expected = array('Model.select');
  1207. $this->assertEquals($expected, $this->Form->fields);
  1208. $this->Form->fields = array();
  1209. $this->Form->select('Model.select', $options, array('multiple' => true));
  1210. $this->assertEquals($expected, $this->Form->fields);
  1211. }
  1212. /**
  1213. * testFormSecuredRadio method
  1214. *
  1215. * @return void
  1216. */
  1217. public function testFormSecuredRadio() {
  1218. $this->Form->request['_Token'] = array('key' => 'testKey');
  1219. $this->assertEquals(array(), $this->Form->fields);
  1220. $options = array('1' => 'option1', '2' => 'option2');
  1221. $this->Form->radio('Test.test', $options);
  1222. $expected = array('Test.test');
  1223. $this->assertEquals($expected, $this->Form->fields);
  1224. }
  1225. /**
  1226. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1227. * hashing.
  1228. *
  1229. * @return void
  1230. */
  1231. public function testFormSecuredAndDisabled() {
  1232. $this->Form->request['_Token'] = array('key' => 'testKey');
  1233. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1234. $this->Form->text('Model.text', array('disabled' => true));
  1235. $this->Form->password('Model.text', array('disabled' => true));
  1236. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1237. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1238. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1239. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1240. $this->Form->month('Model.month', array('disabled' => true));
  1241. $this->Form->day('Model.day', array('disabled' => true));
  1242. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1243. $this->Form->minute('Model.minute', array('disabled' => true));
  1244. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1245. $expected = array(
  1246. 'Model.radio' => ''
  1247. );
  1248. $this->assertEquals($expected, $this->Form->fields);
  1249. }
  1250. /**
  1251. * testDisableSecurityUsingForm method
  1252. *
  1253. * @return void
  1254. */
  1255. public function testDisableSecurityUsingForm() {
  1256. $this->Form->request['_Token'] = array(
  1257. 'key' => 'testKey',
  1258. 'disabledFields' => array()
  1259. );
  1260. $this->Form->create();
  1261. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1262. $this->Form->input('Addresses.title');
  1263. $this->Form->input('Addresses.first_name', array('secure' => false));
  1264. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1265. $this->Form->input('Addresses.zip', array(
  1266. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1267. ));
  1268. $result = $this->Form->fields;
  1269. $expected = array(
  1270. 'Addresses.id' => '123456', 'Addresses.title',
  1271. );
  1272. $this->assertEquals($expected, $result);
  1273. }
  1274. /**
  1275. * test disableField
  1276. *
  1277. * @return void
  1278. */
  1279. public function testUnlockFieldAddsToList() {
  1280. $this->Form->request['_Token'] = array(
  1281. 'key' => 'testKey',
  1282. 'unlockedFields' => array()
  1283. );
  1284. $this->Form->create('Contact');
  1285. $this->Form->unlockField('Contact.name');
  1286. $this->Form->text('Contact.name');
  1287. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1288. $this->assertEquals(array(), $this->Form->fields);
  1289. }
  1290. /**
  1291. * test unlockField removing from fields array.
  1292. *
  1293. * @return void
  1294. */
  1295. public function testUnlockFieldRemovingFromFields() {
  1296. $this->Form->request['_Token'] = array(
  1297. 'key' => 'testKey',
  1298. 'unlockedFields' => array()
  1299. );
  1300. $this->Form->create('Contact');
  1301. $this->Form->hidden('Contact.id', array('value' => 1));
  1302. $this->Form->text('Contact.name');
  1303. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1304. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1305. $this->Form->unlockField('Contact.name');
  1306. $this->Form->unlockField('Contact.id');
  1307. $this->assertEquals(array(), $this->Form->fields);
  1308. }
  1309. /**
  1310. * testTagIsInvalid method
  1311. *
  1312. * @return void
  1313. */
  1314. public function testTagIsInvalid() {
  1315. $Contact = ClassRegistry::getObject('Contact');
  1316. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1317. $this->Form->setEntity('Contact.0.email');
  1318. $result = $this->Form->tagIsInvalid();
  1319. $expected = array('Please provide an email');
  1320. $this->assertEquals($expected, $result);
  1321. $this->Form->setEntity('Contact.1.email');
  1322. $result = $this->Form->tagIsInvalid();
  1323. $expected = false;
  1324. $this->assertSame($expected, $result);
  1325. $this->Form->setEntity('Contact.0.name');
  1326. $result = $this->Form->tagIsInvalid();
  1327. $expected = false;
  1328. $this->assertSame($expected, $result);
  1329. }
  1330. /**
  1331. * testPasswordValidation method
  1332. *
  1333. * test validation errors on password input.
  1334. *
  1335. * @return void
  1336. */
  1337. public function testPasswordValidation() {
  1338. $Contact = ClassRegistry::getObject('Contact');
  1339. $Contact->validationErrors['password'] = array('Please provide a password');
  1340. $result = $this->Form->input('Contact.password');
  1341. $expected = array(
  1342. 'div' => array('class' => 'input password error'),
  1343. 'label' => array('for' => 'ContactPassword'),
  1344. 'Password',
  1345. '/label',
  1346. 'input' => array(
  1347. 'type' => 'password', 'name' => 'data[Contact][password]',
  1348. 'id' => 'ContactPassword', 'class' => 'form-error'
  1349. ),
  1350. array('div' => array('class' => 'error-message')),
  1351. 'Please provide a password',
  1352. '/div',
  1353. '/div'
  1354. );
  1355. $this->assertTags($result, $expected);
  1356. }
  1357. /**
  1358. * testEmptyErrorValidation method
  1359. *
  1360. * test validation error div when validation message is an empty string
  1361. *
  1362. * @access public
  1363. * @return void
  1364. */
  1365. public function testEmptyErrorValidation() {
  1366. $this->Form->validationErrors['Contact']['password'] = '';
  1367. $result = $this->Form->input('Contact.password');
  1368. $expected = array(
  1369. 'div' => array('class' => 'input password error'),
  1370. 'label' => array('for' => 'ContactPassword'),
  1371. 'Password',
  1372. '/label',
  1373. 'input' => array(
  1374. 'type' => 'password', 'name' => 'data[Contact][password]',
  1375. 'id' => 'ContactPassword', 'class' => 'form-error'
  1376. ),
  1377. array('div' => array('class' => 'error-message')),
  1378. array(),
  1379. '/div',
  1380. '/div'
  1381. );
  1382. $this->assertTags($result, $expected);
  1383. }
  1384. /**
  1385. * testEmptyInputErrorValidation method
  1386. *
  1387. * test validation error div when validation message is overridden by an empty string when calling input()
  1388. *
  1389. * @access public
  1390. * @return void
  1391. */
  1392. public function testEmptyInputErrorValidation() {
  1393. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1394. $result = $this->Form->input('Contact.password', array('error' => ''));
  1395. $expected = array(
  1396. 'div' => array('class' => 'input password error'),
  1397. 'label' => array('for' => 'ContactPassword'),
  1398. 'Password',
  1399. '/label',
  1400. 'input' => array(
  1401. 'type' => 'password', 'name' => 'data[Contact][password]',
  1402. 'id' => 'ContactPassword', 'class' => 'form-error'
  1403. ),
  1404. array('div' => array('class' => 'error-message')),
  1405. array(),
  1406. '/div',
  1407. '/div'
  1408. );
  1409. $this->assertTags($result, $expected);
  1410. }
  1411. /**
  1412. * testFormValidationAssociated method
  1413. *
  1414. * test display of form errors in conjunction with model::validates.
  1415. *
  1416. * @return void
  1417. */
  1418. public function testFormValidationAssociated() {
  1419. $this->UserForm = ClassRegistry::getObject('UserForm');
  1420. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1421. $data = array(
  1422. 'UserForm' => array('name' => 'user'),
  1423. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1424. );
  1425. $result = $this->UserForm->OpenidUrl->create($data);
  1426. $this->assertFalse(empty($result));
  1427. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1428. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1429. $encoding = strtolower(Configure::read('App.encoding'));
  1430. $expected = array(
  1431. 'form' => array(
  1432. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1433. 'accept-charset' => $encoding
  1434. ),
  1435. 'div' => array('style' => 'display:none;'),
  1436. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1437. '/div'
  1438. );
  1439. $this->assertTags($result, $expected);
  1440. $result = $this->Form->error(
  1441. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1442. );
  1443. $this->assertEquals('Error, not registered', $result);
  1444. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1445. }
  1446. /**
  1447. * testFormValidationAssociatedFirstLevel method
  1448. *
  1449. * test form error display with associated model.
  1450. *
  1451. * @return void
  1452. */
  1453. public function testFormValidationAssociatedFirstLevel() {
  1454. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1455. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1456. $data = array(
  1457. 'ValidateUser' => array('name' => 'mariano'),
  1458. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1459. );
  1460. $result = $this->ValidateUser->create($data);
  1461. $this->assertFalse(empty($result));
  1462. $this->assertFalse($this->ValidateUser->validates());
  1463. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1464. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1465. $encoding = strtolower(Configure::read('App.encoding'));
  1466. $expected = array(
  1467. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1468. 'div' => array('style' => 'display:none;'),
  1469. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1470. '/div'
  1471. );
  1472. $this->assertTags($result, $expected);
  1473. $result = $this->Form->error(
  1474. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1475. );
  1476. $this->assertEquals('Invalid email', $result);
  1477. $result = $this->Form->error(
  1478. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1479. );
  1480. $this->assertEquals('Invalid name', $result);
  1481. $result = $this->Form->error(
  1482. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1483. );
  1484. $this->assertEquals('Invalid city', $result);
  1485. unset($this->ValidateUser->ValidateProfile);
  1486. unset($this->ValidateUser);
  1487. }
  1488. /**
  1489. * testFormValidationAssociatedSecondLevel method
  1490. *
  1491. * test form error display with associated model.
  1492. *
  1493. * @return void
  1494. */
  1495. public function testFormValidationAssociatedSecondLevel() {
  1496. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1497. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1498. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1499. $data = array(
  1500. 'ValidateUser' => array('name' => 'mariano'),
  1501. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1502. 'ValidateItem' => array('name' => 'Item')
  1503. );
  1504. $result = $this->ValidateUser->create($data);
  1505. $this->assertFalse(empty($result));
  1506. $this->assertFalse($this->ValidateUser->validates());
  1507. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1508. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1509. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1510. $encoding = strtolower(Configure::read('App.encoding'));
  1511. $expected = array(
  1512. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1513. 'div' => array('style' => 'display:none;'),
  1514. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1515. '/div'
  1516. );
  1517. $this->assertTags($result, $expected);
  1518. $result = $this->Form->error(
  1519. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1520. );
  1521. $this->assertEquals('Invalid email', $result);
  1522. $result = $this->Form->error(
  1523. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1524. );
  1525. $this->assertEquals('Invalid name', $result);
  1526. $result = $this->Form->error(
  1527. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1528. );
  1529. $result = $this->Form->error(
  1530. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1531. );
  1532. $this->assertEquals('Invalid description', $result);
  1533. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1534. unset($this->ValidateUser->ValidateProfile);
  1535. unset($this->ValidateUser);
  1536. }
  1537. /**
  1538. * testFormValidationMultiRecord method
  1539. *
  1540. * test form error display with multiple records.
  1541. *
  1542. * @return void
  1543. */
  1544. public function testFormValidationMultiRecord() {
  1545. $Contact = ClassRegistry::getObject('Contact');
  1546. $Contact->validationErrors[2] = array(
  1547. 'name' => array('This field cannot be left blank')
  1548. );
  1549. $result = $this->Form->input('Contact.2.name');
  1550. $expected = array(
  1551. 'div' => array('class' => 'input text error'),
  1552. 'label' => array('for' => 'Contact2Name'),
  1553. 'Name',
  1554. '/label',
  1555. 'input' => array(
  1556. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1557. 'class' => 'form-error', 'maxlength' => 255
  1558. ),
  1559. array('div' => array('class' => 'error-message')),
  1560. 'This field cannot be left blank',
  1561. '/div',
  1562. '/div'
  1563. );
  1564. $this->assertTags($result, $expected);
  1565. }
  1566. /**
  1567. * testMultipleInputValidation method
  1568. *
  1569. * test multiple record form validation error display.
  1570. *
  1571. * @return void
  1572. */
  1573. public function testMultipleInputValidation() {
  1574. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1575. $Address->validationErrors[0] = array(
  1576. 'title' => array('This field cannot be empty'),
  1577. 'first_name' => array('This field cannot be empty')
  1578. );
  1579. $Address->validationErrors[1] = array(
  1580. 'last_name' => array('You must have a last name')
  1581. );
  1582. $this->Form->create();
  1583. $result = $this->Form->input('Address.0.title');
  1584. $expected = array(
  1585. 'div' => array('class'),
  1586. 'label' => array('for'),
  1587. 'preg:/[^<]+/',
  1588. '/label',
  1589. 'input' => array(
  1590. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1591. ),
  1592. array('div' => array('class' => 'error-message')),
  1593. 'This field cannot be empty',
  1594. '/div',
  1595. '/div'
  1596. );
  1597. $this->assertTags($result, $expected);
  1598. $result = $this->Form->input('Address.0.first_name');
  1599. $expected = array(
  1600. 'div' => array('class'),
  1601. 'label' => array('for'),
  1602. 'preg:/[^<]+/',
  1603. '/label',
  1604. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1605. array('div' => array('class' => 'error-message')),
  1606. 'This field cannot be empty',
  1607. '/div',
  1608. '/div'
  1609. );
  1610. $this->assertTags($result, $expected);
  1611. $result = $this->Form->input('Address.0.last_name');
  1612. $expected = array(
  1613. 'div' => array('class'),
  1614. 'label' => array('for'),
  1615. 'preg:/[^<]+/',
  1616. '/label',
  1617. 'input' => array('type' => 'text', 'name', 'id'),
  1618. '/div'
  1619. );
  1620. $this->assertTags($result, $expected);
  1621. $result = $this->Form->input('Address.1.last_name');
  1622. $expected = array(
  1623. 'div' => array('class'),
  1624. 'label' => array('for'),
  1625. 'preg:/[^<]+/',
  1626. '/label',
  1627. 'input' => array(
  1628. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1629. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1630. ),
  1631. array('div' => array('class' => 'error-message')),
  1632. 'You must have a last name',
  1633. '/div',
  1634. '/div'
  1635. );
  1636. $this->assertTags($result, $expected);
  1637. }
  1638. /**
  1639. * testInput method
  1640. *
  1641. * Test various incarnations of input().
  1642. *
  1643. * @return void
  1644. */
  1645. public function testInput() {
  1646. $result = $this->Form->input('ValidateUser.balance');
  1647. $expected = array(
  1648. 'div' => array('class'),
  1649. 'label' => array('for'),
  1650. 'Balance',
  1651. '/label',
  1652. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1653. '/div',
  1654. );
  1655. $this->assertTags($result, $expected);
  1656. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1657. $expected = array(
  1658. 'div' => array('class' => 'input text'),
  1659. 'label' => array('for' => 'custom'),
  1660. 'Email',
  1661. '/label',
  1662. array('input' => array(
  1663. 'type' => 'text', 'name' => 'data[Contact][email]',
  1664. 'id' => 'custom', 'maxlength' => 255
  1665. )),
  1666. '/div'
  1667. );
  1668. $this->assertTags($result, $expected);
  1669. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1670. $expected = array(
  1671. '<div',
  1672. 'label' => array('for' => 'ContactEmail'),
  1673. 'Email',
  1674. '/label',
  1675. array('input' => array(
  1676. 'type' => 'text', 'name' => 'data[Contact][email]',
  1677. 'id' => 'ContactEmail', 'maxlength' => 255
  1678. )),
  1679. '/div'
  1680. );
  1681. $this->assertTags($result, $expected);
  1682. $result = $this->Form->hidden('Contact.idontexist');
  1683. $expected = array('input' => array(
  1684. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1685. 'id' => 'ContactIdontexist'
  1686. ));
  1687. $this->assertTags($result, $expected);
  1688. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1689. $expected = array(
  1690. 'div' => array('class' => 'input text'),
  1691. 'label' => array('for' => 'ContactEmail'),
  1692. 'Email',
  1693. '/label',
  1694. array('input' => array(
  1695. 'type' => 'text', 'name' => 'data[Contact][email]',
  1696. 'id' => 'ContactEmail'
  1697. )),
  1698. '/div'
  1699. );
  1700. $this->assertTags($result, $expected);
  1701. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1702. $expected = array(
  1703. 'div' => array('class' => 'input text'),
  1704. 'label' => array('for' => 'Contact5Email'),
  1705. 'Email',
  1706. '/label',
  1707. array('input' => array(
  1708. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1709. 'id' => 'Contact5Email'
  1710. )),
  1711. '/div'
  1712. );
  1713. $this->assertTags($result, $expected);
  1714. $result = $this->Form->input('Contact.password');
  1715. $expected = array(
  1716. 'div' => array('class' => 'input password'),
  1717. 'label' => array('for' => 'ContactPassword'),
  1718. 'Password',
  1719. '/label',
  1720. array('input' => array(
  1721. 'type' => 'password', 'name' => 'data[Contact][password]',
  1722. 'id' => 'ContactPassword'
  1723. )),
  1724. '/div'
  1725. );
  1726. $this->assertTags($result, $expected);
  1727. $result = $this->Form->input('Contact.email', array(
  1728. 'type' => 'file', 'class' => 'textbox'
  1729. ));
  1730. $expected = array(
  1731. 'div' => array('class' => 'input file'),
  1732. 'label' => array('for' => 'ContactEmail'),
  1733. 'Email',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1737. 'id' => 'ContactEmail'
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1743. $result = $this->Form->input('Contact.phone');
  1744. $expected = array(
  1745. 'div' => array('class' => 'input text'),
  1746. 'label' => array('for' => 'ContactPhone'),
  1747. 'Phone',
  1748. '/label',
  1749. array('input' => array(
  1750. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1751. 'value' => 'Hello &amp; World &gt; weird chars',
  1752. 'id' => 'ContactPhone', 'maxlength' => 255
  1753. )),
  1754. '/div'
  1755. );
  1756. $this->assertTags($result, $expected);
  1757. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1758. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1759. $expected = array(
  1760. 'div' => array('class' => 'input text'),
  1761. 'label' => array('for' => 'myId'),
  1762. 'Field',
  1763. '/label',
  1764. 'input' => array(
  1765. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1766. 'value' => 'My value', 'id' => 'myId'
  1767. ),
  1768. '/div'
  1769. );
  1770. $this->assertTags($result, $expected);
  1771. unset($this->Form->request->data);
  1772. $Contact = ClassRegistry::getObject('Contact');
  1773. $Contact->validationErrors['field'] = array('Badness!');
  1774. $result = $this->Form->input('Contact.field');
  1775. $expected = array(
  1776. 'div' => array('class' => 'input text error'),
  1777. 'label' => array('for' => 'ContactField'),
  1778. 'Field',
  1779. '/label',
  1780. 'input' => array(
  1781. 'type' => 'text', 'name' => 'data[Contact][field]',
  1782. 'id' => 'ContactField', 'class' => 'form-error'
  1783. ),
  1784. array('div' => array('class' => 'error-message')),
  1785. 'Badness!',
  1786. '/div',
  1787. '/div'
  1788. );
  1789. $this->assertTags($result, $expected);
  1790. $result = $this->Form->input('Contact.field', array(
  1791. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1792. ));
  1793. $expected = array(
  1794. 'label' => array('for' => 'ContactField'),
  1795. 'Field',
  1796. '/label',
  1797. 'input' => array(
  1798. 'type' => 'text', 'name' => 'data[Contact][field]',
  1799. 'id' => 'ContactField', 'class' => 'form-error'
  1800. ),
  1801. array('span' => array('class' => 'error-message')),
  1802. 'Badness!',
  1803. '/span'
  1804. );
  1805. $this->assertTags($result, $expected);
  1806. $result = $this->Form->input('Contact.field', array(
  1807. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1808. ));
  1809. $expected = array(
  1810. 'div' => array('class' => 'input text error'),
  1811. 'label' => array('for' => 'ContactField'),
  1812. 'Field',
  1813. '/label',
  1814. 'input' => array(
  1815. 'type' => 'text', 'name' => 'data[Contact][field]',
  1816. 'id' => 'ContactField', 'class' => 'form-error'
  1817. ),
  1818. array('div' => array('class' => 'error')),
  1819. 'Badness!',
  1820. '/div'
  1821. );
  1822. $this->assertTags($result, $expected);
  1823. $result = $this->Form->input('Contact.field', array(
  1824. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1825. ));
  1826. $expected = array(
  1827. 'span' => array('class' => 'input text error'),
  1828. 'label' => array('for' => 'ContactField'),
  1829. 'Field',
  1830. '/label',
  1831. 'input' => array(
  1832. 'type' => 'text', 'name' => 'data[Contact][field]',
  1833. 'id' => 'ContactField', 'class' => 'form-error'
  1834. ),
  1835. 'Badness!',
  1836. '/span'
  1837. );
  1838. $this->assertTags($result, $expected);
  1839. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1840. $expected = array(
  1841. 'div' => array('class' => 'input text error'),
  1842. 'label' => array('for' => 'ContactField'),
  1843. 'Field',
  1844. '/label',
  1845. 'input' => array(
  1846. 'type' => 'text', 'name' => 'data[Contact][field]',
  1847. 'id' => 'ContactField', 'class' => 'form-error'
  1848. ),
  1849. 'A message to you, Rudy',
  1850. array('div' => array('class' => 'error-message')),
  1851. 'Badness!',
  1852. '/div',
  1853. '/div'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. $this->Form->setEntity(null);
  1857. $this->Form->setEntity('Contact.field');
  1858. $result = $this->Form->input('Contact.field', array(
  1859. 'after' => 'A message to you, Rudy', 'error' => false
  1860. ));
  1861. $expected = array(
  1862. 'div' => array('class' => 'input text'),
  1863. 'label' => array('for' => 'ContactField'),
  1864. 'Field',
  1865. '/label',
  1866. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1867. 'A message to you, Rudy',
  1868. '/div'
  1869. );
  1870. $this->assertTags($result, $expected);
  1871. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1872. $expected = array(
  1873. 'div' => array('class' => 'input text'),
  1874. 'label' => array('for' => 'ObjectField'),
  1875. 'Field',
  1876. '/label',
  1877. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1878. 'A message to you, Rudy',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $Contact->validationErrors['field'] = array('minLength');
  1883. $result = $this->Form->input('Contact.field', array(
  1884. 'error' => array(
  1885. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1886. 'maxLength' => 'login too large'
  1887. )
  1888. ));
  1889. $expected = array(
  1890. 'div' => array('class' => 'input text error'),
  1891. 'label' => array('for' => 'ContactField'),
  1892. 'Field',
  1893. '/label',
  1894. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1895. array('div' => array('class' => 'error-message')),
  1896. 'Le login doit contenir au moins 2 caractères',
  1897. '/div',
  1898. '/div'
  1899. );
  1900. $this->assertTags($result, $expected);
  1901. $Contact->validationErrors['field'] = array('maxLength');
  1902. $result = $this->Form->input('Contact.field', array(
  1903. 'error' => array(
  1904. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1905. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1906. 'maxLength' => 'login too large',
  1907. )
  1908. ));
  1909. $expected = array(
  1910. 'div' => array('class' => 'input text error'),
  1911. 'label' => array('for' => 'ContactField'),
  1912. 'Field',
  1913. '/label',
  1914. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1915. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1916. 'login too large',
  1917. '/span',
  1918. '/div'
  1919. );
  1920. $this->assertTags($result, $expected);
  1921. }
  1922. /**
  1923. * test input() with checkbox creation
  1924. *
  1925. * @return void
  1926. */
  1927. public function testInputCheckbox() {
  1928. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1929. $expected = array(
  1930. 'div' => array('class' => 'input checkbox'),
  1931. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1932. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1933. '/div'
  1934. );
  1935. $this->assertTags($result, $expected);
  1936. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1937. $expected = array(
  1938. 'div' => array('class' => 'input checkbox'),
  1939. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1940. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1941. '/div'
  1942. );
  1943. $this->assertTags($result, $expected);
  1944. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1945. $expected = array(
  1946. 'div' => array('class' => 'input checkbox'),
  1947. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1948. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1949. '/div'
  1950. );
  1951. $this->assertTags($result, $expected);
  1952. }
  1953. /**
  1954. * test form->input() with time types.
  1955. *
  1956. */
  1957. public function testInputTime() {
  1958. extract($this->dateRegex);
  1959. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1960. $result = explode(':', $result);
  1961. $this->assertRegExp('/option value="23"/', $result[0]);
  1962. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1963. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1964. $result = explode(':', $result);
  1965. $this->assertRegExp('/option value="23"/', $result[0]);
  1966. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1967. $result = $this->Form->input('Model.field', array(
  1968. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1969. ));
  1970. $result = explode(':', $result);
  1971. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1972. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1973. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1974. $result = $this->Form->input('Model.field', array(
  1975. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1976. ));
  1977. $result = explode(':', $result);
  1978. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1979. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1980. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1981. $result = $this->Form->input('prueba', array(
  1982. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1983. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1984. ));
  1985. $result = explode(':', $result);
  1986. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1987. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1988. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1989. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1990. $result = $this->Form->input('Random.start_time', array(
  1991. 'type' => 'time',
  1992. 'selected' => '18:15'
  1993. ));
  1994. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  1995. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  1996. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1997. }
  1998. /**
  1999. * Test interval + selected near the hour roll over.
  2000. *
  2001. * @return void
  2002. */
  2003. public function testTimeSelectedWithInterval() {
  2004. $result = $this->Form->input('Model.start_time', array(
  2005. 'type' => 'time',
  2006. 'interval' => 15,
  2007. 'selected' => '2012-10-23 15:57:00'
  2008. ));
  2009. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2010. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2011. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2012. $result = $this->Form->input('Model.start_time', array(
  2013. 'timeFormat' => 24,
  2014. 'type' => 'time',
  2015. 'interval' => 15,
  2016. 'selected' => '15:57'
  2017. ));
  2018. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2019. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2020. $result = $this->Form->input('Model.start_time', array(
  2021. 'timeFormat' => 24,
  2022. 'type' => 'time',
  2023. 'interval' => 15,
  2024. 'selected' => '23:57'
  2025. ));
  2026. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2027. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2028. $result = $this->Form->input('Model.created', array(
  2029. 'timeFormat' => 24,
  2030. 'type' => 'datetime',
  2031. 'interval' => 15,
  2032. 'selected' => '2012-09-30 23:56'
  2033. ));
  2034. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2035. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2036. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2037. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2038. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2039. }
  2040. /**
  2041. * test form->input() with datetime, date and time types
  2042. *
  2043. * @return void
  2044. */
  2045. public function testInputDatetime() {
  2046. extract($this->dateRegex);
  2047. $result = $this->Form->input('prueba', array(
  2048. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2049. 'maxYear' => date('Y') + 1, 'interval' => 15
  2050. ));
  2051. $result = explode(':', $result);
  2052. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2053. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2054. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2055. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2056. //related to ticket #5013
  2057. $result = $this->Form->input('Contact.date', array(
  2058. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2059. ));
  2060. $this->assertRegExp('/class="customClass"/', $result);
  2061. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2062. $result = $this->Form->input('Contact.date', array(
  2063. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2064. ));
  2065. $this->assertRegExp('/id="customIdDay"/', $result);
  2066. $this->assertRegExp('/id="customIdMonth"/', $result);
  2067. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2068. $result = $this->Form->input('Model.field', array(
  2069. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2070. ));
  2071. $this->assertRegExp('/id="customIDDay"/', $result);
  2072. $this->assertRegExp('/id="customIDHour"/', $result);
  2073. $result = explode('</select><select', $result);
  2074. $result = explode(':', $result[1]);
  2075. $this->assertRegExp('/option value="23"/', $result[0]);
  2076. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2077. $result = $this->Form->input('Model.field', array(
  2078. 'type' => 'datetime', 'timeFormat' => 12
  2079. ));
  2080. $result = explode('</select><select', $result);
  2081. $result = explode(':', $result[1]);
  2082. $this->assertRegExp('/option value="12"/', $result[0]);
  2083. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2084. $this->Form->request->data = array('Contact' => array('created' => null));
  2085. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2086. $expected = array(
  2087. 'div' => array('class' => 'input date'),
  2088. 'label' => array('for' => 'ContactCreatedMonth'),
  2089. 'Created',
  2090. '/label',
  2091. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2092. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2093. $monthsRegex,
  2094. '/select', '-',
  2095. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2096. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2097. $daysRegex,
  2098. '/select', '-',
  2099. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2100. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2101. $yearsRegex,
  2102. '/select',
  2103. '/div'
  2104. );
  2105. $this->assertTags($result, $expected);
  2106. $this->Form->request->data = array('Contact' => array('created' => null));
  2107. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2108. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2109. $this->Form->request->data = array('Contact' => array('created' => null));
  2110. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2111. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2112. $result = $this->Form->input('Contact.created', array(
  2113. 'type' => 'date',
  2114. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2115. 'timeFormat' => 'NONE'
  2116. ));
  2117. $this->assertRegExp('/for\="created-month"/', $result);
  2118. }
  2119. /**
  2120. * Test generating checkboxes in a loop.
  2121. *
  2122. * @return void
  2123. */
  2124. public function testInputCheckboxesInLoop() {
  2125. for ($i = 1; $i < 5; $i++) {
  2126. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2127. $expected = array(
  2128. 'div' => array('class' => 'input checkbox'),
  2129. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2130. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2131. 'label' => array('for' => "Contact{$i}Email"),
  2132. 'Email',
  2133. '/label',
  2134. '/div'
  2135. );
  2136. $this->assertTags($result, $expected);
  2137. }
  2138. }
  2139. /**
  2140. * test input name with leading integer, ensure attributes are generated correctly.
  2141. *
  2142. * @return void
  2143. */
  2144. public function testInputWithLeadingInteger() {
  2145. $result = $this->Form->text('0.Node.title');
  2146. $expected = array(
  2147. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2148. );
  2149. $this->assertTags($result, $expected);
  2150. }
  2151. /**
  2152. * test form->input() with select type inputs.
  2153. *
  2154. * @return void
  2155. */
  2156. public function testInputSelectType() {
  2157. $result = $this->Form->input('email', array(
  2158. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2159. );
  2160. $expected = array(
  2161. 'div' => array('class' => 'input select'),
  2162. 'label' => array('for' => 'email'),
  2163. 'Email',
  2164. '/label',
  2165. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2166. array('option' => array('value' => '')),
  2167. '/option',
  2168. array('option' => array('value' => 'è')),
  2169. 'Firést',
  2170. '/option',
  2171. array('option' => array('value' => 'é')),
  2172. 'Secoènd',
  2173. '/option',
  2174. '/select',
  2175. '/div'
  2176. );
  2177. $this->assertTags($result, $expected);
  2178. $result = $this->Form->input('email', array(
  2179. 'options' => array('First', 'Second'), 'empty' => true)
  2180. );
  2181. $expected = array(
  2182. 'div' => array('class' => 'input select'),
  2183. 'label' => array('for' => 'email'),
  2184. 'Email',
  2185. '/label',
  2186. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2187. array('option' => array('value' => '')),
  2188. '/option',
  2189. array('option' => array('value' => '0')),
  2190. 'First',
  2191. '/option',
  2192. array('option' => array('value' => '1')),
  2193. 'Second',
  2194. '/option',
  2195. '/select',
  2196. '/div'
  2197. );
  2198. $this->assertTags($result, $expected);
  2199. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2200. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2201. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2202. $expected = array(
  2203. 'div' => array('class' => 'input select'),
  2204. 'label' => array('for' => 'ModelUserId'),
  2205. 'User',
  2206. '/label',
  2207. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2208. array('option' => array('value' => '')),
  2209. '/option',
  2210. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2211. 'good',
  2212. '/option',
  2213. array('option' => array('value' => 'other')),
  2214. 'bad',
  2215. '/option',
  2216. '/select',
  2217. '/div'
  2218. );
  2219. $this->assertTags($result, $expected);
  2220. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2221. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2222. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2223. $expected = array(
  2224. 'div' => array('class' => 'input select'),
  2225. 'label' => array('for' => 'ThingUserId'),
  2226. 'User',
  2227. '/label',
  2228. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2229. array('option' => array('value' => '')),
  2230. 'Some Empty',
  2231. '/option',
  2232. array('option' => array('value' => 'value')),
  2233. 'good',
  2234. '/option',
  2235. array('option' => array('value' => 'other')),
  2236. 'bad',
  2237. '/option',
  2238. '/select',
  2239. '/div'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2243. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2244. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input select'),
  2247. 'label' => array('for' => 'ThingUserId'),
  2248. 'User',
  2249. '/label',
  2250. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2251. array('option' => array('value' => '')),
  2252. 'Some Empty',
  2253. '/option',
  2254. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2255. 'good',
  2256. '/option',
  2257. array('option' => array('value' => 'other')),
  2258. 'bad',
  2259. '/option',
  2260. '/select',
  2261. '/div'
  2262. );
  2263. $this->assertTags($result, $expected);
  2264. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2265. $this->Form->request->data = array('User' => array('User' => array('value')));
  2266. $result = $this->Form->input('User.User', array('empty' => true));
  2267. $expected = array(
  2268. 'div' => array('class' => 'input select'),
  2269. 'label' => array('for' => 'UserUser'),
  2270. 'User',
  2271. '/label',
  2272. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2273. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2274. array('option' => array('value' => '')),
  2275. '/option',
  2276. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2277. 'good',
  2278. '/option',
  2279. array('option' => array('value' => 'other')),
  2280. 'bad',
  2281. '/option',
  2282. '/select',
  2283. '/div'
  2284. );
  2285. $this->assertTags($result, $expected);
  2286. $this->Form->data = array();
  2287. $result = $this->Form->input('Publisher.id', array(
  2288. 'label' => 'Publisher',
  2289. 'type' => 'select',
  2290. 'multiple' => 'checkbox',
  2291. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2292. ));
  2293. $expected = array(
  2294. array('div' => array('class' => 'input select')),
  2295. array('label' => array('for' => 'PublisherId')),
  2296. 'Publisher',
  2297. '/label',
  2298. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2299. array('div' => array('class' => 'checkbox')),
  2300. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2301. array('label' => array('for' => 'PublisherIdValue1')),
  2302. 'Label 1',
  2303. '/label',
  2304. '/div',
  2305. array('div' => array('class' => 'checkbox')),
  2306. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2307. array('label' => array('for' => 'PublisherIdValue2')),
  2308. 'Label 2',
  2309. '/label',
  2310. '/div',
  2311. '/div'
  2312. );
  2313. $this->assertTags($result, $expected);
  2314. }
  2315. /**
  2316. * test that input() and a non standard primary key makes a hidden input by default.
  2317. *
  2318. * @return void
  2319. */
  2320. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2321. $this->Form->create('User');
  2322. $this->Form->fieldset = array(
  2323. 'User' => array(
  2324. 'fields' => array(
  2325. 'model_id' => array('type' => 'integer')
  2326. ),
  2327. 'validates' => array(),
  2328. 'key' => 'model_id'
  2329. )
  2330. );
  2331. $result = $this->Form->input('model_id');
  2332. $expected = array(
  2333. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2334. );
  2335. $this->assertTags($result, $expected);
  2336. }
  2337. /**
  2338. * test that overriding the magic select type widget is possible
  2339. *
  2340. * @return void
  2341. */
  2342. public function testInputOverridingMagicSelectType() {
  2343. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2344. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2345. $expected = array(
  2346. 'div' => array('class' => 'input text'),
  2347. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2348. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2349. '/div'
  2350. );
  2351. $this->assertTags($result, $expected);
  2352. //Check that magic types still work for plural/singular vars
  2353. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2354. $result = $this->Form->input('Model.type');
  2355. $expected = array(
  2356. 'div' => array('class' => 'input select'),
  2357. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2358. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2359. array('option' => array('value' => 'value')), 'good', '/option',
  2360. array('option' => array('value' => 'other')), 'bad', '/option',
  2361. '/select',
  2362. '/div'
  2363. );
  2364. $this->assertTags($result, $expected);
  2365. }
  2366. /**
  2367. * Test that magic input() selects can easily be converted into radio types without error.
  2368. *
  2369. * @return void
  2370. */
  2371. public function testInputMagicSelectChangeToRadio() {
  2372. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2373. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2374. $this->assertRegExp('/input type="radio"/', $result);
  2375. }
  2376. /**
  2377. * fields with the same name as the model should work.
  2378. *
  2379. * @return void
  2380. */
  2381. public function testInputWithMatchingFieldAndModelName() {
  2382. $this->Form->create('User');
  2383. $this->Form->fieldset = array(
  2384. 'User' => array(
  2385. 'fields' => array(
  2386. 'User' => array('type' => 'text')
  2387. ),
  2388. 'validates' => array(),
  2389. 'key' => 'id'
  2390. )
  2391. );
  2392. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2393. $result = $this->Form->input('User', array('type' => 'text'));
  2394. $expected = array(
  2395. 'div' => array('class' => 'input text'),
  2396. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2397. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2398. '/div'
  2399. );
  2400. $this->assertTags($result, $expected);
  2401. }
  2402. /**
  2403. * testFormInputs method
  2404. *
  2405. * test correct results from form::inputs().
  2406. *
  2407. * @return void
  2408. */
  2409. public function testFormInputs() {
  2410. $this->Form->create('Contact');
  2411. $result = $this->Form->inputs('The Legend');
  2412. $expected = array(
  2413. '<fieldset',
  2414. '<legend',
  2415. 'The Legend',
  2416. '/legend',
  2417. '*/fieldset',
  2418. );
  2419. $this->assertTags($result, $expected);
  2420. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2421. $expected = array(
  2422. 'fieldset' => array('class' => 'classy-stuff'),
  2423. '<legend',
  2424. 'Field of Dreams',
  2425. '/legend',
  2426. '*/fieldset'
  2427. );
  2428. $this->assertTags($result, $expected);
  2429. $this->Form->create('Contact');
  2430. $this->Form->request['prefix'] = 'admin';
  2431. $this->Form->request['action'] = 'admin_edit';
  2432. $result = $this->Form->inputs();
  2433. $expected = array(
  2434. '<fieldset',
  2435. '<legend',
  2436. 'Edit Contact',
  2437. '/legend',
  2438. '*/fieldset',
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. $this->Form->create('Contact');
  2442. $result = $this->Form->inputs(false);
  2443. $expected = array(
  2444. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2445. array('div' => array('class' => 'input text')),
  2446. '*/div',
  2447. array('div' => array('class' => 'input text')),
  2448. '*/div',
  2449. array('div' => array('class' => 'input text')),
  2450. '*/div',
  2451. array('div' => array('class' => 'input password')),
  2452. '*/div',
  2453. array('div' => array('class' => 'input date')),
  2454. '*/div',
  2455. array('div' => array('class' => 'input date')),
  2456. '*/div',
  2457. array('div' => array('class' => 'input datetime')),
  2458. '*/div',
  2459. array('div' => array('class' => 'input number')),
  2460. '*/div',
  2461. array('div' => array('class' => 'input select')),
  2462. '*/div',
  2463. );
  2464. $this->assertTags($result, $expected);
  2465. $this->Form->create('Contact');
  2466. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2467. $expected = array(
  2468. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2469. array('div' => array('class' => 'input text')),
  2470. '*/div',
  2471. array('div' => array('class' => 'input text')),
  2472. '*/div',
  2473. array('div' => array('class' => 'input text')),
  2474. '*/div',
  2475. array('div' => array('class' => 'input password')),
  2476. '*/div',
  2477. array('div' => array('class' => 'input date')),
  2478. '*/div',
  2479. array('div' => array('class' => 'input date')),
  2480. '*/div',
  2481. array('div' => array('class' => 'input datetime')),
  2482. '*/div',
  2483. array('div' => array('class' => 'input number')),
  2484. '*/div',
  2485. array('div' => array('class' => 'input select')),
  2486. '*/div',
  2487. );
  2488. $this->assertTags($result, $expected);
  2489. $this->Form->create('Contact');
  2490. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2491. $expected = array(
  2492. 'fieldset' => array(),
  2493. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2494. array('div' => array('class' => 'input text')),
  2495. '*/div',
  2496. array('div' => array('class' => 'input text')),
  2497. '*/div',
  2498. array('div' => array('class' => 'input text')),
  2499. '*/div',
  2500. array('div' => array('class' => 'input password')),
  2501. '*/div',
  2502. array('div' => array('class' => 'input date')),
  2503. '*/div',
  2504. array('div' => array('class' => 'input date')),
  2505. '*/div',
  2506. array('div' => array('class' => 'input datetime')),
  2507. '*/div',
  2508. array('div' => array('class' => 'input number')),
  2509. '*/div',
  2510. array('div' => array('class' => 'input select')),
  2511. '*/div',
  2512. '/fieldset'
  2513. );
  2514. $this->assertTags($result, $expected);
  2515. $this->Form->create('Contact');
  2516. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2517. $expected = array(
  2518. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2519. array('div' => array('class' => 'input text')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input text')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input text')),
  2524. '*/div',
  2525. array('div' => array('class' => 'input password')),
  2526. '*/div',
  2527. array('div' => array('class' => 'input date')),
  2528. '*/div',
  2529. array('div' => array('class' => 'input date')),
  2530. '*/div',
  2531. array('div' => array('class' => 'input datetime')),
  2532. '*/div',
  2533. array('div' => array('class' => 'input number')),
  2534. '*/div',
  2535. array('div' => array('class' => 'input select')),
  2536. '*/div',
  2537. );
  2538. $this->assertTags($result, $expected);
  2539. $this->Form->create('Contact');
  2540. $result = $this->Form->inputs('Hello');
  2541. $expected = array(
  2542. 'fieldset' => array(),
  2543. 'legend' => array(),
  2544. 'Hello',
  2545. '/legend',
  2546. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2547. array('div' => array('class' => 'input text')),
  2548. '*/div',
  2549. array('div' => array('class' => 'input text')),
  2550. '*/div',
  2551. array('div' => array('class' => 'input text')),
  2552. '*/div',
  2553. array('div' => array('class' => 'input password')),
  2554. '*/div',
  2555. array('div' => array('class' => 'input date')),
  2556. '*/div',
  2557. array('div' => array('class' => 'input date')),
  2558. '*/div',
  2559. array('div' => array('class' => 'input datetime')),
  2560. '*/div',
  2561. array('div' => array('class' => 'input number')),
  2562. '*/div',
  2563. array('div' => array('class' => 'input select')),
  2564. '*/div',
  2565. '/fieldset'
  2566. );
  2567. $this->assertTags($result, $expected);
  2568. $this->Form->create('Contact');
  2569. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2570. $expected = array(
  2571. 'fieldset' => array(),
  2572. 'legend' => array(),
  2573. 'Hello',
  2574. '/legend',
  2575. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2576. array('div' => array('class' => 'input text')),
  2577. '*/div',
  2578. array('div' => array('class' => 'input text')),
  2579. '*/div',
  2580. array('div' => array('class' => 'input text')),
  2581. '*/div',
  2582. array('div' => array('class' => 'input password')),
  2583. '*/div',
  2584. array('div' => array('class' => 'input date')),
  2585. '*/div',
  2586. array('div' => array('class' => 'input date')),
  2587. '*/div',
  2588. array('div' => array('class' => 'input datetime')),
  2589. '*/div',
  2590. array('div' => array('class' => 'input number')),
  2591. '*/div',
  2592. array('div' => array('class' => 'input select')),
  2593. '*/div',
  2594. '/fieldset'
  2595. );
  2596. $this->assertTags($result, $expected);
  2597. }
  2598. /**
  2599. * testSelectAsCheckbox method
  2600. *
  2601. * test multi-select widget with checkbox formatting.
  2602. *
  2603. * @return void
  2604. */
  2605. public function testSelectAsCheckbox() {
  2606. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2607. $expected = array(
  2608. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2609. array('div' => array('class' => 'checkbox')),
  2610. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2611. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2612. 'first',
  2613. '/label',
  2614. '/div',
  2615. array('div' => array('class' => 'checkbox')),
  2616. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2617. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2618. 'second',
  2619. '/label',
  2620. '/div',
  2621. array('div' => array('class' => 'checkbox')),
  2622. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2623. array('label' => array('for' => 'ModelMultiField2')),
  2624. 'third',
  2625. '/label',
  2626. '/div',
  2627. );
  2628. $this->assertTags($result, $expected);
  2629. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2630. $expected = array(
  2631. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2632. array('div' => array('class' => 'checkbox')),
  2633. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2634. array('label' => array('for' => 'ModelMultiField12')),
  2635. 'half',
  2636. '/label',
  2637. '/div',
  2638. );
  2639. $this->assertTags($result, $expected);
  2640. }
  2641. /**
  2642. * testLabel method
  2643. *
  2644. * test label generation.
  2645. *
  2646. * @return void
  2647. */
  2648. public function testLabel() {
  2649. $this->Form->text('Person.name');
  2650. $result = $this->Form->label();
  2651. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2652. $this->Form->text('Person.name');
  2653. $result = $this->Form->label();
  2654. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2655. $result = $this->Form->label('Person.first_name');
  2656. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2657. $result = $this->Form->label('Person.first_name', 'Your first name');
  2658. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2659. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2660. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2661. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2662. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2663. $result = $this->Form->label('Person.first_name', '');
  2664. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2665. $result = $this->Form->label('Person.2.name', '');
  2666. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2667. }
  2668. /**
  2669. * testTextbox method
  2670. *
  2671. * test textbox element generation
  2672. *
  2673. * @return void
  2674. */
  2675. public function testTextbox() {
  2676. $result = $this->Form->text('Model.field');
  2677. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2678. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2679. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2680. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2681. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2682. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2683. $result = $this->Form->text('Model.text');
  2684. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2685. $Contact = ClassRegistry::getObject('Contact');
  2686. $Contact->validationErrors['text'] = array(true);
  2687. $this->Form->request->data['Contact']['text'] = 'test';
  2688. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2689. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2690. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2691. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2692. $expected = array(
  2693. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2694. );
  2695. $this->assertTags($result, $expected);
  2696. }
  2697. /**
  2698. * testDefaultValue method
  2699. *
  2700. * Test default value setting
  2701. *
  2702. * @return void
  2703. */
  2704. public function testDefaultValue() {
  2705. $this->Form->request->data['Model']['field'] = 'test';
  2706. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2707. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2708. unset($this->Form->request->data['Model']['field']);
  2709. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2710. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2711. }
  2712. /**
  2713. * testCheckboxDefaultValue method
  2714. *
  2715. * Test default value setting on checkbox() method
  2716. *
  2717. * @return void
  2718. */
  2719. public function testCheckboxDefaultValue() {
  2720. $this->Form->request->data['Model']['field'] = false;
  2721. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2722. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2723. unset($this->Form->request->data['Model']['field']);
  2724. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2725. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2726. $this->Form->request->data['Model']['field'] = true;
  2727. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2728. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2729. unset($this->Form->request->data['Model']['field']);
  2730. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2731. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2732. }
  2733. /**
  2734. * testError method
  2735. *
  2736. * Test field error generation
  2737. *
  2738. * @return void
  2739. */
  2740. public function testError() {
  2741. $Contact = ClassRegistry::getObject('Contact');
  2742. $Contact->validationErrors['field'] = array(1);
  2743. $result = $this->Form->error('Contact.field');
  2744. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2745. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2746. $this->assertEquals('Error in field Field', $result);
  2747. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2748. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2749. $this->assertEquals('This field contains invalid input', $result);
  2750. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2751. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2752. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2753. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2754. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2755. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2756. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2757. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2758. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2759. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2760. $this->assertEquals('<strong>Badness!</strong>', $result);
  2761. $Contact->validationErrors['field'] = array("email");
  2762. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2763. $expected = array(
  2764. 'div' => array('class' => 'field-error'),
  2765. 'No good!',
  2766. '/div'
  2767. );
  2768. $this->assertTags($result, $expected);
  2769. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2770. $result = $this->Form->error('Contact.field', array(
  2771. 'notEmpty' => 'Cannot be empty',
  2772. 'email' => 'No good!'
  2773. ));
  2774. $expected = array(
  2775. 'div' => array('class' => 'error-message'),
  2776. 'ul' => array(),
  2777. '<li', 'Cannot be empty', '/li',
  2778. '<li', 'No good!', '/li',
  2779. '<li', 'Something else', '/li',
  2780. '/ul',
  2781. '/div'
  2782. );
  2783. $this->assertTags($result, $expected);
  2784. /** Testing error messages list options **/
  2785. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2786. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2787. $expected = array(
  2788. 'div' => array('class' => 'error-message'),
  2789. 'ol' => array(),
  2790. '<li', 'notEmpty', '/li',
  2791. '<li', 'email', '/li',
  2792. '/ol',
  2793. '/div'
  2794. );
  2795. $this->assertTags($result, $expected);
  2796. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2797. $expected = array(
  2798. 'div' => array('class' => 'error-message'),
  2799. 'ol' => array(),
  2800. '<li', 'notEmpty', '/li',
  2801. '<li', 'email', '/li',
  2802. '/ol',
  2803. '/div'
  2804. );
  2805. $this->assertTags($result, $expected);
  2806. $result = $this->Form->error('Contact.field', null, array(
  2807. 'listOptions' => array(
  2808. 'class' => 'ul-class',
  2809. 'itemOptions' => array(
  2810. 'class' => 'li-class'
  2811. )
  2812. )
  2813. ));
  2814. $expected = array(
  2815. 'div' => array('class' => 'error-message'),
  2816. 'ul' => array('class' => 'ul-class'),
  2817. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2818. array('li' => array('class' => 'li-class')), 'email', '/li',
  2819. '/ul',
  2820. '/div'
  2821. );
  2822. $this->assertTags($result, $expected);
  2823. }
  2824. /**
  2825. * test error options when using form->input();
  2826. *
  2827. * @return void
  2828. */
  2829. public function testInputErrorEscape() {
  2830. $this->Form->create('ValidateProfile');
  2831. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2832. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2833. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2834. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2835. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2836. $this->assertRegExp('/required<br>/', $result);
  2837. }
  2838. /**
  2839. * testPassword method
  2840. *
  2841. * Test password element generation
  2842. *
  2843. * @return void
  2844. */
  2845. public function testPassword() {
  2846. $Contact = ClassRegistry::getObject('Contact');
  2847. $result = $this->Form->password('Contact.field');
  2848. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2849. $Contact->validationErrors['passwd'] = 1;
  2850. $this->Form->request->data['Contact']['passwd'] = 'test';
  2851. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2852. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2853. }
  2854. /**
  2855. * testRadio method
  2856. *
  2857. * Test radio element set generation
  2858. *
  2859. * @return void
  2860. */
  2861. public function testRadio() {
  2862. $result = $this->Form->radio('Model.field', array('option A'));
  2863. $expected = array(
  2864. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2865. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2866. 'label' => array('for' => 'ModelField0'),
  2867. 'option A',
  2868. '/label'
  2869. );
  2870. $this->assertTags($result, $expected);
  2871. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2872. $expected = array(
  2873. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2874. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2875. 'label' => array('for' => 'ModelField12'),
  2876. 'half',
  2877. '/label'
  2878. );
  2879. $this->assertTags($result, $expected);
  2880. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2881. $expected = array(
  2882. 'fieldset' => array(),
  2883. 'legend' => array(),
  2884. 'Field',
  2885. '/legend',
  2886. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2887. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2888. array('label' => array('for' => 'ModelField0')),
  2889. 'option A',
  2890. '/label',
  2891. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2892. array('label' => array('for' => 'ModelField1')),
  2893. 'option B',
  2894. '/label',
  2895. '/fieldset'
  2896. );
  2897. $this->assertTags($result, $expected);
  2898. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2899. $expected = array(
  2900. 'fieldset' => array(),
  2901. 'legend' => array(),
  2902. 'Field',
  2903. '/legend',
  2904. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2905. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2906. array('label' => array('for' => 'ModelField0')),
  2907. 'option A',
  2908. '/label',
  2909. 'br' => array(),
  2910. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2911. array('label' => array('for' => 'ModelField1')),
  2912. 'option B',
  2913. '/label',
  2914. '/fieldset'
  2915. );
  2916. $this->assertTags($result, $expected);
  2917. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2918. $expected = array(
  2919. 'div' => array('class' => 'input radio'),
  2920. 'fieldset' => array(),
  2921. 'legend' => array(),
  2922. 'Legend title',
  2923. '/legend',
  2924. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2925. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2926. array('label' => array('for' => 'NewsletterSubscribe0')),
  2927. 'Unsubscribe',
  2928. '/label',
  2929. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2930. array('label' => array('for' => 'NewsletterSubscribe1')),
  2931. 'Subscribe',
  2932. '/label',
  2933. '/fieldset',
  2934. '/div'
  2935. );
  2936. $this->assertTags($result, $expected);
  2937. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2938. $expected = array(
  2939. 'div' => array('class' => 'input radio'),
  2940. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2941. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2942. array('label' => array('for' => 'NewsletterSubscribe0')),
  2943. 'Unsubscribe',
  2944. '/label',
  2945. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2946. array('label' => array('for' => 'NewsletterSubscribe1')),
  2947. 'Subscribe',
  2948. '/label',
  2949. '/div'
  2950. );
  2951. $this->assertTags($result, $expected);
  2952. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2953. $expected = array(
  2954. 'div' => array('class' => 'input radio'),
  2955. 'fieldset' => array(),
  2956. 'legend' => array(),
  2957. 'Legend title',
  2958. '/legend',
  2959. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2960. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2961. 'Unsubscribe',
  2962. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2963. 'Subscribe',
  2964. '/fieldset',
  2965. '/div'
  2966. );
  2967. $this->assertTags($result, $expected);
  2968. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2969. $expected = array(
  2970. 'div' => array('class' => 'input radio'),
  2971. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2972. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2973. 'Unsubscribe',
  2974. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2975. 'Subscribe',
  2976. '/div'
  2977. );
  2978. $this->assertTags($result, $expected);
  2979. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2980. $expected = array(
  2981. 'div' => array('class' => 'input radio'),
  2982. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2983. 'Unsubscribe',
  2984. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  2985. 'Subscribe',
  2986. '/div'
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2990. $expected = array(
  2991. 'fieldset' => array(),
  2992. 'legend' => array(),
  2993. 'Gender',
  2994. '/legend',
  2995. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  2996. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  2997. array('label' => array('for' => 'EmployeeGenderMale')),
  2998. 'Male',
  2999. '/label',
  3000. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3001. array('label' => array('for' => 'EmployeeGenderFemale')),
  3002. 'Female',
  3003. '/label',
  3004. '/fieldset',
  3005. );
  3006. $this->assertTags($result, $expected);
  3007. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3008. $expected = array(
  3009. 'fieldset' => array(),
  3010. 'legend' => array(),
  3011. 'Gender',
  3012. '/legend',
  3013. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3014. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3015. array('label' => array('for' => 'OfficerGenderMale')),
  3016. 'Male',
  3017. '/label',
  3018. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3019. array('label' => array('for' => 'OfficerGenderFemale')),
  3020. 'Female',
  3021. '/label',
  3022. '/fieldset',
  3023. );
  3024. $this->assertTags($result, $expected);
  3025. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3026. $expected = array(
  3027. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3028. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3029. 'label' => array('for' => 'Contact1Imrequired0'),
  3030. 'option A',
  3031. '/label'
  3032. );
  3033. $this->assertTags($result, $expected);
  3034. $result = $this->Form->radio('Model.1.field', array('option A'));
  3035. $expected = array(
  3036. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3037. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3038. 'label' => array('for' => 'Model1Field0'),
  3039. 'option A',
  3040. '/label'
  3041. );
  3042. $this->assertTags($result, $expected);
  3043. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3044. $expected = array(
  3045. 'fieldset' => array(),
  3046. 'legend' => array(),
  3047. 'Field',
  3048. '/legend',
  3049. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3050. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3051. array('label' => array('for' => 'ModelField0')),
  3052. 'option A',
  3053. '/label',
  3054. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3055. array('label' => array('for' => 'ModelField1')),
  3056. 'option B',
  3057. '/label',
  3058. '/fieldset'
  3059. );
  3060. $this->assertTags($result, $expected);
  3061. $result = $this->Form->radio(
  3062. 'Model.field',
  3063. array('option A', 'option B'),
  3064. array('between' => 'I am between')
  3065. );
  3066. $expected = array(
  3067. 'fieldset' => array(),
  3068. 'legend' => array(),
  3069. 'Field',
  3070. '/legend',
  3071. 'I am between',
  3072. 'input' => array(
  3073. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3074. 'value' => '', 'id' => 'ModelField_'
  3075. ),
  3076. array('input' => array(
  3077. 'type' => 'radio', 'name' => 'data[Model][field]',
  3078. 'value' => '0', 'id' => 'ModelField0'
  3079. )),
  3080. array('label' => array('for' => 'ModelField0')),
  3081. 'option A',
  3082. '/label',
  3083. array('input' => array(
  3084. 'type' => 'radio', 'name' => 'data[Model][field]',
  3085. 'value' => '1', 'id' => 'ModelField1'
  3086. )),
  3087. array('label' => array('for' => 'ModelField1')),
  3088. 'option B',
  3089. '/label',
  3090. '/fieldset'
  3091. );
  3092. $this->assertTags($result, $expected);
  3093. }
  3094. /**
  3095. * Test that radios with a 0 value are selected under the correct conditions.
  3096. *
  3097. * @return void
  3098. */
  3099. public function testRadioOptionWithZeroValue() {
  3100. $expected = array(
  3101. 'fieldset' => array(),
  3102. 'legend' => array(),
  3103. 'Field',
  3104. '/legend',
  3105. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3106. array('label' => array('for' => 'ModelField1')),
  3107. 'Yes',
  3108. '/label',
  3109. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3110. array('label' => array('for' => 'ModelField0')),
  3111. 'No',
  3112. '/label',
  3113. '/fieldset'
  3114. );
  3115. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3116. $this->assertTags($result, $expected);
  3117. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3118. $this->assertTags($result, $expected);
  3119. $expected = array(
  3120. 'fieldset' => array(),
  3121. 'legend' => array(),
  3122. 'Field',
  3123. '/legend',
  3124. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3125. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3126. array('label' => array('for' => 'ModelField1')),
  3127. 'Yes',
  3128. '/label',
  3129. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3130. array('label' => array('for' => 'ModelField0')),
  3131. 'No',
  3132. '/label',
  3133. '/fieldset'
  3134. );
  3135. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3136. $this->assertTags($result, $expected);
  3137. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3138. $this->assertTags($result, $expected);
  3139. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3140. $this->assertTags($result, $expected);
  3141. }
  3142. /**
  3143. * test disabled radio options
  3144. *
  3145. * @return void
  3146. */
  3147. public function testRadioDisabled() {
  3148. $result = $this->Form->radio(
  3149. 'Model.field',
  3150. array('option A', 'option B'),
  3151. array('disabled' => array('option A'), 'value' => '0')
  3152. );
  3153. $expected = array(
  3154. 'fieldset' => array(),
  3155. 'legend' => array(),
  3156. 'Field',
  3157. '/legend',
  3158. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3159. array('label' => array('for' => 'ModelField0')),
  3160. 'option A',
  3161. '/label',
  3162. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3163. array('label' => array('for' => 'ModelField1')),
  3164. 'option B',
  3165. '/label',
  3166. '/fieldset'
  3167. );
  3168. $this->assertTags($result, $expected);
  3169. $result = $this->Form->radio(
  3170. 'Model.field',
  3171. array('option A', 'option B'),
  3172. array('disabled' => true, 'value' => '0')
  3173. );
  3174. $expected = array(
  3175. 'fieldset' => array(),
  3176. 'legend' => array(),
  3177. 'Field',
  3178. '/legend',
  3179. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3180. array('label' => array('for' => 'ModelField0')),
  3181. 'option A',
  3182. '/label',
  3183. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3184. array('label' => array('for' => 'ModelField1')),
  3185. 'option B',
  3186. '/label',
  3187. '/fieldset'
  3188. );
  3189. $this->assertTags($result, $expected);
  3190. $result = $this->Form->radio(
  3191. 'Model.field',
  3192. array('option A', 'option B'),
  3193. array('disabled' => 'disabled', 'value' => '0')
  3194. );
  3195. $expected = array(
  3196. 'fieldset' => array(),
  3197. 'legend' => array(),
  3198. 'Field',
  3199. '/legend',
  3200. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3201. array('label' => array('for' => 'ModelField0')),
  3202. 'option A',
  3203. '/label',
  3204. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3205. array('label' => array('for' => 'ModelField1')),
  3206. 'option B',
  3207. '/label',
  3208. '/fieldset'
  3209. );
  3210. $this->assertTags($result, $expected);
  3211. }
  3212. /**
  3213. * test disabling the hidden input for radio buttons
  3214. *
  3215. * @return void
  3216. */
  3217. public function testRadioHiddenInputDisabling() {
  3218. $result = $this->Form->input('Model.1.field', array(
  3219. 'type' => 'radio',
  3220. 'options' => array('option A'),
  3221. 'hiddenField' => false
  3222. )
  3223. );
  3224. $expected = array(
  3225. 'div' => array('class' => 'input radio'),
  3226. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3227. 'label' => array('for' => 'Model1Field0'),
  3228. 'option A',
  3229. '/label',
  3230. '/div'
  3231. );
  3232. $this->assertTags($result, $expected);
  3233. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3234. $expected = array(
  3235. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3236. 'label' => array('for' => 'Model1Field0'),
  3237. 'option A',
  3238. '/label'
  3239. );
  3240. $this->assertTags($result, $expected);
  3241. }
  3242. /**
  3243. * test adding an empty option for radio buttons
  3244. *
  3245. * @return void
  3246. */
  3247. public function testRadioAddEmptyOption() {
  3248. $result = $this->Form->input('Model.1.field', array(
  3249. 'type' => 'radio',
  3250. 'options' => array('option A'),
  3251. 'empty' => true,
  3252. 'hiddenField' => false
  3253. ));
  3254. $expected = array(
  3255. 'div' => array('class' => 'input radio'),
  3256. 'fieldset' => array(),
  3257. 'legend' => array(),
  3258. 'Field',
  3259. '/legend',
  3260. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3261. array('label' => array('for' => 'Model1Field')),
  3262. __('empty'),
  3263. '/label',
  3264. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3265. array('label' => array('for' => 'Model1Field0')),
  3266. 'option A',
  3267. '/label',
  3268. '/fieldset',
  3269. '/div'
  3270. );
  3271. $this->assertTags($result, $expected);
  3272. $result = $this->Form->input('Model.1.field', array(
  3273. 'type' => 'radio',
  3274. 'options' => array('option A'),
  3275. 'empty' => 'CustomEmptyLabel',
  3276. 'hiddenField' => false
  3277. ));
  3278. $expected = array(
  3279. 'div' => array('class' => 'input radio'),
  3280. 'fieldset' => array(),
  3281. 'legend' => array(),
  3282. 'Field',
  3283. '/legend',
  3284. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3285. array('label' => array('for' => 'Model1Field')),
  3286. 'CustomEmptyLabel',
  3287. '/label',
  3288. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3289. array('label' => array('for' => 'Model1Field0')),
  3290. 'option A',
  3291. '/label',
  3292. '/fieldset',
  3293. '/div'
  3294. );
  3295. $this->assertTags($result, $expected);
  3296. $result = $this->Form->input('Model.1.field', array(
  3297. 'type' => 'radio',
  3298. 'options' => array('option A'),
  3299. 'empty' => false,
  3300. 'hiddenField' => false
  3301. ));
  3302. $this->assertTextNotContains('"Model1Field"', $result);
  3303. }
  3304. /**
  3305. * testSelect method
  3306. *
  3307. * Test select element generation.
  3308. *
  3309. * @return void
  3310. */
  3311. public function testSelect() {
  3312. $result = $this->Form->select('Model.field', array());
  3313. $expected = array(
  3314. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3315. array('option' => array('value' => '')),
  3316. '/option',
  3317. '/select'
  3318. );
  3319. $this->assertTags($result, $expected);
  3320. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3321. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3322. $expected = array(
  3323. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3324. array('option' => array('value' => '')),
  3325. '/option',
  3326. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3327. 'good',
  3328. '/option',
  3329. array('option' => array('value' => 'other')),
  3330. 'bad',
  3331. '/option',
  3332. '/select'
  3333. );
  3334. $this->assertTags($result, $expected);
  3335. $this->Form->request->data = array();
  3336. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3337. $expected = array(
  3338. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3339. array('option' => array('value' => '')),
  3340. '/option',
  3341. array('option' => array('value' => 'value')),
  3342. 'good',
  3343. '/option',
  3344. array('option' => array('value' => 'other')),
  3345. 'bad',
  3346. '/option',
  3347. '/select'
  3348. );
  3349. $this->assertTags($result, $expected);
  3350. $result = $this->Form->select(
  3351. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3352. array('empty' => false)
  3353. );
  3354. $expected = array(
  3355. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3356. array('option' => array('value' => 'first')),
  3357. 'first &quot;html&quot; &lt;chars&gt;',
  3358. '/option',
  3359. array('option' => array('value' => 'second')),
  3360. 'value',
  3361. '/option',
  3362. '/select'
  3363. );
  3364. $this->assertTags($result, $expected);
  3365. $result = $this->Form->select(
  3366. 'Model.field',
  3367. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3368. array('escape' => false, 'empty' => false)
  3369. );
  3370. $expected = array(
  3371. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3372. array('option' => array('value' => 'first')),
  3373. 'first "html" <chars>',
  3374. '/option',
  3375. array('option' => array('value' => 'second')),
  3376. 'value',
  3377. '/option',
  3378. '/select'
  3379. );
  3380. $this->assertTags($result, $expected);
  3381. $options = array(
  3382. array('value' => 'first', 'name' => 'First'),
  3383. array('value' => 'first', 'name' => 'Another First'),
  3384. );
  3385. $result = $this->Form->select(
  3386. 'Model.field',
  3387. $options,
  3388. array('escape' => false, 'empty' => false)
  3389. );
  3390. $expected = array(
  3391. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3392. array('option' => array('value' => 'first')),
  3393. 'First',
  3394. '/option',
  3395. array('option' => array('value' => 'first')),
  3396. 'Another First',
  3397. '/option',
  3398. '/select'
  3399. );
  3400. $this->assertTags($result, $expected);
  3401. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3402. $result = $this->Form->select(
  3403. 'Model.contact_id',
  3404. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3405. array('escape' => false, 'empty' => 'pick something')
  3406. );
  3407. $expected = array(
  3408. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3409. array('option' => array('value' => '')), 'pick something', '/option',
  3410. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3411. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3412. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3413. '/select'
  3414. );
  3415. $this->assertTags($result, $expected);
  3416. $this->Form->request->data['Model']['field'] = 0;
  3417. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3418. $expected = array(
  3419. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3420. array('option' => array('value' => '')), '/option',
  3421. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3422. array('option' => array('value' => '1')), 'Yes', '/option',
  3423. '/select'
  3424. );
  3425. $this->assertTags($result, $expected);
  3426. }
  3427. /**
  3428. * test that select() with optiongroups listens to the escape param.
  3429. *
  3430. * @return void
  3431. */
  3432. public function testSelectOptionGroupEscaping() {
  3433. $options = array(
  3434. '>< Key' => array(
  3435. 1 => 'One',
  3436. 2 => 'Two'
  3437. )
  3438. );
  3439. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3440. $expected = array(
  3441. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3442. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3443. array('option' => array('value' => '1')), 'One', '/option',
  3444. array('option' => array('value' => '2')), 'Two', '/option',
  3445. '/optgroup',
  3446. '/select'
  3447. );
  3448. $this->assertTags($result, $expected);
  3449. $options = array(
  3450. '>< Key' => array(
  3451. 1 => 'One',
  3452. 2 => 'Two'
  3453. )
  3454. );
  3455. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3456. $expected = array(
  3457. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3458. 'optgroup' => array('label' => '>< Key'),
  3459. array('option' => array('value' => '1')), 'One', '/option',
  3460. array('option' => array('value' => '2')), 'Two', '/option',
  3461. '/optgroup',
  3462. '/select'
  3463. );
  3464. $this->assertTags($result, $expected);
  3465. }
  3466. /**
  3467. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3468. *
  3469. * @return void
  3470. */
  3471. public function testSelectWithNullAttributes() {
  3472. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3473. $expected = array(
  3474. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3475. array('option' => array('value' => '0')),
  3476. 'first',
  3477. '/option',
  3478. array('option' => array('value' => '1')),
  3479. 'second',
  3480. '/option',
  3481. '/select'
  3482. );
  3483. $this->assertTags($result, $expected);
  3484. }
  3485. /**
  3486. * testNestedSelect method
  3487. *
  3488. * test select element generation with optgroups
  3489. *
  3490. * @return void
  3491. */
  3492. public function testNestedSelect() {
  3493. $result = $this->Form->select(
  3494. 'Model.field',
  3495. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3496. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3497. )), array('empty' => false)
  3498. );
  3499. $expected = array(
  3500. 'select' => array('name' => 'data[Model][field]',
  3501. 'id' => 'ModelField'),
  3502. array('option' => array('value' => 1)),
  3503. 'One',
  3504. '/option',
  3505. array('option' => array('value' => 2)),
  3506. 'Two',
  3507. '/option',
  3508. array('optgroup' => array('label' => 'Three')),
  3509. array('option' => array('value' => 4)),
  3510. 'Four',
  3511. '/option',
  3512. array('option' => array('value' => 5)),
  3513. 'Five',
  3514. '/option',
  3515. '/optgroup',
  3516. '/select'
  3517. );
  3518. $this->assertTags($result, $expected);
  3519. $result = $this->Form->select(
  3520. 'Model.field',
  3521. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3522. array('showParents' => true, 'empty' => false)
  3523. );
  3524. $expected = array(
  3525. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3526. array('option' => array('value' => 1)),
  3527. 'One',
  3528. '/option',
  3529. array('option' => array('value' => 2)),
  3530. 'Two',
  3531. '/option',
  3532. array('optgroup' => array('label' => 'Three')),
  3533. array('option' => array('value' => 3)),
  3534. 'Three',
  3535. '/option',
  3536. array('option' => array('value' => 4)),
  3537. 'Four',
  3538. '/option',
  3539. '/optgroup',
  3540. '/select'
  3541. );
  3542. $this->assertTags($result, $expected);
  3543. }
  3544. /**
  3545. * testSelectMultiple method
  3546. *
  3547. * test generation of multiple select elements
  3548. *
  3549. * @return void
  3550. */
  3551. public function testSelectMultiple() {
  3552. $options = array('first', 'second', 'third');
  3553. $result = $this->Form->select(
  3554. 'Model.multi_field', $options, array('multiple' => true)
  3555. );
  3556. $expected = array(
  3557. 'input' => array(
  3558. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3559. ),
  3560. 'select' => array(
  3561. 'name' => 'data[Model][multi_field][]',
  3562. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3563. ),
  3564. array('option' => array('value' => '0')),
  3565. 'first',
  3566. '/option',
  3567. array('option' => array('value' => '1')),
  3568. 'second',
  3569. '/option',
  3570. array('option' => array('value' => '2')),
  3571. 'third',
  3572. '/option',
  3573. '/select'
  3574. );
  3575. $this->assertTags($result, $expected);
  3576. $result = $this->Form->select(
  3577. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3578. );
  3579. $expected = array(
  3580. 'input' => array(
  3581. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3582. ),
  3583. 'select' => array(
  3584. 'name' => 'data[Model][multi_field][]',
  3585. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3586. ),
  3587. array('option' => array('value' => '0')),
  3588. 'first',
  3589. '/option',
  3590. array('option' => array('value' => '1')),
  3591. 'second',
  3592. '/option',
  3593. array('option' => array('value' => '2')),
  3594. 'third',
  3595. '/option',
  3596. '/select'
  3597. );
  3598. $this->assertTags($result, $expected);
  3599. $result = $this->Form->select(
  3600. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3601. );
  3602. $expected = array(
  3603. 'input' => array(
  3604. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3605. ),
  3606. 'select' => array(
  3607. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3608. 'multiple' => 'multiple'
  3609. ),
  3610. array('option' => array('value' => '0', 'selected' => 'selected')),
  3611. 'first',
  3612. '/option',
  3613. array('option' => array('value' => '1', 'selected' => 'selected')),
  3614. 'second',
  3615. '/option',
  3616. array('option' => array('value' => '2')),
  3617. 'third',
  3618. '/option',
  3619. '/select'
  3620. );
  3621. $this->assertTags($result, $expected);
  3622. $result = $this->Form->select(
  3623. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3624. );
  3625. $expected = array(
  3626. 'select' => array(
  3627. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3628. ),
  3629. array('option' => array('value' => '0', 'selected' => 'selected')),
  3630. 'first',
  3631. '/option',
  3632. array('option' => array('value' => '1', 'selected' => 'selected')),
  3633. 'second',
  3634. '/option',
  3635. array('option' => array('value' => '2')),
  3636. 'third',
  3637. '/option',
  3638. '/select'
  3639. );
  3640. $this->assertTags($result, $expected);
  3641. }
  3642. /**
  3643. * test generation of habtm select boxes.
  3644. *
  3645. * @return void
  3646. */
  3647. public function testHabtmSelectBox() {
  3648. $this->View->viewVars['contactTags'] = array(
  3649. 1 => 'blue',
  3650. 2 => 'red',
  3651. 3 => 'green'
  3652. );
  3653. $this->Form->request->data = array(
  3654. 'Contact' => array(),
  3655. 'ContactTag' => array(
  3656. array(
  3657. 'id' => 1,
  3658. 'name' => 'blue'
  3659. ),
  3660. array(
  3661. 'id' => 3,
  3662. 'name' => 'green'
  3663. )
  3664. )
  3665. );
  3666. $this->Form->create('Contact');
  3667. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3668. $expected = array(
  3669. 'input' => array(
  3670. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3671. ),
  3672. 'select' => array(
  3673. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3674. 'multiple' => 'multiple'
  3675. ),
  3676. array('option' => array('value' => '1', 'selected' => 'selected')),
  3677. 'blue',
  3678. '/option',
  3679. array('option' => array('value' => '2')),
  3680. 'red',
  3681. '/option',
  3682. array('option' => array('value' => '3', 'selected' => 'selected')),
  3683. 'green',
  3684. '/option',
  3685. '/select'
  3686. );
  3687. $this->assertTags($result, $expected);
  3688. }
  3689. /**
  3690. * test generation of multi select elements in checkbox format
  3691. *
  3692. * @return void
  3693. */
  3694. public function testSelectMultipleCheckboxes() {
  3695. $result = $this->Form->select(
  3696. 'Model.multi_field',
  3697. array('first', 'second', 'third'),
  3698. array('multiple' => 'checkbox')
  3699. );
  3700. $expected = array(
  3701. 'input' => array(
  3702. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3703. ),
  3704. array('div' => array('class' => 'checkbox')),
  3705. array('input' => array(
  3706. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3707. 'value' => '0', 'id' => 'ModelMultiField0'
  3708. )),
  3709. array('label' => array('for' => 'ModelMultiField0')),
  3710. 'first',
  3711. '/label',
  3712. '/div',
  3713. array('div' => array('class' => 'checkbox')),
  3714. array('input' => array(
  3715. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3716. 'value' => '1', 'id' => 'ModelMultiField1'
  3717. )),
  3718. array('label' => array('for' => 'ModelMultiField1')),
  3719. 'second',
  3720. '/label',
  3721. '/div',
  3722. array('div' => array('class' => 'checkbox')),
  3723. array('input' => array(
  3724. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3725. 'value' => '2', 'id' => 'ModelMultiField2'
  3726. )),
  3727. array('label' => array('for' => 'ModelMultiField2')),
  3728. 'third',
  3729. '/label',
  3730. '/div'
  3731. );
  3732. $this->assertTags($result, $expected);
  3733. $result = $this->Form->select(
  3734. 'Model.multi_field',
  3735. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3736. array('multiple' => 'checkbox')
  3737. );
  3738. $expected = array(
  3739. 'input' => array(
  3740. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3741. ),
  3742. array('div' => array('class' => 'checkbox')),
  3743. array('input' => array(
  3744. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3745. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3746. )),
  3747. array('label' => array('for' => 'ModelMultiFieldA')),
  3748. 'first',
  3749. '/label',
  3750. '/div',
  3751. array('div' => array('class' => 'checkbox')),
  3752. array('input' => array(
  3753. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3754. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3755. )),
  3756. array('label' => array('for' => 'ModelMultiFieldB')),
  3757. 'second',
  3758. '/label',
  3759. '/div',
  3760. array('div' => array('class' => 'checkbox')),
  3761. array('input' => array(
  3762. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3763. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3764. )),
  3765. array('label' => array('for' => 'ModelMultiFieldC')),
  3766. 'third',
  3767. '/label',
  3768. '/div'
  3769. );
  3770. $this->assertTags($result, $expected);
  3771. $result = $this->Form->select(
  3772. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3773. );
  3774. $expected = array(
  3775. 'input' => array(
  3776. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3777. ),
  3778. array('div' => array('class' => 'checkbox')),
  3779. array('input' => array(
  3780. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3781. 'value' => '1', 'id' => 'ModelMultiField1'
  3782. )),
  3783. array('label' => array('for' => 'ModelMultiField1')),
  3784. 'first',
  3785. '/label',
  3786. '/div'
  3787. );
  3788. $this->assertTags($result, $expected);
  3789. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3790. $result = $this->Form->select(
  3791. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3792. );
  3793. $expected = array(
  3794. 'input' => array(
  3795. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3796. ),
  3797. array('div' => array('class' => 'checkbox')),
  3798. array('input' => array(
  3799. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3800. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3801. )),
  3802. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3803. 'first',
  3804. '/label',
  3805. '/div',
  3806. array('div' => array('class' => 'checkbox')),
  3807. array('input' => array(
  3808. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3809. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3810. )),
  3811. array('label' => array('for' => 'ModelTagsArray')),
  3812. 'Array',
  3813. '/label',
  3814. '/div'
  3815. );
  3816. $this->assertTags($result, $expected);
  3817. }
  3818. /**
  3819. * test multiple checkboxes with div styles.
  3820. *
  3821. * @return void
  3822. */
  3823. public function testSelectMultipleCheckboxDiv() {
  3824. $result = $this->Form->select(
  3825. 'Model.tags',
  3826. array('first', 'second'),
  3827. array('multiple' => 'checkbox', 'class' => 'my-class')
  3828. );
  3829. $expected = array(
  3830. 'input' => array(
  3831. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3832. ),
  3833. array('div' => array('class' => 'my-class')),
  3834. array('input' => array(
  3835. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3836. 'value' => '0', 'id' => 'ModelTags0'
  3837. )),
  3838. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3839. '/div',
  3840. array('div' => array('class' => 'my-class')),
  3841. array('input' => array(
  3842. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3843. 'value' => '1', 'id' => 'ModelTags1'
  3844. )),
  3845. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3846. '/div'
  3847. );
  3848. $this->assertTags($result, $expected);
  3849. $result = $this->Form->input('Model.tags', array(
  3850. 'options' => array('first', 'second'),
  3851. 'multiple' => 'checkbox',
  3852. 'class' => 'my-class',
  3853. 'div' => false,
  3854. 'label' => false
  3855. ));
  3856. $this->assertTags($result, $expected);
  3857. $Contact = ClassRegistry::getObject('Contact');
  3858. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3859. $result = $this->Form->input('Contact.tags', array(
  3860. 'options' => array('one'),
  3861. 'multiple' => 'checkbox',
  3862. 'label' => false,
  3863. 'div' => false
  3864. ));
  3865. $expected = array(
  3866. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3867. array('div' => array('class' => 'checkbox form-error')),
  3868. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3869. array('label' => array('for' => 'ContactTags0')),
  3870. 'one',
  3871. '/label',
  3872. '/div'
  3873. );
  3874. $this->assertTags($result, $expected);
  3875. $result = $this->Form->input('Contact.tags', array(
  3876. 'options' => array('one'),
  3877. 'multiple' => 'checkbox',
  3878. 'class' => 'mycheckbox',
  3879. 'label' => false,
  3880. 'div' => false
  3881. ));
  3882. $expected = array(
  3883. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3884. array('div' => array('class' => 'mycheckbox form-error')),
  3885. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3886. array('label' => array('for' => 'ContactTags0')),
  3887. 'one',
  3888. '/label',
  3889. '/div'
  3890. );
  3891. $this->assertTags($result, $expected);
  3892. }
  3893. /**
  3894. * Checks the security hash array generated for multiple-input checkbox elements
  3895. *
  3896. * @return void
  3897. */
  3898. public function testSelectMultipleCheckboxSecurity() {
  3899. $this->Form->request['_Token'] = array('key' => 'testKey');
  3900. $this->assertEquals(array(), $this->Form->fields);
  3901. $result = $this->Form->select(
  3902. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3903. array('multiple' => 'checkbox')
  3904. );
  3905. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3906. $result = $this->Form->secure($this->Form->fields);
  3907. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3908. $this->assertRegExp('/"' . $key . '"/', $result);
  3909. }
  3910. /**
  3911. * Multiple select elements should always be secured as they always participate
  3912. * in the POST data.
  3913. *
  3914. * @return void
  3915. */
  3916. public function testSelectMultipleSecureWithNoOptions() {
  3917. $this->Form->request['_Token'] = array('key' => 'testkey');
  3918. $this->assertEquals(array(), $this->Form->fields);
  3919. $result = $this->Form->select(
  3920. 'Model.select',
  3921. array(),
  3922. array('multiple' => true)
  3923. );
  3924. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3925. }
  3926. /**
  3927. * When a select box has no options it should not be added to the fields list
  3928. * as it always fail post validation.
  3929. *
  3930. * @return void
  3931. */
  3932. public function testSelectNoSecureWithNoOptions() {
  3933. $this->Form->request['_Token'] = array('key' => 'testkey');
  3934. $this->assertEquals(array(), $this->Form->fields);
  3935. $this->Form->select(
  3936. 'Model.select',
  3937. array()
  3938. );
  3939. $this->assertEquals(array(), $this->Form->fields);
  3940. $this->Form->select(
  3941. 'Model.select',
  3942. array(),
  3943. array('empty' => true)
  3944. );
  3945. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3946. }
  3947. /**
  3948. * testInputMultipleCheckboxes method
  3949. *
  3950. * test input() resulting in multi select elements being generated.
  3951. *
  3952. * @return void
  3953. */
  3954. public function testInputMultipleCheckboxes() {
  3955. $result = $this->Form->input('Model.multi_field', array(
  3956. 'options' => array('first', 'second', 'third'),
  3957. 'multiple' => 'checkbox'
  3958. ));
  3959. $expected = array(
  3960. array('div' => array('class' => 'input select')),
  3961. array('label' => array('for' => 'ModelMultiField')),
  3962. 'Multi Field',
  3963. '/label',
  3964. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3965. array('div' => array('class' => 'checkbox')),
  3966. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3967. array('label' => array('for' => 'ModelMultiField0')),
  3968. 'first',
  3969. '/label',
  3970. '/div',
  3971. array('div' => array('class' => 'checkbox')),
  3972. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3973. array('label' => array('for' => 'ModelMultiField1')),
  3974. 'second',
  3975. '/label',
  3976. '/div',
  3977. array('div' => array('class' => 'checkbox')),
  3978. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3979. array('label' => array('for' => 'ModelMultiField2')),
  3980. 'third',
  3981. '/label',
  3982. '/div',
  3983. '/div'
  3984. );
  3985. $this->assertTags($result, $expected);
  3986. $result = $this->Form->input('Model.multi_field', array(
  3987. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3988. 'multiple' => 'checkbox'
  3989. ));
  3990. $expected = array(
  3991. array('div' => array('class' => 'input select')),
  3992. array('label' => array('for' => 'ModelMultiField')),
  3993. 'Multi Field',
  3994. '/label',
  3995. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3996. array('div' => array('class' => 'checkbox')),
  3997. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3998. array('label' => array('for' => 'ModelMultiFieldA')),
  3999. 'first',
  4000. '/label',
  4001. '/div',
  4002. array('div' => array('class' => 'checkbox')),
  4003. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4004. array('label' => array('for' => 'ModelMultiFieldB')),
  4005. 'second',
  4006. '/label',
  4007. '/div',
  4008. array('div' => array('class' => 'checkbox')),
  4009. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4010. array('label' => array('for' => 'ModelMultiFieldC')),
  4011. 'third',
  4012. '/label',
  4013. '/div',
  4014. '/div'
  4015. );
  4016. $this->assertTags($result, $expected);
  4017. $result = $this->Form->input('Model.multi_field', array(
  4018. 'options' => array('1' => 'first'),
  4019. 'multiple' => 'checkbox',
  4020. 'label' => false,
  4021. 'div' => false
  4022. ));
  4023. $expected = array(
  4024. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4025. array('div' => array('class' => 'checkbox')),
  4026. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4027. array('label' => array('for' => 'ModelMultiField1')),
  4028. 'first',
  4029. '/label',
  4030. '/div'
  4031. );
  4032. $this->assertTags($result, $expected);
  4033. $result = $this->Form->input('Model.multi_field', array(
  4034. 'options' => array('2' => 'second'),
  4035. 'multiple' => 'checkbox',
  4036. 'label' => false,
  4037. 'div' => false
  4038. ));
  4039. $expected = array(
  4040. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4041. array('div' => array('class' => 'checkbox')),
  4042. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4043. array('label' => array('for' => 'ModelMultiField2')),
  4044. 'second',
  4045. '/label',
  4046. '/div'
  4047. );
  4048. $this->assertTags($result, $expected);
  4049. }
  4050. /**
  4051. * testSelectHiddenFieldOmission method
  4052. *
  4053. * test that select() with 'hiddenField' => false omits the hidden field
  4054. *
  4055. * @return void
  4056. */
  4057. public function testSelectHiddenFieldOmission() {
  4058. $result = $this->Form->select('Model.multi_field',
  4059. array('first', 'second'),
  4060. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4061. );
  4062. $expected = array(
  4063. array('div' => array('class' => 'checkbox')),
  4064. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4065. array('label' => array('for' => 'ModelMultiField0')),
  4066. 'first',
  4067. '/label',
  4068. '/div',
  4069. array('div' => array('class' => 'checkbox')),
  4070. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4071. array('label' => array('for' => 'ModelMultiField1')),
  4072. 'second',
  4073. '/label',
  4074. '/div'
  4075. );
  4076. $this->assertTags($result, $expected);
  4077. $result = $this->Form->input('Model.multi_field', array(
  4078. 'options' => array('first', 'second'),
  4079. 'multiple' => 'checkbox',
  4080. 'hiddenField' => false
  4081. ));
  4082. $expected = array(
  4083. array('div' => array('class' => 'input select')),
  4084. array('label' => array('for' => 'ModelMultiField')),
  4085. 'Multi Field',
  4086. '/label',
  4087. array('div' => array('class' => 'checkbox')),
  4088. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4089. array('label' => array('for' => 'ModelMultiField0')),
  4090. 'first',
  4091. '/label',
  4092. '/div',
  4093. array('div' => array('class' => 'checkbox')),
  4094. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4095. array('label' => array('for' => 'ModelMultiField1')),
  4096. 'second',
  4097. '/label',
  4098. '/div',
  4099. '/div'
  4100. );
  4101. $this->assertTags($result, $expected);
  4102. }
  4103. /**
  4104. * test that select() with multiple = checkbox works with overriding name attribute.
  4105. *
  4106. * @return void
  4107. */
  4108. public function testSelectCheckboxMultipleOverrideName() {
  4109. $result = $this->Form->input('category', array(
  4110. 'type' => 'select',
  4111. 'multiple' => 'checkbox',
  4112. 'name' => 'data[fish]',
  4113. 'options' => array('1', '2'),
  4114. 'div' => false,
  4115. 'label' => false,
  4116. ));
  4117. $expected = array(
  4118. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4119. array('div' => array('class' => 'checkbox')),
  4120. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4121. array('label' => array('for' => 'Category0')), '1', '/label',
  4122. '/div',
  4123. array('div' => array('class' => 'checkbox')),
  4124. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4125. array('label' => array('for' => 'Category1')), '2', '/label',
  4126. '/div'
  4127. );
  4128. $this->assertTags($result, $expected);
  4129. }
  4130. /**
  4131. * Test that 'id' overrides all the checkbox id's as well.
  4132. *
  4133. * @return void
  4134. */
  4135. public function testSelectCheckboxMultipleId() {
  4136. $result = $this->Form->select(
  4137. 'Model.multi_field',
  4138. array('first', 'second', 'third'),
  4139. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4140. );
  4141. $expected = array(
  4142. 'input' => array(
  4143. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4144. ),
  4145. array('div' => array('class' => 'checkbox')),
  4146. array('input' => array(
  4147. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4148. 'value' => '0', 'id' => 'CustomId0'
  4149. )),
  4150. array('label' => array('for' => 'CustomId0')),
  4151. 'first',
  4152. '/label',
  4153. '/div',
  4154. array('div' => array('class' => 'checkbox')),
  4155. array('input' => array(
  4156. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4157. 'value' => '1', 'id' => 'CustomId1'
  4158. )),
  4159. array('label' => array('for' => 'CustomId1')),
  4160. 'second',
  4161. '/label',
  4162. '/div',
  4163. array('div' => array('class' => 'checkbox')),
  4164. array('input' => array(
  4165. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4166. 'value' => '2', 'id' => 'CustomId2'
  4167. )),
  4168. array('label' => array('for' => 'CustomId2')),
  4169. 'third',
  4170. '/label',
  4171. '/div'
  4172. );
  4173. $this->assertTags($result, $expected);
  4174. }
  4175. /**
  4176. * testCheckbox method
  4177. *
  4178. * Test generation of checkboxes
  4179. *
  4180. * @return void
  4181. */
  4182. public function testCheckbox() {
  4183. $result = $this->Form->checkbox('Model.field');
  4184. $expected = array(
  4185. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4186. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4187. );
  4188. $this->assertTags($result, $expected);
  4189. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4190. $expected = array(
  4191. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4192. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4193. );
  4194. $this->assertTags($result, $expected);
  4195. $Contact = ClassRegistry::getObject('Contact');
  4196. $Contact->validationErrors['field'] = 1;
  4197. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4198. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4199. $expected = array(
  4200. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4201. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4202. );
  4203. $this->assertTags($result, $expected);
  4204. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4205. $expected = array(
  4206. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4207. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4208. );
  4209. $this->assertTags($result, $expected);
  4210. $this->Form->request->data['Contact']['field'] = '';
  4211. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4212. $expected = array(
  4213. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4214. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4215. );
  4216. $this->assertTags($result, $expected);
  4217. $Contact->validationErrors = array();
  4218. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4219. $expected = array(
  4220. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4221. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4222. );
  4223. $this->assertTags($result, $expected);
  4224. $this->Form->request->data['Contact']['published'] = 1;
  4225. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4226. $expected = array(
  4227. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4228. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4229. );
  4230. $this->assertTags($result, $expected);
  4231. $this->Form->request->data['Contact']['published'] = 0;
  4232. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4233. $expected = array(
  4234. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4235. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4236. );
  4237. $this->assertTags($result, $expected);
  4238. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4239. $expected = array(
  4240. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4241. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4242. );
  4243. $this->assertTags($result, $expected);
  4244. $result = $this->Form->checkbox('CustomField.1.value');
  4245. $expected = array(
  4246. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4247. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4248. );
  4249. $this->assertTags($result, $expected);
  4250. }
  4251. /**
  4252. * test checkbox() with a custom name attribute
  4253. *
  4254. * @return void
  4255. */
  4256. public function testCheckboxCustomNameAttribute() {
  4257. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4258. $expected = array(
  4259. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4260. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4261. );
  4262. $this->assertTags($result, $expected);
  4263. }
  4264. /**
  4265. * test the checked option for checkboxes.
  4266. *
  4267. * @return void
  4268. */
  4269. public function testCheckboxCheckedOption() {
  4270. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4271. $expected = array(
  4272. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4273. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4274. );
  4275. $this->assertTags($result, $expected);
  4276. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4277. $expected = array(
  4278. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4279. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4280. );
  4281. $this->assertTags($result, $expected);
  4282. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4283. $expected = array(
  4284. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4285. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4286. );
  4287. $this->assertTags($result, $expected);
  4288. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4289. $expected = array(
  4290. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4291. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4292. );
  4293. $this->assertTags($result, $expected);
  4294. $this->Form->request->data['Model']['field'] = 1;
  4295. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4296. $expected = array(
  4297. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4298. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4299. );
  4300. $this->assertTags($result, $expected);
  4301. }
  4302. /**
  4303. * Test that disabled attribute works on both the checkbox and hidden input.
  4304. *
  4305. * @return void
  4306. */
  4307. public function testCheckboxDisabling() {
  4308. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4309. $expected = array(
  4310. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4311. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4312. );
  4313. $this->assertTags($result, $expected);
  4314. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4315. $expected = array(
  4316. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4317. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4318. );
  4319. $this->assertTags($result, $expected);
  4320. }
  4321. /**
  4322. * Test that the hidden input for checkboxes can be omitted or set to a
  4323. * specific value.
  4324. *
  4325. * @return void
  4326. */
  4327. public function testCheckboxHiddenField() {
  4328. $result = $this->Form->input('UserForm.something', array(
  4329. 'type' => 'checkbox',
  4330. 'hiddenField' => false
  4331. ));
  4332. $expected = array(
  4333. 'div' => array('class' => 'input checkbox'),
  4334. array('input' => array(
  4335. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4336. 'value' => '1', 'id' => 'UserFormSomething'
  4337. )),
  4338. 'label' => array('for' => 'UserFormSomething'),
  4339. 'Something',
  4340. '/label',
  4341. '/div'
  4342. );
  4343. $this->assertTags($result, $expected);
  4344. $result = $this->Form->input('UserForm.something', array(
  4345. 'type' => 'checkbox',
  4346. 'value' => 'Y',
  4347. 'hiddenField' => 'N',
  4348. ));
  4349. $expected = array(
  4350. 'div' => array('class' => 'input checkbox'),
  4351. array('input' => array(
  4352. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4353. 'value' => 'N', 'id' => 'UserFormSomething_'
  4354. )),
  4355. array('input' => array(
  4356. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4357. 'value' => 'Y', 'id' => 'UserFormSomething'
  4358. )),
  4359. 'label' => array('for' => 'UserFormSomething'),
  4360. 'Something',
  4361. '/label',
  4362. '/div'
  4363. );
  4364. $this->assertTags($result, $expected);
  4365. }
  4366. /**
  4367. * testDateTime method
  4368. *
  4369. * Test generation of date/time select elements
  4370. *
  4371. * @return void
  4372. */
  4373. public function testDateTime() {
  4374. extract($this->dateRegex);
  4375. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4376. $now = strtotime('now');
  4377. $expected = array(
  4378. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4379. $daysRegex,
  4380. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4381. date('j', $now),
  4382. '/option',
  4383. '*/select',
  4384. '-',
  4385. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4386. $monthsRegex,
  4387. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4388. date('F', $now),
  4389. '/option',
  4390. '*/select',
  4391. '-',
  4392. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4393. $yearsRegex,
  4394. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4395. date('Y', $now),
  4396. '/option',
  4397. '*/select',
  4398. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4399. $hoursRegex,
  4400. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4401. date('g', $now),
  4402. '/option',
  4403. '*/select',
  4404. ':',
  4405. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4406. $minutesRegex,
  4407. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4408. date('i', $now),
  4409. '/option',
  4410. '*/select',
  4411. ' ',
  4412. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4413. $meridianRegex,
  4414. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4415. date('a', $now),
  4416. '/option',
  4417. '*/select'
  4418. );
  4419. $this->assertTags($result, $expected);
  4420. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4421. $expected = array(
  4422. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4423. $daysRegex,
  4424. array('option' => array('value' => '')),
  4425. '/option',
  4426. '*/select',
  4427. '-',
  4428. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4429. $monthsRegex,
  4430. array('option' => array('value' => '')),
  4431. '/option',
  4432. '*/select',
  4433. '-',
  4434. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4435. $yearsRegex,
  4436. array('option' => array('value' => '')),
  4437. '/option',
  4438. '*/select',
  4439. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4440. $hoursRegex,
  4441. array('option' => array('value' => '')),
  4442. '/option',
  4443. '*/select',
  4444. ':',
  4445. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4446. $minutesRegex,
  4447. array('option' => array('value' => '')),
  4448. '/option',
  4449. '*/select',
  4450. ' ',
  4451. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4452. $meridianRegex,
  4453. array('option' => array('value' => '')),
  4454. '/option',
  4455. '*/select'
  4456. );
  4457. $this->assertTags($result, $expected);
  4458. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4459. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4460. $this->assertTags($result, $expected);
  4461. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4462. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4463. $this->assertTags($result, $expected);
  4464. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4465. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4466. $expected = array(
  4467. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4468. $daysRegex,
  4469. array('option' => array('value' => '')),
  4470. '/option',
  4471. '*/select',
  4472. '-',
  4473. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4474. $monthsRegex,
  4475. array('option' => array('value' => '')),
  4476. '/option',
  4477. '*/select',
  4478. '-',
  4479. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4480. $yearsRegex,
  4481. array('option' => array('value' => '')),
  4482. '/option',
  4483. '*/select',
  4484. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4485. $hoursRegex,
  4486. array('option' => array('value' => '')),
  4487. '/option',
  4488. '*/select',
  4489. ':',
  4490. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4491. $minutesRegex,
  4492. array('option' => array('value' => '')),
  4493. '/option',
  4494. array('option' => array('value' => '00')),
  4495. '00',
  4496. '/option',
  4497. array('option' => array('value' => '05')),
  4498. '05',
  4499. '/option',
  4500. array('option' => array('value' => '10')),
  4501. '10',
  4502. '/option',
  4503. '*/select',
  4504. ' ',
  4505. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4506. $meridianRegex,
  4507. array('option' => array('value' => '')),
  4508. '/option',
  4509. '*/select'
  4510. );
  4511. $this->assertTags($result, $expected);
  4512. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4513. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4514. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4515. $this->Form->request->data['Contact']['data'] = null;
  4516. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4517. $expected = array(
  4518. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4519. $daysRegex,
  4520. array('option' => array('value' => '')),
  4521. '/option',
  4522. '*/select',
  4523. '-',
  4524. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4525. $monthsRegex,
  4526. array('option' => array('value' => '')),
  4527. '/option',
  4528. '*/select',
  4529. '-',
  4530. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4531. $yearsRegex,
  4532. array('option' => array('value' => '')),
  4533. '/option',
  4534. '*/select',
  4535. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4536. $hoursRegex,
  4537. array('option' => array('value' => '')),
  4538. '/option',
  4539. '*/select',
  4540. ':',
  4541. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4542. $minutesRegex,
  4543. array('option' => array('value' => '')),
  4544. '/option',
  4545. '*/select',
  4546. ' ',
  4547. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4548. $meridianRegex,
  4549. array('option' => array('value' => '')),
  4550. '/option',
  4551. '*/select'
  4552. );
  4553. $this->assertTags($result, $expected);
  4554. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4555. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4556. $now = strtotime($this->Form->data['Model']['field']);
  4557. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4558. $expected = array(
  4559. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4560. $daysRegex,
  4561. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4562. date('j', $now),
  4563. '/option',
  4564. '*/select',
  4565. '-',
  4566. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4567. $monthsRegex,
  4568. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4569. date('F', $now),
  4570. '/option',
  4571. '*/select',
  4572. '-',
  4573. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4574. $yearsRegex,
  4575. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4576. date('Y', $now),
  4577. '/option',
  4578. '*/select',
  4579. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4580. $hoursRegex,
  4581. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4582. date('g', $now),
  4583. '/option',
  4584. '*/select',
  4585. ':',
  4586. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4587. $minutesRegex,
  4588. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4589. date('i', $now),
  4590. '/option',
  4591. '*/select',
  4592. ' ',
  4593. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4594. $meridianRegex,
  4595. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4596. date('a', $now),
  4597. '/option',
  4598. '*/select'
  4599. );
  4600. $this->assertTags($result, $expected);
  4601. $selected = strtotime('2008-10-26 12:33:00');
  4602. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4603. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4604. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4605. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4606. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4607. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4608. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4609. $this->Form->create('Contact');
  4610. $result = $this->Form->input('published');
  4611. $now = strtotime('now');
  4612. $expected = array(
  4613. 'div' => array('class' => 'input date'),
  4614. 'label' => array('for' => 'ContactPublishedMonth'),
  4615. 'Published',
  4616. '/label',
  4617. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4618. $monthsRegex,
  4619. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4620. date('F', $now),
  4621. '/option',
  4622. '*/select',
  4623. '-',
  4624. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4625. $daysRegex,
  4626. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4627. date('j', $now),
  4628. '/option',
  4629. '*/select',
  4630. '-',
  4631. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4632. $yearsRegex,
  4633. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4634. date('Y', $now),
  4635. '/option',
  4636. '*/select',
  4637. '/div'
  4638. );
  4639. $this->assertTags($result, $expected);
  4640. $result = $this->Form->input('published2', array('type' => 'date'));
  4641. $now = strtotime('now');
  4642. $expected = array(
  4643. 'div' => array('class' => 'input date'),
  4644. 'label' => array('for' => 'ContactPublished2Month'),
  4645. 'Published2',
  4646. '/label',
  4647. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4648. $monthsRegex,
  4649. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4650. date('F', $now),
  4651. '/option',
  4652. '*/select',
  4653. '-',
  4654. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4655. $daysRegex,
  4656. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4657. date('j', $now),
  4658. '/option',
  4659. '*/select',
  4660. '-',
  4661. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4662. $yearsRegex,
  4663. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4664. date('Y', $now),
  4665. '/option',
  4666. '*/select',
  4667. '/div'
  4668. );
  4669. $this->assertTags($result, $expected);
  4670. $this->Form->create('Contact');
  4671. $result = $this->Form->input('published', array('monthNames' => false));
  4672. $now = strtotime('now');
  4673. $expected = array(
  4674. 'div' => array('class' => 'input date'),
  4675. 'label' => array('for' => 'ContactPublishedMonth'),
  4676. 'Published',
  4677. '/label',
  4678. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4679. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4680. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4681. date('m', $now),
  4682. '/option',
  4683. '*/select',
  4684. '-',
  4685. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4686. $daysRegex,
  4687. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4688. date('j', $now),
  4689. '/option',
  4690. '*/select',
  4691. '-',
  4692. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4693. $yearsRegex,
  4694. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4695. date('Y', $now),
  4696. '/option',
  4697. '*/select',
  4698. '/div'
  4699. );
  4700. $this->assertTags($result, $expected);
  4701. $result = $this->Form->input('published', array('type' => 'time'));
  4702. $now = strtotime('now');
  4703. $expected = array(
  4704. 'div' => array('class' => 'input time'),
  4705. 'label' => array('for' => 'ContactPublishedHour'),
  4706. 'Published',
  4707. '/label',
  4708. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4709. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4710. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4711. date('g', $now),
  4712. '/option',
  4713. '*/select',
  4714. ':',
  4715. );
  4716. $this->assertTags($result, $expected);
  4717. $result = $this->Form->input('published', array(
  4718. 'timeFormat' => 24,
  4719. 'interval' => 5,
  4720. 'selected' => strtotime('2009-09-03 13:37:00'),
  4721. 'type' => 'datetime'
  4722. ));
  4723. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4724. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4725. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4726. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4727. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4728. $this->assertNoErrors();
  4729. $this->Form->request->data['Contact'] = array(
  4730. 'date' => array(
  4731. 'day' => '',
  4732. 'month' => '',
  4733. 'year' => '',
  4734. 'hour' => '',
  4735. 'min' => '',
  4736. 'meridian' => ''
  4737. )
  4738. );
  4739. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4740. }
  4741. /**
  4742. * test that datetime() and default values work.
  4743. *
  4744. * @return void
  4745. */
  4746. public function testDatetimeWithDefault() {
  4747. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4748. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4749. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4750. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4751. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4752. 'default' => '2009-06-01 11:15:30'
  4753. ));
  4754. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4755. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4756. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4757. }
  4758. /**
  4759. * test that bogus non-date time data doesn't cause errors.
  4760. *
  4761. * @return void
  4762. */
  4763. public function testDateTimeWithBogusData() {
  4764. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4765. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4766. }
  4767. /**
  4768. * testDateTimeEmptyAsArray
  4769. *
  4770. * @return void
  4771. */
  4772. public function testDateTimeEmptyAsArray() {
  4773. $result = $this->Form->dateTime('Contact.date',
  4774. 'DMY',
  4775. '12',
  4776. array(
  4777. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4778. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4779. )
  4780. )
  4781. );
  4782. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4783. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4784. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4785. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4786. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4787. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4788. $result = $this->Form->dateTime('Contact.date',
  4789. 'DMY',
  4790. '12',
  4791. array(
  4792. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4793. )
  4794. );
  4795. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4796. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4797. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4798. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4799. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4800. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4801. }
  4802. /**
  4803. * testFormDateTimeMulti method
  4804. *
  4805. * test multiple datetime element generation
  4806. *
  4807. * @return void
  4808. */
  4809. public function testFormDateTimeMulti() {
  4810. extract($this->dateRegex);
  4811. $result = $this->Form->dateTime('Contact.1.updated');
  4812. $expected = array(
  4813. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4814. $daysRegex,
  4815. array('option' => array('value' => '')),
  4816. '/option',
  4817. '*/select',
  4818. '-',
  4819. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4820. $monthsRegex,
  4821. array('option' => array('value' => '')),
  4822. '/option',
  4823. '*/select',
  4824. '-',
  4825. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4826. $yearsRegex,
  4827. array('option' => array('value' => '')),
  4828. '/option',
  4829. '*/select',
  4830. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4831. $hoursRegex,
  4832. array('option' => array('value' => '')),
  4833. '/option',
  4834. '*/select',
  4835. ':',
  4836. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4837. $minutesRegex,
  4838. array('option' => array('value' => '')),
  4839. '/option',
  4840. '*/select',
  4841. ' ',
  4842. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4843. $meridianRegex,
  4844. array('option' => array('value' => '')),
  4845. '/option',
  4846. '*/select'
  4847. );
  4848. $this->assertTags($result, $expected);
  4849. $result = $this->Form->dateTime('Contact.2.updated');
  4850. $expected = array(
  4851. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4852. $daysRegex,
  4853. array('option' => array('value' => '')),
  4854. '/option',
  4855. '*/select',
  4856. '-',
  4857. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4858. $monthsRegex,
  4859. array('option' => array('value' => '')),
  4860. '/option',
  4861. '*/select',
  4862. '-',
  4863. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4864. $yearsRegex,
  4865. array('option' => array('value' => '')),
  4866. '/option',
  4867. '*/select',
  4868. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4869. $hoursRegex,
  4870. array('option' => array('value' => '')),
  4871. '/option',
  4872. '*/select',
  4873. ':',
  4874. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4875. $minutesRegex,
  4876. array('option' => array('value' => '')),
  4877. '/option',
  4878. '*/select',
  4879. ' ',
  4880. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4881. $meridianRegex,
  4882. array('option' => array('value' => '')),
  4883. '/option',
  4884. '*/select'
  4885. );
  4886. $this->assertTags($result, $expected);
  4887. }
  4888. /**
  4889. * When changing the date format, the label should always focus the first select box when
  4890. * clicked.
  4891. *
  4892. * @return void
  4893. */
  4894. public function testDateTimeLabelIdMatchesFirstInput() {
  4895. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4896. $this->assertContains('label for="ModelDateMonth"', $result);
  4897. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4898. $this->assertContains('label for="ModelDateDay"', $result);
  4899. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4900. $this->assertContains('label for="ModelDateYear"', $result);
  4901. }
  4902. /**
  4903. * testMonth method
  4904. *
  4905. * @return void
  4906. */
  4907. public function testMonth() {
  4908. $result = $this->Form->month('Model.field');
  4909. $expected = array(
  4910. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4911. array('option' => array('value' => '')),
  4912. '/option',
  4913. array('option' => array('value' => '01')),
  4914. date('F', strtotime('2008-01-01 00:00:00')),
  4915. '/option',
  4916. array('option' => array('value' => '02')),
  4917. date('F', strtotime('2008-02-01 00:00:00')),
  4918. '/option',
  4919. '*/select',
  4920. );
  4921. $this->assertTags($result, $expected);
  4922. $result = $this->Form->month('Model.field', array('empty' => true));
  4923. $expected = array(
  4924. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4925. array('option' => array('value' => '')),
  4926. '/option',
  4927. array('option' => array('value' => '01')),
  4928. date('F', strtotime('2008-01-01 00:00:00')),
  4929. '/option',
  4930. array('option' => array('value' => '02')),
  4931. date('F', strtotime('2008-02-01 00:00:00')),
  4932. '/option',
  4933. '*/select',
  4934. );
  4935. $this->assertTags($result, $expected);
  4936. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4937. $expected = array(
  4938. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4939. array('option' => array('value' => '')),
  4940. '/option',
  4941. array('option' => array('value' => '01')),
  4942. '01',
  4943. '/option',
  4944. array('option' => array('value' => '02')),
  4945. '02',
  4946. '/option',
  4947. '*/select',
  4948. );
  4949. $this->assertTags($result, $expected);
  4950. $monthNames = array(
  4951. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4952. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4953. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4954. $expected = array(
  4955. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4956. array('option' => array('value' => '')),
  4957. '/option',
  4958. array('option' => array('value' => '01')),
  4959. 'Jan',
  4960. '/option',
  4961. array('option' => array('value' => '02')),
  4962. 'Feb',
  4963. '/option',
  4964. '*/select',
  4965. );
  4966. $this->assertTags($result, $expected);
  4967. }
  4968. /**
  4969. * testDay method
  4970. *
  4971. * @return void
  4972. */
  4973. public function testDay() {
  4974. extract($this->dateRegex);
  4975. $result = $this->Form->day('Model.field', array('value' => false));
  4976. $expected = array(
  4977. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4978. array('option' => array('value' => '')),
  4979. '/option',
  4980. array('option' => array('value' => '01')),
  4981. '1',
  4982. '/option',
  4983. array('option' => array('value' => '02')),
  4984. '2',
  4985. '/option',
  4986. $daysRegex,
  4987. '/select',
  4988. );
  4989. $this->assertTags($result, $expected);
  4990. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4991. $result = $this->Form->day('Model.field');
  4992. $expected = array(
  4993. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4994. array('option' => array('value' => '')),
  4995. '/option',
  4996. array('option' => array('value' => '01')),
  4997. '1',
  4998. '/option',
  4999. array('option' => array('value' => '02')),
  5000. '2',
  5001. '/option',
  5002. $daysRegex,
  5003. array('option' => array('value' => '10', 'selected' => 'selected')),
  5004. '10',
  5005. '/option',
  5006. $daysRegex,
  5007. '/select',
  5008. );
  5009. $this->assertTags($result, $expected);
  5010. $this->Form->request->data['Model']['field'] = '';
  5011. $result = $this->Form->day('Model.field', array('value' => '10'));
  5012. $expected = array(
  5013. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5014. array('option' => array('value' => '')),
  5015. '/option',
  5016. array('option' => array('value' => '01')),
  5017. '1',
  5018. '/option',
  5019. array('option' => array('value' => '02')),
  5020. '2',
  5021. '/option',
  5022. $daysRegex,
  5023. array('option' => array('value' => '10', 'selected' => 'selected')),
  5024. '10',
  5025. '/option',
  5026. $daysRegex,
  5027. '/select',
  5028. );
  5029. $this->assertTags($result, $expected);
  5030. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5031. $result = $this->Form->day('Model.field', array('value' => true));
  5032. $expected = array(
  5033. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5034. array('option' => array('value' => '')),
  5035. '/option',
  5036. array('option' => array('value' => '01')),
  5037. '1',
  5038. '/option',
  5039. array('option' => array('value' => '02')),
  5040. '2',
  5041. '/option',
  5042. $daysRegex,
  5043. array('option' => array('value' => '10', 'selected' => 'selected')),
  5044. '10',
  5045. '/option',
  5046. $daysRegex,
  5047. '/select',
  5048. );
  5049. $this->assertTags($result, $expected);
  5050. }
  5051. /**
  5052. * testMinute method
  5053. *
  5054. * @return void
  5055. */
  5056. public function testMinute() {
  5057. extract($this->dateRegex);
  5058. $result = $this->Form->minute('Model.field');
  5059. $expected = array(
  5060. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5061. array('option' => array('value' => '')),
  5062. '/option',
  5063. array('option' => array('value' => '00')),
  5064. '00',
  5065. '/option',
  5066. array('option' => array('value' => '01')),
  5067. '01',
  5068. '/option',
  5069. array('option' => array('value' => '02')),
  5070. '02',
  5071. '/option',
  5072. $minutesRegex,
  5073. '/select',
  5074. );
  5075. $this->assertTags($result, $expected);
  5076. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5077. $result = $this->Form->minute('Model.field');
  5078. $expected = array(
  5079. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5080. array('option' => array('value' => '')),
  5081. '/option',
  5082. array('option' => array('value' => '00')),
  5083. '00',
  5084. '/option',
  5085. array('option' => array('value' => '01')),
  5086. '01',
  5087. '/option',
  5088. array('option' => array('value' => '02')),
  5089. '02',
  5090. '/option',
  5091. $minutesRegex,
  5092. array('option' => array('value' => '12', 'selected' => 'selected')),
  5093. '12',
  5094. '/option',
  5095. $minutesRegex,
  5096. '/select',
  5097. );
  5098. $this->assertTags($result, $expected);
  5099. $this->Form->request->data['Model']['field'] = '';
  5100. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5101. $expected = array(
  5102. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5103. array('option' => array('value' => '')),
  5104. '/option',
  5105. array('option' => array('value' => '00')),
  5106. '00',
  5107. '/option',
  5108. array('option' => array('value' => '05')),
  5109. '05',
  5110. '/option',
  5111. array('option' => array('value' => '10')),
  5112. '10',
  5113. '/option',
  5114. $minutesRegex,
  5115. '/select',
  5116. );
  5117. $this->assertTags($result, $expected);
  5118. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5119. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5120. $expected = array(
  5121. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5122. array('option' => array('value' => '')),
  5123. '/option',
  5124. array('option' => array('value' => '00')),
  5125. '00',
  5126. '/option',
  5127. array('option' => array('value' => '05')),
  5128. '05',
  5129. '/option',
  5130. array('option' => array('value' => '10', 'selected' => 'selected')),
  5131. '10',
  5132. '/option',
  5133. $minutesRegex,
  5134. '/select',
  5135. );
  5136. $this->assertTags($result, $expected);
  5137. }
  5138. /**
  5139. * testHour method
  5140. *
  5141. * @return void
  5142. */
  5143. public function testHour() {
  5144. extract($this->dateRegex);
  5145. $result = $this->Form->hour('Model.field', false);
  5146. $expected = array(
  5147. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5148. array('option' => array('value' => '')),
  5149. '/option',
  5150. array('option' => array('value' => '01')),
  5151. '1',
  5152. '/option',
  5153. array('option' => array('value' => '02')),
  5154. '2',
  5155. '/option',
  5156. $hoursRegex,
  5157. '/select',
  5158. );
  5159. $this->assertTags($result, $expected);
  5160. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5161. $result = $this->Form->hour('Model.field', false);
  5162. $expected = array(
  5163. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5164. array('option' => array('value' => '')),
  5165. '/option',
  5166. array('option' => array('value' => '01')),
  5167. '1',
  5168. '/option',
  5169. array('option' => array('value' => '02')),
  5170. '2',
  5171. '/option',
  5172. $hoursRegex,
  5173. array('option' => array('value' => '12', 'selected' => 'selected')),
  5174. '12',
  5175. '/option',
  5176. '/select',
  5177. );
  5178. $this->assertTags($result, $expected);
  5179. $this->Form->request->data['Model']['field'] = '';
  5180. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5181. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5182. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5183. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5184. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5185. $result = $this->Form->hour('Model.field', true);
  5186. $expected = array(
  5187. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5188. array('option' => array('value' => '')),
  5189. '/option',
  5190. array('option' => array('value' => '00', 'selected' => 'selected')),
  5191. '0',
  5192. '/option',
  5193. array('option' => array('value' => '01')),
  5194. '1',
  5195. '/option',
  5196. array('option' => array('value' => '02')),
  5197. '2',
  5198. '/option',
  5199. $hoursRegex,
  5200. '/select',
  5201. );
  5202. $this->assertTags($result, $expected);
  5203. unset($this->Form->request->data['Model']['field']);
  5204. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5205. $thisHour = date('H');
  5206. $optValue = date('G');
  5207. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5208. }
  5209. /**
  5210. * testYear method
  5211. *
  5212. * @return void
  5213. */
  5214. public function testYear() {
  5215. $result = $this->Form->year('Model.field', 2006, 2007);
  5216. $expected = array(
  5217. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5218. array('option' => array('value' => '')),
  5219. '/option',
  5220. array('option' => array('value' => '2007')),
  5221. '2007',
  5222. '/option',
  5223. array('option' => array('value' => '2006')),
  5224. '2006',
  5225. '/option',
  5226. '/select',
  5227. );
  5228. $this->assertTags($result, $expected);
  5229. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5230. $expected = array(
  5231. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5232. array('option' => array('value' => '')),
  5233. '/option',
  5234. array('option' => array('value' => '2006')),
  5235. '2006',
  5236. '/option',
  5237. array('option' => array('value' => '2007')),
  5238. '2007',
  5239. '/option',
  5240. '/select',
  5241. );
  5242. $this->assertTags($result, $expected);
  5243. $this->request->data['Contact']['published'] = '';
  5244. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5245. $expected = array(
  5246. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5247. array('option' => array('value' => '')),
  5248. '/option',
  5249. array('option' => array('value' => '2007')),
  5250. '2007',
  5251. '/option',
  5252. array('option' => array('value' => '2006')),
  5253. '2006',
  5254. '/option',
  5255. '/select',
  5256. );
  5257. $this->assertTags($result, $expected);
  5258. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5259. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5260. $expected = array(
  5261. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5262. array('option' => array('value' => '2007')),
  5263. '2007',
  5264. '/option',
  5265. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5266. '2006',
  5267. '/option',
  5268. '/select',
  5269. );
  5270. $this->assertTags($result, $expected);
  5271. $this->Form->request->data['Contact']['published'] = '';
  5272. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5273. $expected = array(
  5274. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5275. array('option' => array('value' => '')),
  5276. '/option',
  5277. array('option' => array('value' => '2007')),
  5278. '2007',
  5279. '/option',
  5280. array('option' => array('value' => '2006')),
  5281. '2006',
  5282. '/option',
  5283. '/select',
  5284. );
  5285. $this->assertTags($result, $expected);
  5286. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5287. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5288. $expected = array(
  5289. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5290. array('option' => array('value' => '2007')),
  5291. '2007',
  5292. '/option',
  5293. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5294. '2006',
  5295. '/option',
  5296. '/select',
  5297. );
  5298. $this->assertTags($result, $expected);
  5299. $this->Form->request->data['Contact']['published'] = '';
  5300. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5301. $expected = array(
  5302. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5303. array('option' => array('value' => '')),
  5304. '/option',
  5305. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5306. '2007',
  5307. '/option',
  5308. array('option' => array('value' => '2006')),
  5309. '2006',
  5310. '/option',
  5311. '/select',
  5312. );
  5313. $this->assertTags($result, $expected);
  5314. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5315. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5316. $expected = array(
  5317. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5318. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5319. '2007',
  5320. '/option',
  5321. array('option' => array('value' => '2006')),
  5322. '2006',
  5323. '/option',
  5324. '/select',
  5325. );
  5326. $this->assertTags($result, $expected);
  5327. $this->Form->request->data['Contact']['published'] = '';
  5328. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5329. $expected = array(
  5330. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5331. array('option' => array('value' => '2008')),
  5332. '2008',
  5333. '/option',
  5334. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5335. '2007',
  5336. '/option',
  5337. array('option' => array('value' => '2006')),
  5338. '2006',
  5339. '/option',
  5340. '/select',
  5341. );
  5342. $this->assertTags($result, $expected);
  5343. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5344. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5345. $expected = array(
  5346. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5347. array('option' => array('value' => '2008')),
  5348. '2008',
  5349. '/option',
  5350. array('option' => array('value' => '2007')),
  5351. '2007',
  5352. '/option',
  5353. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5354. '2006',
  5355. '/option',
  5356. '/select',
  5357. );
  5358. $this->assertTags($result, $expected);
  5359. $this->Form->request->data = array();
  5360. $this->Form->create('Contact');
  5361. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5362. $expected = array(
  5363. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5364. array('option' => array('value' => '2008')),
  5365. '2008',
  5366. '/option',
  5367. array('option' => array('value' => '2007')),
  5368. '2007',
  5369. '/option',
  5370. array('option' => array('value' => '2006')),
  5371. '2006',
  5372. '/option',
  5373. '/select',
  5374. );
  5375. $this->assertTags($result, $expected);
  5376. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5377. $this->assertContains('data[Contact][published][year]', $result);
  5378. }
  5379. /**
  5380. * testTextArea method
  5381. *
  5382. * @return void
  5383. */
  5384. public function testTextArea() {
  5385. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5386. $result = $this->Form->textarea('Model.field');
  5387. $expected = array(
  5388. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5389. 'some test data',
  5390. '/textarea',
  5391. );
  5392. $this->assertTags($result, $expected);
  5393. $result = $this->Form->textarea('Model.tmp');
  5394. $expected = array(
  5395. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5396. '/textarea',
  5397. );
  5398. $this->assertTags($result, $expected);
  5399. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5400. $result = $this->Form->textarea('Model.field');
  5401. $expected = array(
  5402. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5403. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5404. '/textarea',
  5405. );
  5406. $this->assertTags($result, $expected);
  5407. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5408. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5409. $expected = array(
  5410. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5411. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5412. '/textarea',
  5413. );
  5414. $this->assertTags($result, $expected);
  5415. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5416. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5417. $expected = array(
  5418. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5419. '/textarea'
  5420. );
  5421. $this->assertTags($result, $expected);
  5422. }
  5423. /**
  5424. * testTextAreaWithStupidCharacters method
  5425. *
  5426. * test text area with non-ascii characters
  5427. *
  5428. * @return void
  5429. */
  5430. public function testTextAreaWithStupidCharacters() {
  5431. $this->loadFixtures('Post');
  5432. $result = $this->Form->input('Post.content', array(
  5433. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5434. ));
  5435. $expected = array(
  5436. 'div' => array('class' => 'input text'),
  5437. 'label' => array('for' => 'PostContent'),
  5438. 'Current Text',
  5439. '/label',
  5440. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5441. 'GREAT®',
  5442. '/textarea',
  5443. '/div'
  5444. );
  5445. $this->assertTags($result, $expected);
  5446. }
  5447. /**
  5448. * testHiddenField method
  5449. *
  5450. * @return void
  5451. */
  5452. public function testHiddenField() {
  5453. $Contact = ClassRegistry::getObject('Contact');
  5454. $Contact->validationErrors['field'] = 1;
  5455. $this->Form->request->data['Contact']['field'] = 'test';
  5456. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5457. $this->assertTags($result, array(
  5458. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5459. );
  5460. }
  5461. /**
  5462. * testFileUploadField method
  5463. *
  5464. * @return void
  5465. */
  5466. public function testFileUploadField() {
  5467. $result = $this->Form->file('Model.upload');
  5468. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5469. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5470. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5471. $expected = array(
  5472. 'div' => array('class' => 'input file'),
  5473. 'label' => array('for' => 'ModelUpload'),
  5474. 'Upload',
  5475. '/label',
  5476. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5477. '/div'
  5478. );
  5479. $this->assertTags($result, $expected);
  5480. }
  5481. /**
  5482. * test File upload input on a model not used in create();
  5483. *
  5484. * @return void
  5485. */
  5486. public function testFileUploadOnOtherModel() {
  5487. $this->Form->create('ValidateUser', array('type' => 'file'));
  5488. $result = $this->Form->file('ValidateProfile.city');
  5489. $expected = array(
  5490. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5491. );
  5492. $this->assertTags($result, $expected);
  5493. }
  5494. /**
  5495. * testButton method
  5496. *
  5497. * @return void
  5498. */
  5499. public function testButton() {
  5500. $result = $this->Form->button('Hi');
  5501. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5502. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5503. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5504. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5505. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5506. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5507. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5508. $result = $this->Form->button('No type', array('type' => false));
  5509. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5510. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5511. $this->assertNotRegExp('/\&039/', $result);
  5512. }
  5513. /**
  5514. * Test that button() makes unlocked fields by default.
  5515. *
  5516. * @return void
  5517. */
  5518. public function testButtonUnlockedByDefault() {
  5519. $this->Form->request->params['_Token']['key'] = 'secured';
  5520. $this->Form->button('Save', array('name' => 'save'));
  5521. $this->Form->button('Clear');
  5522. $result = $this->Form->unlockField();
  5523. $this->assertEquals(array('save'), $result);
  5524. }
  5525. /**
  5526. * testPostButton method
  5527. *
  5528. * @return void
  5529. */
  5530. public function testPostButton() {
  5531. $result = $this->Form->postButton('Hi', '/controller/action');
  5532. $this->assertTags($result, array(
  5533. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5534. 'div' => array('style' => 'display:none;'),
  5535. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5536. '/div',
  5537. 'button' => array('type' => 'submit'),
  5538. 'Hi',
  5539. '/button',
  5540. '/form'
  5541. ));
  5542. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5543. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5544. }
  5545. /**
  5546. * Test that postButton adds _Token fields.
  5547. *
  5548. * @return void
  5549. */
  5550. public function testSecurePostButton() {
  5551. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5552. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5553. $expected = array(
  5554. 'form' => array(
  5555. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5556. ),
  5557. array('div' => array('style' => 'display:none;')),
  5558. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5559. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5560. '/div',
  5561. 'button' => array('type' => 'submit'),
  5562. 'Delete',
  5563. '/button',
  5564. array('div' => array('style' => 'display:none;')),
  5565. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5566. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5567. '/div',
  5568. '/form',
  5569. );
  5570. $this->assertTags($result, $expected);
  5571. }
  5572. /**
  5573. * testPostLink method
  5574. *
  5575. * @return void
  5576. */
  5577. public function testPostLink() {
  5578. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5579. $this->assertTags($result, array(
  5580. 'form' => array(
  5581. 'method' => 'post', 'action' => '/posts/delete/1',
  5582. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5583. ),
  5584. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5585. '/form',
  5586. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5587. 'Delete',
  5588. '/a'
  5589. ));
  5590. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5591. $this->assertTags($result, array(
  5592. 'form' => array(
  5593. 'method' => 'post', 'action' => '/posts/delete/1',
  5594. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5595. ),
  5596. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5597. '/form',
  5598. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5599. 'Delete',
  5600. '/a'
  5601. ));
  5602. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5603. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5604. }
  5605. /**
  5606. * Test that postLink adds _Token fields.
  5607. *
  5608. * @return void
  5609. */
  5610. public function testSecurePostLink() {
  5611. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5612. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5613. $expected = array(
  5614. 'form' => array(
  5615. 'method' => 'post', 'action' => '/posts/delete/1',
  5616. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5617. ),
  5618. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5619. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5620. 'div' => array('style' => 'display:none;'),
  5621. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5622. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5623. '/div',
  5624. '/form',
  5625. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5626. 'Delete',
  5627. '/a'
  5628. );
  5629. $this->assertTags($result, $expected);
  5630. }
  5631. /**
  5632. * testSubmitButton method
  5633. *
  5634. * @return void
  5635. */
  5636. public function testSubmitButton() {
  5637. $result = $this->Form->submit('');
  5638. $expected = array(
  5639. 'div' => array('class' => 'submit'),
  5640. 'input' => array('type' => 'submit', 'value' => ''),
  5641. '/div'
  5642. );
  5643. $this->assertTags($result, $expected);
  5644. $result = $this->Form->submit('Test Submit');
  5645. $expected = array(
  5646. 'div' => array('class' => 'submit'),
  5647. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5648. '/div'
  5649. );
  5650. $this->assertTags($result, $expected);
  5651. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5652. $expected = array(
  5653. 'span' => array('class' => 'submit'),
  5654. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5655. '/span'
  5656. );
  5657. $this->assertTags($result, $expected);
  5658. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5659. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5660. $this->assertTags($result, $expected);
  5661. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5662. $expected = array(
  5663. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5664. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5665. '/div'
  5666. );
  5667. $this->assertTags($result, $expected);
  5668. $result = $this->Form->submit('Next >');
  5669. $expected = array(
  5670. 'div' => array('class' => 'submit'),
  5671. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5672. '/div'
  5673. );
  5674. $this->assertTags($result, $expected);
  5675. $result = $this->Form->submit('Next >', array('escape' => false));
  5676. $expected = array(
  5677. 'div' => array('class' => 'submit'),
  5678. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5679. '/div'
  5680. );
  5681. $this->assertTags($result, $expected);
  5682. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5683. $expected = array(
  5684. 'div' => array('class' => 'submit'),
  5685. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5686. '/div'
  5687. );
  5688. $this->assertTags($result, $expected);
  5689. $before = '--before--';
  5690. $after = '--after--';
  5691. $result = $this->Form->submit('Test', array('before' => $before));
  5692. $expected = array(
  5693. 'div' => array('class' => 'submit'),
  5694. '--before--',
  5695. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5696. '/div'
  5697. );
  5698. $this->assertTags($result, $expected);
  5699. $result = $this->Form->submit('Test', array('after' => $after));
  5700. $expected = array(
  5701. 'div' => array('class' => 'submit'),
  5702. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5703. '--after--',
  5704. '/div'
  5705. );
  5706. $this->assertTags($result, $expected);
  5707. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5708. $expected = array(
  5709. 'div' => array('class' => 'submit'),
  5710. '--before--',
  5711. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5712. '--after--',
  5713. '/div'
  5714. );
  5715. $this->assertTags($result, $expected);
  5716. }
  5717. /**
  5718. * test image submit types.
  5719. *
  5720. * @return void
  5721. */
  5722. public function testSubmitImage() {
  5723. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5724. $expected = array(
  5725. 'div' => array('class' => 'submit'),
  5726. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5727. '/div'
  5728. );
  5729. $this->assertTags($result, $expected);
  5730. $result = $this->Form->submit('/relative/cake.power.gif');
  5731. $expected = array(
  5732. 'div' => array('class' => 'submit'),
  5733. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5734. '/div'
  5735. );
  5736. $this->assertTags($result, $expected);
  5737. $result = $this->Form->submit('cake.power.gif');
  5738. $expected = array(
  5739. 'div' => array('class' => 'submit'),
  5740. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5741. '/div'
  5742. );
  5743. $this->assertTags($result, $expected);
  5744. $result = $this->Form->submit('Not.an.image');
  5745. $expected = array(
  5746. 'div' => array('class' => 'submit'),
  5747. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5748. '/div'
  5749. );
  5750. $this->assertTags($result, $expected);
  5751. $after = '--after--';
  5752. $before = '--before--';
  5753. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5754. $expected = array(
  5755. 'div' => array('class' => 'submit'),
  5756. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5757. '--after--',
  5758. '/div'
  5759. );
  5760. $this->assertTags($result, $expected);
  5761. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5762. $expected = array(
  5763. 'div' => array('class' => 'submit'),
  5764. '--before--',
  5765. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5766. '/div'
  5767. );
  5768. $this->assertTags($result, $expected);
  5769. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5770. $expected = array(
  5771. 'div' => array('class' => 'submit'),
  5772. '--before--',
  5773. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5774. '--after--',
  5775. '/div'
  5776. );
  5777. $this->assertTags($result, $expected);
  5778. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5779. $expected = array(
  5780. 'div' => array('class' => 'submit'),
  5781. '--before--',
  5782. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5783. '--after--',
  5784. '/div'
  5785. );
  5786. $this->assertTags($result, $expected);
  5787. }
  5788. /**
  5789. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5790. * be submitted at a time.
  5791. *
  5792. * @return void
  5793. */
  5794. public function testSubmitUnlockedByDefault() {
  5795. $this->Form->request->params['_Token']['key'] = 'secured';
  5796. $this->Form->submit('Go go');
  5797. $this->Form->submit('Save', array('name' => 'save'));
  5798. $result = $this->Form->unlockField();
  5799. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5800. }
  5801. /**
  5802. * Test submit image with timestamps.
  5803. *
  5804. * @return void
  5805. */
  5806. public function testSubmitImageTimestamp() {
  5807. Configure::write('Asset.timestamp', 'force');
  5808. $result = $this->Form->submit('cake.power.gif');
  5809. $expected = array(
  5810. 'div' => array('class' => 'submit'),
  5811. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5812. '/div'
  5813. );
  5814. $this->assertTags($result, $expected);
  5815. }
  5816. /**
  5817. * test the create() method
  5818. *
  5819. * @return void
  5820. */
  5821. public function testCreate() {
  5822. $result = $this->Form->create('Contact');
  5823. $encoding = strtolower(Configure::read('App.encoding'));
  5824. $expected = array(
  5825. 'form' => array(
  5826. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5827. 'accept-charset' => $encoding
  5828. ),
  5829. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5830. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5831. '/div'
  5832. );
  5833. $this->assertTags($result, $expected);
  5834. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5835. $expected = array('form' => array(
  5836. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5837. 'accept-charset' => $encoding
  5838. ));
  5839. $this->assertTags($result, $expected);
  5840. $result = $this->Form->create('Contact', array('type' => 'get'));
  5841. $expected = array('form' => array(
  5842. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5843. 'accept-charset' => $encoding
  5844. ));
  5845. $this->assertTags($result, $expected);
  5846. $result = $this->Form->create('Contact', array('type' => 'put'));
  5847. $expected = array(
  5848. 'form' => array(
  5849. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5850. 'accept-charset' => $encoding
  5851. ),
  5852. 'div' => array('style' => 'display:none;'),
  5853. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5854. '/div'
  5855. );
  5856. $this->assertTags($result, $expected);
  5857. $result = $this->Form->create('Contact', array('type' => 'file'));
  5858. $expected = array(
  5859. 'form' => array(
  5860. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5861. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5862. ),
  5863. 'div' => array('style' => 'display:none;'),
  5864. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5865. '/div'
  5866. );
  5867. $this->assertTags($result, $expected);
  5868. $this->Form->request->data['Contact']['id'] = 1;
  5869. $this->Form->request->here = '/contacts/edit/1';
  5870. $this->Form->request['action'] = 'edit';
  5871. $result = $this->Form->create('Contact');
  5872. $expected = array(
  5873. 'form' => array(
  5874. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5875. 'accept-charset' => $encoding
  5876. ),
  5877. 'div' => array('style' => 'display:none;'),
  5878. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5879. '/div'
  5880. );
  5881. $this->assertTags($result, $expected);
  5882. $this->Form->request->data['Contact']['id'] = 1;
  5883. $this->Form->request->here = '/contacts/edit/1';
  5884. $this->Form->request['action'] = 'edit';
  5885. $result = $this->Form->create('Contact', array('type' => 'file'));
  5886. $expected = array(
  5887. 'form' => array(
  5888. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5889. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5890. ),
  5891. 'div' => array('style' => 'display:none;'),
  5892. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5893. '/div'
  5894. );
  5895. $this->assertTags($result, $expected);
  5896. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5897. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5898. $expected = array(
  5899. 'form' => array(
  5900. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5901. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5902. ),
  5903. 'div' => array('style' => 'display:none;'),
  5904. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5905. '/div'
  5906. );
  5907. $this->assertTags($result, $expected);
  5908. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5909. $expected = array(
  5910. 'form' => array(
  5911. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5912. 'accept-charset' => $encoding
  5913. ),
  5914. 'div' => array('style' => 'display:none;'),
  5915. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5916. '/div'
  5917. );
  5918. $this->assertTags($result, $expected);
  5919. $this->Form->request['action'] = 'add';
  5920. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5921. $expected = array(
  5922. 'form' => array(
  5923. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5924. 'accept-charset' => $encoding
  5925. ),
  5926. 'div' => array('style' => 'display:none;'),
  5927. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5928. '/div'
  5929. );
  5930. $this->assertTags($result, $expected);
  5931. $result = $this->Form->create('User', array('action' => 'login'));
  5932. $expected = array(
  5933. 'form' => array(
  5934. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5935. 'accept-charset' => $encoding
  5936. ),
  5937. 'div' => array('style' => 'display:none;'),
  5938. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5939. '/div'
  5940. );
  5941. $this->assertTags($result, $expected);
  5942. $result = $this->Form->create('User', array('url' => '/users/login'));
  5943. $expected = array(
  5944. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5945. 'div' => array('style' => 'display:none;'),
  5946. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5947. '/div'
  5948. );
  5949. $this->assertTags($result, $expected);
  5950. $this->Form->request['controller'] = 'pages';
  5951. $result = $this->Form->create('User', array('action' => 'signup'));
  5952. $expected = array(
  5953. 'form' => array(
  5954. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5955. 'accept-charset' => $encoding
  5956. ),
  5957. 'div' => array('style' => 'display:none;'),
  5958. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5959. '/div'
  5960. );
  5961. $this->assertTags($result, $expected);
  5962. $this->Form->request->data = array();
  5963. $this->Form->request['controller'] = 'contacts';
  5964. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5965. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5966. $expected = array(
  5967. 'form' => array(
  5968. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5969. 'accept-charset' => 'utf-8'
  5970. ),
  5971. 'div' => array('style' => 'display:none;'),
  5972. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5973. '/div'
  5974. );
  5975. $this->assertTags($result, $expected);
  5976. }
  5977. /**
  5978. * Test the onsubmit option for create()
  5979. *
  5980. * @return void
  5981. */
  5982. public function testCreateOnSubmit() {
  5983. $this->Form->request->data = array();
  5984. $this->Form->request['controller'] = 'contacts';
  5985. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5986. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5987. $expected = array(
  5988. 'form' => array(
  5989. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5990. 'accept-charset' => 'utf-8'
  5991. ),
  5992. 'div' => array('style' => 'display:none;'),
  5993. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5994. '/div'
  5995. );
  5996. $this->assertTags($result, $expected);
  5997. $this->Form->request->data = array();
  5998. $this->Form->request['controller'] = 'contacts';
  5999. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6000. $result = $this->Form->create(array(
  6001. 'url' => array('action' => 'index', 'param'),
  6002. 'default' => false,
  6003. 'onsubmit' => 'someFunction();'
  6004. ));
  6005. $expected = array(
  6006. 'form' => array(
  6007. 'id' => 'ContactAddForm', 'method' => 'post',
  6008. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6009. 'action' => '/contacts/index/param',
  6010. 'accept-charset' => 'utf-8'
  6011. ),
  6012. 'div' => array('style' => 'display:none;'),
  6013. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6014. '/div'
  6015. );
  6016. $this->assertTags($result, $expected);
  6017. }
  6018. /**
  6019. * test create() with automatic url generation
  6020. *
  6021. * @return void
  6022. */
  6023. public function testCreateAutoUrl() {
  6024. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6025. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6026. $this->Form->request->base = '/base_url';
  6027. $result = $this->Form->create('Contact');
  6028. $expected = array(
  6029. 'form' => array(
  6030. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6031. 'accept-charset' => 'utf-8'
  6032. ),
  6033. 'div' => array('style' => 'display:none;'),
  6034. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6035. '/div'
  6036. );
  6037. $this->assertTags($result, $expected);
  6038. $this->Form->request['action'] = 'delete';
  6039. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6040. $this->Form->request->base = '/base_url';
  6041. $result = $this->Form->create('Contact');
  6042. $expected = array(
  6043. 'form' => array(
  6044. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6045. 'accept-charset' => 'utf-8'
  6046. ),
  6047. 'div' => array('style' => 'display:none;'),
  6048. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6049. '/div'
  6050. );
  6051. $this->assertTags($result, $expected);
  6052. }
  6053. /**
  6054. * test create() with a custom route
  6055. *
  6056. * @return void
  6057. */
  6058. public function testCreateCustomRoute() {
  6059. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6060. $encoding = strtolower(Configure::read('App.encoding'));
  6061. $result = $this->Form->create('User', array('action' => 'login'));
  6062. $expected = array(
  6063. 'form' => array(
  6064. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6065. 'accept-charset' => $encoding
  6066. ),
  6067. 'div' => array('style' => 'display:none;'),
  6068. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6069. '/div'
  6070. );
  6071. $this->assertTags($result, $expected);
  6072. }
  6073. /**
  6074. * test that inputDefaults are stored and used.
  6075. *
  6076. * @return void
  6077. */
  6078. public function testCreateWithInputDefaults() {
  6079. $this->Form->create('User', array(
  6080. 'inputDefaults' => array(
  6081. 'div' => false,
  6082. 'label' => false,
  6083. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6084. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6085. )
  6086. ));
  6087. $result = $this->Form->input('username');
  6088. $expected = array(
  6089. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6090. );
  6091. $this->assertTags($result, $expected);
  6092. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6093. $expected = array(
  6094. 'div' => array('class' => 'input text'),
  6095. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6096. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6097. '/div'
  6098. );
  6099. $this->assertTags($result, $expected);
  6100. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6101. $expected = array(
  6102. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6103. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6104. );
  6105. $this->assertTags($result, $expected);
  6106. $this->Form->create('User', array(
  6107. 'inputDefaults' => array(
  6108. 'div' => false,
  6109. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6110. )
  6111. ));
  6112. $result = $this->Form->input('username', array('div' => true));
  6113. $expected = array(
  6114. 'div' => array('class' => 'input text'),
  6115. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6116. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6117. '/div'
  6118. );
  6119. $this->assertTags($result, $expected);
  6120. }
  6121. /**
  6122. * test automatic accept-charset overriding
  6123. *
  6124. * @return void
  6125. */
  6126. public function testCreateWithAcceptCharset() {
  6127. $result = $this->Form->create('UserForm', array(
  6128. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6129. )
  6130. );
  6131. $expected = array(
  6132. 'form' => array(
  6133. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6134. 'accept-charset' => 'iso-8859-1'
  6135. ),
  6136. 'div' => array('style' => 'display:none;'),
  6137. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6138. '/div'
  6139. );
  6140. $this->assertTags($result, $expected);
  6141. }
  6142. /**
  6143. * Test base form url when url param is passed with multiple parameters (&)
  6144. *
  6145. */
  6146. public function testCreateQuerystringrequest() {
  6147. $encoding = strtolower(Configure::read('App.encoding'));
  6148. $result = $this->Form->create('Contact', array(
  6149. 'type' => 'post',
  6150. 'escape' => false,
  6151. 'url' => array(
  6152. 'controller' => 'controller',
  6153. 'action' => 'action',
  6154. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6155. )
  6156. ));
  6157. $expected = array(
  6158. 'form' => array(
  6159. 'id' => 'ContactAddForm',
  6160. 'method' => 'post',
  6161. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6162. 'accept-charset' => $encoding
  6163. ),
  6164. 'div' => array('style' => 'display:none;'),
  6165. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6166. '/div'
  6167. );
  6168. $this->assertTags($result, $expected);
  6169. $result = $this->Form->create('Contact', array(
  6170. 'type' => 'post',
  6171. 'url' => array(
  6172. 'controller' => 'controller',
  6173. 'action' => 'action',
  6174. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6175. )
  6176. ));
  6177. $expected = array(
  6178. 'form' => array(
  6179. 'id' => 'ContactAddForm',
  6180. 'method' => 'post',
  6181. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6182. 'accept-charset' => $encoding
  6183. ),
  6184. 'div' => array('style' => 'display:none;'),
  6185. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6186. '/div'
  6187. );
  6188. $this->assertTags($result, $expected);
  6189. }
  6190. /**
  6191. * test that create() doesn't cause errors by multiple id's being in the primary key
  6192. * as could happen with multiple select or checkboxes.
  6193. *
  6194. * @return void
  6195. */
  6196. public function testCreateWithMultipleIdInData() {
  6197. $encoding = strtolower(Configure::read('App.encoding'));
  6198. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6199. $result = $this->Form->create('Contact');
  6200. $expected = array(
  6201. 'form' => array(
  6202. 'id' => 'ContactAddForm',
  6203. 'method' => 'post',
  6204. 'action' => '/contacts/add',
  6205. 'accept-charset' => $encoding
  6206. ),
  6207. 'div' => array('style' => 'display:none;'),
  6208. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6209. '/div'
  6210. );
  6211. $this->assertTags($result, $expected);
  6212. }
  6213. /**
  6214. * test that create() doesn't add in extra passed params.
  6215. *
  6216. * @return void
  6217. */
  6218. public function testCreatePassedArgs() {
  6219. $encoding = strtolower(Configure::read('App.encoding'));
  6220. $this->Form->request->data['Contact']['id'] = 1;
  6221. $result = $this->Form->create('Contact', array(
  6222. 'type' => 'post',
  6223. 'escape' => false,
  6224. 'url' => array(
  6225. 'action' => 'edit',
  6226. 'myparam'
  6227. )
  6228. ));
  6229. $expected = array(
  6230. 'form' => array(
  6231. 'id' => 'ContactAddForm',
  6232. 'method' => 'post',
  6233. 'action' => '/contacts/edit/myparam',
  6234. 'accept-charset' => $encoding
  6235. ),
  6236. 'div' => array('style' => 'display:none;'),
  6237. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6238. '/div'
  6239. );
  6240. $this->assertTags($result, $expected);
  6241. }
  6242. /**
  6243. * test creating a get form, and get form inputs.
  6244. *
  6245. * @return void
  6246. */
  6247. public function testGetFormCreate() {
  6248. $encoding = strtolower(Configure::read('App.encoding'));
  6249. $result = $this->Form->create('Contact', array('type' => 'get'));
  6250. $this->assertTags($result, array('form' => array(
  6251. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6252. 'accept-charset' => $encoding
  6253. )));
  6254. $result = $this->Form->text('Contact.name');
  6255. $this->assertTags($result, array('input' => array(
  6256. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6257. )));
  6258. $result = $this->Form->password('password');
  6259. $this->assertTags($result, array('input' => array(
  6260. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6261. )));
  6262. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6263. $result = $this->Form->text('user_form');
  6264. $this->assertTags($result, array('input' => array(
  6265. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6266. )));
  6267. }
  6268. /**
  6269. * test get form, and inputs when the model param is false
  6270. *
  6271. * @return void
  6272. */
  6273. public function testGetFormWithFalseModel() {
  6274. $encoding = strtolower(Configure::read('App.encoding'));
  6275. $this->Form->request['controller'] = 'contact_test';
  6276. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6277. $expected = array('form' => array(
  6278. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6279. 'accept-charset' => $encoding
  6280. ));
  6281. $this->assertTags($result, $expected);
  6282. $result = $this->Form->text('reason');
  6283. $expected = array(
  6284. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6285. );
  6286. $this->assertTags($result, $expected);
  6287. }
  6288. /**
  6289. * test that datetime() works with GET style forms.
  6290. *
  6291. * @return void
  6292. */
  6293. public function testDateTimeWithGetForms() {
  6294. extract($this->dateRegex);
  6295. $this->Form->create('Contact', array('type' => 'get'));
  6296. $result = $this->Form->datetime('created');
  6297. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6298. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6299. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6300. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6301. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6302. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6303. }
  6304. /**
  6305. * testEditFormWithData method
  6306. *
  6307. * test auto populating form elements from submitted data.
  6308. *
  6309. * @return void
  6310. */
  6311. public function testEditFormWithData() {
  6312. $this->Form->request->data = array('Person' => array(
  6313. 'id' => 1,
  6314. 'first_name' => 'Nate',
  6315. 'last_name' => 'Abele',
  6316. 'email' => 'nate@example.com'
  6317. ));
  6318. $this->Form->request->addParams(array(
  6319. 'models' => array('Person'),
  6320. 'controller' => 'people',
  6321. 'action' => 'add'
  6322. ));
  6323. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6324. $this->Form->create();
  6325. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6326. $expected = array(
  6327. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6328. 'select' => array(
  6329. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6330. ),
  6331. array('option' => array('value' => 1)), 'Nate', '/option',
  6332. array('option' => array('value' => 2)), 'Garrett', '/option',
  6333. array('option' => array('value' => 3)), 'Larry', '/option',
  6334. '/select'
  6335. );
  6336. $this->assertTags($result, $expected);
  6337. }
  6338. /**
  6339. * Test that required fields are created for various types of validation.
  6340. *
  6341. * @return void
  6342. */
  6343. public function testFormInputRequiredDetection() {
  6344. $this->Form->create('Contact');
  6345. $result = $this->Form->input('Contact.non_existing');
  6346. $expected = array(
  6347. 'div' => array('class' => 'input text'),
  6348. 'label' => array('for' => 'ContactNonExisting'),
  6349. 'Non Existing',
  6350. '/label',
  6351. 'input' => array(
  6352. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6353. 'id' => 'ContactNonExisting'
  6354. ),
  6355. '/div'
  6356. );
  6357. $this->assertTags($result, $expected);
  6358. $result = $this->Form->input('Contact.imrequired');
  6359. $expected = array(
  6360. 'div' => array('class' => 'input text required'),
  6361. 'label' => array('for' => 'ContactImrequired'),
  6362. 'Imrequired',
  6363. '/label',
  6364. 'input' => array(
  6365. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6366. 'id' => 'ContactImrequired'
  6367. ),
  6368. '/div'
  6369. );
  6370. $this->assertTags($result, $expected);
  6371. $result = $this->Form->input('Contact.imalsorequired');
  6372. $expected = array(
  6373. 'div' => array('class' => 'input text required'),
  6374. 'label' => array('for' => 'ContactImalsorequired'),
  6375. 'Imalsorequired',
  6376. '/label',
  6377. 'input' => array(
  6378. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6379. 'id' => 'ContactImalsorequired'
  6380. ),
  6381. '/div'
  6382. );
  6383. $this->assertTags($result, $expected);
  6384. $result = $this->Form->input('Contact.imrequiredtoo');
  6385. $expected = array(
  6386. 'div' => array('class' => 'input text required'),
  6387. 'label' => array('for' => 'ContactImrequiredtoo'),
  6388. 'Imrequiredtoo',
  6389. '/label',
  6390. 'input' => array(
  6391. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6392. 'id' => 'ContactImrequiredtoo'
  6393. ),
  6394. '/div'
  6395. );
  6396. $this->assertTags($result, $expected);
  6397. $result = $this->Form->input('Contact.required_one');
  6398. $expected = array(
  6399. 'div' => array('class' => 'input text required'),
  6400. 'label' => array('for' => 'ContactRequiredOne'),
  6401. 'Required One',
  6402. '/label',
  6403. 'input' => array(
  6404. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6405. 'id' => 'ContactRequiredOne'
  6406. ),
  6407. '/div'
  6408. );
  6409. $this->assertTags($result, $expected);
  6410. $result = $this->Form->input('Contact.string_required');
  6411. $expected = array(
  6412. 'div' => array('class' => 'input text required'),
  6413. 'label' => array('for' => 'ContactStringRequired'),
  6414. 'String Required',
  6415. '/label',
  6416. 'input' => array(
  6417. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6418. 'id' => 'ContactStringRequired'
  6419. ),
  6420. '/div'
  6421. );
  6422. $this->assertTags($result, $expected);
  6423. $result = $this->Form->input('Contact.imnotrequired');
  6424. $expected = array(
  6425. 'div' => array('class' => 'input text'),
  6426. 'label' => array('for' => 'ContactImnotrequired'),
  6427. 'Imnotrequired',
  6428. '/label',
  6429. 'input' => array(
  6430. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6431. 'id' => 'ContactImnotrequired'
  6432. ),
  6433. '/div'
  6434. );
  6435. $this->assertTags($result, $expected);
  6436. $result = $this->Form->input('Contact.imalsonotrequired');
  6437. $expected = array(
  6438. 'div' => array('class' => 'input text'),
  6439. 'label' => array('for' => 'ContactImalsonotrequired'),
  6440. 'Imalsonotrequired',
  6441. '/label',
  6442. 'input' => array(
  6443. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6444. 'id' => 'ContactImalsonotrequired'
  6445. ),
  6446. '/div'
  6447. );
  6448. $this->assertTags($result, $expected);
  6449. $result = $this->Form->input('Contact.imnotrequiredeither');
  6450. $expected = array(
  6451. 'div' => array('class' => 'input text'),
  6452. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6453. 'Imnotrequiredeither',
  6454. '/label',
  6455. 'input' => array(
  6456. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6457. 'id' => 'ContactImnotrequiredeither'
  6458. ),
  6459. '/div'
  6460. );
  6461. $this->assertTags($result, $expected);
  6462. }
  6463. /**
  6464. * testFormMagicInput method
  6465. *
  6466. * @return void
  6467. */
  6468. public function testFormMagicInput() {
  6469. $encoding = strtolower(Configure::read('App.encoding'));
  6470. $result = $this->Form->create('Contact');
  6471. $expected = array(
  6472. 'form' => array(
  6473. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6474. 'accept-charset' => $encoding
  6475. ),
  6476. 'div' => array('style' => 'display:none;'),
  6477. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6478. '/div'
  6479. );
  6480. $this->assertTags($result, $expected);
  6481. $result = $this->Form->input('name');
  6482. $expected = array(
  6483. 'div' => array('class' => 'input text'),
  6484. 'label' => array('for' => 'ContactName'),
  6485. 'Name',
  6486. '/label',
  6487. 'input' => array(
  6488. 'type' => 'text', 'name' => 'data[Contact][name]',
  6489. 'id' => 'ContactName', 'maxlength' => '255'
  6490. ),
  6491. '/div'
  6492. );
  6493. $this->assertTags($result, $expected);
  6494. $result = $this->Form->input('non_existing_field_in_contact_model');
  6495. $expected = array(
  6496. 'div' => array('class' => 'input text'),
  6497. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6498. 'Non Existing Field In Contact Model',
  6499. '/label',
  6500. 'input' => array(
  6501. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6502. 'id' => 'ContactNonExistingFieldInContactModel'
  6503. ),
  6504. '/div'
  6505. );
  6506. $this->assertTags($result, $expected);
  6507. $result = $this->Form->input('Address.street');
  6508. $expected = array(
  6509. 'div' => array('class' => 'input text'),
  6510. 'label' => array('for' => 'AddressStreet'),
  6511. 'Street',
  6512. '/label',
  6513. 'input' => array(
  6514. 'type' => 'text', 'name' => 'data[Address][street]',
  6515. 'id' => 'AddressStreet'
  6516. ),
  6517. '/div'
  6518. );
  6519. $this->assertTags($result, $expected);
  6520. $result = $this->Form->input('Address.non_existing_field_in_model');
  6521. $expected = array(
  6522. 'div' => array('class' => 'input text'),
  6523. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6524. 'Non Existing Field In Model',
  6525. '/label',
  6526. 'input' => array(
  6527. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6528. 'id' => 'AddressNonExistingFieldInModel'
  6529. ),
  6530. '/div'
  6531. );
  6532. $this->assertTags($result, $expected);
  6533. $result = $this->Form->input('name', array('div' => false));
  6534. $expected = array(
  6535. 'label' => array('for' => 'ContactName'),
  6536. 'Name',
  6537. '/label',
  6538. 'input' => array(
  6539. 'type' => 'text', 'name' => 'data[Contact][name]',
  6540. 'id' => 'ContactName', 'maxlength' => '255'
  6541. )
  6542. );
  6543. $this->assertTags($result, $expected);
  6544. extract($this->dateRegex);
  6545. $now = strtotime('now');
  6546. $result = $this->Form->input('Contact.published', array('div' => false));
  6547. $expected = array(
  6548. 'label' => array('for' => 'ContactPublishedMonth'),
  6549. 'Published',
  6550. '/label',
  6551. array('select' => array(
  6552. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6553. )),
  6554. $monthsRegex,
  6555. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6556. date('F', $now),
  6557. '/option',
  6558. '*/select',
  6559. '-',
  6560. array('select' => array(
  6561. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6562. )),
  6563. $daysRegex,
  6564. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6565. date('j', $now),
  6566. '/option',
  6567. '*/select',
  6568. '-',
  6569. array('select' => array(
  6570. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6571. )),
  6572. $yearsRegex,
  6573. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6574. date('Y', $now),
  6575. '*/select'
  6576. );
  6577. $this->assertTags($result, $expected);
  6578. $result = $this->Form->input('Contact.updated', array('div' => false));
  6579. $expected = array(
  6580. 'label' => array('for' => 'ContactUpdatedMonth'),
  6581. 'Updated',
  6582. '/label',
  6583. array('select' => array(
  6584. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6585. )),
  6586. $monthsRegex,
  6587. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6588. date('F', $now),
  6589. '/option',
  6590. '*/select',
  6591. '-',
  6592. array('select' => array(
  6593. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6594. )),
  6595. $daysRegex,
  6596. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6597. date('j', $now),
  6598. '/option',
  6599. '*/select',
  6600. '-',
  6601. array('select' => array(
  6602. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6603. )),
  6604. $yearsRegex,
  6605. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6606. date('Y', $now),
  6607. '*/select'
  6608. );
  6609. $this->assertTags($result, $expected);
  6610. $result = $this->Form->input('UserForm.stuff');
  6611. $expected = array(
  6612. 'div' => array('class' => 'input text'),
  6613. 'label' => array('for' => 'UserFormStuff'),
  6614. 'Stuff',
  6615. '/label',
  6616. 'input' => array(
  6617. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6618. 'id' => 'UserFormStuff', 'maxlength' => 10
  6619. ),
  6620. '/div'
  6621. );
  6622. $this->assertTags($result, $expected);
  6623. }
  6624. /**
  6625. * testForMagicInputNonExistingNorValidated method
  6626. *
  6627. * @return void
  6628. */
  6629. public function testForMagicInputNonExistingNorValidated() {
  6630. $encoding = strtolower(Configure::read('App.encoding'));
  6631. $result = $this->Form->create('Contact');
  6632. $expected = array(
  6633. 'form' => array(
  6634. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6635. 'accept-charset' => $encoding
  6636. ),
  6637. 'div' => array('style' => 'display:none;'),
  6638. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6639. '/div'
  6640. );
  6641. $this->assertTags($result, $expected);
  6642. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6643. $expected = array(
  6644. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6645. 'Non Existing Nor Validated',
  6646. '/label',
  6647. 'input' => array(
  6648. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6649. 'id' => 'ContactNonExistingNorValidated'
  6650. )
  6651. );
  6652. $this->assertTags($result, $expected);
  6653. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6654. 'div' => false, 'value' => 'my value'
  6655. ));
  6656. $expected = array(
  6657. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6658. 'Non Existing Nor Validated',
  6659. '/label',
  6660. 'input' => array(
  6661. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6662. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6663. )
  6664. );
  6665. $this->assertTags($result, $expected);
  6666. $this->Form->request->data = array(
  6667. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6668. ));
  6669. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6670. $expected = array(
  6671. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6672. 'Non Existing Nor Validated',
  6673. '/label',
  6674. 'input' => array(
  6675. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6676. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6677. )
  6678. );
  6679. $this->assertTags($result, $expected);
  6680. }
  6681. /**
  6682. * testFormMagicInputLabel method
  6683. *
  6684. * @return void
  6685. */
  6686. public function testFormMagicInputLabel() {
  6687. $encoding = strtolower(Configure::read('App.encoding'));
  6688. $result = $this->Form->create('Contact');
  6689. $expected = array(
  6690. 'form' => array(
  6691. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6692. 'accept-charset' => $encoding
  6693. ),
  6694. 'div' => array('style' => 'display:none;'),
  6695. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6696. '/div'
  6697. );
  6698. $this->assertTags($result, $expected);
  6699. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6700. $this->assertTags($result, array('input' => array(
  6701. 'name' => 'data[Contact][name]', 'type' => 'text',
  6702. 'id' => 'ContactName', 'maxlength' => '255')
  6703. ));
  6704. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6705. $expected = array(
  6706. 'label' => array('for' => 'ContactName'),
  6707. 'My label',
  6708. '/label',
  6709. 'input' => array(
  6710. 'type' => 'text', 'name' => 'data[Contact][name]',
  6711. 'id' => 'ContactName', 'maxlength' => '255'
  6712. )
  6713. );
  6714. $this->assertTags($result, $expected);
  6715. $result = $this->Form->input('Contact.name', array(
  6716. 'div' => false, 'label' => array('class' => 'mandatory')
  6717. ));
  6718. $expected = array(
  6719. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6720. 'Name',
  6721. '/label',
  6722. 'input' => array(
  6723. 'type' => 'text', 'name' => 'data[Contact][name]',
  6724. 'id' => 'ContactName', 'maxlength' => '255'
  6725. )
  6726. );
  6727. $this->assertTags($result, $expected);
  6728. $result = $this->Form->input('Contact.name', array(
  6729. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6730. ));
  6731. $expected = array(
  6732. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6733. 'My label',
  6734. '/label',
  6735. 'input' => array(
  6736. 'type' => 'text', 'name' => 'data[Contact][name]',
  6737. 'id' => 'ContactName', 'maxlength' => '255'
  6738. )
  6739. );
  6740. $this->assertTags($result, $expected);
  6741. $result = $this->Form->input('Contact.name', array(
  6742. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6743. ));
  6744. $expected = array(
  6745. 'label' => array('for' => 'my_id'),
  6746. 'Name',
  6747. '/label',
  6748. 'input' => array(
  6749. 'type' => 'text', 'name' => 'data[Contact][name]',
  6750. 'id' => 'my_id', 'maxlength' => '255'
  6751. )
  6752. );
  6753. $this->assertTags($result, $expected);
  6754. $result = $this->Form->input('1.id');
  6755. $this->assertTags($result, array('input' => array(
  6756. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6757. 'id' => 'Contact1Id'
  6758. )));
  6759. $result = $this->Form->input("1.name");
  6760. $expected = array(
  6761. 'div' => array('class' => 'input text'),
  6762. 'label' => array('for' => 'Contact1Name'),
  6763. 'Name',
  6764. '/label',
  6765. 'input' => array(
  6766. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6767. 'id' => 'Contact1Name', 'maxlength' => '255'
  6768. ),
  6769. '/div'
  6770. );
  6771. $this->assertTags($result, $expected);
  6772. $result = $this->Form->input('Contact.1.id');
  6773. $this->assertTags($result, array(
  6774. 'input' => array(
  6775. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6776. 'id' => 'Contact1Id'
  6777. )
  6778. ));
  6779. $result = $this->Form->input("Model.1.name");
  6780. $expected = array(
  6781. 'div' => array('class' => 'input text'),
  6782. 'label' => array('for' => 'Model1Name'),
  6783. 'Name',
  6784. '/label',
  6785. 'input' => array(
  6786. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6787. 'id' => 'Model1Name'
  6788. ),
  6789. '/div'
  6790. );
  6791. $this->assertTags($result, $expected);
  6792. }
  6793. /**
  6794. * testFormEnd method
  6795. *
  6796. * @return void
  6797. */
  6798. public function testFormEnd() {
  6799. $this->assertEquals('</form>', $this->Form->end());
  6800. $result = $this->Form->end('');
  6801. $expected = array(
  6802. 'div' => array('class' => 'submit'),
  6803. 'input' => array('type' => 'submit', 'value' => ''),
  6804. '/div',
  6805. '/form'
  6806. );
  6807. $this->assertTags($result, $expected);
  6808. $result = $this->Form->end(array('label' => ''));
  6809. $expected = array(
  6810. 'div' => array('class' => 'submit'),
  6811. 'input' => array('type' => 'submit', 'value' => ''),
  6812. '/div',
  6813. '/form'
  6814. );
  6815. $this->assertTags($result, $expected);
  6816. $result = $this->Form->end('save');
  6817. $expected = array(
  6818. 'div' => array('class' => 'submit'),
  6819. 'input' => array('type' => 'submit', 'value' => 'save'),
  6820. '/div',
  6821. '/form'
  6822. );
  6823. $this->assertTags($result, $expected);
  6824. $result = $this->Form->end(array('label' => 'save'));
  6825. $expected = array(
  6826. 'div' => array('class' => 'submit'),
  6827. 'input' => array('type' => 'submit', 'value' => 'save'),
  6828. '/div',
  6829. '/form'
  6830. );
  6831. $this->assertTags($result, $expected);
  6832. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6833. $expected = array(
  6834. 'div' => array('class' => 'submit'),
  6835. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6836. '/div',
  6837. '/form'
  6838. );
  6839. $this->assertTags($result, $expected);
  6840. $result = $this->Form->end(array('name' => 'Whatever'));
  6841. $expected = array(
  6842. 'div' => array('class' => 'submit'),
  6843. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6844. '/div',
  6845. '/form'
  6846. );
  6847. $this->assertTags($result, $expected);
  6848. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6849. $expected = array(
  6850. 'div' => array('class' => 'good'),
  6851. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6852. '/div',
  6853. '/form'
  6854. );
  6855. $this->assertTags($result, $expected);
  6856. $result = $this->Form->end(array(
  6857. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6858. ));
  6859. $expected = array(
  6860. 'div' => array('class' => 'good'),
  6861. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6862. '/div',
  6863. '/form'
  6864. );
  6865. $this->assertTags($result, $expected);
  6866. }
  6867. /**
  6868. * testMultipleFormWithIdFields method
  6869. *
  6870. * @return void
  6871. */
  6872. public function testMultipleFormWithIdFields() {
  6873. $this->Form->create('UserForm');
  6874. $result = $this->Form->input('id');
  6875. $this->assertTags($result, array('input' => array(
  6876. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6877. )));
  6878. $result = $this->Form->input('ValidateItem.id');
  6879. $this->assertTags($result, array('input' => array(
  6880. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6881. 'id' => 'ValidateItemId'
  6882. )));
  6883. $result = $this->Form->input('ValidateUser.id');
  6884. $this->assertTags($result, array('input' => array(
  6885. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6886. 'id' => 'ValidateUserId'
  6887. )));
  6888. }
  6889. /**
  6890. * testDbLessModel method
  6891. *
  6892. * @return void
  6893. */
  6894. public function testDbLessModel() {
  6895. $this->Form->create('TestMail');
  6896. $result = $this->Form->input('name');
  6897. $expected = array(
  6898. 'div' => array('class' => 'input text'),
  6899. 'label' => array('for' => 'TestMailName'),
  6900. 'Name',
  6901. '/label',
  6902. 'input' => array(
  6903. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6904. 'id' => 'TestMailName'
  6905. ),
  6906. '/div'
  6907. );
  6908. $this->assertTags($result, $expected);
  6909. ClassRegistry::init('TestMail');
  6910. $this->Form->create('TestMail');
  6911. $result = $this->Form->input('name');
  6912. $expected = array(
  6913. 'div' => array('class' => 'input text'),
  6914. 'label' => array('for' => 'TestMailName'),
  6915. 'Name',
  6916. '/label',
  6917. 'input' => array(
  6918. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6919. 'id' => 'TestMailName'
  6920. ),
  6921. '/div'
  6922. );
  6923. $this->assertTags($result, $expected);
  6924. }
  6925. /**
  6926. * testBrokenness method
  6927. *
  6928. * @return void
  6929. */
  6930. public function testBrokenness() {
  6931. /*
  6932. * #4 This test has two parents and four children. By default (as of r7117) both
  6933. * parents are show but the first parent is missing a child. This is the inconsistency
  6934. * in the default behaviour - one parent has all children, the other does not - dependent
  6935. * on the data values.
  6936. */
  6937. $result = $this->Form->select('Model.field', array(
  6938. 'Fred' => array(
  6939. 'freds_son_1' => 'Fred',
  6940. 'freds_son_2' => 'Freddie'
  6941. ),
  6942. 'Bert' => array(
  6943. 'berts_son_1' => 'Albert',
  6944. 'berts_son_2' => 'Bertie')
  6945. ),
  6946. array('showParents' => true, 'empty' => false)
  6947. );
  6948. $expected = array(
  6949. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6950. array('optgroup' => array('label' => 'Fred')),
  6951. array('option' => array('value' => 'freds_son_1')),
  6952. 'Fred',
  6953. '/option',
  6954. array('option' => array('value' => 'freds_son_2')),
  6955. 'Freddie',
  6956. '/option',
  6957. '/optgroup',
  6958. array('optgroup' => array('label' => 'Bert')),
  6959. array('option' => array('value' => 'berts_son_1')),
  6960. 'Albert',
  6961. '/option',
  6962. array('option' => array('value' => 'berts_son_2')),
  6963. 'Bertie',
  6964. '/option',
  6965. '/optgroup',
  6966. '/select'
  6967. );
  6968. $this->assertTags($result, $expected);
  6969. /*
  6970. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6971. * changed, so we should expect the same select list data, just with a different name
  6972. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6973. * This is where data corruption can occur, because when a select value is missing from
  6974. * a list a form will substitute the first value in the list - without the user knowing.
  6975. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6976. * affect the availability of 3 => 'Three' as a valid option.
  6977. */
  6978. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6979. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6980. ));
  6981. $result = $this->Form->select(
  6982. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6983. );
  6984. $expected = array(
  6985. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6986. array('option' => array('value' => 1)),
  6987. 'One',
  6988. '/option',
  6989. array('option' => array('value' => 2)),
  6990. 'Two',
  6991. '/option',
  6992. array('optgroup' => array('label' => 'Three')),
  6993. array('option' => array('value' => 3)),
  6994. 'Three',
  6995. '/option',
  6996. array('option' => array('value' => 4)),
  6997. 'Four',
  6998. '/option',
  6999. array('option' => array('value' => 5)),
  7000. 'Five',
  7001. '/option',
  7002. '/optgroup',
  7003. '/select'
  7004. );
  7005. $this->assertTags($result, $expected);
  7006. }
  7007. /**
  7008. * Test the generation of fields for a multi record form.
  7009. *
  7010. * @return void
  7011. */
  7012. public function testMultiRecordForm() {
  7013. $this->Form->create('ValidateProfile');
  7014. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7015. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7016. $expected = array(
  7017. 'div' => array('class' => 'input textarea'),
  7018. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7019. 'Name',
  7020. '/label',
  7021. 'textarea' => array(
  7022. 'id' => 'ValidateProfile1ValidateItem2Name',
  7023. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7024. 'cols' => 30,
  7025. 'rows' => 6
  7026. ),
  7027. 'Value',
  7028. '/textarea',
  7029. '/div'
  7030. );
  7031. $this->assertTags($result, $expected);
  7032. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7033. $expected = array(
  7034. 'div' => array('class' => 'input date'),
  7035. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7036. 'Created',
  7037. '/label',
  7038. array('select' => array(
  7039. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7040. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7041. )
  7042. ),
  7043. array('option' => array('value' => '')), '/option',
  7044. $this->dateRegex['monthsRegex'],
  7045. '/select', '-',
  7046. array('select' => array(
  7047. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7048. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7049. )
  7050. ),
  7051. array('option' => array('value' => '')), '/option',
  7052. $this->dateRegex['daysRegex'],
  7053. '/select', '-',
  7054. array('select' => array(
  7055. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7056. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7057. )
  7058. ),
  7059. array('option' => array('value' => '')), '/option',
  7060. $this->dateRegex['yearsRegex'],
  7061. '/select',
  7062. '/div'
  7063. );
  7064. $this->assertTags($result, $expected);
  7065. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7066. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7067. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7068. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7069. $expected = array(
  7070. 'div' => array('class' => 'input select error'),
  7071. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7072. 'Profile',
  7073. '/label',
  7074. 'select' => array(
  7075. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7076. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7077. 'class' => 'form-error'
  7078. ),
  7079. '/select',
  7080. array('div' => array('class' => 'error-message')),
  7081. 'Error',
  7082. '/div',
  7083. '/div'
  7084. );
  7085. $this->assertTags($result, $expected);
  7086. }
  7087. /**
  7088. * test the correct display of multi-record form validation errors.
  7089. *
  7090. * @return void
  7091. */
  7092. public function testMultiRecordFormValidationErrors() {
  7093. $this->Form->create('ValidateProfile');
  7094. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7095. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7096. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7097. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7098. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7099. $result = $this->Form->error('ValidateProfile.2.city');
  7100. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7101. $result = $this->Form->error('2.city');
  7102. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7103. }
  7104. /**
  7105. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7106. *
  7107. * @return void
  7108. */
  7109. public function testInputTemplate() {
  7110. $result = $this->Form->input('Contact.email', array(
  7111. 'type' => 'text', 'format' => array('input')
  7112. ));
  7113. $expected = array(
  7114. 'div' => array('class' => 'input text'),
  7115. 'input' => array(
  7116. 'type' => 'text', 'name' => 'data[Contact][email]',
  7117. 'id' => 'ContactEmail'
  7118. ),
  7119. '/div'
  7120. );
  7121. $this->assertTags($result, $expected);
  7122. $result = $this->Form->input('Contact.email', array(
  7123. 'type' => 'text', 'format' => array('input', 'label'),
  7124. 'label' => '<em>Email (required)</em>'
  7125. ));
  7126. $expected = array(
  7127. 'div' => array('class' => 'input text'),
  7128. array('input' => array(
  7129. 'type' => 'text', 'name' => 'data[Contact][email]',
  7130. 'id' => 'ContactEmail'
  7131. )),
  7132. 'label' => array('for' => 'ContactEmail'),
  7133. 'em' => array(),
  7134. 'Email (required)',
  7135. '/em',
  7136. '/label',
  7137. '/div'
  7138. );
  7139. $this->assertTags($result, $expected);
  7140. $result = $this->Form->input('Contact.email', array(
  7141. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7142. 'between' => '<div>Something in the middle</div>',
  7143. 'after' => '<span>Some text at the end</span>'
  7144. ));
  7145. $expected = array(
  7146. 'div' => array('class' => 'input text'),
  7147. array('input' => array(
  7148. 'type' => 'text', 'name' => 'data[Contact][email]',
  7149. 'id' => 'ContactEmail'
  7150. )),
  7151. array('div' => array()),
  7152. 'Something in the middle',
  7153. '/div',
  7154. 'label' => array('for' => 'ContactEmail'),
  7155. 'Email',
  7156. '/label',
  7157. 'span' => array(),
  7158. 'Some text at the end',
  7159. '/span',
  7160. '/div'
  7161. );
  7162. $this->assertTags($result, $expected);
  7163. $result = $this->Form->input('Contact.method', array(
  7164. 'type' => 'radio',
  7165. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7166. 'between' => 'I am between',
  7167. ));
  7168. $expected = array(
  7169. 'div' => array('class' => 'input radio'),
  7170. 'fieldset' => array(),
  7171. 'legend' => array(),
  7172. 'Method',
  7173. '/legend',
  7174. 'I am between',
  7175. 'input' => array(
  7176. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7177. 'value' => '', 'id' => 'ContactMethod_'
  7178. ),
  7179. array('input' => array(
  7180. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7181. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7182. )),
  7183. array('label' => array('for' => 'ContactMethodEmail')),
  7184. 'Email',
  7185. '/label',
  7186. array('input' => array(
  7187. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7188. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7189. )),
  7190. array('label' => array('for' => 'ContactMethodPigeon')),
  7191. 'Pigeon',
  7192. '/label',
  7193. '/fieldset',
  7194. '/div',
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. }
  7198. /**
  7199. * test that some html5 inputs + FormHelper::__call() work
  7200. *
  7201. * @return void
  7202. */
  7203. public function testHtml5Inputs() {
  7204. $result = $this->Form->email('User.email');
  7205. $expected = array(
  7206. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7207. );
  7208. $this->assertTags($result, $expected);
  7209. $result = $this->Form->search('User.query');
  7210. $expected = array(
  7211. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7212. );
  7213. $this->assertTags($result, $expected);
  7214. $result = $this->Form->search('User.query', array('value' => 'test'));
  7215. $expected = array(
  7216. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7217. );
  7218. $this->assertTags($result, $expected);
  7219. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7220. $expected = array(
  7221. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7222. );
  7223. $this->assertTags($result, $expected);
  7224. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7225. $expected = array(
  7226. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7227. );
  7228. $this->assertTags($result, $expected);
  7229. }
  7230. /**
  7231. *
  7232. * @expectedException CakeException
  7233. * @return void
  7234. */
  7235. public function testHtml5InputException() {
  7236. $this->Form->email();
  7237. }
  7238. /**
  7239. * Tests that a model can be loaded from the model names passed in the request object
  7240. *
  7241. * @return void
  7242. */
  7243. public function testIntrospectModelFromRequest() {
  7244. $this->loadFixtures('Post');
  7245. App::build(array(
  7246. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7247. ));
  7248. CakePlugin::load('TestPlugin');
  7249. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7250. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7251. $this->Form->create('TestPluginPost');
  7252. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7253. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7254. CakePlugin::unload();
  7255. App::build();
  7256. }
  7257. /**
  7258. * Tests that it is possible to set the validation errors directly in the helper for a field
  7259. *
  7260. * @return void
  7261. */
  7262. public function testCustomValidationErrors() {
  7263. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7264. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7265. $this->assertEquals('Badness!', $result);
  7266. }
  7267. /**
  7268. * Tests that the 'on' key validates as expected on create
  7269. *
  7270. * @return void
  7271. */
  7272. public function testRequiredOnCreate() {
  7273. $this->Form->create('Contact');
  7274. $result = $this->Form->input('Contact.imrequiredonupdate');
  7275. $expected = array(
  7276. 'div' => array('class' => 'input text'),
  7277. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7278. 'Imrequiredonupdate',
  7279. '/label',
  7280. 'input' => array(
  7281. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7282. 'id' => 'ContactImrequiredonupdate'
  7283. ),
  7284. '/div'
  7285. );
  7286. $this->assertTags($result, $expected);
  7287. $result = $this->Form->input('Contact.imrequiredoncreate');
  7288. $expected = array(
  7289. 'div' => array('class' => 'input text required'),
  7290. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7291. 'Imrequiredoncreate',
  7292. '/label',
  7293. 'input' => array(
  7294. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7295. 'id' => 'ContactImrequiredoncreate'
  7296. ),
  7297. '/div'
  7298. );
  7299. $this->assertTags($result, $expected);
  7300. $result = $this->Form->input('Contact.imrequiredonboth');
  7301. $expected = array(
  7302. 'div' => array('class' => 'input text required'),
  7303. 'label' => array('for' => 'ContactImrequiredonboth'),
  7304. 'Imrequiredonboth',
  7305. '/label',
  7306. 'input' => array(
  7307. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7308. 'id' => 'ContactImrequiredonboth'
  7309. ),
  7310. '/div'
  7311. );
  7312. $this->assertTags($result, $expected);
  7313. $result = $this->Form->input('Contact.imrequired');
  7314. $expected = array(
  7315. 'div' => array('class' => 'input text required'),
  7316. 'label' => array('for' => 'ContactImrequired'),
  7317. 'Imrequired',
  7318. '/label',
  7319. 'input' => array(
  7320. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7321. 'id' => 'ContactImrequired'
  7322. ),
  7323. '/div'
  7324. );
  7325. $this->assertTags($result, $expected);
  7326. }
  7327. /**
  7328. * Tests that the 'on' key validates as expected on update
  7329. *
  7330. * @return void
  7331. */
  7332. public function testRequiredOnUpdate() {
  7333. $this->Form->request->data['Contact']['id'] = 1;
  7334. $this->Form->create('Contact');
  7335. $result = $this->Form->input('Contact.imrequiredonupdate');
  7336. $expected = array(
  7337. 'div' => array('class' => 'input text required'),
  7338. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7339. 'Imrequiredonupdate',
  7340. '/label',
  7341. 'input' => array(
  7342. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7343. 'id' => 'ContactImrequiredonupdate'
  7344. ),
  7345. '/div'
  7346. );
  7347. $this->assertTags($result, $expected);
  7348. $result = $this->Form->input('Contact.imrequiredoncreate');
  7349. $expected = array(
  7350. 'div' => array('class' => 'input text'),
  7351. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7352. 'Imrequiredoncreate',
  7353. '/label',
  7354. 'input' => array(
  7355. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7356. 'id' => 'ContactImrequiredoncreate'
  7357. ),
  7358. '/div'
  7359. );
  7360. $this->assertTags($result, $expected);
  7361. $result = $this->Form->input('Contact.imrequiredonboth');
  7362. $expected = array(
  7363. 'div' => array('class' => 'input text required'),
  7364. 'label' => array('for' => 'ContactImrequiredonboth'),
  7365. 'Imrequiredonboth',
  7366. '/label',
  7367. 'input' => array(
  7368. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7369. 'id' => 'ContactImrequiredonboth'
  7370. ),
  7371. '/div'
  7372. );
  7373. $this->assertTags($result, $expected);
  7374. $result = $this->Form->input('Contact.imrequired');
  7375. $expected = array(
  7376. 'div' => array('class' => 'input text required'),
  7377. 'label' => array('for' => 'ContactImrequired'),
  7378. 'Imrequired',
  7379. '/label',
  7380. 'input' => array(
  7381. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7382. 'id' => 'ContactImrequired'
  7383. ),
  7384. '/div'
  7385. );
  7386. $this->assertTags($result, $expected);
  7387. }
  7388. /**
  7389. * Test inputDefaults setter and getter
  7390. *
  7391. * @return void
  7392. */
  7393. public function testInputDefaults() {
  7394. $this->Form->create('Contact');
  7395. $this->Form->inputDefaults(array(
  7396. 'label' => false,
  7397. 'div' => array(
  7398. 'style' => 'color: #000;'
  7399. )
  7400. ));
  7401. $result = $this->Form->input('Contact.field1');
  7402. $expected = array(
  7403. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7404. 'input' => array(
  7405. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7406. 'id' => 'ContactField1'
  7407. ),
  7408. '/div'
  7409. );
  7410. $this->assertTags($result, $expected);
  7411. $this->Form->inputDefaults(array(
  7412. 'div' => false,
  7413. 'label' => 'Label',
  7414. ));
  7415. $result = $this->Form->input('Contact.field1');
  7416. $expected = array(
  7417. 'label' => array('for' => 'ContactField1'),
  7418. 'Label',
  7419. '/label',
  7420. 'input' => array(
  7421. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7422. 'id' => 'ContactField1'
  7423. ),
  7424. );
  7425. $this->assertTags($result, $expected);
  7426. $this->Form->inputDefaults(array(
  7427. 'label' => false,
  7428. ), true);
  7429. $result = $this->Form->input('Contact.field1');
  7430. $expected = array(
  7431. 'input' => array(
  7432. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7433. 'id' => 'ContactField1'
  7434. ),
  7435. );
  7436. $this->assertTags($result, $expected);
  7437. $result = $this->Form->inputDefaults();
  7438. $expected = array(
  7439. 'div' => false,
  7440. 'label' => false,
  7441. );
  7442. $this->assertEqual($result, $expected);
  7443. }
  7444. }