FormHelperTest.php 264 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357
  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)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $hash .= ':' . 'Model.valid';
  710. $hash = urlencode($hash);
  711. $expected = array(
  712. 'div' => array('style' => 'display:none;'),
  713. array('input' => array(
  714. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  715. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  716. )),
  717. array('input' => array(
  718. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  719. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  720. )),
  721. '/div'
  722. );
  723. $this->assertTags($result, $expected);
  724. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  725. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  726. $result = $this->Form->secure($fields);
  727. $expected = array(
  728. 'div' => array('class' => 'hidden'),
  729. array('input' => array(
  730. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  731. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  732. )),
  733. array('input' => array(
  734. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  735. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  736. )),
  737. '/div'
  738. );
  739. $this->assertTags($result, $expected);
  740. }
  741. /**
  742. * Tests correct generation of number fields for double and float fields
  743. *
  744. * @return void
  745. */
  746. public function testTextFieldGenerationForFloats() {
  747. $model = ClassRegistry::getObject('Contact');
  748. $model->setSchema(array('foo' => array(
  749. 'type' => 'float',
  750. 'null' => false,
  751. 'default' => null,
  752. 'length' => null
  753. )));
  754. $this->Form->create('Contact');
  755. $result = $this->Form->input('foo');
  756. $expected = array(
  757. 'div' => array('class' => 'input number'),
  758. 'label' => array('for' => 'ContactFoo'),
  759. 'Foo',
  760. '/label',
  761. array('input' => array(
  762. 'type' => 'number',
  763. 'name' => 'data[Contact][foo]',
  764. 'id' => 'ContactFoo',
  765. 'step' => 'any'
  766. )),
  767. '/div'
  768. );
  769. $this->assertTags($result, $expected);
  770. $result = $this->Form->input('foo', array('step' => 0.5));
  771. $expected = array(
  772. 'div' => array('class' => 'input number'),
  773. 'label' => array('for' => 'ContactFoo'),
  774. 'Foo',
  775. '/label',
  776. array('input' => array(
  777. 'type' => 'number',
  778. 'name' => 'data[Contact][foo]',
  779. 'id' => 'ContactFoo',
  780. 'step' => '0.5'
  781. )),
  782. '/div'
  783. );
  784. $this->assertTags($result, $expected);
  785. }
  786. /**
  787. * Tests correct generation of number fields for integer fields
  788. *
  789. * @access public
  790. * @return void
  791. */
  792. public function testTextFieldTypeNumberGenerationForIntegers() {
  793. $model = ClassRegistry::getObject('Contact');
  794. $model->setSchema(array('foo' => array(
  795. 'type' => 'integer',
  796. 'null' => false,
  797. 'default' => null,
  798. 'length' => null
  799. )));
  800. $this->Form->create('Contact');
  801. $result = $this->Form->input('foo');
  802. $expected = array(
  803. 'div' => array('class' => 'input number'),
  804. 'label' => array('for' => 'ContactFoo'),
  805. 'Foo',
  806. '/label',
  807. array('input' => array(
  808. 'type' => 'number', 'name' => 'data[Contact][foo]',
  809. 'id' => 'ContactFoo'
  810. )),
  811. '/div'
  812. );
  813. $this->assertTags($result, $expected);
  814. }
  815. /**
  816. * testFormSecurityMultipleFields method
  817. *
  818. * Test secure() with multiple row form. Ensure hash is correct.
  819. *
  820. * @return void
  821. */
  822. public function testFormSecurityMultipleFields() {
  823. $key = 'testKey';
  824. $fields = array(
  825. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  826. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  827. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  828. );
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $result = $this->Form->secure($fields);
  831. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  832. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  833. $expected = array(
  834. 'div' => array('style' => 'display:none;'),
  835. array('input' => array(
  836. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  837. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  838. )),
  839. array('input' => array(
  840. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  841. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  842. )),
  843. '/div'
  844. );
  845. $this->assertTags($result, $expected);
  846. }
  847. /**
  848. * testFormSecurityMultipleSubmitButtons
  849. *
  850. * test form submit generation and ensure that _Token is only created on end()
  851. *
  852. * @return void
  853. */
  854. public function testFormSecurityMultipleSubmitButtons() {
  855. $key = 'testKey';
  856. $this->Form->request['_Token'] = array('key' => $key);
  857. $this->Form->create('Addresses');
  858. $this->Form->input('Address.title');
  859. $this->Form->input('Address.first_name');
  860. $result = $this->Form->submit('Save', array('name' => 'save'));
  861. $expected = array(
  862. 'div' => array('class' => 'submit'),
  863. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  864. '/div',
  865. );
  866. $this->assertTags($result, $expected);
  867. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  868. $expected = array(
  869. 'div' => array('class' => 'submit'),
  870. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  871. '/div',
  872. );
  873. $this->assertTags($result, $expected);
  874. $result = $this->Form->end(null);
  875. $expected = array(
  876. 'div' => array('style' => 'display:none;'),
  877. array('input' => array(
  878. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  879. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  880. )),
  881. array('input' => array(
  882. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  883. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  884. )),
  885. '/div'
  886. );
  887. $this->assertTags($result, $expected);
  888. }
  889. /**
  890. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  891. *
  892. * @return void
  893. */
  894. public function testSecurityButtonNestedNamed() {
  895. $key = 'testKey';
  896. $this->Form->request['_Token'] = array('key' => $key);
  897. $this->Form->create('Addresses');
  898. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  899. $result = $this->Form->unlockField();
  900. $this->assertEquals(array('Address.button'), $result);
  901. }
  902. /**
  903. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  904. *
  905. * @return void
  906. */
  907. public function testSecuritySubmitNestedNamed() {
  908. $key = 'testKey';
  909. $this->Form->request['_Token'] = array('key' => $key);
  910. $this->Form->create('Addresses');
  911. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  912. $result = $this->Form->unlockField();
  913. $this->assertEquals(array('Address.button'), $result);
  914. }
  915. /**
  916. * Test that the correct fields are unlocked for image submits with no names.
  917. *
  918. * @return void
  919. */
  920. public function testSecuritySubmitImageNoName() {
  921. $key = 'testKey';
  922. $this->Form->request['_Token'] = array('key' => $key);
  923. $this->Form->create('User');
  924. $result = $this->Form->submit('save.png');
  925. $expected = array(
  926. 'div' => array('class' => 'submit'),
  927. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  928. '/div'
  929. );
  930. $this->assertTags($result, $expected);
  931. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  932. }
  933. /**
  934. * Test that the correct fields are unlocked for image submits with names.
  935. *
  936. * @return void
  937. */
  938. public function testSecuritySubmitImageName() {
  939. $key = 'testKey';
  940. $this->Form->request['_Token'] = array('key' => $key);
  941. $this->Form->create('User');
  942. $result = $this->Form->submit('save.png', array('name' => 'test'));
  943. $expected = array(
  944. 'div' => array('class' => 'submit'),
  945. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  946. '/div'
  947. );
  948. $this->assertTags($result, $expected);
  949. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  950. }
  951. /**
  952. * testFormSecurityMultipleInputFields method
  953. *
  954. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  955. *
  956. * @return void
  957. */
  958. public function testFormSecurityMultipleInputFields() {
  959. $key = 'testKey';
  960. $this->Form->request['_Token'] = array('key' => $key);
  961. $this->Form->create('Addresses');
  962. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  963. $this->Form->input('Addresses.0.title');
  964. $this->Form->input('Addresses.0.first_name');
  965. $this->Form->input('Addresses.0.last_name');
  966. $this->Form->input('Addresses.0.address');
  967. $this->Form->input('Addresses.0.city');
  968. $this->Form->input('Addresses.0.phone');
  969. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  970. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  971. $this->Form->input('Addresses.1.title');
  972. $this->Form->input('Addresses.1.first_name');
  973. $this->Form->input('Addresses.1.last_name');
  974. $this->Form->input('Addresses.1.address');
  975. $this->Form->input('Addresses.1.city');
  976. $this->Form->input('Addresses.1.phone');
  977. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  978. $result = $this->Form->secure($this->Form->fields);
  979. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  980. $expected = array(
  981. 'div' => array('style' => 'display:none;'),
  982. array('input' => array(
  983. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  984. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  985. )),
  986. array('input' => array(
  987. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  988. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  989. )),
  990. '/div'
  991. );
  992. $this->assertTags($result, $expected);
  993. }
  994. /**
  995. * Test form security with Model.field.0 style inputs
  996. *
  997. * @return void
  998. */
  999. public function testFormSecurityArrayFields() {
  1000. $key = 'testKey';
  1001. $this->Form->request->params['_Token']['key'] = $key;
  1002. $this->Form->create('Address');
  1003. $this->Form->input('Address.primary.1');
  1004. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1005. $this->Form->input('Address.secondary.1.0');
  1006. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1007. }
  1008. /**
  1009. * testFormSecurityMultipleInputDisabledFields method
  1010. *
  1011. * test secure form generation with multiple records and disabled fields.
  1012. *
  1013. * @return void
  1014. */
  1015. public function testFormSecurityMultipleInputDisabledFields() {
  1016. $key = 'testKey';
  1017. $this->Form->request->params['_Token'] = array(
  1018. 'key' => $key,
  1019. 'unlockedFields' => array('first_name', 'address')
  1020. );
  1021. $this->Form->create();
  1022. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1023. $this->Form->input('Addresses.0.title');
  1024. $this->Form->input('Addresses.0.first_name');
  1025. $this->Form->input('Addresses.0.last_name');
  1026. $this->Form->input('Addresses.0.address');
  1027. $this->Form->input('Addresses.0.city');
  1028. $this->Form->input('Addresses.0.phone');
  1029. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1030. $this->Form->input('Addresses.1.title');
  1031. $this->Form->input('Addresses.1.first_name');
  1032. $this->Form->input('Addresses.1.last_name');
  1033. $this->Form->input('Addresses.1.address');
  1034. $this->Form->input('Addresses.1.city');
  1035. $this->Form->input('Addresses.1.phone');
  1036. $result = $this->Form->secure($this->Form->fields);
  1037. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1038. $expected = array(
  1039. 'div' => array('style' => 'display:none;'),
  1040. array('input' => array(
  1041. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1042. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1043. )),
  1044. array('input' => array(
  1045. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1046. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1047. )),
  1048. '/div'
  1049. );
  1050. $this->assertTags($result, $expected);
  1051. }
  1052. /**
  1053. * testFormSecurityInputDisabledFields method
  1054. *
  1055. * Test single record form with disabled fields.
  1056. *
  1057. * @return void
  1058. */
  1059. public function testFormSecurityInputUnlockedFields() {
  1060. $key = 'testKey';
  1061. $this->Form->request['_Token'] = array(
  1062. 'key' => $key,
  1063. 'unlockedFields' => array('first_name', 'address')
  1064. );
  1065. $this->Form->create();
  1066. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1067. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1068. $this->Form->input('Addresses.title');
  1069. $this->Form->input('Addresses.first_name');
  1070. $this->Form->input('Addresses.last_name');
  1071. $this->Form->input('Addresses.address');
  1072. $this->Form->input('Addresses.city');
  1073. $this->Form->input('Addresses.phone');
  1074. $result = $this->Form->fields;
  1075. $expected = array(
  1076. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1077. 'Addresses.city', 'Addresses.phone'
  1078. );
  1079. $this->assertEquals($expected, $result);
  1080. $result = $this->Form->secure($expected);
  1081. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1082. $expected = array(
  1083. 'div' => array('style' => 'display:none;'),
  1084. array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1086. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1087. )),
  1088. array('input' => array(
  1089. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1090. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. }
  1096. /**
  1097. * test securing inputs with custom name attributes.
  1098. *
  1099. * @return void
  1100. */
  1101. public function testFormSecureWithCustomNameAttribute() {
  1102. $this->Form->request->params['_Token']['key'] = 'testKey';
  1103. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1104. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1105. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1106. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1107. }
  1108. /**
  1109. * testFormSecuredInput method
  1110. *
  1111. * Test generation of entire secure form, assertions made on input() output.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecuredInput() {
  1116. $this->Form->request['_Token'] = array('key' => 'testKey');
  1117. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1118. $encoding = strtolower(Configure::read('App.encoding'));
  1119. $expected = array(
  1120. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1121. 'div' => array('style' => 'display:none;'),
  1122. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1123. array('input' => array(
  1124. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1125. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1126. )),
  1127. '/div'
  1128. );
  1129. $this->assertTags($result, $expected);
  1130. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1131. $expected = array(
  1132. 'div' => array('class' => 'input text'),
  1133. 'label' => array('for' => 'UserFormPublished'),
  1134. 'Published',
  1135. '/label',
  1136. array('input' => array(
  1137. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1138. 'id' => 'UserFormPublished'
  1139. )),
  1140. '/div'
  1141. );
  1142. $this->assertTags($result, $expected);
  1143. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1144. $expected = array(
  1145. 'div' => array('class' => 'input text'),
  1146. 'label' => array('for' => 'UserFormOther'),
  1147. 'Other',
  1148. '/label',
  1149. array('input' => array(
  1150. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1151. 'id' => 'UserFormOther'
  1152. )),
  1153. '/div'
  1154. );
  1155. $this->assertTags($result, $expected);
  1156. $result = $this->Form->hidden('UserForm.stuff');
  1157. $expected = array('input' => array(
  1158. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1159. 'id' => 'UserFormStuff'
  1160. ));
  1161. $this->assertTags($result, $expected);
  1162. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1163. $expected = array('input' => array(
  1164. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1165. 'value' => '0', 'id' => 'UserFormHidden'
  1166. ));
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1169. $expected = array(
  1170. 'div' => array('class' => 'input checkbox'),
  1171. array('input' => array(
  1172. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1173. 'value' => '0', 'id' => 'UserFormSomething_'
  1174. )),
  1175. array('input' => array(
  1176. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1177. 'value' => '1', 'id' => 'UserFormSomething'
  1178. )),
  1179. 'label' => array('for' => 'UserFormSomething'),
  1180. 'Something',
  1181. '/label',
  1182. '/div'
  1183. );
  1184. $this->assertTags($result, $expected);
  1185. $result = $this->Form->fields;
  1186. $expected = array(
  1187. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1188. 'UserForm.hidden' => '0', 'UserForm.something'
  1189. );
  1190. $this->assertEquals($expected, $result);
  1191. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1192. $result = $this->Form->secure($this->Form->fields);
  1193. $expected = array(
  1194. 'div' => array('style' => 'display:none;'),
  1195. array('input' => array(
  1196. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1197. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1198. )),
  1199. array('input' => array(
  1200. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1201. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1202. )),
  1203. '/div'
  1204. );
  1205. $this->assertTags($result, $expected);
  1206. }
  1207. /**
  1208. * Tests that the correct keys are added to the field hash index
  1209. *
  1210. * @return void
  1211. */
  1212. public function testFormSecuredFileInput() {
  1213. $this->Form->request['_Token'] = array('key' => 'testKey');
  1214. $this->assertEquals(array(), $this->Form->fields);
  1215. $result = $this->Form->file('Attachment.file');
  1216. $expected = array(
  1217. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1218. 'Attachment.file.error', 'Attachment.file.size'
  1219. );
  1220. $this->assertEquals($expected, $this->Form->fields);
  1221. }
  1222. /**
  1223. * test that multiple selects keys are added to field hash
  1224. *
  1225. * @return void
  1226. */
  1227. public function testFormSecuredMultipleSelect() {
  1228. $this->Form->request['_Token'] = array('key' => 'testKey');
  1229. $this->assertEquals(array(), $this->Form->fields);
  1230. $options = array('1' => 'one', '2' => 'two');
  1231. $this->Form->select('Model.select', $options);
  1232. $expected = array('Model.select');
  1233. $this->assertEquals($expected, $this->Form->fields);
  1234. $this->Form->fields = array();
  1235. $this->Form->select('Model.select', $options, array('multiple' => true));
  1236. $this->assertEquals($expected, $this->Form->fields);
  1237. }
  1238. /**
  1239. * testFormSecuredRadio method
  1240. *
  1241. * @return void
  1242. */
  1243. public function testFormSecuredRadio() {
  1244. $this->Form->request['_Token'] = array('key' => 'testKey');
  1245. $this->assertEquals(array(), $this->Form->fields);
  1246. $options = array('1' => 'option1', '2' => 'option2');
  1247. $this->Form->radio('Test.test', $options);
  1248. $expected = array('Test.test');
  1249. $this->assertEquals($expected, $this->Form->fields);
  1250. }
  1251. /**
  1252. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1253. * hashing.
  1254. *
  1255. * @return void
  1256. */
  1257. public function testFormSecuredAndDisabled() {
  1258. $this->Form->request['_Token'] = array('key' => 'testKey');
  1259. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1260. $this->Form->text('Model.text', array('disabled' => true));
  1261. $this->Form->password('Model.text', array('disabled' => true));
  1262. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1263. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1264. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1265. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1266. $this->Form->month('Model.month', array('disabled' => true));
  1267. $this->Form->day('Model.day', array('disabled' => true));
  1268. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1269. $this->Form->minute('Model.minute', array('disabled' => true));
  1270. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1271. $expected = array(
  1272. 'Model.radio' => ''
  1273. );
  1274. $this->assertEquals($expected, $this->Form->fields);
  1275. }
  1276. /**
  1277. * testDisableSecurityUsingForm method
  1278. *
  1279. * @return void
  1280. */
  1281. public function testDisableSecurityUsingForm() {
  1282. $this->Form->request['_Token'] = array(
  1283. 'key' => 'testKey',
  1284. 'disabledFields' => array()
  1285. );
  1286. $this->Form->create();
  1287. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1288. $this->Form->input('Addresses.title');
  1289. $this->Form->input('Addresses.first_name', array('secure' => false));
  1290. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1291. $this->Form->input('Addresses.zip', array(
  1292. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1293. ));
  1294. $result = $this->Form->fields;
  1295. $expected = array(
  1296. 'Addresses.id' => '123456', 'Addresses.title',
  1297. );
  1298. $this->assertEquals($expected, $result);
  1299. }
  1300. /**
  1301. * test disableField
  1302. *
  1303. * @return void
  1304. */
  1305. public function testUnlockFieldAddsToList() {
  1306. $this->Form->request['_Token'] = array(
  1307. 'key' => 'testKey',
  1308. 'unlockedFields' => array()
  1309. );
  1310. $this->Form->create('Contact');
  1311. $this->Form->unlockField('Contact.name');
  1312. $this->Form->text('Contact.name');
  1313. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1314. $this->assertEquals(array(), $this->Form->fields);
  1315. }
  1316. /**
  1317. * test unlockField removing from fields array.
  1318. *
  1319. * @return void
  1320. */
  1321. public function testUnlockFieldRemovingFromFields() {
  1322. $this->Form->request['_Token'] = array(
  1323. 'key' => 'testKey',
  1324. 'unlockedFields' => array()
  1325. );
  1326. $this->Form->create('Contact');
  1327. $this->Form->hidden('Contact.id', array('value' => 1));
  1328. $this->Form->text('Contact.name');
  1329. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1330. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1331. $this->Form->unlockField('Contact.name');
  1332. $this->Form->unlockField('Contact.id');
  1333. $this->assertEquals(array(), $this->Form->fields);
  1334. }
  1335. /**
  1336. * testTagIsInvalid method
  1337. *
  1338. * @return void
  1339. */
  1340. public function testTagIsInvalid() {
  1341. $Contact = ClassRegistry::getObject('Contact');
  1342. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1343. $this->Form->setEntity('Contact.0.email');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $expected = array('Please provide an email');
  1346. $this->assertEquals($expected, $result);
  1347. $this->Form->setEntity('Contact.1.email');
  1348. $result = $this->Form->tagIsInvalid();
  1349. $expected = false;
  1350. $this->assertSame($expected, $result);
  1351. $this->Form->setEntity('Contact.0.name');
  1352. $result = $this->Form->tagIsInvalid();
  1353. $expected = false;
  1354. $this->assertSame($expected, $result);
  1355. }
  1356. /**
  1357. * Test validation errors.
  1358. *
  1359. * @return void
  1360. */
  1361. public function testPasswordValidation() {
  1362. $Contact = ClassRegistry::getObject('Contact');
  1363. $Contact->validationErrors['password'] = array('Please provide a password');
  1364. $result = $this->Form->input('Contact.password');
  1365. $expected = array(
  1366. 'div' => array('class' => 'input password error'),
  1367. 'label' => array('for' => 'ContactPassword'),
  1368. 'Password',
  1369. '/label',
  1370. 'input' => array(
  1371. 'type' => 'password', 'name' => 'data[Contact][password]',
  1372. 'id' => 'ContactPassword', 'class' => 'form-error'
  1373. ),
  1374. array('div' => array('class' => 'error-message')),
  1375. 'Please provide a password',
  1376. '/div',
  1377. '/div'
  1378. );
  1379. $this->assertTags($result, $expected);
  1380. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1381. $expected = array(
  1382. 'div' => array('class' => 'input password error'),
  1383. 'label' => array('for' => 'ContactPassword'),
  1384. 'Password',
  1385. '/label',
  1386. 'input' => array(
  1387. 'type' => 'password', 'name' => 'data[Contact][password]',
  1388. 'id' => 'ContactPassword', 'class' => 'form-error'
  1389. ),
  1390. '/div'
  1391. );
  1392. $this->assertTags($result, $expected);
  1393. }
  1394. /**
  1395. * Test validation errors, when validation message is an empty string.
  1396. *
  1397. * @access public
  1398. * @return void
  1399. */
  1400. public function testEmptyErrorValidation() {
  1401. $this->Form->validationErrors['Contact']['password'] = '';
  1402. $result = $this->Form->input('Contact.password');
  1403. $expected = array(
  1404. 'div' => array('class' => 'input password error'),
  1405. 'label' => array('for' => 'ContactPassword'),
  1406. 'Password',
  1407. '/label',
  1408. 'input' => array(
  1409. 'type' => 'password', 'name' => 'data[Contact][password]',
  1410. 'id' => 'ContactPassword', 'class' => 'form-error'
  1411. ),
  1412. array('div' => array('class' => 'error-message')),
  1413. array(),
  1414. '/div',
  1415. '/div'
  1416. );
  1417. $this->assertTags($result, $expected);
  1418. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1419. $expected = array(
  1420. 'div' => array('class' => 'input password error'),
  1421. 'label' => array('for' => 'ContactPassword'),
  1422. 'Password',
  1423. '/label',
  1424. 'input' => array(
  1425. 'type' => 'password', 'name' => 'data[Contact][password]',
  1426. 'id' => 'ContactPassword', 'class' => 'form-error'
  1427. ),
  1428. '/div'
  1429. );
  1430. $this->assertTags($result, $expected);
  1431. }
  1432. /**
  1433. * Test validation errors, when calling input() overriding validation message by an empty string.
  1434. *
  1435. * @access public
  1436. * @return void
  1437. */
  1438. public function testEmptyInputErrorValidation() {
  1439. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1440. $result = $this->Form->input('Contact.password', array('error' => ''));
  1441. $expected = array(
  1442. 'div' => array('class' => 'input password error'),
  1443. 'label' => array('for' => 'ContactPassword'),
  1444. 'Password',
  1445. '/label',
  1446. 'input' => array(
  1447. 'type' => 'password', 'name' => 'data[Contact][password]',
  1448. 'id' => 'ContactPassword', 'class' => 'form-error'
  1449. ),
  1450. array('div' => array('class' => 'error-message')),
  1451. array(),
  1452. '/div',
  1453. '/div'
  1454. );
  1455. $this->assertTags($result, $expected);
  1456. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1457. $expected = array(
  1458. 'div' => array('class' => 'input password error'),
  1459. 'label' => array('for' => 'ContactPassword'),
  1460. 'Password',
  1461. '/label',
  1462. 'input' => array(
  1463. 'type' => 'password', 'name' => 'data[Contact][password]',
  1464. 'id' => 'ContactPassword', 'class' => 'form-error'
  1465. ),
  1466. '/div'
  1467. );
  1468. $this->assertTags($result, $expected);
  1469. }
  1470. /**
  1471. * testFormValidationAssociated method
  1472. *
  1473. * test display of form errors in conjunction with model::validates.
  1474. *
  1475. * @return void
  1476. */
  1477. public function testFormValidationAssociated() {
  1478. $this->UserForm = ClassRegistry::getObject('UserForm');
  1479. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1480. $data = array(
  1481. 'UserForm' => array('name' => 'user'),
  1482. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1483. );
  1484. $result = $this->UserForm->OpenidUrl->create($data);
  1485. $this->assertFalse(empty($result));
  1486. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1487. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1488. $encoding = strtolower(Configure::read('App.encoding'));
  1489. $expected = array(
  1490. 'form' => array(
  1491. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1492. 'accept-charset' => $encoding
  1493. ),
  1494. 'div' => array('style' => 'display:none;'),
  1495. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1496. '/div'
  1497. );
  1498. $this->assertTags($result, $expected);
  1499. $result = $this->Form->error(
  1500. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Error, not registered', $result);
  1503. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1504. }
  1505. /**
  1506. * testFormValidationAssociatedFirstLevel method
  1507. *
  1508. * test form error display with associated model.
  1509. *
  1510. * @return void
  1511. */
  1512. public function testFormValidationAssociatedFirstLevel() {
  1513. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1514. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1515. $data = array(
  1516. 'ValidateUser' => array('name' => 'mariano'),
  1517. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1518. );
  1519. $result = $this->ValidateUser->create($data);
  1520. $this->assertFalse(empty($result));
  1521. $this->assertFalse($this->ValidateUser->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1523. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1524. $encoding = strtolower(Configure::read('App.encoding'));
  1525. $expected = array(
  1526. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1527. 'div' => array('style' => 'display:none;'),
  1528. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1529. '/div'
  1530. );
  1531. $this->assertTags($result, $expected);
  1532. $result = $this->Form->error(
  1533. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1534. );
  1535. $this->assertEquals('Invalid email', $result);
  1536. $result = $this->Form->error(
  1537. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid name', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1542. );
  1543. $this->assertEquals('Invalid city', $result);
  1544. unset($this->ValidateUser->ValidateProfile);
  1545. unset($this->ValidateUser);
  1546. }
  1547. /**
  1548. * testFormValidationAssociatedSecondLevel method
  1549. *
  1550. * test form error display with associated model.
  1551. *
  1552. * @return void
  1553. */
  1554. public function testFormValidationAssociatedSecondLevel() {
  1555. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1556. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1557. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1558. $data = array(
  1559. 'ValidateUser' => array('name' => 'mariano'),
  1560. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1561. 'ValidateItem' => array('name' => 'Item')
  1562. );
  1563. $result = $this->ValidateUser->create($data);
  1564. $this->assertFalse(empty($result));
  1565. $this->assertFalse($this->ValidateUser->validates());
  1566. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1567. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1568. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1569. $encoding = strtolower(Configure::read('App.encoding'));
  1570. $expected = array(
  1571. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1572. 'div' => array('style' => 'display:none;'),
  1573. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1574. '/div'
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. $result = $this->Form->error(
  1578. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1579. );
  1580. $this->assertEquals('Invalid email', $result);
  1581. $result = $this->Form->error(
  1582. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1583. );
  1584. $this->assertEquals('Invalid name', $result);
  1585. $result = $this->Form->error(
  1586. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1587. );
  1588. $result = $this->Form->error(
  1589. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1590. );
  1591. $this->assertEquals('Invalid description', $result);
  1592. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1593. unset($this->ValidateUser->ValidateProfile);
  1594. unset($this->ValidateUser);
  1595. }
  1596. /**
  1597. * testFormValidationMultiRecord method
  1598. *
  1599. * test form error display with multiple records.
  1600. *
  1601. * @return void
  1602. */
  1603. public function testFormValidationMultiRecord() {
  1604. $Contact = ClassRegistry::getObject('Contact');
  1605. $Contact->validationErrors[2] = array(
  1606. 'name' => array('This field cannot be left blank')
  1607. );
  1608. $result = $this->Form->input('Contact.2.name');
  1609. $expected = array(
  1610. 'div' => array('class' => 'input text error'),
  1611. 'label' => array('for' => 'Contact2Name'),
  1612. 'Name',
  1613. '/label',
  1614. 'input' => array(
  1615. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1616. 'class' => 'form-error', 'maxlength' => 255
  1617. ),
  1618. array('div' => array('class' => 'error-message')),
  1619. 'This field cannot be left blank',
  1620. '/div',
  1621. '/div'
  1622. );
  1623. $this->assertTags($result, $expected);
  1624. }
  1625. /**
  1626. * testMultipleInputValidation method
  1627. *
  1628. * test multiple record form validation error display.
  1629. *
  1630. * @return void
  1631. */
  1632. public function testMultipleInputValidation() {
  1633. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1634. $Address->validationErrors[0] = array(
  1635. 'title' => array('This field cannot be empty'),
  1636. 'first_name' => array('This field cannot be empty')
  1637. );
  1638. $Address->validationErrors[1] = array(
  1639. 'last_name' => array('You must have a last name')
  1640. );
  1641. $this->Form->create();
  1642. $result = $this->Form->input('Address.0.title');
  1643. $expected = array(
  1644. 'div' => array('class'),
  1645. 'label' => array('for'),
  1646. 'preg:/[^<]+/',
  1647. '/label',
  1648. 'input' => array(
  1649. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1650. ),
  1651. array('div' => array('class' => 'error-message')),
  1652. 'This field cannot be empty',
  1653. '/div',
  1654. '/div'
  1655. );
  1656. $this->assertTags($result, $expected);
  1657. $result = $this->Form->input('Address.0.first_name');
  1658. $expected = array(
  1659. 'div' => array('class'),
  1660. 'label' => array('for'),
  1661. 'preg:/[^<]+/',
  1662. '/label',
  1663. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1664. array('div' => array('class' => 'error-message')),
  1665. 'This field cannot be empty',
  1666. '/div',
  1667. '/div'
  1668. );
  1669. $this->assertTags($result, $expected);
  1670. $result = $this->Form->input('Address.0.last_name');
  1671. $expected = array(
  1672. 'div' => array('class'),
  1673. 'label' => array('for'),
  1674. 'preg:/[^<]+/',
  1675. '/label',
  1676. 'input' => array('type' => 'text', 'name', 'id'),
  1677. '/div'
  1678. );
  1679. $this->assertTags($result, $expected);
  1680. $result = $this->Form->input('Address.1.last_name');
  1681. $expected = array(
  1682. 'div' => array('class'),
  1683. 'label' => array('for'),
  1684. 'preg:/[^<]+/',
  1685. '/label',
  1686. 'input' => array(
  1687. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1688. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1689. ),
  1690. array('div' => array('class' => 'error-message')),
  1691. 'You must have a last name',
  1692. '/div',
  1693. '/div'
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. }
  1697. /**
  1698. * testInput method
  1699. *
  1700. * Test various incarnations of input().
  1701. *
  1702. * @return void
  1703. */
  1704. public function testInput() {
  1705. $result = $this->Form->input('ValidateUser.balance');
  1706. $expected = array(
  1707. 'div' => array('class'),
  1708. 'label' => array('for'),
  1709. 'Balance',
  1710. '/label',
  1711. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1712. '/div',
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1716. $expected = array(
  1717. 'div' => array('class' => 'input text'),
  1718. 'label' => array('for' => 'custom'),
  1719. 'Email',
  1720. '/label',
  1721. array('input' => array(
  1722. 'type' => 'text', 'name' => 'data[Contact][email]',
  1723. 'id' => 'custom', 'maxlength' => 255
  1724. )),
  1725. '/div'
  1726. );
  1727. $this->assertTags($result, $expected);
  1728. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1729. $expected = array(
  1730. '<div',
  1731. 'label' => array('for' => 'ContactEmail'),
  1732. 'Email',
  1733. '/label',
  1734. array('input' => array(
  1735. 'type' => 'text', 'name' => 'data[Contact][email]',
  1736. 'id' => 'ContactEmail', 'maxlength' => 255
  1737. )),
  1738. '/div'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->hidden('Contact.idontexist');
  1742. $expected = array('input' => array(
  1743. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1744. 'id' => 'ContactIdontexist'
  1745. ));
  1746. $this->assertTags($result, $expected);
  1747. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1748. $expected = array(
  1749. 'div' => array('class' => 'input text'),
  1750. 'label' => array('for' => 'ContactEmail'),
  1751. 'Email',
  1752. '/label',
  1753. array('input' => array(
  1754. 'type' => 'text', 'name' => 'data[Contact][email]',
  1755. 'id' => 'ContactEmail'
  1756. )),
  1757. '/div'
  1758. );
  1759. $this->assertTags($result, $expected);
  1760. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1761. $expected = array(
  1762. 'div' => array('class' => 'input text'),
  1763. 'label' => array('for' => 'Contact5Email'),
  1764. 'Email',
  1765. '/label',
  1766. array('input' => array(
  1767. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1768. 'id' => 'Contact5Email'
  1769. )),
  1770. '/div'
  1771. );
  1772. $this->assertTags($result, $expected);
  1773. $result = $this->Form->input('Contact.password');
  1774. $expected = array(
  1775. 'div' => array('class' => 'input password'),
  1776. 'label' => array('for' => 'ContactPassword'),
  1777. 'Password',
  1778. '/label',
  1779. array('input' => array(
  1780. 'type' => 'password', 'name' => 'data[Contact][password]',
  1781. 'id' => 'ContactPassword'
  1782. )),
  1783. '/div'
  1784. );
  1785. $this->assertTags($result, $expected);
  1786. $result = $this->Form->input('Contact.email', array(
  1787. 'type' => 'file', 'class' => 'textbox'
  1788. ));
  1789. $expected = array(
  1790. 'div' => array('class' => 'input file'),
  1791. 'label' => array('for' => 'ContactEmail'),
  1792. 'Email',
  1793. '/label',
  1794. array('input' => array(
  1795. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1796. 'id' => 'ContactEmail'
  1797. )),
  1798. '/div'
  1799. );
  1800. $this->assertTags($result, $expected);
  1801. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1802. $result = $this->Form->input('Contact.phone');
  1803. $expected = array(
  1804. 'div' => array('class' => 'input text'),
  1805. 'label' => array('for' => 'ContactPhone'),
  1806. 'Phone',
  1807. '/label',
  1808. array('input' => array(
  1809. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1810. 'value' => 'Hello &amp; World &gt; weird chars',
  1811. 'id' => 'ContactPhone', 'maxlength' => 255
  1812. )),
  1813. '/div'
  1814. );
  1815. $this->assertTags($result, $expected);
  1816. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1817. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1818. $expected = array(
  1819. 'div' => array('class' => 'input text'),
  1820. 'label' => array('for' => 'myId'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1825. 'value' => 'My value', 'id' => 'myId'
  1826. ),
  1827. '/div'
  1828. );
  1829. $this->assertTags($result, $expected);
  1830. unset($this->Form->request->data);
  1831. $Contact = ClassRegistry::getObject('Contact');
  1832. $Contact->validationErrors['field'] = array('Badness!');
  1833. $result = $this->Form->input('Contact.field');
  1834. $expected = array(
  1835. 'div' => array('class' => 'input text error'),
  1836. 'label' => array('for' => 'ContactField'),
  1837. 'Field',
  1838. '/label',
  1839. 'input' => array(
  1840. 'type' => 'text', 'name' => 'data[Contact][field]',
  1841. 'id' => 'ContactField', 'class' => 'form-error'
  1842. ),
  1843. array('div' => array('class' => 'error-message')),
  1844. 'Badness!',
  1845. '/div',
  1846. '/div'
  1847. );
  1848. $this->assertTags($result, $expected);
  1849. $result = $this->Form->input('Contact.field', array(
  1850. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1851. ));
  1852. $expected = array(
  1853. 'label' => array('for' => 'ContactField'),
  1854. 'Field',
  1855. '/label',
  1856. 'input' => array(
  1857. 'type' => 'text', 'name' => 'data[Contact][field]',
  1858. 'id' => 'ContactField', 'class' => 'form-error'
  1859. ),
  1860. array('span' => array('class' => 'error-message')),
  1861. 'Badness!',
  1862. '/span'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $result = $this->Form->input('Contact.field', array(
  1866. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1867. ));
  1868. $expected = array(
  1869. 'div' => array('class' => 'input text error'),
  1870. 'label' => array('for' => 'ContactField'),
  1871. 'Field',
  1872. '/label',
  1873. 'input' => array(
  1874. 'type' => 'text', 'name' => 'data[Contact][field]',
  1875. 'id' => 'ContactField', 'class' => 'form-error'
  1876. ),
  1877. array('div' => array('class' => 'error')),
  1878. 'Badness!',
  1879. '/div'
  1880. );
  1881. $this->assertTags($result, $expected);
  1882. $result = $this->Form->input('Contact.field', array(
  1883. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1884. ));
  1885. $expected = array(
  1886. 'span' => array('class' => 'input text error'),
  1887. 'label' => array('for' => 'ContactField'),
  1888. 'Field',
  1889. '/label',
  1890. 'input' => array(
  1891. 'type' => 'text', 'name' => 'data[Contact][field]',
  1892. 'id' => 'ContactField', 'class' => 'form-error'
  1893. ),
  1894. 'Badness!',
  1895. '/span'
  1896. );
  1897. $this->assertTags($result, $expected);
  1898. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1899. $expected = array(
  1900. 'div' => array('class' => 'input text error'),
  1901. 'label' => array('for' => 'ContactField'),
  1902. 'Field',
  1903. '/label',
  1904. 'input' => array(
  1905. 'type' => 'text', 'name' => 'data[Contact][field]',
  1906. 'id' => 'ContactField', 'class' => 'form-error'
  1907. ),
  1908. 'A message to you, Rudy',
  1909. array('div' => array('class' => 'error-message')),
  1910. 'Badness!',
  1911. '/div',
  1912. '/div'
  1913. );
  1914. $this->assertTags($result, $expected);
  1915. $this->Form->setEntity(null);
  1916. $this->Form->setEntity('Contact.field');
  1917. $result = $this->Form->input('Contact.field', array(
  1918. 'after' => 'A message to you, Rudy', 'error' => false
  1919. ));
  1920. $expected = array(
  1921. 'div' => array('class' => 'input text'),
  1922. 'label' => array('for' => 'ContactField'),
  1923. 'Field',
  1924. '/label',
  1925. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1926. 'A message to you, Rudy',
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1931. $expected = array(
  1932. 'div' => array('class' => 'input text'),
  1933. 'label' => array('for' => 'ObjectField'),
  1934. 'Field',
  1935. '/label',
  1936. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1937. 'A message to you, Rudy',
  1938. '/div'
  1939. );
  1940. $this->assertTags($result, $expected);
  1941. $Contact->validationErrors['field'] = array('minLength');
  1942. $result = $this->Form->input('Contact.field', array(
  1943. 'error' => array(
  1944. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1945. 'maxLength' => 'login too large'
  1946. )
  1947. ));
  1948. $expected = array(
  1949. 'div' => array('class' => 'input text error'),
  1950. 'label' => array('for' => 'ContactField'),
  1951. 'Field',
  1952. '/label',
  1953. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1954. array('div' => array('class' => 'error-message')),
  1955. 'Le login doit contenir au moins 2 caractères',
  1956. '/div',
  1957. '/div'
  1958. );
  1959. $this->assertTags($result, $expected);
  1960. $Contact->validationErrors['field'] = array('maxLength');
  1961. $result = $this->Form->input('Contact.field', array(
  1962. 'error' => array(
  1963. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1964. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1965. 'maxLength' => 'login too large',
  1966. )
  1967. ));
  1968. $expected = array(
  1969. 'div' => array('class' => 'input text error'),
  1970. 'label' => array('for' => 'ContactField'),
  1971. 'Field',
  1972. '/label',
  1973. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1974. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1975. 'login too large',
  1976. '/span',
  1977. '/div'
  1978. );
  1979. $this->assertTags($result, $expected);
  1980. }
  1981. /**
  1982. * Test that inputs with 0 can be created.
  1983. *
  1984. * @return void
  1985. */
  1986. public function testInputZero() {
  1987. $this->Form->create('User');
  1988. $result = $this->Form->input('0');
  1989. $expected = array(
  1990. 'div' => array('class' => 'input text'),
  1991. 'label' => array('for' => 'User0'), '/label',
  1992. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1993. '/div'
  1994. );
  1995. $this->assertTags($result, $expected);
  1996. }
  1997. /**
  1998. * test input() with checkbox creation
  1999. *
  2000. * @return void
  2001. */
  2002. public function testInputCheckbox() {
  2003. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2004. $expected = array(
  2005. 'div' => array('class' => 'input checkbox'),
  2006. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2007. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2008. '/div'
  2009. );
  2010. $this->assertTags($result, $expected);
  2011. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2012. $expected = array(
  2013. 'div' => array('class' => 'input checkbox'),
  2014. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2015. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2016. '/div'
  2017. );
  2018. $this->assertTags($result, $expected);
  2019. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2020. $expected = array(
  2021. 'div' => array('class' => 'input checkbox'),
  2022. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2023. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2024. '/div'
  2025. );
  2026. $this->assertTags($result, $expected);
  2027. }
  2028. /**
  2029. * test form->input() with time types.
  2030. *
  2031. */
  2032. public function testInputTime() {
  2033. extract($this->dateRegex);
  2034. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2035. $result = explode(':', $result);
  2036. $this->assertRegExp('/option value="23"/', $result[0]);
  2037. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2038. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2039. $result = explode(':', $result);
  2040. $this->assertRegExp('/option value="23"/', $result[0]);
  2041. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2042. $result = $this->Form->input('Model.field', array(
  2043. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2044. ));
  2045. $result = explode(':', $result);
  2046. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2047. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2048. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2049. $result = $this->Form->input('Model.field', array(
  2050. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2051. ));
  2052. $result = explode(':', $result);
  2053. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2054. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2055. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2056. $result = $this->Form->input('prueba', array(
  2057. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2058. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2059. ));
  2060. $result = explode(':', $result);
  2061. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2062. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2063. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2064. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2065. $result = $this->Form->input('Random.start_time', array(
  2066. 'type' => 'time',
  2067. 'selected' => '18:15'
  2068. ));
  2069. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2070. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2071. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2072. }
  2073. /**
  2074. * Test interval + selected near the hour roll over.
  2075. *
  2076. * @return void
  2077. */
  2078. public function testTimeSelectedWithInterval() {
  2079. $result = $this->Form->input('Model.start_time', array(
  2080. 'type' => 'time',
  2081. 'interval' => 15,
  2082. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2083. ));
  2084. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2085. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2086. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2087. $result = $this->Form->input('Model.start_time', array(
  2088. 'type' => 'time',
  2089. 'interval' => 15,
  2090. 'selected' => '2012-10-23 15:57:00'
  2091. ));
  2092. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2093. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2094. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2095. $result = $this->Form->input('Model.start_time', array(
  2096. 'timeFormat' => 24,
  2097. 'type' => 'time',
  2098. 'interval' => 15,
  2099. 'selected' => '15:57'
  2100. ));
  2101. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2102. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2103. $result = $this->Form->input('Model.start_time', array(
  2104. 'timeFormat' => 24,
  2105. 'type' => 'time',
  2106. 'interval' => 15,
  2107. 'selected' => '23:57'
  2108. ));
  2109. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2110. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2111. $result = $this->Form->input('Model.created', array(
  2112. 'timeFormat' => 24,
  2113. 'type' => 'datetime',
  2114. 'interval' => 15,
  2115. 'selected' => '2012-09-30 23:56'
  2116. ));
  2117. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2118. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2119. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2120. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2121. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2122. }
  2123. /**
  2124. * test form->input() with datetime, date and time types
  2125. *
  2126. * @return void
  2127. */
  2128. public function testInputDatetime() {
  2129. extract($this->dateRegex);
  2130. $result = $this->Form->input('prueba', array(
  2131. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2132. 'maxYear' => date('Y') + 1, 'interval' => 15
  2133. ));
  2134. $result = explode(':', $result);
  2135. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2136. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2137. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2138. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2139. //related to ticket #5013
  2140. $result = $this->Form->input('Contact.date', array(
  2141. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2142. ));
  2143. $this->assertRegExp('/class="customClass"/', $result);
  2144. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2145. $result = $this->Form->input('Contact.date', array(
  2146. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2147. ));
  2148. $this->assertRegExp('/id="customIdDay"/', $result);
  2149. $this->assertRegExp('/id="customIdMonth"/', $result);
  2150. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2151. $result = $this->Form->input('Model.field', array(
  2152. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2153. ));
  2154. $this->assertRegExp('/id="customIDDay"/', $result);
  2155. $this->assertRegExp('/id="customIDHour"/', $result);
  2156. $result = explode('</select><select', $result);
  2157. $result = explode(':', $result[1]);
  2158. $this->assertRegExp('/option value="23"/', $result[0]);
  2159. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2160. $result = $this->Form->input('Model.field', array(
  2161. 'type' => 'datetime', 'timeFormat' => 12
  2162. ));
  2163. $result = explode('</select><select', $result);
  2164. $result = explode(':', $result[1]);
  2165. $this->assertRegExp('/option value="12"/', $result[0]);
  2166. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2167. $this->Form->request->data = array('Contact' => array('created' => null));
  2168. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2169. $expected = array(
  2170. 'div' => array('class' => 'input date'),
  2171. 'label' => array('for' => 'ContactCreatedMonth'),
  2172. 'Created',
  2173. '/label',
  2174. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2175. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2176. $monthsRegex,
  2177. '/select', '-',
  2178. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2179. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2180. $daysRegex,
  2181. '/select', '-',
  2182. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2183. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2184. $yearsRegex,
  2185. '/select',
  2186. '/div'
  2187. );
  2188. $this->assertTags($result, $expected);
  2189. $this->Form->request->data = array('Contact' => array('created' => null));
  2190. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2191. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2192. $this->Form->request->data = array('Contact' => array('created' => null));
  2193. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2194. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2195. $result = $this->Form->input('Contact.created', array(
  2196. 'type' => 'date',
  2197. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2198. 'timeFormat' => 'NONE'
  2199. ));
  2200. $this->assertRegExp('/for\="created-month"/', $result);
  2201. }
  2202. /**
  2203. * Test generating checkboxes in a loop.
  2204. *
  2205. * @return void
  2206. */
  2207. public function testInputCheckboxesInLoop() {
  2208. for ($i = 1; $i < 5; $i++) {
  2209. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2210. $expected = array(
  2211. 'div' => array('class' => 'input checkbox'),
  2212. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2213. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2214. 'label' => array('for' => "Contact{$i}Email"),
  2215. 'Email',
  2216. '/label',
  2217. '/div'
  2218. );
  2219. $this->assertTags($result, $expected);
  2220. }
  2221. }
  2222. /**
  2223. * Test generating checkboxes with disabled elements.
  2224. *
  2225. * @return void
  2226. */
  2227. public function testInputCheckboxWithDisabledElements() {
  2228. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2229. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2230. $expected = array(
  2231. array('div' => array('class' => 'input select')),
  2232. array('label' => array('for' => "ContactMultiple")),
  2233. 'Multiple',
  2234. '/label',
  2235. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2236. array('div' => array('class' => 'checkbox')),
  2237. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2238. array('label' => array('for' => "ContactMultiple1")),
  2239. 'One',
  2240. '/label',
  2241. '/div',
  2242. array('div' => array('class' => 'checkbox')),
  2243. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2244. array('label' => array('for' => "ContactMultiple2")),
  2245. 'Two',
  2246. '/label',
  2247. '/div',
  2248. array('div' => array('class' => 'checkbox')),
  2249. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2250. array('label' => array('for' => "ContactMultiple3")),
  2251. 'Three',
  2252. '/label',
  2253. '/div',
  2254. '/div'
  2255. );
  2256. $this->assertTags($result, $expected);
  2257. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2258. $this->assertTags($result, $expected);
  2259. $disabled = array('2', 3);
  2260. $expected = array(
  2261. array('div' => array('class' => 'input select')),
  2262. array('label' => array('for' => "ContactMultiple")),
  2263. 'Multiple',
  2264. '/label',
  2265. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2266. array('div' => array('class' => 'checkbox')),
  2267. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2268. array('label' => array('for' => "ContactMultiple1")),
  2269. 'One',
  2270. '/label',
  2271. '/div',
  2272. array('div' => array('class' => 'checkbox')),
  2273. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2274. array('label' => array('for' => "ContactMultiple2")),
  2275. 'Two',
  2276. '/label',
  2277. '/div',
  2278. array('div' => array('class' => 'checkbox')),
  2279. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2280. array('label' => array('for' => "ContactMultiple3")),
  2281. 'Three',
  2282. '/label',
  2283. '/div',
  2284. '/div'
  2285. );
  2286. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2287. $this->assertTags($result, $expected);
  2288. }
  2289. /**
  2290. * test input name with leading integer, ensure attributes are generated correctly.
  2291. *
  2292. * @return void
  2293. */
  2294. public function testInputWithLeadingInteger() {
  2295. $result = $this->Form->text('0.Node.title');
  2296. $expected = array(
  2297. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2298. );
  2299. $this->assertTags($result, $expected);
  2300. }
  2301. /**
  2302. * test form->input() with select type inputs.
  2303. *
  2304. * @return void
  2305. */
  2306. public function testInputSelectType() {
  2307. $result = $this->Form->input('email', array(
  2308. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2309. );
  2310. $expected = array(
  2311. 'div' => array('class' => 'input select'),
  2312. 'label' => array('for' => 'email'),
  2313. 'Email',
  2314. '/label',
  2315. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2316. array('option' => array('value' => '')),
  2317. '/option',
  2318. array('option' => array('value' => 'è')),
  2319. 'Firést',
  2320. '/option',
  2321. array('option' => array('value' => 'é')),
  2322. 'Secoènd',
  2323. '/option',
  2324. '/select',
  2325. '/div'
  2326. );
  2327. $this->assertTags($result, $expected);
  2328. $result = $this->Form->input('email', array(
  2329. 'options' => array('First', 'Second'), 'empty' => true)
  2330. );
  2331. $expected = array(
  2332. 'div' => array('class' => 'input select'),
  2333. 'label' => array('for' => 'email'),
  2334. 'Email',
  2335. '/label',
  2336. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2337. array('option' => array('value' => '')),
  2338. '/option',
  2339. array('option' => array('value' => '0')),
  2340. 'First',
  2341. '/option',
  2342. array('option' => array('value' => '1')),
  2343. 'Second',
  2344. '/option',
  2345. '/select',
  2346. '/div'
  2347. );
  2348. $this->assertTags($result, $expected);
  2349. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2350. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2351. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2352. $expected = array(
  2353. 'div' => array('class' => 'input select'),
  2354. 'label' => array('for' => 'ModelUserId'),
  2355. 'User',
  2356. '/label',
  2357. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2358. array('option' => array('value' => '')),
  2359. '/option',
  2360. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2361. 'good',
  2362. '/option',
  2363. array('option' => array('value' => 'other')),
  2364. 'bad',
  2365. '/option',
  2366. '/select',
  2367. '/div'
  2368. );
  2369. $this->assertTags($result, $expected);
  2370. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2371. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2372. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2373. $expected = array(
  2374. 'div' => array('class' => 'input select'),
  2375. 'label' => array('for' => 'ThingUserId'),
  2376. 'User',
  2377. '/label',
  2378. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2379. array('option' => array('value' => '')),
  2380. 'Some Empty',
  2381. '/option',
  2382. array('option' => array('value' => 'value')),
  2383. 'good',
  2384. '/option',
  2385. array('option' => array('value' => 'other')),
  2386. 'bad',
  2387. '/option',
  2388. '/select',
  2389. '/div'
  2390. );
  2391. $this->assertTags($result, $expected);
  2392. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2393. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2394. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2395. $expected = array(
  2396. 'div' => array('class' => 'input select'),
  2397. 'label' => array('for' => 'ThingUserId'),
  2398. 'User',
  2399. '/label',
  2400. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2401. array('option' => array('value' => '')),
  2402. 'Some Empty',
  2403. '/option',
  2404. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2405. 'good',
  2406. '/option',
  2407. array('option' => array('value' => 'other')),
  2408. 'bad',
  2409. '/option',
  2410. '/select',
  2411. '/div'
  2412. );
  2413. $this->assertTags($result, $expected);
  2414. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2415. $this->Form->request->data = array('User' => array('User' => array('value')));
  2416. $result = $this->Form->input('User.User', array('empty' => true));
  2417. $expected = array(
  2418. 'div' => array('class' => 'input select'),
  2419. 'label' => array('for' => 'UserUser'),
  2420. 'User',
  2421. '/label',
  2422. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2423. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2424. array('option' => array('value' => '')),
  2425. '/option',
  2426. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2427. 'good',
  2428. '/option',
  2429. array('option' => array('value' => 'other')),
  2430. 'bad',
  2431. '/option',
  2432. '/select',
  2433. '/div'
  2434. );
  2435. $this->assertTags($result, $expected);
  2436. $this->Form->data = array();
  2437. $result = $this->Form->input('Publisher.id', array(
  2438. 'label' => 'Publisher',
  2439. 'type' => 'select',
  2440. 'multiple' => 'checkbox',
  2441. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2442. ));
  2443. $expected = array(
  2444. array('div' => array('class' => 'input select')),
  2445. array('label' => array('for' => 'PublisherId')),
  2446. 'Publisher',
  2447. '/label',
  2448. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2449. array('div' => array('class' => 'checkbox')),
  2450. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2451. array('label' => array('for' => 'PublisherIdValue1')),
  2452. 'Label 1',
  2453. '/label',
  2454. '/div',
  2455. array('div' => array('class' => 'checkbox')),
  2456. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2457. array('label' => array('for' => 'PublisherIdValue2')),
  2458. 'Label 2',
  2459. '/label',
  2460. '/div',
  2461. '/div'
  2462. );
  2463. $this->assertTags($result, $expected);
  2464. }
  2465. /**
  2466. * test that input() and a non standard primary key makes a hidden input by default.
  2467. *
  2468. * @return void
  2469. */
  2470. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2471. $this->Form->create('User');
  2472. $this->Form->fieldset = array(
  2473. 'User' => array(
  2474. 'fields' => array(
  2475. 'model_id' => array('type' => 'integer')
  2476. ),
  2477. 'validates' => array(),
  2478. 'key' => 'model_id'
  2479. )
  2480. );
  2481. $result = $this->Form->input('model_id');
  2482. $expected = array(
  2483. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2484. );
  2485. $this->assertTags($result, $expected);
  2486. }
  2487. /**
  2488. * test that overriding the magic select type widget is possible
  2489. *
  2490. * @return void
  2491. */
  2492. public function testInputOverridingMagicSelectType() {
  2493. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2494. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2495. $expected = array(
  2496. 'div' => array('class' => 'input text'),
  2497. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2498. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2499. '/div'
  2500. );
  2501. $this->assertTags($result, $expected);
  2502. //Check that magic types still work for plural/singular vars
  2503. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2504. $result = $this->Form->input('Model.type');
  2505. $expected = array(
  2506. 'div' => array('class' => 'input select'),
  2507. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2508. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2509. array('option' => array('value' => 'value')), 'good', '/option',
  2510. array('option' => array('value' => 'other')), 'bad', '/option',
  2511. '/select',
  2512. '/div'
  2513. );
  2514. $this->assertTags($result, $expected);
  2515. }
  2516. /**
  2517. * Test that magic input() selects are created for type=number
  2518. *
  2519. * @return void
  2520. */
  2521. public function testInputMagicSelectForTypeNumber() {
  2522. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2523. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2524. $result = $this->Form->input('ValidateUser.balance');
  2525. $expected = array(
  2526. 'div' => array('class' => 'input select'),
  2527. 'label' => array('for' => 'ValidateUserBalance'),
  2528. 'Balance',
  2529. '/label',
  2530. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2531. array('option' => array('value' => '0')),
  2532. 'nothing',
  2533. '/option',
  2534. array('option' => array('value' => '1', 'selected' => 'selected')),
  2535. 'some',
  2536. '/option',
  2537. array('option' => array('value' => '100')),
  2538. 'a lot',
  2539. '/option',
  2540. '/select',
  2541. '/div'
  2542. );
  2543. $this->assertTags($result, $expected);
  2544. }
  2545. /**
  2546. * Test that magic input() selects can easily be converted into radio types without error.
  2547. *
  2548. * @return void
  2549. */
  2550. public function testInputMagicSelectChangeToRadio() {
  2551. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2552. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2553. $this->assertRegExp('/input type="radio"/', $result);
  2554. }
  2555. /**
  2556. * fields with the same name as the model should work.
  2557. *
  2558. * @return void
  2559. */
  2560. public function testInputWithMatchingFieldAndModelName() {
  2561. $this->Form->create('User');
  2562. $this->Form->fieldset = array(
  2563. 'User' => array(
  2564. 'fields' => array(
  2565. 'User' => array('type' => 'text')
  2566. ),
  2567. 'validates' => array(),
  2568. 'key' => 'id'
  2569. )
  2570. );
  2571. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2572. $result = $this->Form->input('User', array('type' => 'text'));
  2573. $expected = array(
  2574. 'div' => array('class' => 'input text'),
  2575. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2576. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2577. '/div'
  2578. );
  2579. $this->assertTags($result, $expected);
  2580. }
  2581. /**
  2582. * testFormInputs method
  2583. *
  2584. * test correct results from form::inputs().
  2585. *
  2586. * @return void
  2587. */
  2588. public function testFormInputs() {
  2589. $this->Form->create('Contact');
  2590. $result = $this->Form->inputs('The Legend');
  2591. $expected = array(
  2592. '<fieldset',
  2593. '<legend',
  2594. 'The Legend',
  2595. '/legend',
  2596. '*/fieldset',
  2597. );
  2598. $this->assertTags($result, $expected);
  2599. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2600. $expected = array(
  2601. 'fieldset' => array('class' => 'classy-stuff'),
  2602. '<legend',
  2603. 'Field of Dreams',
  2604. '/legend',
  2605. '*/fieldset'
  2606. );
  2607. $this->assertTags($result, $expected);
  2608. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2609. $this->assertTags($result, $expected);
  2610. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2611. $this->assertTags($result, $expected);
  2612. $this->Form->create('Contact');
  2613. $this->Form->request['prefix'] = 'admin';
  2614. $this->Form->request['action'] = 'admin_edit';
  2615. $result = $this->Form->inputs();
  2616. $expected = array(
  2617. '<fieldset',
  2618. '<legend',
  2619. 'Edit Contact',
  2620. '/legend',
  2621. '*/fieldset',
  2622. );
  2623. $this->assertTags($result, $expected);
  2624. $this->Form->create('Contact');
  2625. $result = $this->Form->inputs(false);
  2626. $expected = array(
  2627. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2628. array('div' => array('class' => 'input text')),
  2629. '*/div',
  2630. array('div' => array('class' => 'input text')),
  2631. '*/div',
  2632. array('div' => array('class' => 'input text')),
  2633. '*/div',
  2634. array('div' => array('class' => 'input password')),
  2635. '*/div',
  2636. array('div' => array('class' => 'input date')),
  2637. '*/div',
  2638. array('div' => array('class' => 'input date')),
  2639. '*/div',
  2640. array('div' => array('class' => 'input datetime')),
  2641. '*/div',
  2642. array('div' => array('class' => 'input number')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input select')),
  2645. '*/div',
  2646. );
  2647. $this->assertTags($result, $expected);
  2648. $this->Form->create('Contact');
  2649. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2650. $expected = array(
  2651. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2652. array('div' => array('class' => 'input text')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input text')),
  2655. '*/div',
  2656. array('div' => array('class' => 'input text')),
  2657. '*/div',
  2658. array('div' => array('class' => 'input password')),
  2659. '*/div',
  2660. array('div' => array('class' => 'input date')),
  2661. '*/div',
  2662. array('div' => array('class' => 'input date')),
  2663. '*/div',
  2664. array('div' => array('class' => 'input datetime')),
  2665. '*/div',
  2666. array('div' => array('class' => 'input number')),
  2667. '*/div',
  2668. array('div' => array('class' => 'input select')),
  2669. '*/div',
  2670. );
  2671. $this->assertTags($result, $expected);
  2672. $this->Form->create('Contact');
  2673. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2674. $this->assertTags($result, $expected);
  2675. $this->Form->create('Contact');
  2676. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2677. $expected = array(
  2678. 'fieldset' => array(),
  2679. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2680. array('div' => array('class' => 'input text')),
  2681. '*/div',
  2682. array('div' => array('class' => 'input text')),
  2683. '*/div',
  2684. array('div' => array('class' => 'input text')),
  2685. '*/div',
  2686. array('div' => array('class' => 'input password')),
  2687. '*/div',
  2688. array('div' => array('class' => 'input date')),
  2689. '*/div',
  2690. array('div' => array('class' => 'input date')),
  2691. '*/div',
  2692. array('div' => array('class' => 'input datetime')),
  2693. '*/div',
  2694. array('div' => array('class' => 'input number')),
  2695. '*/div',
  2696. array('div' => array('class' => 'input select')),
  2697. '*/div',
  2698. '/fieldset'
  2699. );
  2700. $this->assertTags($result, $expected);
  2701. $this->Form->create('Contact');
  2702. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2703. $expected = array(
  2704. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2705. array('div' => array('class' => 'input text')),
  2706. '*/div',
  2707. array('div' => array('class' => 'input text')),
  2708. '*/div',
  2709. array('div' => array('class' => 'input text')),
  2710. '*/div',
  2711. array('div' => array('class' => 'input password')),
  2712. '*/div',
  2713. array('div' => array('class' => 'input date')),
  2714. '*/div',
  2715. array('div' => array('class' => 'input date')),
  2716. '*/div',
  2717. array('div' => array('class' => 'input datetime')),
  2718. '*/div',
  2719. array('div' => array('class' => 'input number')),
  2720. '*/div',
  2721. array('div' => array('class' => 'input select')),
  2722. '*/div',
  2723. );
  2724. $this->assertTags($result, $expected);
  2725. $this->Form->create('Contact');
  2726. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2727. $this->assertTags($result, $expected);
  2728. $this->Form->create('Contact');
  2729. $result = $this->Form->inputs('Hello');
  2730. $expected = array(
  2731. 'fieldset' => array(),
  2732. 'legend' => array(),
  2733. 'Hello',
  2734. '/legend',
  2735. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2736. array('div' => array('class' => 'input text')),
  2737. '*/div',
  2738. array('div' => array('class' => 'input text')),
  2739. '*/div',
  2740. array('div' => array('class' => 'input text')),
  2741. '*/div',
  2742. array('div' => array('class' => 'input password')),
  2743. '*/div',
  2744. array('div' => array('class' => 'input date')),
  2745. '*/div',
  2746. array('div' => array('class' => 'input date')),
  2747. '*/div',
  2748. array('div' => array('class' => 'input datetime')),
  2749. '*/div',
  2750. array('div' => array('class' => 'input number')),
  2751. '*/div',
  2752. array('div' => array('class' => 'input select')),
  2753. '*/div',
  2754. '/fieldset'
  2755. );
  2756. $this->assertTags($result, $expected);
  2757. $this->Form->create('Contact');
  2758. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2759. $expected = array(
  2760. 'fieldset' => array(),
  2761. 'legend' => array(),
  2762. 'Hello',
  2763. '/legend',
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2765. array('div' => array('class' => 'input text')),
  2766. '*/div',
  2767. array('div' => array('class' => 'input text')),
  2768. '*/div',
  2769. array('div' => array('class' => 'input text')),
  2770. '*/div',
  2771. array('div' => array('class' => 'input password')),
  2772. '*/div',
  2773. array('div' => array('class' => 'input date')),
  2774. '*/div',
  2775. array('div' => array('class' => 'input date')),
  2776. '*/div',
  2777. array('div' => array('class' => 'input datetime')),
  2778. '*/div',
  2779. array('div' => array('class' => 'input number')),
  2780. '*/div',
  2781. array('div' => array('class' => 'input select')),
  2782. '*/div',
  2783. '/fieldset'
  2784. );
  2785. $this->assertTags($result, $expected);
  2786. $this->Form->create('Contact');
  2787. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2788. $this->assertTags($result, $expected);
  2789. }
  2790. /**
  2791. * testSelectAsCheckbox method
  2792. *
  2793. * test multi-select widget with checkbox formatting.
  2794. *
  2795. * @return void
  2796. */
  2797. public function testSelectAsCheckbox() {
  2798. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2799. $expected = array(
  2800. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2801. array('div' => array('class' => 'checkbox')),
  2802. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2803. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2804. 'first',
  2805. '/label',
  2806. '/div',
  2807. array('div' => array('class' => 'checkbox')),
  2808. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2809. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2810. 'second',
  2811. '/label',
  2812. '/div',
  2813. array('div' => array('class' => 'checkbox')),
  2814. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2815. array('label' => array('for' => 'ModelMultiField2')),
  2816. 'third',
  2817. '/label',
  2818. '/div',
  2819. );
  2820. $this->assertTags($result, $expected);
  2821. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2822. $expected = array(
  2823. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2824. array('div' => array('class' => 'checkbox')),
  2825. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2826. array('label' => array('for' => 'ModelMultiField12')),
  2827. 'half',
  2828. '/label',
  2829. '/div',
  2830. );
  2831. $this->assertTags($result, $expected);
  2832. }
  2833. /**
  2834. * testLabel method
  2835. *
  2836. * test label generation.
  2837. *
  2838. * @return void
  2839. */
  2840. public function testLabel() {
  2841. $this->Form->text('Person.name');
  2842. $result = $this->Form->label();
  2843. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2844. $this->Form->text('Person.name');
  2845. $result = $this->Form->label();
  2846. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2847. $result = $this->Form->label('Person.first_name');
  2848. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2849. $result = $this->Form->label('Person.first_name', 'Your first name');
  2850. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2851. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2852. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2853. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2854. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2855. $result = $this->Form->label('Person.first_name', '');
  2856. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2857. $result = $this->Form->label('Person.2.name', '');
  2858. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2859. }
  2860. /**
  2861. * testTextbox method
  2862. *
  2863. * test textbox element generation
  2864. *
  2865. * @return void
  2866. */
  2867. public function testTextbox() {
  2868. $result = $this->Form->text('Model.field');
  2869. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2870. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2871. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2872. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2873. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2874. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2875. $result = $this->Form->text('Model.text');
  2876. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2877. $Contact = ClassRegistry::getObject('Contact');
  2878. $Contact->validationErrors['text'] = array(true);
  2879. $this->Form->request->data['Contact']['text'] = 'test';
  2880. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2881. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2882. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2883. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2884. $expected = array(
  2885. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2886. );
  2887. $this->assertTags($result, $expected);
  2888. }
  2889. /**
  2890. * testDefaultValue method
  2891. *
  2892. * Test default value setting
  2893. *
  2894. * @return void
  2895. */
  2896. public function testDefaultValue() {
  2897. $this->Form->request->data['Model']['field'] = 'test';
  2898. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2899. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2900. unset($this->Form->request->data['Model']['field']);
  2901. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2902. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2903. }
  2904. /**
  2905. * testCheckboxDefaultValue method
  2906. *
  2907. * Test default value setting on checkbox() method
  2908. *
  2909. * @return void
  2910. */
  2911. public function testCheckboxDefaultValue() {
  2912. $this->Form->request->data['Model']['field'] = false;
  2913. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2914. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2915. unset($this->Form->request->data['Model']['field']);
  2916. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2917. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2918. $this->Form->request->data['Model']['field'] = true;
  2919. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2920. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2921. unset($this->Form->request->data['Model']['field']);
  2922. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2923. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2924. }
  2925. /**
  2926. * testError method
  2927. *
  2928. * Test field error generation
  2929. *
  2930. * @return void
  2931. */
  2932. public function testError() {
  2933. $Contact = ClassRegistry::getObject('Contact');
  2934. $Contact->validationErrors['field'] = array(1);
  2935. $result = $this->Form->error('Contact.field');
  2936. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2937. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2938. $this->assertEquals('Error in field Field', $result);
  2939. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2940. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2941. $this->assertEquals('This field contains invalid input', $result);
  2942. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2943. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2944. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2945. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2946. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2947. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2948. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2949. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2950. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2951. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2952. $this->assertEquals('<strong>Badness!</strong>', $result);
  2953. $Contact->validationErrors['field'] = array("email");
  2954. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2955. $expected = array(
  2956. 'div' => array('class' => 'field-error'),
  2957. 'No good!',
  2958. '/div'
  2959. );
  2960. $this->assertTags($result, $expected);
  2961. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2962. $result = $this->Form->error('Contact.field', array(
  2963. 'notEmpty' => 'Cannot be empty',
  2964. 'email' => 'No good!'
  2965. ));
  2966. $expected = array(
  2967. 'div' => array('class' => 'error-message'),
  2968. 'ul' => array(),
  2969. '<li', 'Cannot be empty', '/li',
  2970. '<li', 'No good!', '/li',
  2971. '<li', 'Something else', '/li',
  2972. '/ul',
  2973. '/div'
  2974. );
  2975. $this->assertTags($result, $expected);
  2976. /** Testing error messages list options **/
  2977. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2978. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2979. $expected = array(
  2980. 'div' => array('class' => 'error-message'),
  2981. 'ol' => array(),
  2982. '<li', 'notEmpty', '/li',
  2983. '<li', 'email', '/li',
  2984. '/ol',
  2985. '/div'
  2986. );
  2987. $this->assertTags($result, $expected);
  2988. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2989. $expected = array(
  2990. 'div' => array('class' => 'error-message'),
  2991. 'ol' => array(),
  2992. '<li', 'notEmpty', '/li',
  2993. '<li', 'email', '/li',
  2994. '/ol',
  2995. '/div'
  2996. );
  2997. $this->assertTags($result, $expected);
  2998. $result = $this->Form->error('Contact.field', null, array(
  2999. 'listOptions' => array(
  3000. 'class' => 'ul-class',
  3001. 'itemOptions' => array(
  3002. 'class' => 'li-class'
  3003. )
  3004. )
  3005. ));
  3006. $expected = array(
  3007. 'div' => array('class' => 'error-message'),
  3008. 'ul' => array('class' => 'ul-class'),
  3009. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3010. array('li' => array('class' => 'li-class')), 'email', '/li',
  3011. '/ul',
  3012. '/div'
  3013. );
  3014. $this->assertTags($result, $expected);
  3015. }
  3016. /**
  3017. * test error options when using form->input();
  3018. *
  3019. * @return void
  3020. */
  3021. public function testInputErrorEscape() {
  3022. $this->Form->create('ValidateProfile');
  3023. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3024. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3025. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  3026. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3027. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  3028. $this->assertRegExp('/required<br>/', $result);
  3029. }
  3030. /**
  3031. * testPassword method
  3032. *
  3033. * Test password element generation
  3034. *
  3035. * @return void
  3036. */
  3037. public function testPassword() {
  3038. $Contact = ClassRegistry::getObject('Contact');
  3039. $result = $this->Form->password('Contact.field');
  3040. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3041. $Contact->validationErrors['passwd'] = 1;
  3042. $this->Form->request->data['Contact']['passwd'] = 'test';
  3043. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3044. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3045. }
  3046. /**
  3047. * testRadio method
  3048. *
  3049. * Test radio element set generation
  3050. *
  3051. * @return void
  3052. */
  3053. public function testRadio() {
  3054. $result = $this->Form->radio('Model.field', array('option A'));
  3055. $expected = array(
  3056. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3057. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3058. 'label' => array('for' => 'ModelField0'),
  3059. 'option A',
  3060. '/label'
  3061. );
  3062. $this->assertTags($result, $expected);
  3063. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3064. $expected = array(
  3065. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3066. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3067. 'label' => array('for' => 'ModelField12'),
  3068. 'half',
  3069. '/label'
  3070. );
  3071. $this->assertTags($result, $expected);
  3072. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3073. $expected = array(
  3074. 'fieldset' => array(),
  3075. 'legend' => array(),
  3076. 'Field',
  3077. '/legend',
  3078. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3079. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3080. array('label' => array('for' => 'ModelField0')),
  3081. 'option A',
  3082. '/label',
  3083. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3084. array('label' => array('for' => 'ModelField1')),
  3085. 'option B',
  3086. '/label',
  3087. '/fieldset'
  3088. );
  3089. $this->assertTags($result, $expected);
  3090. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3091. $expected = array(
  3092. 'fieldset' => array(),
  3093. 'legend' => array(),
  3094. 'Field',
  3095. '/legend',
  3096. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3097. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3098. array('label' => array('for' => 'ModelField0')),
  3099. 'option A',
  3100. '/label',
  3101. 'br' => array(),
  3102. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3103. array('label' => array('for' => 'ModelField1')),
  3104. 'option B',
  3105. '/label',
  3106. '/fieldset'
  3107. );
  3108. $this->assertTags($result, $expected);
  3109. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3110. $expected = array(
  3111. 'div' => array('class' => 'input radio'),
  3112. 'fieldset' => array(),
  3113. 'legend' => array(),
  3114. 'Legend title',
  3115. '/legend',
  3116. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3117. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3118. array('label' => array('for' => 'NewsletterSubscribe0')),
  3119. 'Unsubscribe',
  3120. '/label',
  3121. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3122. array('label' => array('for' => 'NewsletterSubscribe1')),
  3123. 'Subscribe',
  3124. '/label',
  3125. '/fieldset',
  3126. '/div'
  3127. );
  3128. $this->assertTags($result, $expected);
  3129. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3130. $expected = array(
  3131. 'div' => array('class' => 'input radio'),
  3132. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3133. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3134. array('label' => array('for' => 'NewsletterSubscribe0')),
  3135. 'Unsubscribe',
  3136. '/label',
  3137. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3138. array('label' => array('for' => 'NewsletterSubscribe1')),
  3139. 'Subscribe',
  3140. '/label',
  3141. '/div'
  3142. );
  3143. $this->assertTags($result, $expected);
  3144. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3145. $expected = array(
  3146. 'div' => array('class' => 'input radio'),
  3147. 'fieldset' => array(),
  3148. 'legend' => array(),
  3149. 'Legend title',
  3150. '/legend',
  3151. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3152. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3153. 'Unsubscribe',
  3154. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3155. 'Subscribe',
  3156. '/fieldset',
  3157. '/div'
  3158. );
  3159. $this->assertTags($result, $expected);
  3160. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3161. $expected = array(
  3162. 'div' => array('class' => 'input radio'),
  3163. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3164. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3165. 'Unsubscribe',
  3166. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3167. 'Subscribe',
  3168. '/div'
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3172. $expected = array(
  3173. 'div' => array('class' => 'input radio'),
  3174. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3175. 'Unsubscribe',
  3176. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3177. 'Subscribe',
  3178. '/div'
  3179. );
  3180. $this->assertTags($result, $expected);
  3181. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3182. $expected = array(
  3183. 'fieldset' => array(),
  3184. 'legend' => array(),
  3185. 'Gender',
  3186. '/legend',
  3187. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3188. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3189. array('label' => array('for' => 'EmployeeGenderMale')),
  3190. 'Male',
  3191. '/label',
  3192. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3193. array('label' => array('for' => 'EmployeeGenderFemale')),
  3194. 'Female',
  3195. '/label',
  3196. '/fieldset',
  3197. );
  3198. $this->assertTags($result, $expected);
  3199. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3200. $expected = array(
  3201. 'fieldset' => array(),
  3202. 'legend' => array(),
  3203. 'Gender',
  3204. '/legend',
  3205. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3206. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3207. array('label' => array('for' => 'OfficerGenderMale')),
  3208. 'Male',
  3209. '/label',
  3210. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3211. array('label' => array('for' => 'OfficerGenderFemale')),
  3212. 'Female',
  3213. '/label',
  3214. '/fieldset',
  3215. );
  3216. $this->assertTags($result, $expected);
  3217. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3218. $expected = array(
  3219. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3220. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3221. 'label' => array('for' => 'Contact1Imrequired0'),
  3222. 'option A',
  3223. '/label'
  3224. );
  3225. $this->assertTags($result, $expected);
  3226. $result = $this->Form->radio('Model.1.field', array('option A'));
  3227. $expected = array(
  3228. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3229. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3230. 'label' => array('for' => 'Model1Field0'),
  3231. 'option A',
  3232. '/label'
  3233. );
  3234. $this->assertTags($result, $expected);
  3235. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3236. $expected = array(
  3237. 'fieldset' => array(),
  3238. 'legend' => array(),
  3239. 'Field',
  3240. '/legend',
  3241. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3242. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3243. array('label' => array('for' => 'ModelField0')),
  3244. 'option A',
  3245. '/label',
  3246. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3247. array('label' => array('for' => 'ModelField1')),
  3248. 'option B',
  3249. '/label',
  3250. '/fieldset'
  3251. );
  3252. $this->assertTags($result, $expected);
  3253. $result = $this->Form->radio(
  3254. 'Model.field',
  3255. array('option A', 'option B'),
  3256. array('between' => 'I am between')
  3257. );
  3258. $expected = array(
  3259. 'fieldset' => array(),
  3260. 'legend' => array(),
  3261. 'Field',
  3262. '/legend',
  3263. 'I am between',
  3264. 'input' => array(
  3265. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3266. 'value' => '', 'id' => 'ModelField_'
  3267. ),
  3268. array('input' => array(
  3269. 'type' => 'radio', 'name' => 'data[Model][field]',
  3270. 'value' => '0', 'id' => 'ModelField0'
  3271. )),
  3272. array('label' => array('for' => 'ModelField0')),
  3273. 'option A',
  3274. '/label',
  3275. array('input' => array(
  3276. 'type' => 'radio', 'name' => 'data[Model][field]',
  3277. 'value' => '1', 'id' => 'ModelField1'
  3278. )),
  3279. array('label' => array('for' => 'ModelField1')),
  3280. 'option B',
  3281. '/label',
  3282. '/fieldset'
  3283. );
  3284. $this->assertTags($result, $expected);
  3285. }
  3286. /**
  3287. * Test that radios with a 0 value are selected under the correct conditions.
  3288. *
  3289. * @return void
  3290. */
  3291. public function testRadioOptionWithZeroValue() {
  3292. $expected = array(
  3293. 'fieldset' => array(),
  3294. 'legend' => array(),
  3295. 'Field',
  3296. '/legend',
  3297. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3298. array('label' => array('for' => 'ModelField1')),
  3299. 'Yes',
  3300. '/label',
  3301. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3302. array('label' => array('for' => 'ModelField0')),
  3303. 'No',
  3304. '/label',
  3305. '/fieldset'
  3306. );
  3307. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3308. $this->assertTags($result, $expected);
  3309. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3310. $this->assertTags($result, $expected);
  3311. $expected = array(
  3312. 'fieldset' => array(),
  3313. 'legend' => array(),
  3314. 'Field',
  3315. '/legend',
  3316. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3317. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3318. array('label' => array('for' => 'ModelField1')),
  3319. 'Yes',
  3320. '/label',
  3321. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3322. array('label' => array('for' => 'ModelField0')),
  3323. 'No',
  3324. '/label',
  3325. '/fieldset'
  3326. );
  3327. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3328. $this->assertTags($result, $expected);
  3329. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3330. $this->assertTags($result, $expected);
  3331. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3332. $this->assertTags($result, $expected);
  3333. }
  3334. /**
  3335. * test disabled radio options
  3336. *
  3337. * @return void
  3338. */
  3339. public function testRadioDisabled() {
  3340. $result = $this->Form->radio(
  3341. 'Model.field',
  3342. array('option A', 'option B'),
  3343. array('disabled' => array('option A'), 'value' => '0')
  3344. );
  3345. $expected = array(
  3346. 'fieldset' => array(),
  3347. 'legend' => array(),
  3348. 'Field',
  3349. '/legend',
  3350. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3351. array('label' => array('for' => 'ModelField0')),
  3352. 'option A',
  3353. '/label',
  3354. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3355. array('label' => array('for' => 'ModelField1')),
  3356. 'option B',
  3357. '/label',
  3358. '/fieldset'
  3359. );
  3360. $this->assertTags($result, $expected);
  3361. $result = $this->Form->radio(
  3362. 'Model.field',
  3363. array('option A', 'option B'),
  3364. array('disabled' => true, 'value' => '0')
  3365. );
  3366. $expected = array(
  3367. 'fieldset' => array(),
  3368. 'legend' => array(),
  3369. 'Field',
  3370. '/legend',
  3371. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3372. array('label' => array('for' => 'ModelField0')),
  3373. 'option A',
  3374. '/label',
  3375. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3376. array('label' => array('for' => 'ModelField1')),
  3377. 'option B',
  3378. '/label',
  3379. '/fieldset'
  3380. );
  3381. $this->assertTags($result, $expected);
  3382. $result = $this->Form->radio(
  3383. 'Model.field',
  3384. array('option A', 'option B'),
  3385. array('disabled' => 'disabled', 'value' => '0')
  3386. );
  3387. $expected = array(
  3388. 'fieldset' => array(),
  3389. 'legend' => array(),
  3390. 'Field',
  3391. '/legend',
  3392. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3393. array('label' => array('for' => 'ModelField0')),
  3394. 'option A',
  3395. '/label',
  3396. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3397. array('label' => array('for' => 'ModelField1')),
  3398. 'option B',
  3399. '/label',
  3400. '/fieldset'
  3401. );
  3402. $this->assertTags($result, $expected);
  3403. }
  3404. /**
  3405. * test disabling the hidden input for radio buttons
  3406. *
  3407. * @return void
  3408. */
  3409. public function testRadioHiddenInputDisabling() {
  3410. $result = $this->Form->input('Model.1.field', array(
  3411. 'type' => 'radio',
  3412. 'options' => array('option A'),
  3413. 'hiddenField' => false
  3414. )
  3415. );
  3416. $expected = array(
  3417. 'div' => array('class' => 'input radio'),
  3418. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3419. 'label' => array('for' => 'Model1Field0'),
  3420. 'option A',
  3421. '/label',
  3422. '/div'
  3423. );
  3424. $this->assertTags($result, $expected);
  3425. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3426. $expected = array(
  3427. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3428. 'label' => array('for' => 'Model1Field0'),
  3429. 'option A',
  3430. '/label'
  3431. );
  3432. $this->assertTags($result, $expected);
  3433. }
  3434. /**
  3435. * test adding an empty option for radio buttons
  3436. *
  3437. * @return void
  3438. */
  3439. public function testRadioAddEmptyOption() {
  3440. $result = $this->Form->input('Model.1.field', array(
  3441. 'type' => 'radio',
  3442. 'options' => array('option A'),
  3443. 'empty' => true,
  3444. 'hiddenField' => false
  3445. ));
  3446. $expected = array(
  3447. 'div' => array('class' => 'input radio'),
  3448. 'fieldset' => array(),
  3449. 'legend' => array(),
  3450. 'Field',
  3451. '/legend',
  3452. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3453. array('label' => array('for' => 'Model1Field')),
  3454. __('empty'),
  3455. '/label',
  3456. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3457. array('label' => array('for' => 'Model1Field0')),
  3458. 'option A',
  3459. '/label',
  3460. '/fieldset',
  3461. '/div'
  3462. );
  3463. $this->assertTags($result, $expected);
  3464. $result = $this->Form->input('Model.1.field', array(
  3465. 'type' => 'radio',
  3466. 'options' => array('option A'),
  3467. 'empty' => 'CustomEmptyLabel',
  3468. 'hiddenField' => false
  3469. ));
  3470. $expected = array(
  3471. 'div' => array('class' => 'input radio'),
  3472. 'fieldset' => array(),
  3473. 'legend' => array(),
  3474. 'Field',
  3475. '/legend',
  3476. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3477. array('label' => array('for' => 'Model1Field')),
  3478. 'CustomEmptyLabel',
  3479. '/label',
  3480. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3481. array('label' => array('for' => 'Model1Field0')),
  3482. 'option A',
  3483. '/label',
  3484. '/fieldset',
  3485. '/div'
  3486. );
  3487. $this->assertTags($result, $expected);
  3488. $result = $this->Form->input('Model.1.field', array(
  3489. 'type' => 'radio',
  3490. 'options' => array('option A'),
  3491. 'empty' => false,
  3492. 'hiddenField' => false
  3493. ));
  3494. $this->assertTextNotContains('"Model1Field"', $result);
  3495. }
  3496. /**
  3497. * testSelect method
  3498. *
  3499. * Test select element generation.
  3500. *
  3501. * @return void
  3502. */
  3503. public function testSelect() {
  3504. $result = $this->Form->select('Model.field', array());
  3505. $expected = array(
  3506. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3507. array('option' => array('value' => '')),
  3508. '/option',
  3509. '/select'
  3510. );
  3511. $this->assertTags($result, $expected);
  3512. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3513. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3514. $expected = array(
  3515. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3516. array('option' => array('value' => '')),
  3517. '/option',
  3518. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3519. 'good',
  3520. '/option',
  3521. array('option' => array('value' => 'other')),
  3522. 'bad',
  3523. '/option',
  3524. '/select'
  3525. );
  3526. $this->assertTags($result, $expected);
  3527. $this->Form->request->data = array();
  3528. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3529. $expected = array(
  3530. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3531. array('option' => array('value' => '')),
  3532. '/option',
  3533. array('option' => array('value' => 'value')),
  3534. 'good',
  3535. '/option',
  3536. array('option' => array('value' => 'other')),
  3537. 'bad',
  3538. '/option',
  3539. '/select'
  3540. );
  3541. $this->assertTags($result, $expected);
  3542. $result = $this->Form->select(
  3543. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3544. array('empty' => false)
  3545. );
  3546. $expected = array(
  3547. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3548. array('option' => array('value' => 'first')),
  3549. 'first &quot;html&quot; &lt;chars&gt;',
  3550. '/option',
  3551. array('option' => array('value' => 'second')),
  3552. 'value',
  3553. '/option',
  3554. '/select'
  3555. );
  3556. $this->assertTags($result, $expected);
  3557. $result = $this->Form->select(
  3558. 'Model.field',
  3559. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3560. array('escape' => false, 'empty' => false)
  3561. );
  3562. $expected = array(
  3563. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3564. array('option' => array('value' => 'first')),
  3565. 'first "html" <chars>',
  3566. '/option',
  3567. array('option' => array('value' => 'second')),
  3568. 'value',
  3569. '/option',
  3570. '/select'
  3571. );
  3572. $this->assertTags($result, $expected);
  3573. $options = array(
  3574. array('value' => 'first', 'name' => 'First'),
  3575. array('value' => 'first', 'name' => 'Another First'),
  3576. );
  3577. $result = $this->Form->select(
  3578. 'Model.field',
  3579. $options,
  3580. array('escape' => false, 'empty' => false)
  3581. );
  3582. $expected = array(
  3583. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3584. array('option' => array('value' => 'first')),
  3585. 'First',
  3586. '/option',
  3587. array('option' => array('value' => 'first')),
  3588. 'Another First',
  3589. '/option',
  3590. '/select'
  3591. );
  3592. $this->assertTags($result, $expected);
  3593. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3594. $result = $this->Form->select(
  3595. 'Model.contact_id',
  3596. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3597. array('escape' => false, 'empty' => 'pick something')
  3598. );
  3599. $expected = array(
  3600. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3601. array('option' => array('value' => '')), 'pick something', '/option',
  3602. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3603. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3604. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3605. '/select'
  3606. );
  3607. $this->assertTags($result, $expected);
  3608. $this->Form->request->data['Model']['field'] = 0;
  3609. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3610. $expected = array(
  3611. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3612. array('option' => array('value' => '')), '/option',
  3613. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3614. array('option' => array('value' => '1')), 'Yes', '/option',
  3615. '/select'
  3616. );
  3617. $this->assertTags($result, $expected);
  3618. }
  3619. /**
  3620. * test that select() with optiongroups listens to the escape param.
  3621. *
  3622. * @return void
  3623. */
  3624. public function testSelectOptionGroupEscaping() {
  3625. $options = array(
  3626. '>< Key' => array(
  3627. 1 => 'One',
  3628. 2 => 'Two'
  3629. )
  3630. );
  3631. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3632. $expected = array(
  3633. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3634. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3635. array('option' => array('value' => '1')), 'One', '/option',
  3636. array('option' => array('value' => '2')), 'Two', '/option',
  3637. '/optgroup',
  3638. '/select'
  3639. );
  3640. $this->assertTags($result, $expected);
  3641. $options = array(
  3642. '>< Key' => array(
  3643. 1 => 'One',
  3644. 2 => 'Two'
  3645. )
  3646. );
  3647. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3648. $expected = array(
  3649. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3650. 'optgroup' => array('label' => '>< Key'),
  3651. array('option' => array('value' => '1')), 'One', '/option',
  3652. array('option' => array('value' => '2')), 'Two', '/option',
  3653. '/optgroup',
  3654. '/select'
  3655. );
  3656. $this->assertTags($result, $expected);
  3657. }
  3658. /**
  3659. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3660. *
  3661. * @return void
  3662. */
  3663. public function testSelectWithNullAttributes() {
  3664. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3665. $expected = array(
  3666. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3667. array('option' => array('value' => '0')),
  3668. 'first',
  3669. '/option',
  3670. array('option' => array('value' => '1')),
  3671. 'second',
  3672. '/option',
  3673. '/select'
  3674. );
  3675. $this->assertTags($result, $expected);
  3676. }
  3677. /**
  3678. * testNestedSelect method
  3679. *
  3680. * test select element generation with optgroups
  3681. *
  3682. * @return void
  3683. */
  3684. public function testNestedSelect() {
  3685. $result = $this->Form->select(
  3686. 'Model.field',
  3687. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3688. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3689. )), array('empty' => false)
  3690. );
  3691. $expected = array(
  3692. 'select' => array('name' => 'data[Model][field]',
  3693. 'id' => 'ModelField'),
  3694. array('option' => array('value' => 1)),
  3695. 'One',
  3696. '/option',
  3697. array('option' => array('value' => 2)),
  3698. 'Two',
  3699. '/option',
  3700. array('optgroup' => array('label' => 'Three')),
  3701. array('option' => array('value' => 4)),
  3702. 'Four',
  3703. '/option',
  3704. array('option' => array('value' => 5)),
  3705. 'Five',
  3706. '/option',
  3707. '/optgroup',
  3708. '/select'
  3709. );
  3710. $this->assertTags($result, $expected);
  3711. $result = $this->Form->select(
  3712. 'Model.field',
  3713. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3714. array('showParents' => true, 'empty' => false)
  3715. );
  3716. $expected = array(
  3717. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3718. array('option' => array('value' => 1)),
  3719. 'One',
  3720. '/option',
  3721. array('option' => array('value' => 2)),
  3722. 'Two',
  3723. '/option',
  3724. array('optgroup' => array('label' => 'Three')),
  3725. array('option' => array('value' => 3)),
  3726. 'Three',
  3727. '/option',
  3728. array('option' => array('value' => 4)),
  3729. 'Four',
  3730. '/option',
  3731. '/optgroup',
  3732. '/select'
  3733. );
  3734. $this->assertTags($result, $expected);
  3735. }
  3736. /**
  3737. * testSelectMultiple method
  3738. *
  3739. * test generation of multiple select elements
  3740. *
  3741. * @return void
  3742. */
  3743. public function testSelectMultiple() {
  3744. $options = array('first', 'second', 'third');
  3745. $result = $this->Form->select(
  3746. 'Model.multi_field', $options, array('multiple' => true)
  3747. );
  3748. $expected = array(
  3749. 'input' => array(
  3750. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3751. ),
  3752. 'select' => array(
  3753. 'name' => 'data[Model][multi_field][]',
  3754. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3755. ),
  3756. array('option' => array('value' => '0')),
  3757. 'first',
  3758. '/option',
  3759. array('option' => array('value' => '1')),
  3760. 'second',
  3761. '/option',
  3762. array('option' => array('value' => '2')),
  3763. 'third',
  3764. '/option',
  3765. '/select'
  3766. );
  3767. $this->assertTags($result, $expected);
  3768. $result = $this->Form->select(
  3769. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3770. );
  3771. $expected = array(
  3772. 'input' => array(
  3773. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3774. ),
  3775. 'select' => array(
  3776. 'name' => 'data[Model][multi_field][]',
  3777. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3778. ),
  3779. array('option' => array('value' => '0')),
  3780. 'first',
  3781. '/option',
  3782. array('option' => array('value' => '1')),
  3783. 'second',
  3784. '/option',
  3785. array('option' => array('value' => '2')),
  3786. 'third',
  3787. '/option',
  3788. '/select'
  3789. );
  3790. $this->assertTags($result, $expected);
  3791. $result = $this->Form->select(
  3792. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3793. );
  3794. $expected = array(
  3795. 'input' => array(
  3796. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3797. ),
  3798. 'select' => array(
  3799. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3800. 'multiple' => 'multiple'
  3801. ),
  3802. array('option' => array('value' => '0', 'selected' => 'selected')),
  3803. 'first',
  3804. '/option',
  3805. array('option' => array('value' => '1', 'selected' => 'selected')),
  3806. 'second',
  3807. '/option',
  3808. array('option' => array('value' => '2')),
  3809. 'third',
  3810. '/option',
  3811. '/select'
  3812. );
  3813. $this->assertTags($result, $expected);
  3814. $result = $this->Form->select(
  3815. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3816. );
  3817. $expected = array(
  3818. 'select' => array(
  3819. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3820. ),
  3821. array('option' => array('value' => '0', 'selected' => 'selected')),
  3822. 'first',
  3823. '/option',
  3824. array('option' => array('value' => '1', 'selected' => 'selected')),
  3825. 'second',
  3826. '/option',
  3827. array('option' => array('value' => '2')),
  3828. 'third',
  3829. '/option',
  3830. '/select'
  3831. );
  3832. $this->assertTags($result, $expected);
  3833. }
  3834. /**
  3835. * test generation of habtm select boxes.
  3836. *
  3837. * @return void
  3838. */
  3839. public function testHabtmSelectBox() {
  3840. $this->View->viewVars['contactTags'] = array(
  3841. 1 => 'blue',
  3842. 2 => 'red',
  3843. 3 => 'green'
  3844. );
  3845. $this->Form->request->data = array(
  3846. 'Contact' => array(),
  3847. 'ContactTag' => array(
  3848. array(
  3849. 'id' => 1,
  3850. 'name' => 'blue'
  3851. ),
  3852. array(
  3853. 'id' => 3,
  3854. 'name' => 'green'
  3855. )
  3856. )
  3857. );
  3858. $this->Form->create('Contact');
  3859. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3860. $expected = array(
  3861. 'input' => array(
  3862. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3863. ),
  3864. 'select' => array(
  3865. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3866. 'multiple' => 'multiple'
  3867. ),
  3868. array('option' => array('value' => '1', 'selected' => 'selected')),
  3869. 'blue',
  3870. '/option',
  3871. array('option' => array('value' => '2')),
  3872. 'red',
  3873. '/option',
  3874. array('option' => array('value' => '3', 'selected' => 'selected')),
  3875. 'green',
  3876. '/option',
  3877. '/select'
  3878. );
  3879. $this->assertTags($result, $expected);
  3880. }
  3881. /**
  3882. * test generation of multi select elements in checkbox format
  3883. *
  3884. * @return void
  3885. */
  3886. public function testSelectMultipleCheckboxes() {
  3887. $result = $this->Form->select(
  3888. 'Model.multi_field',
  3889. array('first', 'second', 'third'),
  3890. array('multiple' => 'checkbox')
  3891. );
  3892. $expected = array(
  3893. 'input' => array(
  3894. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3895. ),
  3896. array('div' => array('class' => 'checkbox')),
  3897. array('input' => array(
  3898. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3899. 'value' => '0', 'id' => 'ModelMultiField0'
  3900. )),
  3901. array('label' => array('for' => 'ModelMultiField0')),
  3902. 'first',
  3903. '/label',
  3904. '/div',
  3905. array('div' => array('class' => 'checkbox')),
  3906. array('input' => array(
  3907. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3908. 'value' => '1', 'id' => 'ModelMultiField1'
  3909. )),
  3910. array('label' => array('for' => 'ModelMultiField1')),
  3911. 'second',
  3912. '/label',
  3913. '/div',
  3914. array('div' => array('class' => 'checkbox')),
  3915. array('input' => array(
  3916. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3917. 'value' => '2', 'id' => 'ModelMultiField2'
  3918. )),
  3919. array('label' => array('for' => 'ModelMultiField2')),
  3920. 'third',
  3921. '/label',
  3922. '/div'
  3923. );
  3924. $this->assertTags($result, $expected);
  3925. $result = $this->Form->select(
  3926. 'Model.multi_field',
  3927. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3928. array('multiple' => 'checkbox')
  3929. );
  3930. $expected = array(
  3931. 'input' => array(
  3932. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3933. ),
  3934. array('div' => array('class' => 'checkbox')),
  3935. array('input' => array(
  3936. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3937. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3938. )),
  3939. array('label' => array('for' => 'ModelMultiFieldA')),
  3940. 'first',
  3941. '/label',
  3942. '/div',
  3943. array('div' => array('class' => 'checkbox')),
  3944. array('input' => array(
  3945. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3946. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3947. )),
  3948. array('label' => array('for' => 'ModelMultiFieldB')),
  3949. 'second',
  3950. '/label',
  3951. '/div',
  3952. array('div' => array('class' => 'checkbox')),
  3953. array('input' => array(
  3954. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3955. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3956. )),
  3957. array('label' => array('for' => 'ModelMultiFieldC')),
  3958. 'third',
  3959. '/label',
  3960. '/div'
  3961. );
  3962. $this->assertTags($result, $expected);
  3963. $result = $this->Form->select(
  3964. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3965. );
  3966. $expected = array(
  3967. 'input' => array(
  3968. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3969. ),
  3970. array('div' => array('class' => 'checkbox')),
  3971. array('input' => array(
  3972. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3973. 'value' => '1', 'id' => 'ModelMultiField1'
  3974. )),
  3975. array('label' => array('for' => 'ModelMultiField1')),
  3976. 'first',
  3977. '/label',
  3978. '/div'
  3979. );
  3980. $this->assertTags($result, $expected);
  3981. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3982. $result = $this->Form->select(
  3983. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3984. );
  3985. $expected = array(
  3986. 'input' => array(
  3987. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3988. ),
  3989. array('div' => array('class' => 'checkbox')),
  3990. array('input' => array(
  3991. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3992. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3993. )),
  3994. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3995. 'first',
  3996. '/label',
  3997. '/div',
  3998. array('div' => array('class' => 'checkbox')),
  3999. array('input' => array(
  4000. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4001. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4002. )),
  4003. array('label' => array('for' => 'ModelTagsArray')),
  4004. 'Array',
  4005. '/label',
  4006. '/div'
  4007. );
  4008. $this->assertTags($result, $expected);
  4009. }
  4010. /**
  4011. * test multiple checkboxes with div styles.
  4012. *
  4013. * @return void
  4014. */
  4015. public function testSelectMultipleCheckboxDiv() {
  4016. $result = $this->Form->select(
  4017. 'Model.tags',
  4018. array('first', 'second'),
  4019. array('multiple' => 'checkbox', 'class' => 'my-class')
  4020. );
  4021. $expected = array(
  4022. 'input' => array(
  4023. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4024. ),
  4025. array('div' => array('class' => 'my-class')),
  4026. array('input' => array(
  4027. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4028. 'value' => '0', 'id' => 'ModelTags0'
  4029. )),
  4030. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4031. '/div',
  4032. array('div' => array('class' => 'my-class')),
  4033. array('input' => array(
  4034. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4035. 'value' => '1', 'id' => 'ModelTags1'
  4036. )),
  4037. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4038. '/div'
  4039. );
  4040. $this->assertTags($result, $expected);
  4041. $result = $this->Form->input('Model.tags', array(
  4042. 'options' => array('first', 'second'),
  4043. 'multiple' => 'checkbox',
  4044. 'class' => 'my-class',
  4045. 'div' => false,
  4046. 'label' => false
  4047. ));
  4048. $this->assertTags($result, $expected);
  4049. $Contact = ClassRegistry::getObject('Contact');
  4050. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4051. $result = $this->Form->input('Contact.tags', array(
  4052. 'options' => array('one'),
  4053. 'multiple' => 'checkbox',
  4054. 'label' => false,
  4055. 'div' => false
  4056. ));
  4057. $expected = array(
  4058. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4059. array('div' => array('class' => 'checkbox form-error')),
  4060. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4061. array('label' => array('for' => 'ContactTags0')),
  4062. 'one',
  4063. '/label',
  4064. '/div'
  4065. );
  4066. $this->assertTags($result, $expected);
  4067. $result = $this->Form->input('Contact.tags', array(
  4068. 'options' => array('one'),
  4069. 'multiple' => 'checkbox',
  4070. 'class' => 'mycheckbox',
  4071. 'label' => false,
  4072. 'div' => false
  4073. ));
  4074. $expected = array(
  4075. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4076. array('div' => array('class' => 'mycheckbox form-error')),
  4077. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4078. array('label' => array('for' => 'ContactTags0')),
  4079. 'one',
  4080. '/label',
  4081. '/div'
  4082. );
  4083. $this->assertTags($result, $expected);
  4084. }
  4085. /**
  4086. * Checks the security hash array generated for multiple-input checkbox elements
  4087. *
  4088. * @return void
  4089. */
  4090. public function testSelectMultipleCheckboxSecurity() {
  4091. $this->Form->request['_Token'] = array('key' => 'testKey');
  4092. $this->assertEquals(array(), $this->Form->fields);
  4093. $result = $this->Form->select(
  4094. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4095. array('multiple' => 'checkbox')
  4096. );
  4097. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4098. $result = $this->Form->secure($this->Form->fields);
  4099. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4100. $this->assertRegExp('/"' . $key . '"/', $result);
  4101. }
  4102. /**
  4103. * Multiple select elements should always be secured as they always participate
  4104. * in the POST data.
  4105. *
  4106. * @return void
  4107. */
  4108. public function testSelectMultipleSecureWithNoOptions() {
  4109. $this->Form->request['_Token'] = array('key' => 'testkey');
  4110. $this->assertEquals(array(), $this->Form->fields);
  4111. $result = $this->Form->select(
  4112. 'Model.select',
  4113. array(),
  4114. array('multiple' => true)
  4115. );
  4116. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4117. }
  4118. /**
  4119. * When a select box has no options it should not be added to the fields list
  4120. * as it always fail post validation.
  4121. *
  4122. * @return void
  4123. */
  4124. public function testSelectNoSecureWithNoOptions() {
  4125. $this->Form->request['_Token'] = array('key' => 'testkey');
  4126. $this->assertEquals(array(), $this->Form->fields);
  4127. $this->Form->select(
  4128. 'Model.select',
  4129. array()
  4130. );
  4131. $this->assertEquals(array(), $this->Form->fields);
  4132. $this->Form->select(
  4133. 'Model.select',
  4134. array(),
  4135. array('empty' => true)
  4136. );
  4137. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4138. }
  4139. /**
  4140. * testInputMultipleCheckboxes method
  4141. *
  4142. * test input() resulting in multi select elements being generated.
  4143. *
  4144. * @return void
  4145. */
  4146. public function testInputMultipleCheckboxes() {
  4147. $result = $this->Form->input('Model.multi_field', array(
  4148. 'options' => array('first', 'second', 'third'),
  4149. 'multiple' => 'checkbox'
  4150. ));
  4151. $expected = array(
  4152. array('div' => array('class' => 'input select')),
  4153. array('label' => array('for' => 'ModelMultiField')),
  4154. 'Multi Field',
  4155. '/label',
  4156. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4157. array('div' => array('class' => 'checkbox')),
  4158. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4159. array('label' => array('for' => 'ModelMultiField0')),
  4160. 'first',
  4161. '/label',
  4162. '/div',
  4163. array('div' => array('class' => 'checkbox')),
  4164. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4165. array('label' => array('for' => 'ModelMultiField1')),
  4166. 'second',
  4167. '/label',
  4168. '/div',
  4169. array('div' => array('class' => 'checkbox')),
  4170. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4171. array('label' => array('for' => 'ModelMultiField2')),
  4172. 'third',
  4173. '/label',
  4174. '/div',
  4175. '/div'
  4176. );
  4177. $this->assertTags($result, $expected);
  4178. $result = $this->Form->input('Model.multi_field', array(
  4179. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4180. 'multiple' => 'checkbox'
  4181. ));
  4182. $expected = array(
  4183. array('div' => array('class' => 'input select')),
  4184. array('label' => array('for' => 'ModelMultiField')),
  4185. 'Multi Field',
  4186. '/label',
  4187. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4188. array('div' => array('class' => 'checkbox')),
  4189. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4190. array('label' => array('for' => 'ModelMultiFieldA')),
  4191. 'first',
  4192. '/label',
  4193. '/div',
  4194. array('div' => array('class' => 'checkbox')),
  4195. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4196. array('label' => array('for' => 'ModelMultiFieldB')),
  4197. 'second',
  4198. '/label',
  4199. '/div',
  4200. array('div' => array('class' => 'checkbox')),
  4201. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4202. array('label' => array('for' => 'ModelMultiFieldC')),
  4203. 'third',
  4204. '/label',
  4205. '/div',
  4206. '/div'
  4207. );
  4208. $this->assertTags($result, $expected);
  4209. $result = $this->Form->input('Model.multi_field', array(
  4210. 'options' => array('1' => 'first'),
  4211. 'multiple' => 'checkbox',
  4212. 'label' => false,
  4213. 'div' => false
  4214. ));
  4215. $expected = array(
  4216. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4217. array('div' => array('class' => 'checkbox')),
  4218. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4219. array('label' => array('for' => 'ModelMultiField1')),
  4220. 'first',
  4221. '/label',
  4222. '/div'
  4223. );
  4224. $this->assertTags($result, $expected);
  4225. $result = $this->Form->input('Model.multi_field', array(
  4226. 'options' => array('2' => 'second'),
  4227. 'multiple' => 'checkbox',
  4228. 'label' => false,
  4229. 'div' => false
  4230. ));
  4231. $expected = array(
  4232. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4233. array('div' => array('class' => 'checkbox')),
  4234. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4235. array('label' => array('for' => 'ModelMultiField2')),
  4236. 'second',
  4237. '/label',
  4238. '/div'
  4239. );
  4240. $this->assertTags($result, $expected);
  4241. }
  4242. /**
  4243. * testSelectHiddenFieldOmission method
  4244. *
  4245. * test that select() with 'hiddenField' => false omits the hidden field
  4246. *
  4247. * @return void
  4248. */
  4249. public function testSelectHiddenFieldOmission() {
  4250. $result = $this->Form->select('Model.multi_field',
  4251. array('first', 'second'),
  4252. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4253. );
  4254. $expected = array(
  4255. array('div' => array('class' => 'checkbox')),
  4256. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4257. array('label' => array('for' => 'ModelMultiField0')),
  4258. 'first',
  4259. '/label',
  4260. '/div',
  4261. array('div' => array('class' => 'checkbox')),
  4262. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4263. array('label' => array('for' => 'ModelMultiField1')),
  4264. 'second',
  4265. '/label',
  4266. '/div'
  4267. );
  4268. $this->assertTags($result, $expected);
  4269. $result = $this->Form->input('Model.multi_field', array(
  4270. 'options' => array('first', 'second'),
  4271. 'multiple' => 'checkbox',
  4272. 'hiddenField' => false
  4273. ));
  4274. $expected = array(
  4275. array('div' => array('class' => 'input select')),
  4276. array('label' => array('for' => 'ModelMultiField')),
  4277. 'Multi Field',
  4278. '/label',
  4279. array('div' => array('class' => 'checkbox')),
  4280. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4281. array('label' => array('for' => 'ModelMultiField0')),
  4282. 'first',
  4283. '/label',
  4284. '/div',
  4285. array('div' => array('class' => 'checkbox')),
  4286. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4287. array('label' => array('for' => 'ModelMultiField1')),
  4288. 'second',
  4289. '/label',
  4290. '/div',
  4291. '/div'
  4292. );
  4293. $this->assertTags($result, $expected);
  4294. }
  4295. /**
  4296. * test that select() with multiple = checkbox works with overriding name attribute.
  4297. *
  4298. * @return void
  4299. */
  4300. public function testSelectCheckboxMultipleOverrideName() {
  4301. $result = $this->Form->input('category', array(
  4302. 'type' => 'select',
  4303. 'multiple' => 'checkbox',
  4304. 'name' => 'data[fish]',
  4305. 'options' => array('1', '2'),
  4306. 'div' => false,
  4307. 'label' => false,
  4308. ));
  4309. $expected = array(
  4310. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4311. array('div' => array('class' => 'checkbox')),
  4312. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4313. array('label' => array('for' => 'Category0')), '1', '/label',
  4314. '/div',
  4315. array('div' => array('class' => 'checkbox')),
  4316. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4317. array('label' => array('for' => 'Category1')), '2', '/label',
  4318. '/div'
  4319. );
  4320. $this->assertTags($result, $expected);
  4321. }
  4322. /**
  4323. * Test that 'id' overrides all the checkbox id's as well.
  4324. *
  4325. * @return void
  4326. */
  4327. public function testSelectCheckboxMultipleId() {
  4328. $result = $this->Form->select(
  4329. 'Model.multi_field',
  4330. array('first', 'second', 'third'),
  4331. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4332. );
  4333. $expected = array(
  4334. 'input' => array(
  4335. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4336. ),
  4337. array('div' => array('class' => 'checkbox')),
  4338. array('input' => array(
  4339. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4340. 'value' => '0', 'id' => 'CustomId0'
  4341. )),
  4342. array('label' => array('for' => 'CustomId0')),
  4343. 'first',
  4344. '/label',
  4345. '/div',
  4346. array('div' => array('class' => 'checkbox')),
  4347. array('input' => array(
  4348. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4349. 'value' => '1', 'id' => 'CustomId1'
  4350. )),
  4351. array('label' => array('for' => 'CustomId1')),
  4352. 'second',
  4353. '/label',
  4354. '/div',
  4355. array('div' => array('class' => 'checkbox')),
  4356. array('input' => array(
  4357. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4358. 'value' => '2', 'id' => 'CustomId2'
  4359. )),
  4360. array('label' => array('for' => 'CustomId2')),
  4361. 'third',
  4362. '/label',
  4363. '/div'
  4364. );
  4365. $this->assertTags($result, $expected);
  4366. }
  4367. /**
  4368. * testCheckbox method
  4369. *
  4370. * Test generation of checkboxes
  4371. *
  4372. * @return void
  4373. */
  4374. public function testCheckbox() {
  4375. $result = $this->Form->checkbox('Model.field');
  4376. $expected = array(
  4377. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4378. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4379. );
  4380. $this->assertTags($result, $expected);
  4381. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4382. $expected = array(
  4383. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4384. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4385. );
  4386. $this->assertTags($result, $expected);
  4387. $Contact = ClassRegistry::getObject('Contact');
  4388. $Contact->validationErrors['field'] = 1;
  4389. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4390. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4391. $expected = array(
  4392. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4393. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4394. );
  4395. $this->assertTags($result, $expected);
  4396. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4397. $expected = array(
  4398. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4399. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4400. );
  4401. $this->assertTags($result, $expected);
  4402. $this->Form->request->data['Contact']['field'] = '';
  4403. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4404. $expected = array(
  4405. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4406. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4407. );
  4408. $this->assertTags($result, $expected);
  4409. $Contact->validationErrors = array();
  4410. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4411. $expected = array(
  4412. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4413. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4414. );
  4415. $this->assertTags($result, $expected);
  4416. $this->Form->request->data['Contact']['published'] = 1;
  4417. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4418. $expected = array(
  4419. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4420. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4421. );
  4422. $this->assertTags($result, $expected);
  4423. $this->Form->request->data['Contact']['published'] = 0;
  4424. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4425. $expected = array(
  4426. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4427. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4428. );
  4429. $this->assertTags($result, $expected);
  4430. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4431. $expected = array(
  4432. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4433. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4434. );
  4435. $this->assertTags($result, $expected);
  4436. $result = $this->Form->checkbox('CustomField.1.value');
  4437. $expected = array(
  4438. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4439. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4440. );
  4441. $this->assertTags($result, $expected);
  4442. }
  4443. /**
  4444. * test checkbox() with a custom name attribute
  4445. *
  4446. * @return void
  4447. */
  4448. public function testCheckboxCustomNameAttribute() {
  4449. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4450. $expected = array(
  4451. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4452. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4453. );
  4454. $this->assertTags($result, $expected);
  4455. }
  4456. /**
  4457. * test the checked option for checkboxes.
  4458. *
  4459. * @return void
  4460. */
  4461. public function testCheckboxCheckedOption() {
  4462. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4463. $expected = array(
  4464. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4465. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4466. );
  4467. $this->assertTags($result, $expected);
  4468. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4469. $expected = array(
  4470. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4471. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4472. );
  4473. $this->assertTags($result, $expected);
  4474. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4475. $expected = array(
  4476. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4477. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4478. );
  4479. $this->assertTags($result, $expected);
  4480. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4481. $expected = array(
  4482. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4483. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4484. );
  4485. $this->assertTags($result, $expected);
  4486. $this->Form->request->data['Model']['field'] = 1;
  4487. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4488. $expected = array(
  4489. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4490. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4491. );
  4492. $this->assertTags($result, $expected);
  4493. }
  4494. /**
  4495. * Test that disabled attribute works on both the checkbox and hidden input.
  4496. *
  4497. * @return void
  4498. */
  4499. public function testCheckboxDisabling() {
  4500. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4501. $expected = array(
  4502. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4503. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4504. );
  4505. $this->assertTags($result, $expected);
  4506. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4507. $expected = array(
  4508. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4509. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4510. );
  4511. $this->assertTags($result, $expected);
  4512. }
  4513. /**
  4514. * Test that the hidden input for checkboxes can be omitted or set to a
  4515. * specific value.
  4516. *
  4517. * @return void
  4518. */
  4519. public function testCheckboxHiddenField() {
  4520. $result = $this->Form->input('UserForm.something', array(
  4521. 'type' => 'checkbox',
  4522. 'hiddenField' => false
  4523. ));
  4524. $expected = array(
  4525. 'div' => array('class' => 'input checkbox'),
  4526. array('input' => array(
  4527. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4528. 'value' => '1', 'id' => 'UserFormSomething'
  4529. )),
  4530. 'label' => array('for' => 'UserFormSomething'),
  4531. 'Something',
  4532. '/label',
  4533. '/div'
  4534. );
  4535. $this->assertTags($result, $expected);
  4536. $result = $this->Form->input('UserForm.something', array(
  4537. 'type' => 'checkbox',
  4538. 'value' => 'Y',
  4539. 'hiddenField' => 'N',
  4540. ));
  4541. $expected = array(
  4542. 'div' => array('class' => 'input checkbox'),
  4543. array('input' => array(
  4544. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4545. 'value' => 'N', 'id' => 'UserFormSomething_'
  4546. )),
  4547. array('input' => array(
  4548. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4549. 'value' => 'Y', 'id' => 'UserFormSomething'
  4550. )),
  4551. 'label' => array('for' => 'UserFormSomething'),
  4552. 'Something',
  4553. '/label',
  4554. '/div'
  4555. );
  4556. $this->assertTags($result, $expected);
  4557. }
  4558. /**
  4559. * testDateTime method
  4560. *
  4561. * Test generation of date/time select elements
  4562. *
  4563. * @return void
  4564. */
  4565. public function testDateTime() {
  4566. extract($this->dateRegex);
  4567. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4568. $now = strtotime('now');
  4569. $expected = array(
  4570. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4571. $daysRegex,
  4572. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4573. date('j', $now),
  4574. '/option',
  4575. '*/select',
  4576. '-',
  4577. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4578. $monthsRegex,
  4579. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4580. date('F', $now),
  4581. '/option',
  4582. '*/select',
  4583. '-',
  4584. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4585. $yearsRegex,
  4586. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4587. date('Y', $now),
  4588. '/option',
  4589. '*/select',
  4590. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4591. $hoursRegex,
  4592. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4593. date('g', $now),
  4594. '/option',
  4595. '*/select',
  4596. ':',
  4597. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4598. $minutesRegex,
  4599. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4600. date('i', $now),
  4601. '/option',
  4602. '*/select',
  4603. ' ',
  4604. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4605. $meridianRegex,
  4606. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4607. date('a', $now),
  4608. '/option',
  4609. '*/select'
  4610. );
  4611. $this->assertTags($result, $expected);
  4612. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4613. $expected = array(
  4614. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4615. $daysRegex,
  4616. array('option' => array('value' => '')),
  4617. '/option',
  4618. '*/select',
  4619. '-',
  4620. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4621. $monthsRegex,
  4622. array('option' => array('value' => '')),
  4623. '/option',
  4624. '*/select',
  4625. '-',
  4626. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4627. $yearsRegex,
  4628. array('option' => array('value' => '')),
  4629. '/option',
  4630. '*/select',
  4631. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4632. $hoursRegex,
  4633. array('option' => array('value' => '')),
  4634. '/option',
  4635. '*/select',
  4636. ':',
  4637. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4638. $minutesRegex,
  4639. array('option' => array('value' => '')),
  4640. '/option',
  4641. '*/select',
  4642. ' ',
  4643. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4644. $meridianRegex,
  4645. array('option' => array('value' => '')),
  4646. '/option',
  4647. '*/select'
  4648. );
  4649. $this->assertTags($result, $expected);
  4650. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4651. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4652. $this->assertTags($result, $expected);
  4653. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4654. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4655. $this->assertTags($result, $expected);
  4656. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4657. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4658. $expected = array(
  4659. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4660. $daysRegex,
  4661. array('option' => array('value' => '')),
  4662. '/option',
  4663. '*/select',
  4664. '-',
  4665. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4666. $monthsRegex,
  4667. array('option' => array('value' => '')),
  4668. '/option',
  4669. '*/select',
  4670. '-',
  4671. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4672. $yearsRegex,
  4673. array('option' => array('value' => '')),
  4674. '/option',
  4675. '*/select',
  4676. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4677. $hoursRegex,
  4678. array('option' => array('value' => '')),
  4679. '/option',
  4680. '*/select',
  4681. ':',
  4682. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4683. $minutesRegex,
  4684. array('option' => array('value' => '')),
  4685. '/option',
  4686. array('option' => array('value' => '00')),
  4687. '00',
  4688. '/option',
  4689. array('option' => array('value' => '05')),
  4690. '05',
  4691. '/option',
  4692. array('option' => array('value' => '10')),
  4693. '10',
  4694. '/option',
  4695. '*/select',
  4696. ' ',
  4697. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4698. $meridianRegex,
  4699. array('option' => array('value' => '')),
  4700. '/option',
  4701. '*/select'
  4702. );
  4703. $this->assertTags($result, $expected);
  4704. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4705. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4706. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4707. $this->Form->request->data['Contact']['data'] = null;
  4708. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4709. $expected = array(
  4710. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4711. $daysRegex,
  4712. array('option' => array('value' => '')),
  4713. '/option',
  4714. '*/select',
  4715. '-',
  4716. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4717. $monthsRegex,
  4718. array('option' => array('value' => '')),
  4719. '/option',
  4720. '*/select',
  4721. '-',
  4722. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4723. $yearsRegex,
  4724. array('option' => array('value' => '')),
  4725. '/option',
  4726. '*/select',
  4727. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4728. $hoursRegex,
  4729. array('option' => array('value' => '')),
  4730. '/option',
  4731. '*/select',
  4732. ':',
  4733. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4734. $minutesRegex,
  4735. array('option' => array('value' => '')),
  4736. '/option',
  4737. '*/select',
  4738. ' ',
  4739. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4740. $meridianRegex,
  4741. array('option' => array('value' => '')),
  4742. '/option',
  4743. '*/select'
  4744. );
  4745. $this->assertTags($result, $expected);
  4746. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4747. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4748. $now = strtotime($this->Form->data['Model']['field']);
  4749. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4750. $expected = array(
  4751. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4752. $daysRegex,
  4753. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4754. date('j', $now),
  4755. '/option',
  4756. '*/select',
  4757. '-',
  4758. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4759. $monthsRegex,
  4760. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4761. date('F', $now),
  4762. '/option',
  4763. '*/select',
  4764. '-',
  4765. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4766. $yearsRegex,
  4767. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4768. date('Y', $now),
  4769. '/option',
  4770. '*/select',
  4771. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4772. $hoursRegex,
  4773. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4774. date('g', $now),
  4775. '/option',
  4776. '*/select',
  4777. ':',
  4778. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4779. $minutesRegex,
  4780. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4781. date('i', $now),
  4782. '/option',
  4783. '*/select',
  4784. ' ',
  4785. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4786. $meridianRegex,
  4787. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4788. date('a', $now),
  4789. '/option',
  4790. '*/select'
  4791. );
  4792. $this->assertTags($result, $expected);
  4793. $selected = strtotime('2008-10-26 12:33:00');
  4794. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4795. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4796. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4797. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4798. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4799. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4800. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4801. $this->Form->create('Contact');
  4802. $result = $this->Form->input('published');
  4803. $now = strtotime('now');
  4804. $expected = array(
  4805. 'div' => array('class' => 'input date'),
  4806. 'label' => array('for' => 'ContactPublishedMonth'),
  4807. 'Published',
  4808. '/label',
  4809. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4810. $monthsRegex,
  4811. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4812. date('F', $now),
  4813. '/option',
  4814. '*/select',
  4815. '-',
  4816. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4817. $daysRegex,
  4818. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4819. date('j', $now),
  4820. '/option',
  4821. '*/select',
  4822. '-',
  4823. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4824. $yearsRegex,
  4825. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4826. date('Y', $now),
  4827. '/option',
  4828. '*/select',
  4829. '/div'
  4830. );
  4831. $this->assertTags($result, $expected);
  4832. $result = $this->Form->input('published2', array('type' => 'date'));
  4833. $now = strtotime('now');
  4834. $expected = array(
  4835. 'div' => array('class' => 'input date'),
  4836. 'label' => array('for' => 'ContactPublished2Month'),
  4837. 'Published2',
  4838. '/label',
  4839. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4840. $monthsRegex,
  4841. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4842. date('F', $now),
  4843. '/option',
  4844. '*/select',
  4845. '-',
  4846. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4847. $daysRegex,
  4848. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4849. date('j', $now),
  4850. '/option',
  4851. '*/select',
  4852. '-',
  4853. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4854. $yearsRegex,
  4855. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4856. date('Y', $now),
  4857. '/option',
  4858. '*/select',
  4859. '/div'
  4860. );
  4861. $this->assertTags($result, $expected);
  4862. $this->Form->create('Contact');
  4863. $result = $this->Form->input('published', array('monthNames' => false));
  4864. $now = strtotime('now');
  4865. $expected = array(
  4866. 'div' => array('class' => 'input date'),
  4867. 'label' => array('for' => 'ContactPublishedMonth'),
  4868. 'Published',
  4869. '/label',
  4870. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4871. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4872. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4873. date('m', $now),
  4874. '/option',
  4875. '*/select',
  4876. '-',
  4877. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4878. $daysRegex,
  4879. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4880. date('j', $now),
  4881. '/option',
  4882. '*/select',
  4883. '-',
  4884. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4885. $yearsRegex,
  4886. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4887. date('Y', $now),
  4888. '/option',
  4889. '*/select',
  4890. '/div'
  4891. );
  4892. $this->assertTags($result, $expected);
  4893. $result = $this->Form->input('published', array('type' => 'time'));
  4894. $now = strtotime('now');
  4895. $expected = array(
  4896. 'div' => array('class' => 'input time'),
  4897. 'label' => array('for' => 'ContactPublishedHour'),
  4898. 'Published',
  4899. '/label',
  4900. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4901. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4902. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4903. date('g', $now),
  4904. '/option',
  4905. '*/select',
  4906. ':',
  4907. );
  4908. $this->assertTags($result, $expected);
  4909. $result = $this->Form->input('published', array(
  4910. 'timeFormat' => 24,
  4911. 'interval' => 5,
  4912. 'selected' => strtotime('2009-09-03 13:37:00'),
  4913. 'type' => 'datetime'
  4914. ));
  4915. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4916. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4917. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4918. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4919. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4920. $this->assertNoErrors();
  4921. $this->Form->request->data['Contact'] = array(
  4922. 'date' => array(
  4923. 'day' => '',
  4924. 'month' => '',
  4925. 'year' => '',
  4926. 'hour' => '',
  4927. 'min' => '',
  4928. 'meridian' => ''
  4929. )
  4930. );
  4931. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4932. }
  4933. /**
  4934. * test that datetime() and default values work.
  4935. *
  4936. * @return void
  4937. */
  4938. public function testDatetimeWithDefault() {
  4939. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4940. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4941. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4942. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4943. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4944. 'default' => '2009-06-01 11:15:30'
  4945. ));
  4946. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4947. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4948. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4949. }
  4950. /**
  4951. * test that bogus non-date time data doesn't cause errors.
  4952. *
  4953. * @return void
  4954. */
  4955. public function testDateTimeWithBogusData() {
  4956. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4957. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4958. }
  4959. /**
  4960. * testDateTimeEmptyAsArray
  4961. *
  4962. * @return void
  4963. */
  4964. public function testDateTimeEmptyAsArray() {
  4965. $result = $this->Form->dateTime('Contact.date',
  4966. 'DMY',
  4967. '12',
  4968. array(
  4969. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4970. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4971. )
  4972. )
  4973. );
  4974. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4975. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4976. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4977. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4978. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4979. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4980. $result = $this->Form->dateTime('Contact.date',
  4981. 'DMY',
  4982. '12',
  4983. array(
  4984. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4985. )
  4986. );
  4987. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4988. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4989. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4990. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4991. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4992. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4993. }
  4994. /**
  4995. * testFormDateTimeMulti method
  4996. *
  4997. * test multiple datetime element generation
  4998. *
  4999. * @return void
  5000. */
  5001. public function testFormDateTimeMulti() {
  5002. extract($this->dateRegex);
  5003. $result = $this->Form->dateTime('Contact.1.updated');
  5004. $expected = array(
  5005. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5006. $daysRegex,
  5007. array('option' => array('value' => '')),
  5008. '/option',
  5009. '*/select',
  5010. '-',
  5011. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5012. $monthsRegex,
  5013. array('option' => array('value' => '')),
  5014. '/option',
  5015. '*/select',
  5016. '-',
  5017. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5018. $yearsRegex,
  5019. array('option' => array('value' => '')),
  5020. '/option',
  5021. '*/select',
  5022. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5023. $hoursRegex,
  5024. array('option' => array('value' => '')),
  5025. '/option',
  5026. '*/select',
  5027. ':',
  5028. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5029. $minutesRegex,
  5030. array('option' => array('value' => '')),
  5031. '/option',
  5032. '*/select',
  5033. ' ',
  5034. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5035. $meridianRegex,
  5036. array('option' => array('value' => '')),
  5037. '/option',
  5038. '*/select'
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $result = $this->Form->dateTime('Contact.2.updated');
  5042. $expected = array(
  5043. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5044. $daysRegex,
  5045. array('option' => array('value' => '')),
  5046. '/option',
  5047. '*/select',
  5048. '-',
  5049. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5050. $monthsRegex,
  5051. array('option' => array('value' => '')),
  5052. '/option',
  5053. '*/select',
  5054. '-',
  5055. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5056. $yearsRegex,
  5057. array('option' => array('value' => '')),
  5058. '/option',
  5059. '*/select',
  5060. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5061. $hoursRegex,
  5062. array('option' => array('value' => '')),
  5063. '/option',
  5064. '*/select',
  5065. ':',
  5066. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5067. $minutesRegex,
  5068. array('option' => array('value' => '')),
  5069. '/option',
  5070. '*/select',
  5071. ' ',
  5072. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5073. $meridianRegex,
  5074. array('option' => array('value' => '')),
  5075. '/option',
  5076. '*/select'
  5077. );
  5078. $this->assertTags($result, $expected);
  5079. }
  5080. /**
  5081. * When changing the date format, the label should always focus the first select box when
  5082. * clicked.
  5083. *
  5084. * @return void
  5085. */
  5086. public function testDateTimeLabelIdMatchesFirstInput() {
  5087. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5088. $this->assertContains('label for="ModelDateMonth"', $result);
  5089. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5090. $this->assertContains('label for="ModelDateDay"', $result);
  5091. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5092. $this->assertContains('label for="ModelDateYear"', $result);
  5093. }
  5094. /**
  5095. * testMonth method
  5096. *
  5097. * @return void
  5098. */
  5099. public function testMonth() {
  5100. $result = $this->Form->month('Model.field');
  5101. $expected = array(
  5102. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5103. array('option' => array('value' => '')),
  5104. '/option',
  5105. array('option' => array('value' => '01')),
  5106. date('F', strtotime('2008-01-01 00:00:00')),
  5107. '/option',
  5108. array('option' => array('value' => '02')),
  5109. date('F', strtotime('2008-02-01 00:00:00')),
  5110. '/option',
  5111. '*/select',
  5112. );
  5113. $this->assertTags($result, $expected);
  5114. $result = $this->Form->month('Model.field', array('empty' => true));
  5115. $expected = array(
  5116. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5117. array('option' => array('value' => '')),
  5118. '/option',
  5119. array('option' => array('value' => '01')),
  5120. date('F', strtotime('2008-01-01 00:00:00')),
  5121. '/option',
  5122. array('option' => array('value' => '02')),
  5123. date('F', strtotime('2008-02-01 00:00:00')),
  5124. '/option',
  5125. '*/select',
  5126. );
  5127. $this->assertTags($result, $expected);
  5128. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5129. $expected = array(
  5130. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5131. array('option' => array('value' => '')),
  5132. '/option',
  5133. array('option' => array('value' => '01')),
  5134. '01',
  5135. '/option',
  5136. array('option' => array('value' => '02')),
  5137. '02',
  5138. '/option',
  5139. '*/select',
  5140. );
  5141. $this->assertTags($result, $expected);
  5142. $monthNames = array(
  5143. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5144. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5145. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5146. $expected = array(
  5147. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5148. array('option' => array('value' => '')),
  5149. '/option',
  5150. array('option' => array('value' => '01')),
  5151. 'Jan',
  5152. '/option',
  5153. array('option' => array('value' => '02')),
  5154. 'Feb',
  5155. '/option',
  5156. '*/select',
  5157. );
  5158. $this->assertTags($result, $expected);
  5159. }
  5160. /**
  5161. * testDay method
  5162. *
  5163. * @return void
  5164. */
  5165. public function testDay() {
  5166. extract($this->dateRegex);
  5167. $result = $this->Form->day('Model.field', array('value' => false));
  5168. $expected = array(
  5169. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5170. array('option' => array('value' => '')),
  5171. '/option',
  5172. array('option' => array('value' => '01')),
  5173. '1',
  5174. '/option',
  5175. array('option' => array('value' => '02')),
  5176. '2',
  5177. '/option',
  5178. $daysRegex,
  5179. '/select',
  5180. );
  5181. $this->assertTags($result, $expected);
  5182. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5183. $result = $this->Form->day('Model.field');
  5184. $expected = array(
  5185. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5186. array('option' => array('value' => '')),
  5187. '/option',
  5188. array('option' => array('value' => '01')),
  5189. '1',
  5190. '/option',
  5191. array('option' => array('value' => '02')),
  5192. '2',
  5193. '/option',
  5194. $daysRegex,
  5195. array('option' => array('value' => '10', 'selected' => 'selected')),
  5196. '10',
  5197. '/option',
  5198. $daysRegex,
  5199. '/select',
  5200. );
  5201. $this->assertTags($result, $expected);
  5202. $this->Form->request->data['Model']['field'] = '';
  5203. $result = $this->Form->day('Model.field', array('value' => '10'));
  5204. $expected = array(
  5205. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5206. array('option' => array('value' => '')),
  5207. '/option',
  5208. array('option' => array('value' => '01')),
  5209. '1',
  5210. '/option',
  5211. array('option' => array('value' => '02')),
  5212. '2',
  5213. '/option',
  5214. $daysRegex,
  5215. array('option' => array('value' => '10', 'selected' => 'selected')),
  5216. '10',
  5217. '/option',
  5218. $daysRegex,
  5219. '/select',
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5223. $result = $this->Form->day('Model.field', array('value' => true));
  5224. $expected = array(
  5225. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5226. array('option' => array('value' => '')),
  5227. '/option',
  5228. array('option' => array('value' => '01')),
  5229. '1',
  5230. '/option',
  5231. array('option' => array('value' => '02')),
  5232. '2',
  5233. '/option',
  5234. $daysRegex,
  5235. array('option' => array('value' => '10', 'selected' => 'selected')),
  5236. '10',
  5237. '/option',
  5238. $daysRegex,
  5239. '/select',
  5240. );
  5241. $this->assertTags($result, $expected);
  5242. }
  5243. /**
  5244. * testMinute method
  5245. *
  5246. * @return void
  5247. */
  5248. public function testMinute() {
  5249. extract($this->dateRegex);
  5250. $result = $this->Form->minute('Model.field');
  5251. $expected = array(
  5252. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5253. array('option' => array('value' => '')),
  5254. '/option',
  5255. array('option' => array('value' => '00')),
  5256. '00',
  5257. '/option',
  5258. array('option' => array('value' => '01')),
  5259. '01',
  5260. '/option',
  5261. array('option' => array('value' => '02')),
  5262. '02',
  5263. '/option',
  5264. $minutesRegex,
  5265. '/select',
  5266. );
  5267. $this->assertTags($result, $expected);
  5268. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5269. $result = $this->Form->minute('Model.field');
  5270. $expected = array(
  5271. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5272. array('option' => array('value' => '')),
  5273. '/option',
  5274. array('option' => array('value' => '00')),
  5275. '00',
  5276. '/option',
  5277. array('option' => array('value' => '01')),
  5278. '01',
  5279. '/option',
  5280. array('option' => array('value' => '02')),
  5281. '02',
  5282. '/option',
  5283. $minutesRegex,
  5284. array('option' => array('value' => '12', 'selected' => 'selected')),
  5285. '12',
  5286. '/option',
  5287. $minutesRegex,
  5288. '/select',
  5289. );
  5290. $this->assertTags($result, $expected);
  5291. $this->Form->request->data['Model']['field'] = '';
  5292. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5293. $expected = array(
  5294. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5295. array('option' => array('value' => '')),
  5296. '/option',
  5297. array('option' => array('value' => '00')),
  5298. '00',
  5299. '/option',
  5300. array('option' => array('value' => '05')),
  5301. '05',
  5302. '/option',
  5303. array('option' => array('value' => '10')),
  5304. '10',
  5305. '/option',
  5306. $minutesRegex,
  5307. '/select',
  5308. );
  5309. $this->assertTags($result, $expected);
  5310. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5311. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5312. $expected = array(
  5313. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5314. array('option' => array('value' => '')),
  5315. '/option',
  5316. array('option' => array('value' => '00')),
  5317. '00',
  5318. '/option',
  5319. array('option' => array('value' => '05')),
  5320. '05',
  5321. '/option',
  5322. array('option' => array('value' => '10', 'selected' => 'selected')),
  5323. '10',
  5324. '/option',
  5325. $minutesRegex,
  5326. '/select',
  5327. );
  5328. $this->assertTags($result, $expected);
  5329. }
  5330. /**
  5331. * testHour method
  5332. *
  5333. * @return void
  5334. */
  5335. public function testHour() {
  5336. extract($this->dateRegex);
  5337. $result = $this->Form->hour('Model.field', false);
  5338. $expected = array(
  5339. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5340. array('option' => array('value' => '')),
  5341. '/option',
  5342. array('option' => array('value' => '01')),
  5343. '1',
  5344. '/option',
  5345. array('option' => array('value' => '02')),
  5346. '2',
  5347. '/option',
  5348. $hoursRegex,
  5349. '/select',
  5350. );
  5351. $this->assertTags($result, $expected);
  5352. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5353. $result = $this->Form->hour('Model.field', false);
  5354. $expected = array(
  5355. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5356. array('option' => array('value' => '')),
  5357. '/option',
  5358. array('option' => array('value' => '01')),
  5359. '1',
  5360. '/option',
  5361. array('option' => array('value' => '02')),
  5362. '2',
  5363. '/option',
  5364. $hoursRegex,
  5365. array('option' => array('value' => '12', 'selected' => 'selected')),
  5366. '12',
  5367. '/option',
  5368. '/select',
  5369. );
  5370. $this->assertTags($result, $expected);
  5371. $this->Form->request->data['Model']['field'] = '';
  5372. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5373. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5374. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5375. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5376. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5377. $result = $this->Form->hour('Model.field', true);
  5378. $expected = array(
  5379. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5380. array('option' => array('value' => '')),
  5381. '/option',
  5382. array('option' => array('value' => '00', 'selected' => 'selected')),
  5383. '0',
  5384. '/option',
  5385. array('option' => array('value' => '01')),
  5386. '1',
  5387. '/option',
  5388. array('option' => array('value' => '02')),
  5389. '2',
  5390. '/option',
  5391. $hoursRegex,
  5392. '/select',
  5393. );
  5394. $this->assertTags($result, $expected);
  5395. unset($this->Form->request->data['Model']['field']);
  5396. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5397. $thisHour = date('H');
  5398. $optValue = date('G');
  5399. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5400. }
  5401. /**
  5402. * testYear method
  5403. *
  5404. * @return void
  5405. */
  5406. public function testYear() {
  5407. $result = $this->Form->year('Model.field', 2006, 2007);
  5408. $expected = array(
  5409. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5410. array('option' => array('value' => '')),
  5411. '/option',
  5412. array('option' => array('value' => '2007')),
  5413. '2007',
  5414. '/option',
  5415. array('option' => array('value' => '2006')),
  5416. '2006',
  5417. '/option',
  5418. '/select',
  5419. );
  5420. $this->assertTags($result, $expected);
  5421. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5422. $expected = array(
  5423. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5424. array('option' => array('value' => '')),
  5425. '/option',
  5426. array('option' => array('value' => '2006')),
  5427. '2006',
  5428. '/option',
  5429. array('option' => array('value' => '2007')),
  5430. '2007',
  5431. '/option',
  5432. '/select',
  5433. );
  5434. $this->assertTags($result, $expected);
  5435. $this->request->data['Contact']['published'] = '';
  5436. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5437. $expected = array(
  5438. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5439. array('option' => array('value' => '')),
  5440. '/option',
  5441. array('option' => array('value' => '2007')),
  5442. '2007',
  5443. '/option',
  5444. array('option' => array('value' => '2006')),
  5445. '2006',
  5446. '/option',
  5447. '/select',
  5448. );
  5449. $this->assertTags($result, $expected);
  5450. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5451. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5452. $expected = array(
  5453. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5454. array('option' => array('value' => '2007')),
  5455. '2007',
  5456. '/option',
  5457. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5458. '2006',
  5459. '/option',
  5460. '/select',
  5461. );
  5462. $this->assertTags($result, $expected);
  5463. $this->Form->request->data['Contact']['published'] = '';
  5464. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5465. $expected = array(
  5466. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5467. array('option' => array('value' => '')),
  5468. '/option',
  5469. array('option' => array('value' => '2007')),
  5470. '2007',
  5471. '/option',
  5472. array('option' => array('value' => '2006')),
  5473. '2006',
  5474. '/option',
  5475. '/select',
  5476. );
  5477. $this->assertTags($result, $expected);
  5478. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5479. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5480. $expected = array(
  5481. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5482. array('option' => array('value' => '2007')),
  5483. '2007',
  5484. '/option',
  5485. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5486. '2006',
  5487. '/option',
  5488. '/select',
  5489. );
  5490. $this->assertTags($result, $expected);
  5491. $this->Form->request->data['Contact']['published'] = '';
  5492. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5493. $expected = array(
  5494. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5495. array('option' => array('value' => '')),
  5496. '/option',
  5497. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5498. '2007',
  5499. '/option',
  5500. array('option' => array('value' => '2006')),
  5501. '2006',
  5502. '/option',
  5503. '/select',
  5504. );
  5505. $this->assertTags($result, $expected);
  5506. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5507. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5508. $expected = array(
  5509. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5510. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5511. '2007',
  5512. '/option',
  5513. array('option' => array('value' => '2006')),
  5514. '2006',
  5515. '/option',
  5516. '/select',
  5517. );
  5518. $this->assertTags($result, $expected);
  5519. $this->Form->request->data['Contact']['published'] = '';
  5520. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5521. $expected = array(
  5522. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5523. array('option' => array('value' => '2008')),
  5524. '2008',
  5525. '/option',
  5526. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5527. '2007',
  5528. '/option',
  5529. array('option' => array('value' => '2006')),
  5530. '2006',
  5531. '/option',
  5532. '/select',
  5533. );
  5534. $this->assertTags($result, $expected);
  5535. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5536. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5537. $expected = array(
  5538. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5539. array('option' => array('value' => '2008')),
  5540. '2008',
  5541. '/option',
  5542. array('option' => array('value' => '2007')),
  5543. '2007',
  5544. '/option',
  5545. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5546. '2006',
  5547. '/option',
  5548. '/select',
  5549. );
  5550. $this->assertTags($result, $expected);
  5551. $this->Form->request->data = array();
  5552. $this->Form->create('Contact');
  5553. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5554. $expected = array(
  5555. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5556. array('option' => array('value' => '2008')),
  5557. '2008',
  5558. '/option',
  5559. array('option' => array('value' => '2007')),
  5560. '2007',
  5561. '/option',
  5562. array('option' => array('value' => '2006')),
  5563. '2006',
  5564. '/option',
  5565. '/select',
  5566. );
  5567. $this->assertTags($result, $expected);
  5568. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5569. $this->assertContains('data[Contact][published][year]', $result);
  5570. }
  5571. /**
  5572. * testTextArea method
  5573. *
  5574. * @return void
  5575. */
  5576. public function testTextArea() {
  5577. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5578. $result = $this->Form->textarea('Model.field');
  5579. $expected = array(
  5580. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5581. 'some test data',
  5582. '/textarea',
  5583. );
  5584. $this->assertTags($result, $expected);
  5585. $result = $this->Form->textarea('Model.tmp');
  5586. $expected = array(
  5587. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5588. '/textarea',
  5589. );
  5590. $this->assertTags($result, $expected);
  5591. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5592. $result = $this->Form->textarea('Model.field');
  5593. $expected = array(
  5594. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5595. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5596. '/textarea',
  5597. );
  5598. $this->assertTags($result, $expected);
  5599. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5600. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5601. $expected = array(
  5602. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5603. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5604. '/textarea',
  5605. );
  5606. $this->assertTags($result, $expected);
  5607. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5608. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5609. $expected = array(
  5610. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5611. '/textarea'
  5612. );
  5613. $this->assertTags($result, $expected);
  5614. }
  5615. /**
  5616. * testTextAreaWithStupidCharacters method
  5617. *
  5618. * test text area with non-ascii characters
  5619. *
  5620. * @return void
  5621. */
  5622. public function testTextAreaWithStupidCharacters() {
  5623. $this->loadFixtures('Post');
  5624. $result = $this->Form->input('Post.content', array(
  5625. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5626. ));
  5627. $expected = array(
  5628. 'div' => array('class' => 'input textarea'),
  5629. 'label' => array('for' => 'PostContent'),
  5630. 'Current Text',
  5631. '/label',
  5632. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5633. 'GREAT®',
  5634. '/textarea',
  5635. '/div'
  5636. );
  5637. $this->assertTags($result, $expected);
  5638. }
  5639. /**
  5640. * testHiddenField method
  5641. *
  5642. * @return void
  5643. */
  5644. public function testHiddenField() {
  5645. $Contact = ClassRegistry::getObject('Contact');
  5646. $Contact->validationErrors['field'] = 1;
  5647. $this->Form->request->data['Contact']['field'] = 'test';
  5648. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5649. $this->assertTags($result, array(
  5650. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5651. );
  5652. }
  5653. /**
  5654. * testFileUploadField method
  5655. *
  5656. * @return void
  5657. */
  5658. public function testFileUploadField() {
  5659. $result = $this->Form->file('Model.upload');
  5660. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5661. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5662. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5663. $expected = array(
  5664. 'div' => array('class' => 'input file'),
  5665. 'label' => array('for' => 'ModelUpload'),
  5666. 'Upload',
  5667. '/label',
  5668. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5669. '/div'
  5670. );
  5671. $this->assertTags($result, $expected);
  5672. }
  5673. /**
  5674. * test File upload input on a model not used in create();
  5675. *
  5676. * @return void
  5677. */
  5678. public function testFileUploadOnOtherModel() {
  5679. $this->Form->create('ValidateUser', array('type' => 'file'));
  5680. $result = $this->Form->file('ValidateProfile.city');
  5681. $expected = array(
  5682. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5683. );
  5684. $this->assertTags($result, $expected);
  5685. }
  5686. /**
  5687. * testButton method
  5688. *
  5689. * @return void
  5690. */
  5691. public function testButton() {
  5692. $result = $this->Form->button('Hi');
  5693. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5694. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5695. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5696. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5697. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5698. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5699. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5700. $result = $this->Form->button('No type', array('type' => false));
  5701. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5702. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5703. $this->assertNotRegExp('/\&039/', $result);
  5704. }
  5705. /**
  5706. * Test that button() makes unlocked fields by default.
  5707. *
  5708. * @return void
  5709. */
  5710. public function testButtonUnlockedByDefault() {
  5711. $this->Form->request->params['_Token']['key'] = 'secured';
  5712. $this->Form->button('Save', array('name' => 'save'));
  5713. $this->Form->button('Clear');
  5714. $result = $this->Form->unlockField();
  5715. $this->assertEquals(array('save'), $result);
  5716. }
  5717. /**
  5718. * testPostButton method
  5719. *
  5720. * @return void
  5721. */
  5722. public function testPostButton() {
  5723. $result = $this->Form->postButton('Hi', '/controller/action');
  5724. $this->assertTags($result, array(
  5725. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5726. 'div' => array('style' => 'display:none;'),
  5727. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5728. '/div',
  5729. 'button' => array('type' => 'submit'),
  5730. 'Hi',
  5731. '/button',
  5732. '/form'
  5733. ));
  5734. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5735. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5736. }
  5737. /**
  5738. * Test that postButton adds _Token fields.
  5739. *
  5740. * @return void
  5741. */
  5742. public function testSecurePostButton() {
  5743. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5744. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5745. $expected = array(
  5746. 'form' => array(
  5747. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5748. ),
  5749. array('div' => array('style' => 'display:none;')),
  5750. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5751. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5752. '/div',
  5753. 'button' => array('type' => 'submit'),
  5754. 'Delete',
  5755. '/button',
  5756. array('div' => array('style' => 'display:none;')),
  5757. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5758. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5759. '/div',
  5760. '/form',
  5761. );
  5762. $this->assertTags($result, $expected);
  5763. }
  5764. /**
  5765. * testPostLink method
  5766. *
  5767. * @return void
  5768. */
  5769. public function testPostLink() {
  5770. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5771. $this->assertTags($result, array(
  5772. 'form' => array(
  5773. 'method' => 'post', 'action' => '/posts/delete/1',
  5774. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5775. ),
  5776. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5777. '/form',
  5778. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5779. 'Delete',
  5780. '/a'
  5781. ));
  5782. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5783. $this->assertTags($result, array(
  5784. 'form' => array(
  5785. 'method' => 'post', 'action' => '/posts/delete/1',
  5786. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5787. ),
  5788. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5789. '/form',
  5790. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5791. 'Delete',
  5792. '/a'
  5793. ));
  5794. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5795. $this->assertTags($result, array(
  5796. 'form' => array(
  5797. 'method' => 'post', 'action' => '/posts/delete/1',
  5798. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5799. ),
  5800. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5801. '/form',
  5802. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5803. 'Delete',
  5804. '/a'
  5805. ));
  5806. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5807. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5808. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5809. $this->assertTags($result, array(
  5810. 'form' => array(
  5811. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5812. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5813. ),
  5814. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5815. '/form',
  5816. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5817. 'Delete',
  5818. '/a'
  5819. ));
  5820. }
  5821. /**
  5822. * Test that postLink adds _Token fields.
  5823. *
  5824. * @return void
  5825. */
  5826. public function testSecurePostLink() {
  5827. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5828. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5829. $expected = array(
  5830. 'form' => array(
  5831. 'method' => 'post', 'action' => '/posts/delete/1',
  5832. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5833. ),
  5834. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5835. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5836. 'div' => array('style' => 'display:none;'),
  5837. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5838. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5839. '/div',
  5840. '/form',
  5841. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5842. 'Delete',
  5843. '/a'
  5844. );
  5845. $this->assertTags($result, $expected);
  5846. }
  5847. /**
  5848. * testSubmitButton method
  5849. *
  5850. * @return void
  5851. */
  5852. public function testSubmitButton() {
  5853. $result = $this->Form->submit('');
  5854. $expected = array(
  5855. 'div' => array('class' => 'submit'),
  5856. 'input' => array('type' => 'submit', 'value' => ''),
  5857. '/div'
  5858. );
  5859. $this->assertTags($result, $expected);
  5860. $result = $this->Form->submit('Test Submit');
  5861. $expected = array(
  5862. 'div' => array('class' => 'submit'),
  5863. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5864. '/div'
  5865. );
  5866. $this->assertTags($result, $expected);
  5867. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5868. $expected = array(
  5869. 'span' => array('class' => 'submit'),
  5870. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5871. '/span'
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5875. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5876. $this->assertTags($result, $expected);
  5877. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5878. $expected = array(
  5879. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5880. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5881. '/div'
  5882. );
  5883. $this->assertTags($result, $expected);
  5884. $result = $this->Form->submit('Next >');
  5885. $expected = array(
  5886. 'div' => array('class' => 'submit'),
  5887. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5888. '/div'
  5889. );
  5890. $this->assertTags($result, $expected);
  5891. $result = $this->Form->submit('Next >', array('escape' => false));
  5892. $expected = array(
  5893. 'div' => array('class' => 'submit'),
  5894. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5895. '/div'
  5896. );
  5897. $this->assertTags($result, $expected);
  5898. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5899. $expected = array(
  5900. 'div' => array('class' => 'submit'),
  5901. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5902. '/div'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $before = '--before--';
  5906. $after = '--after--';
  5907. $result = $this->Form->submit('Test', array('before' => $before));
  5908. $expected = array(
  5909. 'div' => array('class' => 'submit'),
  5910. '--before--',
  5911. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5912. '/div'
  5913. );
  5914. $this->assertTags($result, $expected);
  5915. $result = $this->Form->submit('Test', array('after' => $after));
  5916. $expected = array(
  5917. 'div' => array('class' => 'submit'),
  5918. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5919. '--after--',
  5920. '/div'
  5921. );
  5922. $this->assertTags($result, $expected);
  5923. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5924. $expected = array(
  5925. 'div' => array('class' => 'submit'),
  5926. '--before--',
  5927. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5928. '--after--',
  5929. '/div'
  5930. );
  5931. $this->assertTags($result, $expected);
  5932. }
  5933. /**
  5934. * test image submit types.
  5935. *
  5936. * @return void
  5937. */
  5938. public function testSubmitImage() {
  5939. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5940. $expected = array(
  5941. 'div' => array('class' => 'submit'),
  5942. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5943. '/div'
  5944. );
  5945. $this->assertTags($result, $expected);
  5946. $result = $this->Form->submit('/relative/cake.power.gif');
  5947. $expected = array(
  5948. 'div' => array('class' => 'submit'),
  5949. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5950. '/div'
  5951. );
  5952. $this->assertTags($result, $expected);
  5953. $result = $this->Form->submit('cake.power.gif');
  5954. $expected = array(
  5955. 'div' => array('class' => 'submit'),
  5956. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5957. '/div'
  5958. );
  5959. $this->assertTags($result, $expected);
  5960. $result = $this->Form->submit('Not.an.image');
  5961. $expected = array(
  5962. 'div' => array('class' => 'submit'),
  5963. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5964. '/div'
  5965. );
  5966. $this->assertTags($result, $expected);
  5967. $after = '--after--';
  5968. $before = '--before--';
  5969. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5970. $expected = array(
  5971. 'div' => array('class' => 'submit'),
  5972. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5973. '--after--',
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5978. $expected = array(
  5979. 'div' => array('class' => 'submit'),
  5980. '--before--',
  5981. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5982. '/div'
  5983. );
  5984. $this->assertTags($result, $expected);
  5985. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5986. $expected = array(
  5987. 'div' => array('class' => 'submit'),
  5988. '--before--',
  5989. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5990. '--after--',
  5991. '/div'
  5992. );
  5993. $this->assertTags($result, $expected);
  5994. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5995. $expected = array(
  5996. 'div' => array('class' => 'submit'),
  5997. '--before--',
  5998. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5999. '--after--',
  6000. '/div'
  6001. );
  6002. $this->assertTags($result, $expected);
  6003. }
  6004. /**
  6005. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6006. * be submitted at a time.
  6007. *
  6008. * @return void
  6009. */
  6010. public function testSubmitUnlockedByDefault() {
  6011. $this->Form->request->params['_Token']['key'] = 'secured';
  6012. $this->Form->submit('Go go');
  6013. $this->Form->submit('Save', array('name' => 'save'));
  6014. $result = $this->Form->unlockField();
  6015. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6016. }
  6017. /**
  6018. * Test submit image with timestamps.
  6019. *
  6020. * @return void
  6021. */
  6022. public function testSubmitImageTimestamp() {
  6023. Configure::write('Asset.timestamp', 'force');
  6024. $result = $this->Form->submit('cake.power.gif');
  6025. $expected = array(
  6026. 'div' => array('class' => 'submit'),
  6027. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6028. '/div'
  6029. );
  6030. $this->assertTags($result, $expected);
  6031. }
  6032. /**
  6033. * test the create() method
  6034. *
  6035. * @return void
  6036. */
  6037. public function testCreate() {
  6038. $result = $this->Form->create('Contact');
  6039. $encoding = strtolower(Configure::read('App.encoding'));
  6040. $expected = array(
  6041. 'form' => array(
  6042. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6043. 'accept-charset' => $encoding
  6044. ),
  6045. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6046. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6047. '/div'
  6048. );
  6049. $this->assertTags($result, $expected);
  6050. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6051. $expected = array('form' => array(
  6052. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6053. 'accept-charset' => $encoding
  6054. ));
  6055. $this->assertTags($result, $expected);
  6056. $result = $this->Form->create('Contact', array('type' => 'get'));
  6057. $expected = array('form' => array(
  6058. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6059. 'accept-charset' => $encoding
  6060. ));
  6061. $this->assertTags($result, $expected);
  6062. $result = $this->Form->create('Contact', array('type' => 'put'));
  6063. $expected = array(
  6064. 'form' => array(
  6065. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6066. 'accept-charset' => $encoding
  6067. ),
  6068. 'div' => array('style' => 'display:none;'),
  6069. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6070. '/div'
  6071. );
  6072. $this->assertTags($result, $expected);
  6073. $result = $this->Form->create('Contact', array('type' => 'file'));
  6074. $expected = array(
  6075. 'form' => array(
  6076. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6077. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6078. ),
  6079. 'div' => array('style' => 'display:none;'),
  6080. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6081. '/div'
  6082. );
  6083. $this->assertTags($result, $expected);
  6084. $this->Form->request->data['Contact']['id'] = 1;
  6085. $this->Form->request->here = '/contacts/edit/1';
  6086. $this->Form->request['action'] = 'edit';
  6087. $result = $this->Form->create('Contact');
  6088. $expected = array(
  6089. 'form' => array(
  6090. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6091. 'accept-charset' => $encoding
  6092. ),
  6093. 'div' => array('style' => 'display:none;'),
  6094. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6095. '/div'
  6096. );
  6097. $this->assertTags($result, $expected);
  6098. $this->Form->request->data['Contact']['id'] = 1;
  6099. $this->Form->request->here = '/contacts/edit/1';
  6100. $this->Form->request['action'] = 'edit';
  6101. $result = $this->Form->create('Contact', array('type' => 'file'));
  6102. $expected = array(
  6103. 'form' => array(
  6104. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6105. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6106. ),
  6107. 'div' => array('style' => 'display:none;'),
  6108. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6109. '/div'
  6110. );
  6111. $this->assertTags($result, $expected);
  6112. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6113. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6114. $expected = array(
  6115. 'form' => array(
  6116. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6117. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6118. ),
  6119. 'div' => array('style' => 'display:none;'),
  6120. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6121. '/div'
  6122. );
  6123. $this->assertTags($result, $expected);
  6124. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6125. $expected = array(
  6126. 'form' => array(
  6127. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6128. 'accept-charset' => $encoding
  6129. ),
  6130. 'div' => array('style' => 'display:none;'),
  6131. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6132. '/div'
  6133. );
  6134. $this->assertTags($result, $expected);
  6135. $this->Form->request['action'] = 'add';
  6136. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6137. $expected = array(
  6138. 'form' => array(
  6139. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6140. 'accept-charset' => $encoding
  6141. ),
  6142. 'div' => array('style' => 'display:none;'),
  6143. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6144. '/div'
  6145. );
  6146. $this->assertTags($result, $expected);
  6147. $result = $this->Form->create('User', array('action' => 'login'));
  6148. $expected = array(
  6149. 'form' => array(
  6150. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6151. 'accept-charset' => $encoding
  6152. ),
  6153. 'div' => array('style' => 'display:none;'),
  6154. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6155. '/div'
  6156. );
  6157. $this->assertTags($result, $expected);
  6158. $result = $this->Form->create('User', array('url' => '/users/login'));
  6159. $expected = array(
  6160. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6161. 'div' => array('style' => 'display:none;'),
  6162. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6163. '/div'
  6164. );
  6165. $this->assertTags($result, $expected);
  6166. $this->Form->request['controller'] = 'pages';
  6167. $result = $this->Form->create('User', array('action' => 'signup'));
  6168. $expected = array(
  6169. 'form' => array(
  6170. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6171. 'accept-charset' => $encoding
  6172. ),
  6173. 'div' => array('style' => 'display:none;'),
  6174. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6175. '/div'
  6176. );
  6177. $this->assertTags($result, $expected);
  6178. $this->Form->request->data = array();
  6179. $this->Form->request['controller'] = 'contacts';
  6180. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6181. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6182. $expected = array(
  6183. 'form' => array(
  6184. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6185. 'accept-charset' => 'utf-8'
  6186. ),
  6187. 'div' => array('style' => 'display:none;'),
  6188. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6189. '/div'
  6190. );
  6191. $this->assertTags($result, $expected);
  6192. }
  6193. /**
  6194. * Test the onsubmit option for create()
  6195. *
  6196. * @return void
  6197. */
  6198. public function testCreateOnSubmit() {
  6199. $this->Form->request->data = array();
  6200. $this->Form->request['controller'] = 'contacts';
  6201. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6202. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6203. $expected = array(
  6204. 'form' => array(
  6205. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6206. 'accept-charset' => 'utf-8'
  6207. ),
  6208. 'div' => array('style' => 'display:none;'),
  6209. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6210. '/div'
  6211. );
  6212. $this->assertTags($result, $expected);
  6213. $this->Form->request->data = array();
  6214. $this->Form->request['controller'] = 'contacts';
  6215. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6216. $result = $this->Form->create(array(
  6217. 'url' => array('action' => 'index', 'param'),
  6218. 'default' => false,
  6219. 'onsubmit' => 'someFunction();'
  6220. ));
  6221. $expected = array(
  6222. 'form' => array(
  6223. 'id' => 'ContactAddForm', 'method' => 'post',
  6224. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6225. 'action' => '/contacts/index/param',
  6226. 'accept-charset' => 'utf-8'
  6227. ),
  6228. 'div' => array('style' => 'display:none;'),
  6229. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6230. '/div'
  6231. );
  6232. $this->assertTags($result, $expected);
  6233. }
  6234. /**
  6235. * test create() with automatic url generation
  6236. *
  6237. * @return void
  6238. */
  6239. public function testCreateAutoUrl() {
  6240. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6241. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6242. $this->Form->request->base = '/base_url';
  6243. $result = $this->Form->create('Contact');
  6244. $expected = array(
  6245. 'form' => array(
  6246. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6247. 'accept-charset' => 'utf-8'
  6248. ),
  6249. 'div' => array('style' => 'display:none;'),
  6250. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6251. '/div'
  6252. );
  6253. $this->assertTags($result, $expected);
  6254. $this->Form->request['action'] = 'delete';
  6255. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6256. $this->Form->request->base = '/base_url';
  6257. $result = $this->Form->create('Contact');
  6258. $expected = array(
  6259. 'form' => array(
  6260. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6261. 'accept-charset' => 'utf-8'
  6262. ),
  6263. 'div' => array('style' => 'display:none;'),
  6264. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6265. '/div'
  6266. );
  6267. $this->assertTags($result, $expected);
  6268. }
  6269. /**
  6270. * test create() with a custom route
  6271. *
  6272. * @return void
  6273. */
  6274. public function testCreateCustomRoute() {
  6275. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6276. $encoding = strtolower(Configure::read('App.encoding'));
  6277. $result = $this->Form->create('User', array('action' => 'login'));
  6278. $expected = array(
  6279. 'form' => array(
  6280. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6281. 'accept-charset' => $encoding
  6282. ),
  6283. 'div' => array('style' => 'display:none;'),
  6284. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6285. '/div'
  6286. );
  6287. $this->assertTags($result, $expected);
  6288. }
  6289. /**
  6290. * test that inputDefaults are stored and used.
  6291. *
  6292. * @return void
  6293. */
  6294. public function testCreateWithInputDefaults() {
  6295. $this->Form->create('User', array(
  6296. 'inputDefaults' => array(
  6297. 'div' => false,
  6298. 'label' => false,
  6299. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6300. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6301. )
  6302. ));
  6303. $result = $this->Form->input('username');
  6304. $expected = array(
  6305. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6306. );
  6307. $this->assertTags($result, $expected);
  6308. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6309. $expected = array(
  6310. 'div' => array('class' => 'input text'),
  6311. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6312. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6313. '/div'
  6314. );
  6315. $this->assertTags($result, $expected);
  6316. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6317. $expected = array(
  6318. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6319. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6320. );
  6321. $this->assertTags($result, $expected);
  6322. $this->Form->create('User', array(
  6323. 'inputDefaults' => array(
  6324. 'div' => false,
  6325. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6326. )
  6327. ));
  6328. $result = $this->Form->input('username', array('div' => true));
  6329. $expected = array(
  6330. 'div' => array('class' => 'input text'),
  6331. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6332. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6333. '/div'
  6334. );
  6335. $this->assertTags($result, $expected);
  6336. }
  6337. /**
  6338. * test automatic accept-charset overriding
  6339. *
  6340. * @return void
  6341. */
  6342. public function testCreateWithAcceptCharset() {
  6343. $result = $this->Form->create('UserForm', array(
  6344. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6345. )
  6346. );
  6347. $expected = array(
  6348. 'form' => array(
  6349. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6350. 'accept-charset' => 'iso-8859-1'
  6351. ),
  6352. 'div' => array('style' => 'display:none;'),
  6353. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6354. '/div'
  6355. );
  6356. $this->assertTags($result, $expected);
  6357. }
  6358. /**
  6359. * Test base form url when url param is passed with multiple parameters (&)
  6360. *
  6361. */
  6362. public function testCreateQuerystringrequest() {
  6363. $encoding = strtolower(Configure::read('App.encoding'));
  6364. $result = $this->Form->create('Contact', array(
  6365. 'type' => 'post',
  6366. 'escape' => false,
  6367. 'url' => array(
  6368. 'controller' => 'controller',
  6369. 'action' => 'action',
  6370. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6371. )
  6372. ));
  6373. $expected = array(
  6374. 'form' => array(
  6375. 'id' => 'ContactAddForm',
  6376. 'method' => 'post',
  6377. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6378. 'accept-charset' => $encoding
  6379. ),
  6380. 'div' => array('style' => 'display:none;'),
  6381. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6382. '/div'
  6383. );
  6384. $this->assertTags($result, $expected);
  6385. $result = $this->Form->create('Contact', array(
  6386. 'type' => 'post',
  6387. 'url' => array(
  6388. 'controller' => 'controller',
  6389. 'action' => 'action',
  6390. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6391. )
  6392. ));
  6393. $expected = array(
  6394. 'form' => array(
  6395. 'id' => 'ContactAddForm',
  6396. 'method' => 'post',
  6397. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6398. 'accept-charset' => $encoding
  6399. ),
  6400. 'div' => array('style' => 'display:none;'),
  6401. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6402. '/div'
  6403. );
  6404. $this->assertTags($result, $expected);
  6405. }
  6406. /**
  6407. * test that create() doesn't cause errors by multiple id's being in the primary key
  6408. * as could happen with multiple select or checkboxes.
  6409. *
  6410. * @return void
  6411. */
  6412. public function testCreateWithMultipleIdInData() {
  6413. $encoding = strtolower(Configure::read('App.encoding'));
  6414. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6415. $result = $this->Form->create('Contact');
  6416. $expected = array(
  6417. 'form' => array(
  6418. 'id' => 'ContactAddForm',
  6419. 'method' => 'post',
  6420. 'action' => '/contacts/add',
  6421. 'accept-charset' => $encoding
  6422. ),
  6423. 'div' => array('style' => 'display:none;'),
  6424. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6425. '/div'
  6426. );
  6427. $this->assertTags($result, $expected);
  6428. }
  6429. /**
  6430. * test that create() doesn't add in extra passed params.
  6431. *
  6432. * @return void
  6433. */
  6434. public function testCreatePassedArgs() {
  6435. $encoding = strtolower(Configure::read('App.encoding'));
  6436. $this->Form->request->data['Contact']['id'] = 1;
  6437. $result = $this->Form->create('Contact', array(
  6438. 'type' => 'post',
  6439. 'escape' => false,
  6440. 'url' => array(
  6441. 'action' => 'edit',
  6442. 'myparam'
  6443. )
  6444. ));
  6445. $expected = array(
  6446. 'form' => array(
  6447. 'id' => 'ContactAddForm',
  6448. 'method' => 'post',
  6449. 'action' => '/contacts/edit/myparam',
  6450. 'accept-charset' => $encoding
  6451. ),
  6452. 'div' => array('style' => 'display:none;'),
  6453. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6454. '/div'
  6455. );
  6456. $this->assertTags($result, $expected);
  6457. }
  6458. /**
  6459. * test creating a get form, and get form inputs.
  6460. *
  6461. * @return void
  6462. */
  6463. public function testGetFormCreate() {
  6464. $encoding = strtolower(Configure::read('App.encoding'));
  6465. $result = $this->Form->create('Contact', array('type' => 'get'));
  6466. $this->assertTags($result, array('form' => array(
  6467. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6468. 'accept-charset' => $encoding
  6469. )));
  6470. $result = $this->Form->text('Contact.name');
  6471. $this->assertTags($result, array('input' => array(
  6472. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6473. )));
  6474. $result = $this->Form->password('password');
  6475. $this->assertTags($result, array('input' => array(
  6476. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6477. )));
  6478. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6479. $result = $this->Form->text('user_form');
  6480. $this->assertTags($result, array('input' => array(
  6481. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6482. )));
  6483. }
  6484. /**
  6485. * test get form, and inputs when the model param is false
  6486. *
  6487. * @return void
  6488. */
  6489. public function testGetFormWithFalseModel() {
  6490. $encoding = strtolower(Configure::read('App.encoding'));
  6491. $this->Form->request['controller'] = 'contact_test';
  6492. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6493. $expected = array('form' => array(
  6494. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6495. 'accept-charset' => $encoding
  6496. ));
  6497. $this->assertTags($result, $expected);
  6498. $result = $this->Form->text('reason');
  6499. $expected = array(
  6500. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6501. );
  6502. $this->assertTags($result, $expected);
  6503. }
  6504. /**
  6505. * test that datetime() works with GET style forms.
  6506. *
  6507. * @return void
  6508. */
  6509. public function testDateTimeWithGetForms() {
  6510. extract($this->dateRegex);
  6511. $this->Form->create('Contact', array('type' => 'get'));
  6512. $result = $this->Form->datetime('created');
  6513. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6514. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6515. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6516. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6517. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6518. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6519. }
  6520. /**
  6521. * testEditFormWithData method
  6522. *
  6523. * test auto populating form elements from submitted data.
  6524. *
  6525. * @return void
  6526. */
  6527. public function testEditFormWithData() {
  6528. $this->Form->request->data = array('Person' => array(
  6529. 'id' => 1,
  6530. 'first_name' => 'Nate',
  6531. 'last_name' => 'Abele',
  6532. 'email' => 'nate@example.com'
  6533. ));
  6534. $this->Form->request->addParams(array(
  6535. 'models' => array('Person'),
  6536. 'controller' => 'people',
  6537. 'action' => 'add'
  6538. ));
  6539. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6540. $this->Form->create();
  6541. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6542. $expected = array(
  6543. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6544. 'select' => array(
  6545. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6546. ),
  6547. array('option' => array('value' => 1)), 'Nate', '/option',
  6548. array('option' => array('value' => 2)), 'Garrett', '/option',
  6549. array('option' => array('value' => 3)), 'Larry', '/option',
  6550. '/select'
  6551. );
  6552. $this->assertTags($result, $expected);
  6553. }
  6554. /**
  6555. * Test that required fields are created for various types of validation.
  6556. *
  6557. * @return void
  6558. */
  6559. public function testFormInputRequiredDetection() {
  6560. $this->Form->create('Contact');
  6561. $result = $this->Form->input('Contact.non_existing');
  6562. $expected = array(
  6563. 'div' => array('class' => 'input text'),
  6564. 'label' => array('for' => 'ContactNonExisting'),
  6565. 'Non Existing',
  6566. '/label',
  6567. 'input' => array(
  6568. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6569. 'id' => 'ContactNonExisting'
  6570. ),
  6571. '/div'
  6572. );
  6573. $this->assertTags($result, $expected);
  6574. $result = $this->Form->input('Contact.imrequired');
  6575. $expected = array(
  6576. 'div' => array('class' => 'input text required'),
  6577. 'label' => array('for' => 'ContactImrequired'),
  6578. 'Imrequired',
  6579. '/label',
  6580. 'input' => array(
  6581. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6582. 'id' => 'ContactImrequired'
  6583. ),
  6584. '/div'
  6585. );
  6586. $this->assertTags($result, $expected);
  6587. $result = $this->Form->input('Contact.imalsorequired');
  6588. $expected = array(
  6589. 'div' => array('class' => 'input text required'),
  6590. 'label' => array('for' => 'ContactImalsorequired'),
  6591. 'Imalsorequired',
  6592. '/label',
  6593. 'input' => array(
  6594. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6595. 'id' => 'ContactImalsorequired'
  6596. ),
  6597. '/div'
  6598. );
  6599. $this->assertTags($result, $expected);
  6600. $result = $this->Form->input('Contact.imrequiredtoo');
  6601. $expected = array(
  6602. 'div' => array('class' => 'input text required'),
  6603. 'label' => array('for' => 'ContactImrequiredtoo'),
  6604. 'Imrequiredtoo',
  6605. '/label',
  6606. 'input' => array(
  6607. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6608. 'id' => 'ContactImrequiredtoo'
  6609. ),
  6610. '/div'
  6611. );
  6612. $this->assertTags($result, $expected);
  6613. $result = $this->Form->input('Contact.required_one');
  6614. $expected = array(
  6615. 'div' => array('class' => 'input text required'),
  6616. 'label' => array('for' => 'ContactRequiredOne'),
  6617. 'Required One',
  6618. '/label',
  6619. 'input' => array(
  6620. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6621. 'id' => 'ContactRequiredOne'
  6622. ),
  6623. '/div'
  6624. );
  6625. $this->assertTags($result, $expected);
  6626. $result = $this->Form->input('Contact.string_required');
  6627. $expected = array(
  6628. 'div' => array('class' => 'input text required'),
  6629. 'label' => array('for' => 'ContactStringRequired'),
  6630. 'String Required',
  6631. '/label',
  6632. 'input' => array(
  6633. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6634. 'id' => 'ContactStringRequired'
  6635. ),
  6636. '/div'
  6637. );
  6638. $this->assertTags($result, $expected);
  6639. $result = $this->Form->input('Contact.imnotrequired');
  6640. $expected = array(
  6641. 'div' => array('class' => 'input text'),
  6642. 'label' => array('for' => 'ContactImnotrequired'),
  6643. 'Imnotrequired',
  6644. '/label',
  6645. 'input' => array(
  6646. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6647. 'id' => 'ContactImnotrequired'
  6648. ),
  6649. '/div'
  6650. );
  6651. $this->assertTags($result, $expected);
  6652. $result = $this->Form->input('Contact.imalsonotrequired');
  6653. $expected = array(
  6654. 'div' => array('class' => 'input text'),
  6655. 'label' => array('for' => 'ContactImalsonotrequired'),
  6656. 'Imalsonotrequired',
  6657. '/label',
  6658. 'input' => array(
  6659. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6660. 'id' => 'ContactImalsonotrequired'
  6661. ),
  6662. '/div'
  6663. );
  6664. $this->assertTags($result, $expected);
  6665. $result = $this->Form->input('Contact.imalsonotrequired2');
  6666. $expected = array(
  6667. 'div' => array('class' => 'input text'),
  6668. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6669. 'Imalsonotrequired2',
  6670. '/label',
  6671. 'input' => array(
  6672. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6673. 'id' => 'ContactImalsonotrequired2'
  6674. ),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. $result = $this->Form->input('Contact.imnotrequiredeither');
  6679. $expected = array(
  6680. 'div' => array('class' => 'input text'),
  6681. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6682. 'Imnotrequiredeither',
  6683. '/label',
  6684. 'input' => array(
  6685. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6686. 'id' => 'ContactImnotrequiredeither'
  6687. ),
  6688. '/div'
  6689. );
  6690. $this->assertTags($result, $expected);
  6691. $result = $this->Form->input('Contact.iamrequiredalways');
  6692. $expected = array(
  6693. 'div' => array('class' => 'input text required'),
  6694. 'label' => array('for' => 'ContactIamrequiredalways'),
  6695. 'Iamrequiredalways',
  6696. '/label',
  6697. 'input' => array(
  6698. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6699. 'id' => 'ContactIamrequiredalways'
  6700. ),
  6701. '/div'
  6702. );
  6703. $this->assertTags($result, $expected);
  6704. }
  6705. /**
  6706. * testFormMagicInput method
  6707. *
  6708. * @return void
  6709. */
  6710. public function testFormMagicInput() {
  6711. $encoding = strtolower(Configure::read('App.encoding'));
  6712. $result = $this->Form->create('Contact');
  6713. $expected = array(
  6714. 'form' => array(
  6715. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6716. 'accept-charset' => $encoding
  6717. ),
  6718. 'div' => array('style' => 'display:none;'),
  6719. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6720. '/div'
  6721. );
  6722. $this->assertTags($result, $expected);
  6723. $result = $this->Form->input('name');
  6724. $expected = array(
  6725. 'div' => array('class' => 'input text'),
  6726. 'label' => array('for' => 'ContactName'),
  6727. 'Name',
  6728. '/label',
  6729. 'input' => array(
  6730. 'type' => 'text', 'name' => 'data[Contact][name]',
  6731. 'id' => 'ContactName', 'maxlength' => '255'
  6732. ),
  6733. '/div'
  6734. );
  6735. $this->assertTags($result, $expected);
  6736. $result = $this->Form->input('non_existing_field_in_contact_model');
  6737. $expected = array(
  6738. 'div' => array('class' => 'input text'),
  6739. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6740. 'Non Existing Field In Contact Model',
  6741. '/label',
  6742. 'input' => array(
  6743. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6744. 'id' => 'ContactNonExistingFieldInContactModel'
  6745. ),
  6746. '/div'
  6747. );
  6748. $this->assertTags($result, $expected);
  6749. $result = $this->Form->input('Address.street');
  6750. $expected = array(
  6751. 'div' => array('class' => 'input text'),
  6752. 'label' => array('for' => 'AddressStreet'),
  6753. 'Street',
  6754. '/label',
  6755. 'input' => array(
  6756. 'type' => 'text', 'name' => 'data[Address][street]',
  6757. 'id' => 'AddressStreet'
  6758. ),
  6759. '/div'
  6760. );
  6761. $this->assertTags($result, $expected);
  6762. $result = $this->Form->input('Address.non_existing_field_in_model');
  6763. $expected = array(
  6764. 'div' => array('class' => 'input text'),
  6765. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6766. 'Non Existing Field In Model',
  6767. '/label',
  6768. 'input' => array(
  6769. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6770. 'id' => 'AddressNonExistingFieldInModel'
  6771. ),
  6772. '/div'
  6773. );
  6774. $this->assertTags($result, $expected);
  6775. $result = $this->Form->input('name', array('div' => false));
  6776. $expected = array(
  6777. 'label' => array('for' => 'ContactName'),
  6778. 'Name',
  6779. '/label',
  6780. 'input' => array(
  6781. 'type' => 'text', 'name' => 'data[Contact][name]',
  6782. 'id' => 'ContactName', 'maxlength' => '255'
  6783. )
  6784. );
  6785. $this->assertTags($result, $expected);
  6786. extract($this->dateRegex);
  6787. $now = strtotime('now');
  6788. $result = $this->Form->input('Contact.published', array('div' => false));
  6789. $expected = array(
  6790. 'label' => array('for' => 'ContactPublishedMonth'),
  6791. 'Published',
  6792. '/label',
  6793. array('select' => array(
  6794. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6795. )),
  6796. $monthsRegex,
  6797. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6798. date('F', $now),
  6799. '/option',
  6800. '*/select',
  6801. '-',
  6802. array('select' => array(
  6803. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6804. )),
  6805. $daysRegex,
  6806. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6807. date('j', $now),
  6808. '/option',
  6809. '*/select',
  6810. '-',
  6811. array('select' => array(
  6812. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6813. )),
  6814. $yearsRegex,
  6815. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6816. date('Y', $now),
  6817. '*/select'
  6818. );
  6819. $this->assertTags($result, $expected);
  6820. $result = $this->Form->input('Contact.updated', array('div' => false));
  6821. $expected = array(
  6822. 'label' => array('for' => 'ContactUpdatedMonth'),
  6823. 'Updated',
  6824. '/label',
  6825. array('select' => array(
  6826. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6827. )),
  6828. $monthsRegex,
  6829. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6830. date('F', $now),
  6831. '/option',
  6832. '*/select',
  6833. '-',
  6834. array('select' => array(
  6835. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6836. )),
  6837. $daysRegex,
  6838. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6839. date('j', $now),
  6840. '/option',
  6841. '*/select',
  6842. '-',
  6843. array('select' => array(
  6844. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6845. )),
  6846. $yearsRegex,
  6847. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6848. date('Y', $now),
  6849. '*/select'
  6850. );
  6851. $this->assertTags($result, $expected);
  6852. $result = $this->Form->input('UserForm.stuff');
  6853. $expected = array(
  6854. 'div' => array('class' => 'input text'),
  6855. 'label' => array('for' => 'UserFormStuff'),
  6856. 'Stuff',
  6857. '/label',
  6858. 'input' => array(
  6859. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6860. 'id' => 'UserFormStuff', 'maxlength' => 10
  6861. ),
  6862. '/div'
  6863. );
  6864. $this->assertTags($result, $expected);
  6865. }
  6866. /**
  6867. * testForMagicInputNonExistingNorValidated method
  6868. *
  6869. * @return void
  6870. */
  6871. public function testForMagicInputNonExistingNorValidated() {
  6872. $encoding = strtolower(Configure::read('App.encoding'));
  6873. $result = $this->Form->create('Contact');
  6874. $expected = array(
  6875. 'form' => array(
  6876. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6877. 'accept-charset' => $encoding
  6878. ),
  6879. 'div' => array('style' => 'display:none;'),
  6880. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6881. '/div'
  6882. );
  6883. $this->assertTags($result, $expected);
  6884. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6885. $expected = array(
  6886. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6887. 'Non Existing Nor Validated',
  6888. '/label',
  6889. 'input' => array(
  6890. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6891. 'id' => 'ContactNonExistingNorValidated'
  6892. )
  6893. );
  6894. $this->assertTags($result, $expected);
  6895. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6896. 'div' => false, 'value' => 'my value'
  6897. ));
  6898. $expected = array(
  6899. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6900. 'Non Existing Nor Validated',
  6901. '/label',
  6902. 'input' => array(
  6903. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6904. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6905. )
  6906. );
  6907. $this->assertTags($result, $expected);
  6908. $this->Form->request->data = array(
  6909. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6910. ));
  6911. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6912. $expected = array(
  6913. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6914. 'Non Existing Nor Validated',
  6915. '/label',
  6916. 'input' => array(
  6917. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6918. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6919. )
  6920. );
  6921. $this->assertTags($result, $expected);
  6922. }
  6923. /**
  6924. * testFormMagicInputLabel method
  6925. *
  6926. * @return void
  6927. */
  6928. public function testFormMagicInputLabel() {
  6929. $encoding = strtolower(Configure::read('App.encoding'));
  6930. $result = $this->Form->create('Contact');
  6931. $expected = array(
  6932. 'form' => array(
  6933. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6934. 'accept-charset' => $encoding
  6935. ),
  6936. 'div' => array('style' => 'display:none;'),
  6937. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6938. '/div'
  6939. );
  6940. $this->assertTags($result, $expected);
  6941. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6942. $this->assertTags($result, array('input' => array(
  6943. 'name' => 'data[Contact][name]', 'type' => 'text',
  6944. 'id' => 'ContactName', 'maxlength' => '255')
  6945. ));
  6946. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6947. $expected = array(
  6948. 'label' => array('for' => 'ContactName'),
  6949. 'My label',
  6950. '/label',
  6951. 'input' => array(
  6952. 'type' => 'text', 'name' => 'data[Contact][name]',
  6953. 'id' => 'ContactName', 'maxlength' => '255'
  6954. )
  6955. );
  6956. $this->assertTags($result, $expected);
  6957. $result = $this->Form->input('Contact.name', array(
  6958. 'div' => false, 'label' => array('class' => 'mandatory')
  6959. ));
  6960. $expected = array(
  6961. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6962. 'Name',
  6963. '/label',
  6964. 'input' => array(
  6965. 'type' => 'text', 'name' => 'data[Contact][name]',
  6966. 'id' => 'ContactName', 'maxlength' => '255'
  6967. )
  6968. );
  6969. $this->assertTags($result, $expected);
  6970. $result = $this->Form->input('Contact.name', array(
  6971. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6972. ));
  6973. $expected = array(
  6974. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6975. 'My label',
  6976. '/label',
  6977. 'input' => array(
  6978. 'type' => 'text', 'name' => 'data[Contact][name]',
  6979. 'id' => 'ContactName', 'maxlength' => '255'
  6980. )
  6981. );
  6982. $this->assertTags($result, $expected);
  6983. $result = $this->Form->input('Contact.name', array(
  6984. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6985. ));
  6986. $expected = array(
  6987. 'label' => array('for' => 'my_id'),
  6988. 'Name',
  6989. '/label',
  6990. 'input' => array(
  6991. 'type' => 'text', 'name' => 'data[Contact][name]',
  6992. 'id' => 'my_id', 'maxlength' => '255'
  6993. )
  6994. );
  6995. $this->assertTags($result, $expected);
  6996. $result = $this->Form->input('1.id');
  6997. $this->assertTags($result, array('input' => array(
  6998. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6999. 'id' => 'Contact1Id'
  7000. )));
  7001. $result = $this->Form->input("1.name");
  7002. $expected = array(
  7003. 'div' => array('class' => 'input text'),
  7004. 'label' => array('for' => 'Contact1Name'),
  7005. 'Name',
  7006. '/label',
  7007. 'input' => array(
  7008. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7009. 'id' => 'Contact1Name', 'maxlength' => '255'
  7010. ),
  7011. '/div'
  7012. );
  7013. $this->assertTags($result, $expected);
  7014. $result = $this->Form->input('Contact.1.id');
  7015. $this->assertTags($result, array(
  7016. 'input' => array(
  7017. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7018. 'id' => 'Contact1Id'
  7019. )
  7020. ));
  7021. $result = $this->Form->input("Model.1.name");
  7022. $expected = array(
  7023. 'div' => array('class' => 'input text'),
  7024. 'label' => array('for' => 'Model1Name'),
  7025. 'Name',
  7026. '/label',
  7027. 'input' => array(
  7028. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7029. 'id' => 'Model1Name'
  7030. ),
  7031. '/div'
  7032. );
  7033. $this->assertTags($result, $expected);
  7034. }
  7035. /**
  7036. * testFormEnd method
  7037. *
  7038. * @return void
  7039. */
  7040. public function testFormEnd() {
  7041. $this->assertEquals('</form>', $this->Form->end());
  7042. $result = $this->Form->end('');
  7043. $expected = array(
  7044. 'div' => array('class' => 'submit'),
  7045. 'input' => array('type' => 'submit', 'value' => ''),
  7046. '/div',
  7047. '/form'
  7048. );
  7049. $this->assertTags($result, $expected);
  7050. $result = $this->Form->end(array('label' => ''));
  7051. $expected = array(
  7052. 'div' => array('class' => 'submit'),
  7053. 'input' => array('type' => 'submit', 'value' => ''),
  7054. '/div',
  7055. '/form'
  7056. );
  7057. $this->assertTags($result, $expected);
  7058. $result = $this->Form->end('save');
  7059. $expected = array(
  7060. 'div' => array('class' => 'submit'),
  7061. 'input' => array('type' => 'submit', 'value' => 'save'),
  7062. '/div',
  7063. '/form'
  7064. );
  7065. $this->assertTags($result, $expected);
  7066. $result = $this->Form->end(array('label' => 'save'));
  7067. $expected = array(
  7068. 'div' => array('class' => 'submit'),
  7069. 'input' => array('type' => 'submit', 'value' => 'save'),
  7070. '/div',
  7071. '/form'
  7072. );
  7073. $this->assertTags($result, $expected);
  7074. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7075. $expected = array(
  7076. 'div' => array('class' => 'submit'),
  7077. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7078. '/div',
  7079. '/form'
  7080. );
  7081. $this->assertTags($result, $expected);
  7082. $result = $this->Form->end(array('name' => 'Whatever'));
  7083. $expected = array(
  7084. 'div' => array('class' => 'submit'),
  7085. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7086. '/div',
  7087. '/form'
  7088. );
  7089. $this->assertTags($result, $expected);
  7090. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7091. $expected = array(
  7092. 'div' => array('class' => 'good'),
  7093. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7094. '/div',
  7095. '/form'
  7096. );
  7097. $this->assertTags($result, $expected);
  7098. $result = $this->Form->end(array(
  7099. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7100. ));
  7101. $expected = array(
  7102. 'div' => array('class' => 'good'),
  7103. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7104. '/div',
  7105. '/form'
  7106. );
  7107. $this->assertTags($result, $expected);
  7108. }
  7109. /**
  7110. * testMultipleFormWithIdFields method
  7111. *
  7112. * @return void
  7113. */
  7114. public function testMultipleFormWithIdFields() {
  7115. $this->Form->create('UserForm');
  7116. $result = $this->Form->input('id');
  7117. $this->assertTags($result, array('input' => array(
  7118. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7119. )));
  7120. $result = $this->Form->input('ValidateItem.id');
  7121. $this->assertTags($result, array('input' => array(
  7122. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7123. 'id' => 'ValidateItemId'
  7124. )));
  7125. $result = $this->Form->input('ValidateUser.id');
  7126. $this->assertTags($result, array('input' => array(
  7127. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7128. 'id' => 'ValidateUserId'
  7129. )));
  7130. }
  7131. /**
  7132. * testDbLessModel method
  7133. *
  7134. * @return void
  7135. */
  7136. public function testDbLessModel() {
  7137. $this->Form->create('TestMail');
  7138. $result = $this->Form->input('name');
  7139. $expected = array(
  7140. 'div' => array('class' => 'input text'),
  7141. 'label' => array('for' => 'TestMailName'),
  7142. 'Name',
  7143. '/label',
  7144. 'input' => array(
  7145. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7146. 'id' => 'TestMailName'
  7147. ),
  7148. '/div'
  7149. );
  7150. $this->assertTags($result, $expected);
  7151. ClassRegistry::init('TestMail');
  7152. $this->Form->create('TestMail');
  7153. $result = $this->Form->input('name');
  7154. $expected = array(
  7155. 'div' => array('class' => 'input text'),
  7156. 'label' => array('for' => 'TestMailName'),
  7157. 'Name',
  7158. '/label',
  7159. 'input' => array(
  7160. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7161. 'id' => 'TestMailName'
  7162. ),
  7163. '/div'
  7164. );
  7165. $this->assertTags($result, $expected);
  7166. }
  7167. /**
  7168. * testBrokenness method
  7169. *
  7170. * @return void
  7171. */
  7172. public function testBrokenness() {
  7173. /*
  7174. * #4 This test has two parents and four children. By default (as of r7117) both
  7175. * parents are show but the first parent is missing a child. This is the inconsistency
  7176. * in the default behaviour - one parent has all children, the other does not - dependent
  7177. * on the data values.
  7178. */
  7179. $result = $this->Form->select('Model.field', array(
  7180. 'Fred' => array(
  7181. 'freds_son_1' => 'Fred',
  7182. 'freds_son_2' => 'Freddie'
  7183. ),
  7184. 'Bert' => array(
  7185. 'berts_son_1' => 'Albert',
  7186. 'berts_son_2' => 'Bertie')
  7187. ),
  7188. array('showParents' => true, 'empty' => false)
  7189. );
  7190. $expected = array(
  7191. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7192. array('optgroup' => array('label' => 'Fred')),
  7193. array('option' => array('value' => 'freds_son_1')),
  7194. 'Fred',
  7195. '/option',
  7196. array('option' => array('value' => 'freds_son_2')),
  7197. 'Freddie',
  7198. '/option',
  7199. '/optgroup',
  7200. array('optgroup' => array('label' => 'Bert')),
  7201. array('option' => array('value' => 'berts_son_1')),
  7202. 'Albert',
  7203. '/option',
  7204. array('option' => array('value' => 'berts_son_2')),
  7205. 'Bertie',
  7206. '/option',
  7207. '/optgroup',
  7208. '/select'
  7209. );
  7210. $this->assertTags($result, $expected);
  7211. /*
  7212. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7213. * changed, so we should expect the same select list data, just with a different name
  7214. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7215. * This is where data corruption can occur, because when a select value is missing from
  7216. * a list a form will substitute the first value in the list - without the user knowing.
  7217. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7218. * affect the availability of 3 => 'Three' as a valid option.
  7219. */
  7220. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7221. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7222. ));
  7223. $result = $this->Form->select(
  7224. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7225. );
  7226. $expected = array(
  7227. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7228. array('option' => array('value' => 1)),
  7229. 'One',
  7230. '/option',
  7231. array('option' => array('value' => 2)),
  7232. 'Two',
  7233. '/option',
  7234. array('optgroup' => array('label' => 'Three')),
  7235. array('option' => array('value' => 3)),
  7236. 'Three',
  7237. '/option',
  7238. array('option' => array('value' => 4)),
  7239. 'Four',
  7240. '/option',
  7241. array('option' => array('value' => 5)),
  7242. 'Five',
  7243. '/option',
  7244. '/optgroup',
  7245. '/select'
  7246. );
  7247. $this->assertTags($result, $expected);
  7248. }
  7249. /**
  7250. * Test the generation of fields for a multi record form.
  7251. *
  7252. * @return void
  7253. */
  7254. public function testMultiRecordForm() {
  7255. $this->Form->create('ValidateProfile');
  7256. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7257. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7258. $expected = array(
  7259. 'div' => array('class' => 'input textarea'),
  7260. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7261. 'Name',
  7262. '/label',
  7263. 'textarea' => array(
  7264. 'id' => 'ValidateProfile1ValidateItem2Name',
  7265. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7266. 'cols' => 30,
  7267. 'rows' => 6
  7268. ),
  7269. 'Value',
  7270. '/textarea',
  7271. '/div'
  7272. );
  7273. $this->assertTags($result, $expected);
  7274. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7275. $expected = array(
  7276. 'div' => array('class' => 'input date'),
  7277. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7278. 'Created',
  7279. '/label',
  7280. array('select' => array(
  7281. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7282. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7283. )
  7284. ),
  7285. array('option' => array('value' => '')), '/option',
  7286. $this->dateRegex['monthsRegex'],
  7287. '/select', '-',
  7288. array('select' => array(
  7289. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7290. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7291. )
  7292. ),
  7293. array('option' => array('value' => '')), '/option',
  7294. $this->dateRegex['daysRegex'],
  7295. '/select', '-',
  7296. array('select' => array(
  7297. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7298. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7299. )
  7300. ),
  7301. array('option' => array('value' => '')), '/option',
  7302. $this->dateRegex['yearsRegex'],
  7303. '/select',
  7304. '/div'
  7305. );
  7306. $this->assertTags($result, $expected);
  7307. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7308. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7309. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7310. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7311. $expected = array(
  7312. 'div' => array('class' => 'input select error'),
  7313. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7314. 'Profile',
  7315. '/label',
  7316. 'select' => array(
  7317. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7318. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7319. 'class' => 'form-error'
  7320. ),
  7321. '/select',
  7322. array('div' => array('class' => 'error-message')),
  7323. 'Error',
  7324. '/div',
  7325. '/div'
  7326. );
  7327. $this->assertTags($result, $expected);
  7328. }
  7329. /**
  7330. * test the correct display of multi-record form validation errors.
  7331. *
  7332. * @return void
  7333. */
  7334. public function testMultiRecordFormValidationErrors() {
  7335. $this->Form->create('ValidateProfile');
  7336. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7337. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7338. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7339. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7340. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7341. $result = $this->Form->error('ValidateProfile.2.city');
  7342. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7343. $result = $this->Form->error('2.city');
  7344. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7345. }
  7346. /**
  7347. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7348. *
  7349. * @return void
  7350. */
  7351. public function testInputTemplate() {
  7352. $result = $this->Form->input('Contact.email', array(
  7353. 'type' => 'text', 'format' => array('input')
  7354. ));
  7355. $expected = array(
  7356. 'div' => array('class' => 'input text'),
  7357. 'input' => array(
  7358. 'type' => 'text', 'name' => 'data[Contact][email]',
  7359. 'id' => 'ContactEmail'
  7360. ),
  7361. '/div'
  7362. );
  7363. $this->assertTags($result, $expected);
  7364. $result = $this->Form->input('Contact.email', array(
  7365. 'type' => 'text', 'format' => array('input', 'label'),
  7366. 'label' => '<em>Email (required)</em>'
  7367. ));
  7368. $expected = array(
  7369. 'div' => array('class' => 'input text'),
  7370. array('input' => array(
  7371. 'type' => 'text', 'name' => 'data[Contact][email]',
  7372. 'id' => 'ContactEmail'
  7373. )),
  7374. 'label' => array('for' => 'ContactEmail'),
  7375. 'em' => array(),
  7376. 'Email (required)',
  7377. '/em',
  7378. '/label',
  7379. '/div'
  7380. );
  7381. $this->assertTags($result, $expected);
  7382. $result = $this->Form->input('Contact.email', array(
  7383. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7384. 'between' => '<div>Something in the middle</div>',
  7385. 'after' => '<span>Some text at the end</span>'
  7386. ));
  7387. $expected = array(
  7388. 'div' => array('class' => 'input text'),
  7389. array('input' => array(
  7390. 'type' => 'text', 'name' => 'data[Contact][email]',
  7391. 'id' => 'ContactEmail'
  7392. )),
  7393. array('div' => array()),
  7394. 'Something in the middle',
  7395. '/div',
  7396. 'label' => array('for' => 'ContactEmail'),
  7397. 'Email',
  7398. '/label',
  7399. 'span' => array(),
  7400. 'Some text at the end',
  7401. '/span',
  7402. '/div'
  7403. );
  7404. $this->assertTags($result, $expected);
  7405. $result = $this->Form->input('Contact.method', array(
  7406. 'type' => 'radio',
  7407. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7408. 'between' => 'I am between',
  7409. ));
  7410. $expected = array(
  7411. 'div' => array('class' => 'input radio'),
  7412. 'fieldset' => array(),
  7413. 'legend' => array(),
  7414. 'Method',
  7415. '/legend',
  7416. 'I am between',
  7417. 'input' => array(
  7418. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7419. 'value' => '', 'id' => 'ContactMethod_'
  7420. ),
  7421. array('input' => array(
  7422. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7423. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7424. )),
  7425. array('label' => array('for' => 'ContactMethodEmail')),
  7426. 'Email',
  7427. '/label',
  7428. array('input' => array(
  7429. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7430. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7431. )),
  7432. array('label' => array('for' => 'ContactMethodPigeon')),
  7433. 'Pigeon',
  7434. '/label',
  7435. '/fieldset',
  7436. '/div',
  7437. );
  7438. $this->assertTags($result, $expected);
  7439. }
  7440. /**
  7441. * test that some html5 inputs + FormHelper::__call() work
  7442. *
  7443. * @return void
  7444. */
  7445. public function testHtml5Inputs() {
  7446. $result = $this->Form->email('User.email');
  7447. $expected = array(
  7448. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7449. );
  7450. $this->assertTags($result, $expected);
  7451. $result = $this->Form->search('User.query');
  7452. $expected = array(
  7453. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7454. );
  7455. $this->assertTags($result, $expected);
  7456. $result = $this->Form->search('User.query', array('value' => 'test'));
  7457. $expected = array(
  7458. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7459. );
  7460. $this->assertTags($result, $expected);
  7461. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7462. $expected = array(
  7463. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7464. );
  7465. $this->assertTags($result, $expected);
  7466. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7467. $expected = array(
  7468. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7469. );
  7470. $this->assertTags($result, $expected);
  7471. }
  7472. /**
  7473. *
  7474. * @expectedException CakeException
  7475. * @return void
  7476. */
  7477. public function testHtml5InputException() {
  7478. $this->Form->email();
  7479. }
  7480. /**
  7481. * Tests that a model can be loaded from the model names passed in the request object
  7482. *
  7483. * @return void
  7484. */
  7485. public function testIntrospectModelFromRequest() {
  7486. $this->loadFixtures('Post');
  7487. App::build(array(
  7488. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7489. ));
  7490. CakePlugin::load('TestPlugin');
  7491. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7492. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7493. $this->Form->create('TestPluginPost');
  7494. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7495. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7496. CakePlugin::unload();
  7497. App::build();
  7498. }
  7499. /**
  7500. * Tests that it is possible to set the validation errors directly in the helper for a field
  7501. *
  7502. * @return void
  7503. */
  7504. public function testCustomValidationErrors() {
  7505. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7506. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7507. $this->assertEquals('Badness!', $result);
  7508. }
  7509. /**
  7510. * Tests that the 'on' key validates as expected on create
  7511. *
  7512. * @return void
  7513. */
  7514. public function testRequiredOnCreate() {
  7515. $this->Form->create('Contact');
  7516. $result = $this->Form->input('Contact.imrequiredonupdate');
  7517. $expected = array(
  7518. 'div' => array('class' => 'input text'),
  7519. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7520. 'Imrequiredonupdate',
  7521. '/label',
  7522. 'input' => array(
  7523. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7524. 'id' => 'ContactImrequiredonupdate'
  7525. ),
  7526. '/div'
  7527. );
  7528. $this->assertTags($result, $expected);
  7529. $result = $this->Form->input('Contact.imrequiredoncreate');
  7530. $expected = array(
  7531. 'div' => array('class' => 'input text required'),
  7532. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7533. 'Imrequiredoncreate',
  7534. '/label',
  7535. 'input' => array(
  7536. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7537. 'id' => 'ContactImrequiredoncreate'
  7538. ),
  7539. '/div'
  7540. );
  7541. $this->assertTags($result, $expected);
  7542. $result = $this->Form->input('Contact.imrequiredonboth');
  7543. $expected = array(
  7544. 'div' => array('class' => 'input text required'),
  7545. 'label' => array('for' => 'ContactImrequiredonboth'),
  7546. 'Imrequiredonboth',
  7547. '/label',
  7548. 'input' => array(
  7549. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7550. 'id' => 'ContactImrequiredonboth'
  7551. ),
  7552. '/div'
  7553. );
  7554. $this->assertTags($result, $expected);
  7555. $result = $this->Form->input('Contact.imrequired');
  7556. $expected = array(
  7557. 'div' => array('class' => 'input text required'),
  7558. 'label' => array('for' => 'ContactImrequired'),
  7559. 'Imrequired',
  7560. '/label',
  7561. 'input' => array(
  7562. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7563. 'id' => 'ContactImrequired'
  7564. ),
  7565. '/div'
  7566. );
  7567. $this->assertTags($result, $expected);
  7568. }
  7569. /**
  7570. * Tests that the 'on' key validates as expected on update
  7571. *
  7572. * @return void
  7573. */
  7574. public function testRequiredOnUpdate() {
  7575. $this->Form->request->data['Contact']['id'] = 1;
  7576. $this->Form->create('Contact');
  7577. $result = $this->Form->input('Contact.imrequiredonupdate');
  7578. $expected = array(
  7579. 'div' => array('class' => 'input text required'),
  7580. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7581. 'Imrequiredonupdate',
  7582. '/label',
  7583. 'input' => array(
  7584. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7585. 'id' => 'ContactImrequiredonupdate'
  7586. ),
  7587. '/div'
  7588. );
  7589. $this->assertTags($result, $expected);
  7590. $result = $this->Form->input('Contact.imrequiredoncreate');
  7591. $expected = array(
  7592. 'div' => array('class' => 'input text'),
  7593. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7594. 'Imrequiredoncreate',
  7595. '/label',
  7596. 'input' => array(
  7597. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7598. 'id' => 'ContactImrequiredoncreate'
  7599. ),
  7600. '/div'
  7601. );
  7602. $this->assertTags($result, $expected);
  7603. $result = $this->Form->input('Contact.imrequiredonboth');
  7604. $expected = array(
  7605. 'div' => array('class' => 'input text required'),
  7606. 'label' => array('for' => 'ContactImrequiredonboth'),
  7607. 'Imrequiredonboth',
  7608. '/label',
  7609. 'input' => array(
  7610. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7611. 'id' => 'ContactImrequiredonboth'
  7612. ),
  7613. '/div'
  7614. );
  7615. $this->assertTags($result, $expected);
  7616. $result = $this->Form->input('Contact.imrequired');
  7617. $expected = array(
  7618. 'div' => array('class' => 'input text required'),
  7619. 'label' => array('for' => 'ContactImrequired'),
  7620. 'Imrequired',
  7621. '/label',
  7622. 'input' => array(
  7623. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7624. 'id' => 'ContactImrequired'
  7625. ),
  7626. '/div'
  7627. );
  7628. $this->assertTags($result, $expected);
  7629. }
  7630. /**
  7631. * Test inputDefaults setter and getter
  7632. *
  7633. * @return void
  7634. */
  7635. public function testInputDefaults() {
  7636. $this->Form->create('Contact');
  7637. $this->Form->inputDefaults(array(
  7638. 'label' => false,
  7639. 'div' => array(
  7640. 'style' => 'color: #000;'
  7641. )
  7642. ));
  7643. $result = $this->Form->input('Contact.field1');
  7644. $expected = array(
  7645. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7646. 'input' => array(
  7647. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7648. 'id' => 'ContactField1'
  7649. ),
  7650. '/div'
  7651. );
  7652. $this->assertTags($result, $expected);
  7653. $this->Form->inputDefaults(array(
  7654. 'div' => false,
  7655. 'label' => 'Label',
  7656. ));
  7657. $result = $this->Form->input('Contact.field1');
  7658. $expected = array(
  7659. 'label' => array('for' => 'ContactField1'),
  7660. 'Label',
  7661. '/label',
  7662. 'input' => array(
  7663. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7664. 'id' => 'ContactField1'
  7665. ),
  7666. );
  7667. $this->assertTags($result, $expected);
  7668. $this->Form->inputDefaults(array(
  7669. 'label' => false,
  7670. ), true);
  7671. $result = $this->Form->input('Contact.field1');
  7672. $expected = array(
  7673. 'input' => array(
  7674. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7675. 'id' => 'ContactField1'
  7676. ),
  7677. );
  7678. $this->assertTags($result, $expected);
  7679. $result = $this->Form->inputDefaults();
  7680. $expected = array(
  7681. 'div' => false,
  7682. 'label' => false,
  7683. );
  7684. $this->assertEqual($result, $expected);
  7685. }
  7686. }