FormHelperTest.php 239 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing 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. 'string_required' => 'notEmpty',
  99. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  100. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  101. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  102. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  103. 'imalsonotrequired' => array(
  104. 'alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
  105. 'between' => array('rule' => array('between', 5, 30)),
  106. ),
  107. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  108. );
  109. /**
  110. * schema method
  111. *
  112. * @return void
  113. */
  114. public function setSchema($schema) {
  115. $this->_schema = $schema;
  116. }
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  123. /**
  124. * hasAndBelongsToMany property
  125. *
  126. * @var array
  127. */
  128. public $belongsTo = array('User' => array('className' => 'UserForm'));
  129. }
  130. /**
  131. * ContactTagsContact class
  132. *
  133. * @package cake
  134. * @package Cake.Test.Case.View.Helper
  135. */
  136. class ContactTagsContact extends CakeTestModel {
  137. /**
  138. * useTable property
  139. *
  140. * @var bool false
  141. */
  142. public $useTable = false;
  143. /**
  144. * name property
  145. *
  146. * @var string 'Contact'
  147. */
  148. public $name = 'ContactTagsContact';
  149. /**
  150. * Default schema
  151. *
  152. * @var array
  153. */
  154. protected $_schema = array(
  155. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  156. 'contact_tag_id' => array(
  157. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  158. )
  159. );
  160. /**
  161. * schema method
  162. *
  163. * @return void
  164. */
  165. public function setSchema($schema) {
  166. $this->_schema = $schema;
  167. }
  168. }
  169. /**
  170. * ContactNonStandardPk class
  171. *
  172. * @package cake
  173. * @package Cake.Test.Case.View.Helper
  174. */
  175. class ContactNonStandardPk extends Contact {
  176. /**
  177. * primaryKey property
  178. *
  179. * @var string 'pk'
  180. */
  181. public $primaryKey = 'pk';
  182. /**
  183. * name property
  184. *
  185. * @var string 'ContactNonStandardPk'
  186. */
  187. public $name = 'ContactNonStandardPk';
  188. /**
  189. * schema method
  190. *
  191. * @return void
  192. */
  193. public function schema($field = false) {
  194. $this->_schema = parent::schema();
  195. $this->_schema['pk'] = $this->_schema['id'];
  196. unset($this->_schema['id']);
  197. return $this->_schema;
  198. }
  199. }
  200. /**
  201. * ContactTag class
  202. *
  203. * @package cake
  204. * @package Cake.Test.Case.View.Helper
  205. */
  206. class ContactTag extends Model {
  207. /**
  208. * useTable property
  209. *
  210. * @var bool false
  211. */
  212. public $useTable = false;
  213. /**
  214. * schema definition
  215. *
  216. * @var array
  217. */
  218. protected $_schema = array(
  219. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  220. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  221. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  222. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  223. );
  224. }
  225. /**
  226. * UserForm class
  227. *
  228. * @package cake
  229. * @package Cake.Test.Case.View.Helper
  230. */
  231. class UserForm extends CakeTestModel {
  232. /**
  233. * useTable property
  234. *
  235. * @var bool false
  236. */
  237. public $useTable = false;
  238. /**
  239. * primaryKey property
  240. *
  241. * @var string 'id'
  242. */
  243. public $primaryKey = 'id';
  244. /**
  245. * name property
  246. *
  247. * @var string 'UserForm'
  248. */
  249. public $name = 'UserForm';
  250. /**
  251. * hasMany property
  252. *
  253. * @var array
  254. */
  255. public $hasMany = array(
  256. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  257. ));
  258. /**
  259. * schema definition
  260. *
  261. * @var array
  262. */
  263. protected $_schema = array(
  264. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  265. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  266. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  267. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  268. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  269. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  270. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  271. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  272. );
  273. }
  274. /**
  275. * OpenidUrl class
  276. *
  277. * @package cake
  278. * @package Cake.Test.Case.View.Helper
  279. */
  280. class OpenidUrl extends CakeTestModel {
  281. /**
  282. * useTable property
  283. *
  284. * @var bool false
  285. */
  286. public $useTable = false;
  287. /**
  288. * primaryKey property
  289. *
  290. * @var string 'id'
  291. */
  292. public $primaryKey = 'id';
  293. /**
  294. * name property
  295. *
  296. * @var string 'OpenidUrl'
  297. */
  298. public $name = 'OpenidUrl';
  299. /**
  300. * belongsTo property
  301. *
  302. * @var array
  303. */
  304. public $belongsTo = array('UserForm' => array(
  305. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  306. ));
  307. /**
  308. * validate property
  309. *
  310. * @var array
  311. */
  312. public $validate = array('openid_not_registered' => array());
  313. /**
  314. * schema method
  315. *
  316. * @var array
  317. */
  318. protected $_schema = array(
  319. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  320. 'user_form_id' => array(
  321. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  322. ),
  323. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  324. );
  325. /**
  326. * beforeValidate method
  327. *
  328. * @return void
  329. */
  330. public function beforeValidate($options = array()) {
  331. $this->invalidate('openid_not_registered');
  332. return true;
  333. }
  334. }
  335. /**
  336. * ValidateUser class
  337. *
  338. * @package cake
  339. * @package Cake.Test.Case.View.Helper
  340. */
  341. class ValidateUser extends CakeTestModel {
  342. /**
  343. * primaryKey property
  344. *
  345. * @var string 'id'
  346. */
  347. public $primaryKey = 'id';
  348. /**
  349. * useTable property
  350. *
  351. * @var bool false
  352. */
  353. public $useTable = false;
  354. /**
  355. * name property
  356. *
  357. * @var string 'ValidateUser'
  358. */
  359. public $name = 'ValidateUser';
  360. /**
  361. * hasOne property
  362. *
  363. * @var array
  364. */
  365. public $hasOne = array('ValidateProfile' => array(
  366. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  367. ));
  368. /**
  369. * schema method
  370. *
  371. * @var array
  372. */
  373. protected $_schema = array(
  374. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  375. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  376. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  377. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  378. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  379. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  380. );
  381. /**
  382. * beforeValidate method
  383. *
  384. * @return void
  385. */
  386. public function beforeValidate($options = array()) {
  387. $this->invalidate('email');
  388. return false;
  389. }
  390. }
  391. /**
  392. * ValidateProfile class
  393. *
  394. * @package cake
  395. * @package Cake.Test.Case.View.Helper
  396. */
  397. class ValidateProfile extends CakeTestModel {
  398. /**
  399. * primaryKey property
  400. *
  401. * @var string 'id'
  402. */
  403. public $primaryKey = 'id';
  404. /**
  405. * useTable property
  406. *
  407. * @var bool false
  408. */
  409. public $useTable = false;
  410. /**
  411. * schema property
  412. *
  413. * @var array
  414. */
  415. protected $_schema = array(
  416. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  417. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  418. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  419. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  420. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  421. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  422. );
  423. /**
  424. * name property
  425. *
  426. * @var string 'ValidateProfile'
  427. */
  428. public $name = 'ValidateProfile';
  429. /**
  430. * hasOne property
  431. *
  432. * @var array
  433. */
  434. public $hasOne = array('ValidateItem' => array(
  435. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  436. ));
  437. /**
  438. * belongsTo property
  439. *
  440. * @var array
  441. */
  442. public $belongsTo = array('ValidateUser' => array(
  443. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  444. ));
  445. /**
  446. * beforeValidate method
  447. *
  448. * @return void
  449. */
  450. public function beforeValidate($options = array()) {
  451. $this->invalidate('full_name');
  452. $this->invalidate('city');
  453. return false;
  454. }
  455. }
  456. /**
  457. * ValidateItem class
  458. *
  459. * @package cake
  460. * @package Cake.Test.Case.View.Helper
  461. */
  462. class ValidateItem extends CakeTestModel {
  463. /**
  464. * primaryKey property
  465. *
  466. * @var string 'id'
  467. */
  468. public $primaryKey = 'id';
  469. /**
  470. * useTable property
  471. *
  472. * @var bool false
  473. */
  474. public $useTable = false;
  475. /**
  476. * name property
  477. *
  478. * @var string 'ValidateItem'
  479. */
  480. public $name = 'ValidateItem';
  481. /**
  482. * schema property
  483. *
  484. * @var array
  485. */
  486. protected $_schema = array(
  487. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  488. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  489. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  490. 'description' => array(
  491. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  492. ),
  493. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  494. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  495. );
  496. /**
  497. * belongsTo property
  498. *
  499. * @var array
  500. */
  501. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  502. /**
  503. * beforeValidate method
  504. *
  505. * @return void
  506. */
  507. public function beforeValidate($options = array()) {
  508. $this->invalidate('description');
  509. return false;
  510. }
  511. }
  512. /**
  513. * TestMail class
  514. *
  515. * @package cake
  516. * @package Cake.Test.Case.View.Helper
  517. */
  518. class TestMail extends CakeTestModel {
  519. /**
  520. * primaryKey property
  521. *
  522. * @var string 'id'
  523. */
  524. public $primaryKey = 'id';
  525. /**
  526. * useTable property
  527. *
  528. * @var bool false
  529. */
  530. public $useTable = false;
  531. /**
  532. * name property
  533. *
  534. * @var string 'TestMail'
  535. */
  536. public $name = 'TestMail';
  537. }
  538. /**
  539. * FormHelperTest class
  540. *
  541. * @package cake
  542. * @package Cake.Test.Case.View.Helper
  543. */
  544. class FormHelperTest extends CakeTestCase {
  545. /**
  546. * Fixtures to be used
  547. *
  548. * @var array
  549. */
  550. public $fixtures = array('core.post');
  551. /**
  552. * Do not load the fixtures by default
  553. *
  554. * @var boolean
  555. */
  556. public $autoFixtures = false;
  557. /**
  558. * setUp method
  559. *
  560. * @return void
  561. */
  562. public function setUp() {
  563. parent::setUp();
  564. Configure::write('App.base', '');
  565. $this->Controller = new ContactTestController();
  566. $this->View = new View($this->Controller);
  567. $this->Form = new FormHelper($this->View);
  568. $this->Form->Html = new HtmlHelper($this->View);
  569. $this->Form->request = new CakeRequest('contacts/add', false);
  570. $this->Form->request->here = '/contacts/add';
  571. $this->Form->request['action'] = 'add';
  572. $this->Form->request->webroot = '';
  573. $this->Form->request->base = '';
  574. ClassRegistry::addObject('Contact', new Contact());
  575. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  576. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  577. ClassRegistry::addObject('User', new UserForm());
  578. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  579. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  580. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  581. $this->oldSalt = Configure::read('Security.salt');
  582. $this->dateRegex = array(
  583. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  584. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  585. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  586. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  587. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  588. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  589. );
  590. Configure::write('Security.salt', 'foo!');
  591. }
  592. /**
  593. * tearDown method
  594. *
  595. * @return void
  596. */
  597. public function tearDown() {
  598. parent::tearDown();
  599. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  600. Configure::write('Security.salt', $this->oldSalt);
  601. }
  602. /**
  603. * testFormCreateWithSecurity method
  604. *
  605. * Test form->create() with security key.
  606. *
  607. * @return void
  608. */
  609. public function testCreateWithSecurity() {
  610. $this->Form->request['_Token'] = array('key' => 'testKey');
  611. $encoding = strtolower(Configure::read('App.encoding'));
  612. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  613. $expected = array(
  614. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  615. 'div' => array('style' => 'display:none;'),
  616. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  617. array('input' => array(
  618. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  619. )),
  620. '/div'
  621. );
  622. $this->assertTags($result, $expected);
  623. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  624. $expected['form']['id'] = 'MyForm';
  625. $this->assertTags($result, $expected);
  626. }
  627. /**
  628. * test that create() clears the fields property so it starts fresh
  629. *
  630. * @return void
  631. */
  632. public function testCreateClearingFields() {
  633. $this->Form->fields = array('model_id');
  634. $this->Form->create('Contact');
  635. $this->assertEqual($this->Form->fields, array());
  636. }
  637. /**
  638. * Tests form hash generation with model-less data
  639. *
  640. * @return void
  641. */
  642. public function testValidateHashNoModel() {
  643. $this->Form->request['_Token'] = array('key' => 'foo');
  644. $result = $this->Form->secure(array('anything'));
  645. $this->assertPattern('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  646. }
  647. /**
  648. * Tests that models with identical field names get resolved properly
  649. *
  650. * @return void
  651. */
  652. public function testDuplicateFieldNameResolution() {
  653. $result = $this->Form->create('ValidateUser');
  654. $this->assertEqual($this->Form->entity(), array('ValidateUser'));
  655. $result = $this->Form->input('ValidateItem.name');
  656. $this->assertEqual($this->Form->entity(), array('ValidateItem', 'name'));
  657. $result = $this->Form->input('ValidateUser.name');
  658. $this->assertEqual($this->Form->entity(), array('ValidateUser', 'name'));
  659. $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
  660. $this->assertPattern('/type="text"/', $result);
  661. $result = $this->Form->input('ValidateItem.name');
  662. $this->assertEqual($this->Form->entity(), array('ValidateItem', 'name'));
  663. $this->assertPattern('/name="data\[ValidateItem\]\[name\]"/', $result);
  664. $this->assertPattern('/<textarea/', $result);
  665. $result = $this->Form->input('name');
  666. $this->assertEqual($this->Form->entity(), array('ValidateUser', 'name'));
  667. $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
  668. $this->assertPattern('/type="text"/', $result);
  669. }
  670. /**
  671. * Tests that hidden fields generated for checkboxes don't get locked
  672. *
  673. * @return void
  674. */
  675. public function testNoCheckboxLocking() {
  676. $this->Form->request['_Token'] = array('key' => 'foo');
  677. $this->assertIdentical($this->Form->fields, array());
  678. $this->Form->checkbox('check', array('value' => '1'));
  679. $this->assertIdentical($this->Form->fields, array('check'));
  680. }
  681. /**
  682. * testFormSecurityFields method
  683. *
  684. * Test generation of secure form hash generation.
  685. *
  686. * @return void
  687. */
  688. public function testFormSecurityFields() {
  689. $key = 'testKey';
  690. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  691. $this->Form->request['_Token'] = array('key' => $key);
  692. $result = $this->Form->secure($fields);
  693. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  694. $expected .= ':' . 'Model.valid';
  695. $expected = array(
  696. 'div' => array('style' => 'display:none;'),
  697. array('input' => array(
  698. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  699. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  700. )),
  701. array('input' => array(
  702. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  703. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  704. )),
  705. '/div'
  706. );
  707. $this->assertTags($result, $expected);
  708. }
  709. /**
  710. * Tests correct generation of number fields for double and float fields
  711. *
  712. * @return void
  713. */
  714. public function testTextFieldGenerationForFloats() {
  715. $model = ClassRegistry::getObject('Contact');
  716. $model->setSchema(array('foo' => array(
  717. 'type' => 'float',
  718. 'null' => false,
  719. 'default' => null,
  720. 'length' => null
  721. )));
  722. $this->Form->create('Contact');
  723. $result = $this->Form->input('foo');
  724. $expected = array(
  725. 'div' => array('class' => 'input number'),
  726. 'label' => array('for' => 'ContactFoo'),
  727. 'Foo',
  728. '/label',
  729. array('input' => array(
  730. 'type' => 'number',
  731. 'name' => 'data[Contact][foo]',
  732. 'id' => 'ContactFoo',
  733. 'step' => 'any'
  734. )),
  735. '/div'
  736. );
  737. $this->assertTags($result, $expected);
  738. $result = $this->Form->input('foo', array('step' => 0.5));
  739. $expected = array(
  740. 'div' => array('class' => 'input number'),
  741. 'label' => array('for' => 'ContactFoo'),
  742. 'Foo',
  743. '/label',
  744. array('input' => array(
  745. 'type' => 'number',
  746. 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo',
  748. 'step' => '0.5'
  749. )),
  750. '/div'
  751. );
  752. $this->assertTags($result, $expected);
  753. }
  754. /**
  755. * Tests correct generation of number fields for integer fields
  756. *
  757. * @access public
  758. * @return void
  759. */
  760. public function testTextFieldTypeNumberGenerationForIntegers() {
  761. $model = ClassRegistry::getObject('Contact');
  762. $model->setSchema(array('foo' => array(
  763. 'type' => 'integer',
  764. 'null' => false,
  765. 'default' => null,
  766. 'length' => null
  767. )));
  768. $this->Form->create('Contact');
  769. $result = $this->Form->input('foo');
  770. $expected = array(
  771. 'div' => array('class' => 'input number'),
  772. 'label' => array('for' => 'ContactFoo'),
  773. 'Foo',
  774. '/label',
  775. array('input' => array(
  776. 'type' => 'number', 'name' => 'data[Contact][foo]',
  777. 'id' => 'ContactFoo'
  778. )),
  779. '/div'
  780. );
  781. $this->assertTags($result, $expected);
  782. }
  783. /**
  784. * testFormSecurityMultipleFields method
  785. *
  786. * Test secure() with multiple row form. Ensure hash is correct.
  787. *
  788. * @return void
  789. */
  790. public function testFormSecurityMultipleFields() {
  791. $key = 'testKey';
  792. $fields = array(
  793. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  794. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  795. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  796. );
  797. $this->Form->request['_Token'] = array('key' => $key);
  798. $result = $this->Form->secure($fields);
  799. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  800. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  801. $expected = array(
  802. 'div' => array('style' => 'display:none;'),
  803. array('input' => array(
  804. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  805. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  806. )),
  807. array('input' => array(
  808. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  809. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  810. )),
  811. '/div'
  812. );
  813. $this->assertTags($result, $expected);
  814. }
  815. /**
  816. * testFormSecurityMultipleSubmitButtons
  817. *
  818. * test form submit generation and ensure that _Token is only created on end()
  819. *
  820. * @return void
  821. */
  822. public function testFormSecurityMultipleSubmitButtons() {
  823. $key = 'testKey';
  824. $this->Form->request['_Token'] = array('key' => $key);
  825. $this->Form->create('Addresses');
  826. $this->Form->input('Address.title');
  827. $this->Form->input('Address.first_name');
  828. $result = $this->Form->submit('Save', array('name' => 'save'));
  829. $expected = array(
  830. 'div' => array('class' => 'submit'),
  831. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  832. '/div',
  833. );
  834. $this->assertTags($result, $expected);
  835. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  836. $expected = array(
  837. 'div' => array('class' => 'submit'),
  838. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  839. '/div',
  840. );
  841. $this->assertTags($result, $expected);
  842. $result = $this->Form->end(null);
  843. $expected = array(
  844. 'div' => array('style' => 'display:none;'),
  845. array('input' => array(
  846. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  847. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  848. )),
  849. array('input' => array(
  850. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  851. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  852. )),
  853. '/div'
  854. );
  855. $this->assertTags($result, $expected);
  856. }
  857. /**
  858. * Test that the correct fields are unlocked for image submits with no names.
  859. *
  860. * @return void
  861. */
  862. public function testSecuritySubmitImageNoName() {
  863. $key = 'testKey';
  864. $this->Form->request['_Token'] = array('key' => $key);
  865. $this->Form->create('User');
  866. $result = $this->Form->submit('save.png');
  867. $expected = array(
  868. 'div' => array('class' => 'submit'),
  869. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  870. '/div'
  871. );
  872. $this->assertTags($result, $expected);
  873. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  874. }
  875. /**
  876. * Test that the correct fields are unlocked for image submits with names.
  877. *
  878. * @return void
  879. */
  880. public function testSecuritySubmitImageName() {
  881. $key = 'testKey';
  882. $this->Form->request['_Token'] = array('key' => $key);
  883. $this->Form->create('User');
  884. $result = $this->Form->submit('save.png', array('name' => 'test'));
  885. $expected = array(
  886. 'div' => array('class' => 'submit'),
  887. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  888. '/div'
  889. );
  890. $this->assertTags($result, $expected);
  891. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  892. }
  893. /**
  894. * testFormSecurityMultipleInputFields method
  895. *
  896. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  897. *
  898. * @return void
  899. */
  900. public function testFormSecurityMultipleInputFields() {
  901. $key = 'testKey';
  902. $this->Form->request['_Token'] = array('key' => $key);
  903. $this->Form->create('Addresses');
  904. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  905. $this->Form->input('Addresses.0.title');
  906. $this->Form->input('Addresses.0.first_name');
  907. $this->Form->input('Addresses.0.last_name');
  908. $this->Form->input('Addresses.0.address');
  909. $this->Form->input('Addresses.0.city');
  910. $this->Form->input('Addresses.0.phone');
  911. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  912. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  913. $this->Form->input('Addresses.1.title');
  914. $this->Form->input('Addresses.1.first_name');
  915. $this->Form->input('Addresses.1.last_name');
  916. $this->Form->input('Addresses.1.address');
  917. $this->Form->input('Addresses.1.city');
  918. $this->Form->input('Addresses.1.phone');
  919. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  920. $result = $this->Form->secure($this->Form->fields);
  921. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  922. $expected = array(
  923. 'div' => array('style' => 'display:none;'),
  924. array('input' => array(
  925. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  926. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  927. )),
  928. array('input' => array(
  929. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  930. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  931. )),
  932. '/div'
  933. );
  934. $this->assertTags($result, $expected);
  935. }
  936. /**
  937. * Test form security with Model.field.0 style inputs
  938. *
  939. * @return void
  940. */
  941. function testFormSecurityArrayFields() {
  942. $key = 'testKey';
  943. $this->Form->request->params['_Token']['key'] = $key;
  944. $this->Form->create('Address');
  945. $this->Form->input('Address.primary.1');
  946. $this->assertEqual('Address.primary', $this->Form->fields[0]);
  947. }
  948. /**
  949. * testFormSecurityMultipleInputDisabledFields method
  950. *
  951. * test secure form generation with multiple records and disabled fields.
  952. *
  953. * @return void
  954. */
  955. public function testFormSecurityMultipleInputDisabledFields() {
  956. $key = 'testKey';
  957. $this->Form->request->params['_Token'] = array(
  958. 'key' => $key,
  959. 'unlockedFields' => array('first_name', 'address')
  960. );
  961. $this->Form->create();
  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->hidden('Addresses.1.id', array('value' => '654321'));
  970. $this->Form->input('Addresses.1.title');
  971. $this->Form->input('Addresses.1.first_name');
  972. $this->Form->input('Addresses.1.last_name');
  973. $this->Form->input('Addresses.1.address');
  974. $this->Form->input('Addresses.1.city');
  975. $this->Form->input('Addresses.1.phone');
  976. $result = $this->Form->secure($this->Form->fields);
  977. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  978. $expected = array(
  979. 'div' => array('style' => 'display:none;'),
  980. array('input' => array(
  981. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  982. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  983. )),
  984. array('input' => array(
  985. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  986. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  987. )),
  988. '/div'
  989. );
  990. $this->assertTags($result, $expected);
  991. }
  992. /**
  993. * testFormSecurityInputDisabledFields method
  994. *
  995. * Test single record form with disabled fields.
  996. *
  997. * @return void
  998. */
  999. public function testFormSecurityInputUnlockedFields() {
  1000. $key = 'testKey';
  1001. $this->Form->request['_Token'] = array(
  1002. 'key' => $key,
  1003. 'unlockedFields' => array('first_name', 'address')
  1004. );
  1005. $this->Form->create();
  1006. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1007. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1008. $this->Form->input('Addresses.title');
  1009. $this->Form->input('Addresses.first_name');
  1010. $this->Form->input('Addresses.last_name');
  1011. $this->Form->input('Addresses.address');
  1012. $this->Form->input('Addresses.city');
  1013. $this->Form->input('Addresses.phone');
  1014. $result = $this->Form->fields;
  1015. $expected = array(
  1016. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1017. 'Addresses.city', 'Addresses.phone'
  1018. );
  1019. $this->assertEqual($expected, $result);
  1020. $result = $this->Form->secure($expected);
  1021. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1022. $expected = array(
  1023. 'div' => array('style' => 'display:none;'),
  1024. array('input' => array(
  1025. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1026. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1027. )),
  1028. array('input' => array(
  1029. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1030. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1031. )),
  1032. '/div'
  1033. );
  1034. $this->assertTags($result, $expected);
  1035. }
  1036. /**
  1037. * test securing inputs with custom name attributes.
  1038. *
  1039. * @return void
  1040. */
  1041. public function testFormSecureWithCustomNameAttribute() {
  1042. $this->Form->request->params['_Token']['key'] = 'testKey';
  1043. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1044. $this->assertEqual('User.custom', $this->Form->fields[0]);
  1045. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1046. $this->assertEqual('User.custom.another.value', $this->Form->fields[1]);
  1047. }
  1048. /**
  1049. * testFormSecuredInput method
  1050. *
  1051. * Test generation of entire secure form, assertions made on input() output.
  1052. *
  1053. * @return void
  1054. */
  1055. public function testFormSecuredInput() {
  1056. $this->Form->request['_Token'] = array('key' => 'testKey');
  1057. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1058. $encoding = strtolower(Configure::read('App.encoding'));
  1059. $expected = array(
  1060. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1061. 'div' => array('style' => 'display:none;'),
  1062. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1063. array('input' => array(
  1064. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1065. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1066. )),
  1067. '/div'
  1068. );
  1069. $this->assertTags($result, $expected);
  1070. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1071. $expected = array(
  1072. 'div' => array('class' => 'input text'),
  1073. 'label' => array('for' => 'UserFormPublished'),
  1074. 'Published',
  1075. '/label',
  1076. array('input' => array(
  1077. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1078. 'id' => 'UserFormPublished'
  1079. )),
  1080. '/div'
  1081. );
  1082. $this->assertTags($result, $expected);
  1083. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1084. $expected = array(
  1085. 'div' => array('class' => 'input text'),
  1086. 'label' => array('for' => 'UserFormOther'),
  1087. 'Other',
  1088. '/label',
  1089. array('input' => array(
  1090. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1091. 'id' => 'UserFormOther'
  1092. )),
  1093. '/div'
  1094. );
  1095. $this->assertTags($result, $expected);
  1096. $result = $this->Form->hidden('UserForm.stuff');
  1097. $expected = array('input' => array(
  1098. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1099. 'id' => 'UserFormStuff'
  1100. ));
  1101. $this->assertTags($result, $expected);
  1102. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1103. $expected = array('input' => array(
  1104. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1105. 'value' => '0', 'id' => 'UserFormHidden'
  1106. ));
  1107. $this->assertTags($result, $expected);
  1108. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1109. $expected = array(
  1110. 'div' => array('class' => 'input checkbox'),
  1111. array('input' => array(
  1112. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1113. 'value' => '0', 'id' => 'UserFormSomething_'
  1114. )),
  1115. array('input' => array(
  1116. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1117. 'value' => '1', 'id' => 'UserFormSomething'
  1118. )),
  1119. 'label' => array('for' => 'UserFormSomething'),
  1120. 'Something',
  1121. '/label',
  1122. '/div'
  1123. );
  1124. $this->assertTags($result, $expected);
  1125. $result = $this->Form->fields;
  1126. $expected = array(
  1127. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1128. 'UserForm.hidden' => '0', 'UserForm.something'
  1129. );
  1130. $this->assertEqual($expected, $result);
  1131. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1132. $result = $this->Form->secure($this->Form->fields);
  1133. $expected = array(
  1134. 'div' => array('style' => 'display:none;'),
  1135. array('input' => array(
  1136. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1137. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1138. )),
  1139. array('input' => array(
  1140. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1141. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1142. )),
  1143. '/div'
  1144. );
  1145. $this->assertTags($result, $expected);
  1146. }
  1147. /**
  1148. * Tests that the correct keys are added to the field hash index
  1149. *
  1150. * @return void
  1151. */
  1152. public function testFormSecuredFileInput() {
  1153. $this->Form->request['_Token'] = array('key' => 'testKey');
  1154. $this->assertEqual($this->Form->fields, array());
  1155. $result = $this->Form->file('Attachment.file');
  1156. $expected = array (
  1157. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1158. 'Attachment.file.error', 'Attachment.file.size'
  1159. );
  1160. $this->assertEqual($this->Form->fields, $expected);
  1161. }
  1162. /**
  1163. * test that multiple selects keys are added to field hash
  1164. *
  1165. * @return void
  1166. */
  1167. public function testFormSecuredMultipleSelect() {
  1168. $this->Form->request['_Token'] = array('key' => 'testKey');
  1169. $this->assertEqual($this->Form->fields, array());
  1170. $options = array('1' => 'one', '2' => 'two');
  1171. $this->Form->select('Model.select', $options);
  1172. $expected = array('Model.select');
  1173. $this->assertEqual($this->Form->fields, $expected);
  1174. $this->Form->fields = array();
  1175. $this->Form->select('Model.select', $options, array('multiple' => true));
  1176. $this->assertEqual($this->Form->fields, $expected);
  1177. }
  1178. /**
  1179. * testFormSecuredRadio method
  1180. *
  1181. * @return void
  1182. */
  1183. public function testFormSecuredRadio() {
  1184. $this->Form->request['_Token'] = array('key' => 'testKey');
  1185. $this->assertEqual($this->Form->fields, array());
  1186. $options = array('1' => 'option1', '2' => 'option2');
  1187. $this->Form->radio('Test.test', $options);
  1188. $expected = array('Test.test');
  1189. $this->assertEqual($this->Form->fields, $expected);
  1190. }
  1191. /**
  1192. * testDisableSecurityUsingForm method
  1193. *
  1194. * @return void
  1195. */
  1196. public function testDisableSecurityUsingForm() {
  1197. $this->Form->request['_Token'] = array(
  1198. 'key' => 'testKey',
  1199. 'disabledFields' => array()
  1200. );
  1201. $this->Form->create();
  1202. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1203. $this->Form->input('Addresses.title');
  1204. $this->Form->input('Addresses.first_name', array('secure' => false));
  1205. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1206. $this->Form->input('Addresses.zip', array(
  1207. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1208. ));
  1209. $result = $this->Form->fields;
  1210. $expected = array(
  1211. 'Addresses.id' => '123456', 'Addresses.title',
  1212. );
  1213. $this->assertEqual($expected, $result);
  1214. }
  1215. /**
  1216. * test disableField
  1217. *
  1218. * @return void
  1219. */
  1220. public function testUnlockFieldAddsToList() {
  1221. $this->Form->request['_Token'] = array(
  1222. 'key' => 'testKey',
  1223. 'unlockedFields' => array()
  1224. );
  1225. $this->Form->create('Contact');
  1226. $this->Form->unlockField('Contact.name');
  1227. $this->Form->text('Contact.name');
  1228. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1229. $this->assertEquals(array(), $this->Form->fields);
  1230. }
  1231. /**
  1232. * test unlockField removing from fields array.
  1233. *
  1234. * @return void
  1235. */
  1236. public function testUnlockFieldRemovingFromFields() {
  1237. $this->Form->request['_Token'] = array(
  1238. 'key' => 'testKey',
  1239. 'unlockedFields' => array()
  1240. );
  1241. $this->Form->create('Contact');
  1242. $this->Form->hidden('Contact.id', array('value' => 1));
  1243. $this->Form->text('Contact.name');
  1244. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1245. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1246. $this->Form->unlockField('Contact.name');
  1247. $this->Form->unlockField('Contact.id');
  1248. $this->assertEquals(array(), $this->Form->fields);
  1249. }
  1250. /**
  1251. * testPasswordValidation method
  1252. *
  1253. * test validation errors on password input.
  1254. *
  1255. * @return void
  1256. */
  1257. public function testPasswordValidation() {
  1258. $Contact = ClassRegistry::getObject('Contact');
  1259. $Contact->validationErrors['password'] = array('Please provide a password');
  1260. $result = $this->Form->input('Contact.password');
  1261. $expected = array(
  1262. 'div' => array('class' => 'input password error'),
  1263. 'label' => array('for' => 'ContactPassword'),
  1264. 'Password',
  1265. '/label',
  1266. 'input' => array(
  1267. 'type' => 'password', 'name' => 'data[Contact][password]',
  1268. 'id' => 'ContactPassword', 'class' => 'form-error'
  1269. ),
  1270. array('div' => array('class' => 'error-message')),
  1271. 'Please provide a password',
  1272. '/div',
  1273. '/div'
  1274. );
  1275. $this->assertTags($result, $expected);
  1276. }
  1277. /**
  1278. * testEmptyErrorValidation method
  1279. *
  1280. * test validation error div when validation message is an empty string
  1281. *
  1282. * @access public
  1283. * @return void
  1284. */
  1285. function testEmptyErrorValidation() {
  1286. $this->Form->validationErrors['Contact']['password'] = '';
  1287. $result = $this->Form->input('Contact.password');
  1288. $expected = array(
  1289. 'div' => array('class' => 'input password error'),
  1290. 'label' => array('for' => 'ContactPassword'),
  1291. 'Password',
  1292. '/label',
  1293. 'input' => array(
  1294. 'type' => 'password', 'name' => 'data[Contact][password]',
  1295. 'id' => 'ContactPassword', 'class' => 'form-error'
  1296. ),
  1297. array('div' => array('class' => 'error-message')),
  1298. array(),
  1299. '/div',
  1300. '/div'
  1301. );
  1302. $this->assertTags($result, $expected);
  1303. }
  1304. /**
  1305. * testEmptyInputErrorValidation method
  1306. *
  1307. * test validation error div when validation message is overriden by an empty string when calling input()
  1308. *
  1309. * @access public
  1310. * @return void
  1311. */
  1312. function testEmptyInputErrorValidation() {
  1313. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1314. $result = $this->Form->input('Contact.password', array('error' => ''));
  1315. $expected = array(
  1316. 'div' => array('class' => 'input password error'),
  1317. 'label' => array('for' => 'ContactPassword'),
  1318. 'Password',
  1319. '/label',
  1320. 'input' => array(
  1321. 'type' => 'password', 'name' => 'data[Contact][password]',
  1322. 'id' => 'ContactPassword', 'class' => 'form-error'
  1323. ),
  1324. array('div' => array('class' => 'error-message')),
  1325. array(),
  1326. '/div',
  1327. '/div'
  1328. );
  1329. $this->assertTags($result, $expected);
  1330. }
  1331. /**
  1332. * testFormValidationAssociated method
  1333. *
  1334. * test display of form errors in conjunction with model::validates.
  1335. *
  1336. * @return void
  1337. */
  1338. public function testFormValidationAssociated() {
  1339. $this->UserForm = ClassRegistry::getObject('UserForm');
  1340. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1341. $data = array(
  1342. 'UserForm' => array('name' => 'user'),
  1343. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1344. );
  1345. $result = $this->UserForm->OpenidUrl->create($data);
  1346. $this->assertFalse(empty($result));
  1347. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1348. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1349. $encoding = strtolower(Configure::read('App.encoding'));
  1350. $expected = array(
  1351. 'form' => array(
  1352. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1353. 'accept-charset' => $encoding
  1354. ),
  1355. 'div' => array('style' => 'display:none;'),
  1356. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1357. '/div'
  1358. );
  1359. $this->assertTags($result, $expected);
  1360. $result = $this->Form->error(
  1361. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1362. );
  1363. $this->assertEqual($result, 'Error, not registered');
  1364. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1365. }
  1366. /**
  1367. * testFormValidationAssociatedFirstLevel method
  1368. *
  1369. * test form error display with associated model.
  1370. *
  1371. * @return void
  1372. */
  1373. public function testFormValidationAssociatedFirstLevel() {
  1374. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1375. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1376. $data = array(
  1377. 'ValidateUser' => array('name' => 'mariano'),
  1378. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1379. );
  1380. $result = $this->ValidateUser->create($data);
  1381. $this->assertFalse(empty($result));
  1382. $this->assertFalse($this->ValidateUser->validates());
  1383. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1384. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1385. $encoding = strtolower(Configure::read('App.encoding'));
  1386. $expected = array(
  1387. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1388. 'div' => array('style' => 'display:none;'),
  1389. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1390. '/div'
  1391. );
  1392. $this->assertTags($result, $expected);
  1393. $result = $this->Form->error(
  1394. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1395. );
  1396. $this->assertEqual($result, 'Invalid email');
  1397. $result = $this->Form->error(
  1398. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1399. );
  1400. $this->assertEqual($result, 'Invalid name');
  1401. $result = $this->Form->error(
  1402. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1403. );
  1404. $this->assertEqual($result, 'Invalid city');
  1405. unset($this->ValidateUser->ValidateProfile);
  1406. unset($this->ValidateUser);
  1407. }
  1408. /**
  1409. * testFormValidationAssociatedSecondLevel method
  1410. *
  1411. * test form error display with associated model.
  1412. *
  1413. * @return void
  1414. */
  1415. public function testFormValidationAssociatedSecondLevel() {
  1416. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1417. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1418. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1419. $data = array(
  1420. 'ValidateUser' => array('name' => 'mariano'),
  1421. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1422. 'ValidateItem' => array('name' => 'Item')
  1423. );
  1424. $result = $this->ValidateUser->create($data);
  1425. $this->assertFalse(empty($result));
  1426. $this->assertFalse($this->ValidateUser->validates());
  1427. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1428. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1429. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1430. $encoding = strtolower(Configure::read('App.encoding'));
  1431. $expected = array(
  1432. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1433. 'div' => array('style' => 'display:none;'),
  1434. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1435. '/div'
  1436. );
  1437. $this->assertTags($result, $expected);
  1438. $result = $this->Form->error(
  1439. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1440. );
  1441. $this->assertEqual($result, 'Invalid email');
  1442. $result = $this->Form->error(
  1443. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1444. );
  1445. $this->assertEqual($result, 'Invalid name');
  1446. $result = $this->Form->error(
  1447. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1448. );
  1449. $result = $this->Form->error(
  1450. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1451. );
  1452. $this->assertEqual($result, 'Invalid description');
  1453. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1454. unset($this->ValidateUser->ValidateProfile);
  1455. unset($this->ValidateUser);
  1456. }
  1457. /**
  1458. * testFormValidationMultiRecord method
  1459. *
  1460. * test form error display with multiple records.
  1461. *
  1462. * @return void
  1463. */
  1464. public function testFormValidationMultiRecord() {
  1465. $Contact = ClassRegistry::getObject('Contact');
  1466. $Contact->validationErrors[2] = array(
  1467. 'name' => array('This field cannot be left blank')
  1468. );
  1469. $result = $this->Form->input('Contact.2.name');
  1470. $expected = array(
  1471. 'div' => array('class' => 'input text error'),
  1472. 'label' => array('for' => 'Contact2Name'),
  1473. 'Name',
  1474. '/label',
  1475. 'input' => array(
  1476. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1477. 'class' => 'form-error', 'maxlength' => 255
  1478. ),
  1479. array('div' => array('class' => 'error-message')),
  1480. 'This field cannot be left blank',
  1481. '/div',
  1482. '/div'
  1483. );
  1484. $this->assertTags($result, $expected);
  1485. }
  1486. /**
  1487. * testMultipleInputValidation method
  1488. *
  1489. * test multiple record form validation error display.
  1490. *
  1491. * @return void
  1492. */
  1493. public function testMultipleInputValidation() {
  1494. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1495. $Address->validationErrors[0] = array(
  1496. 'title' => array('This field cannot be empty'),
  1497. 'first_name' => array('This field cannot be empty')
  1498. );
  1499. $Address->validationErrors[1] = array(
  1500. 'last_name' => array('You must have a last name')
  1501. );
  1502. $this->Form->create();
  1503. $result = $this->Form->input('Address.0.title');
  1504. $expected = array(
  1505. 'div' => array('class'),
  1506. 'label' => array('for'),
  1507. 'preg:/[^<]+/',
  1508. '/label',
  1509. 'input' => array(
  1510. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1511. ),
  1512. array('div' => array('class' => 'error-message')),
  1513. 'This field cannot be empty',
  1514. '/div',
  1515. '/div'
  1516. );
  1517. $this->assertTags($result, $expected);
  1518. $result = $this->Form->input('Address.0.first_name');
  1519. $expected = array(
  1520. 'div' => array('class'),
  1521. 'label' => array('for'),
  1522. 'preg:/[^<]+/',
  1523. '/label',
  1524. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1525. array('div' => array('class' => 'error-message')),
  1526. 'This field cannot be empty',
  1527. '/div',
  1528. '/div'
  1529. );
  1530. $this->assertTags($result, $expected);
  1531. $result = $this->Form->input('Address.0.last_name');
  1532. $expected = array(
  1533. 'div' => array('class'),
  1534. 'label' => array('for'),
  1535. 'preg:/[^<]+/',
  1536. '/label',
  1537. 'input' => array('type' => 'text', 'name', 'id'),
  1538. '/div'
  1539. );
  1540. $this->assertTags($result, $expected);
  1541. $result = $this->Form->input('Address.1.last_name');
  1542. $expected = array(
  1543. 'div' => array('class'),
  1544. 'label' => array('for'),
  1545. 'preg:/[^<]+/',
  1546. '/label',
  1547. 'input' => array(
  1548. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1549. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1550. ),
  1551. array('div' => array('class' => 'error-message')),
  1552. 'You must have a last name',
  1553. '/div',
  1554. '/div'
  1555. );
  1556. $this->assertTags($result, $expected);
  1557. }
  1558. /**
  1559. * testInput method
  1560. *
  1561. * Test various incarnations of input().
  1562. *
  1563. * @return void
  1564. */
  1565. public function testInput() {
  1566. $result = $this->Form->input('ValidateUser.balance');
  1567. $expected = array(
  1568. 'div' => array('class'),
  1569. 'label' => array('for'),
  1570. 'Balance',
  1571. '/label',
  1572. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1573. '/div',
  1574. );
  1575. $this->assertTags($result, $expected);
  1576. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1577. $expected = array(
  1578. 'div' => array('class' => 'input text'),
  1579. 'label' => array('for' => 'custom'),
  1580. 'Email',
  1581. '/label',
  1582. array('input' => array(
  1583. 'type' => 'text', 'name' => 'data[Contact][email]',
  1584. 'id' => 'custom', 'maxlength' => 255
  1585. )),
  1586. '/div'
  1587. );
  1588. $this->assertTags($result, $expected);
  1589. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1590. $expected = array(
  1591. '<div',
  1592. 'label' => array('for' => 'ContactEmail'),
  1593. 'Email',
  1594. '/label',
  1595. array('input' => array(
  1596. 'type' => 'text', 'name' => 'data[Contact][email]',
  1597. 'id' => 'ContactEmail', 'maxlength' => 255
  1598. )),
  1599. '/div'
  1600. );
  1601. $this->assertTags($result, $expected);
  1602. $result = $this->Form->hidden('Contact.idontexist');
  1603. $expected = array('input' => array(
  1604. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1605. 'id' => 'ContactIdontexist'
  1606. ));
  1607. $this->assertTags($result, $expected);
  1608. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1609. $expected = array(
  1610. 'div' => array('class' => 'input text'),
  1611. 'label' => array('for' => 'ContactEmail'),
  1612. 'Email',
  1613. '/label',
  1614. array('input' => array(
  1615. 'type' => 'text', 'name' => 'data[Contact][email]',
  1616. 'id' => 'ContactEmail'
  1617. )),
  1618. '/div'
  1619. );
  1620. $this->assertTags($result, $expected);
  1621. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1622. $expected = array(
  1623. 'div' => array('class' => 'input text'),
  1624. 'label' => array('for' => 'Contact5Email'),
  1625. 'Email',
  1626. '/label',
  1627. array('input' => array(
  1628. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1629. 'id' => 'Contact5Email'
  1630. )),
  1631. '/div'
  1632. );
  1633. $this->assertTags($result, $expected);
  1634. $result = $this->Form->input('Contact.password');
  1635. $expected = array(
  1636. 'div' => array('class' => 'input password'),
  1637. 'label' => array('for' => 'ContactPassword'),
  1638. 'Password',
  1639. '/label',
  1640. array('input' => array(
  1641. 'type' => 'password', 'name' => 'data[Contact][password]',
  1642. 'id' => 'ContactPassword'
  1643. )),
  1644. '/div'
  1645. );
  1646. $this->assertTags($result, $expected);
  1647. $result = $this->Form->input('Contact.email', array(
  1648. 'type' => 'file', 'class' => 'textbox'
  1649. ));
  1650. $expected = array(
  1651. 'div' => array('class' => 'input file'),
  1652. 'label' => array('for' => 'ContactEmail'),
  1653. 'Email',
  1654. '/label',
  1655. array('input' => array(
  1656. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1657. 'id' => 'ContactEmail'
  1658. )),
  1659. '/div'
  1660. );
  1661. $this->assertTags($result, $expected);
  1662. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1663. $result = $this->Form->input('Contact.phone');
  1664. $expected = array(
  1665. 'div' => array('class' => 'input text'),
  1666. 'label' => array('for' => 'ContactPhone'),
  1667. 'Phone',
  1668. '/label',
  1669. array('input' => array(
  1670. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1671. 'value' => 'Hello &amp; World &gt; weird chars',
  1672. 'id' => 'ContactPhone', 'maxlength' => 255
  1673. )),
  1674. '/div'
  1675. );
  1676. $this->assertTags($result, $expected);
  1677. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1678. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1679. $expected = array(
  1680. 'div' => array('class' => 'input text'),
  1681. 'label' => array('for' => 'myId'),
  1682. 'Field',
  1683. '/label',
  1684. 'input' => array(
  1685. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1686. 'value' => 'My value', 'id' => 'myId'
  1687. ),
  1688. '/div'
  1689. );
  1690. $this->assertTags($result, $expected);
  1691. unset($this->Form->request->data);
  1692. $Contact = ClassRegistry::getObject('Contact');
  1693. $Contact->validationErrors['field'] = array('Badness!');
  1694. $result = $this->Form->input('Contact.field');
  1695. $expected = array(
  1696. 'div' => array('class' => 'input text error'),
  1697. 'label' => array('for' => 'ContactField'),
  1698. 'Field',
  1699. '/label',
  1700. 'input' => array(
  1701. 'type' => 'text', 'name' => 'data[Contact][field]',
  1702. 'id' => 'ContactField', 'class' => 'form-error'
  1703. ),
  1704. array('div' => array('class' => 'error-message')),
  1705. 'Badness!',
  1706. '/div',
  1707. '/div'
  1708. );
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->input('Contact.field', array(
  1711. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1712. ));
  1713. $expected = array(
  1714. 'label' => array('for' => 'ContactField'),
  1715. 'Field',
  1716. '/label',
  1717. 'input' => array(
  1718. 'type' => 'text', 'name' => 'data[Contact][field]',
  1719. 'id' => 'ContactField', 'class' => 'form-error'
  1720. ),
  1721. array('span' => array('class' => 'error-message')),
  1722. 'Badness!',
  1723. '/span'
  1724. );
  1725. $this->assertTags($result, $expected);
  1726. $result = $this->Form->input('Contact.field', array(
  1727. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1728. ));
  1729. $expected = array(
  1730. 'div' => array('class' => 'input text error'),
  1731. 'label' => array('for' => 'ContactField'),
  1732. 'Field',
  1733. '/label',
  1734. 'input' => array(
  1735. 'type' => 'text', 'name' => 'data[Contact][field]',
  1736. 'id' => 'ContactField', 'class' => 'form-error'
  1737. ),
  1738. array('div' => array('class' => 'error')),
  1739. 'Badness!',
  1740. '/div'
  1741. );
  1742. $this->assertTags($result, $expected);
  1743. $result = $this->Form->input('Contact.field', array(
  1744. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1745. ));
  1746. $expected = array(
  1747. 'span' => array('class' => 'input text error'),
  1748. 'label' => array('for' => 'ContactField'),
  1749. 'Field',
  1750. '/label',
  1751. 'input' => array(
  1752. 'type' => 'text', 'name' => 'data[Contact][field]',
  1753. 'id' => 'ContactField', 'class' => 'form-error'
  1754. ),
  1755. 'Badness!',
  1756. '/span'
  1757. );
  1758. $this->assertTags($result, $expected);
  1759. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1760. $expected = array(
  1761. 'div' => array('class' => 'input text error'),
  1762. 'label' => array('for' => 'ContactField'),
  1763. 'Field',
  1764. '/label',
  1765. 'input' => array(
  1766. 'type' => 'text', 'name' => 'data[Contact][field]',
  1767. 'id' => 'ContactField', 'class' => 'form-error'
  1768. ),
  1769. 'A message to you, Rudy',
  1770. array('div' => array('class' => 'error-message')),
  1771. 'Badness!',
  1772. '/div',
  1773. '/div'
  1774. );
  1775. $this->assertTags($result, $expected);
  1776. $this->Form->setEntity(null);
  1777. $this->Form->setEntity('Contact.field');
  1778. $result = $this->Form->input('Contact.field', array(
  1779. 'after' => 'A message to you, Rudy', 'error' => false
  1780. ));
  1781. $expected = array(
  1782. 'div' => array('class' => 'input text'),
  1783. 'label' => array('for' => 'ContactField'),
  1784. 'Field',
  1785. '/label',
  1786. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1787. 'A message to you, Rudy',
  1788. '/div'
  1789. );
  1790. $this->assertTags($result, $expected);
  1791. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1792. $expected = array(
  1793. 'div' => array('class' => 'input text'),
  1794. 'label' => array('for' => 'ObjectField'),
  1795. 'Field',
  1796. '/label',
  1797. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1798. 'A message to you, Rudy',
  1799. '/div'
  1800. );
  1801. $this->assertTags($result, $expected);
  1802. $Contact->validationErrors['field'] = array('minLength');
  1803. $result = $this->Form->input('Contact.field', array(
  1804. 'error' => array(
  1805. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1806. 'maxLength' => 'login too large'
  1807. )
  1808. ));
  1809. $expected = array(
  1810. 'div' => array('class' => 'input text error'),
  1811. 'label' => array('for' => 'ContactField'),
  1812. 'Field',
  1813. '/label',
  1814. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1815. array('div' => array('class' => 'error-message')),
  1816. 'Le login doit contenir au moins 2 caractères',
  1817. '/div',
  1818. '/div'
  1819. );
  1820. $this->assertTags($result, $expected);
  1821. $Contact->validationErrors['field'] = array('maxLength');
  1822. $result = $this->Form->input('Contact.field', array(
  1823. 'error' => array(
  1824. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1825. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1826. 'maxLength' => 'login too large',
  1827. )
  1828. ));
  1829. $expected = array(
  1830. 'div' => array('class' => 'input text error'),
  1831. 'label' => array('for' => 'ContactField'),
  1832. 'Field',
  1833. '/label',
  1834. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1835. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1836. 'login too large',
  1837. '/span',
  1838. '/div'
  1839. );
  1840. $this->assertTags($result, $expected);
  1841. }
  1842. /**
  1843. * test input() with checkbox creation
  1844. *
  1845. * @return void
  1846. */
  1847. public function testInputCheckbox() {
  1848. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1849. $expected = array(
  1850. 'div' => array('class' => 'input checkbox'),
  1851. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1852. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1853. '/div'
  1854. );
  1855. $this->assertTags($result, $expected);
  1856. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1857. $expected = array(
  1858. 'div' => array('class' => 'input checkbox'),
  1859. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1860. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1861. '/div'
  1862. );
  1863. $this->assertTags($result, $expected);
  1864. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1865. $expected = array(
  1866. 'div' => array('class' => 'input checkbox'),
  1867. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1868. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1869. '/div'
  1870. );
  1871. $this->assertTags($result, $expected);
  1872. }
  1873. /**
  1874. * test form->input() with time types.
  1875. *
  1876. */
  1877. public function testInputTime() {
  1878. extract($this->dateRegex);
  1879. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1880. $result = explode(':', $result);
  1881. $this->assertPattern('/option value="23"/', $result[0]);
  1882. $this->assertNoPattern('/option value="24"/', $result[0]);
  1883. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1884. $result = explode(':', $result);
  1885. $this->assertPattern('/option value="23"/', $result[0]);
  1886. $this->assertNoPattern('/option value="24"/', $result[0]);
  1887. $result = $this->Form->input('Model.field', array(
  1888. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1889. ));
  1890. $result = explode(':', $result);
  1891. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1892. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1893. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1894. $result = $this->Form->input('Model.field', array(
  1895. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1896. ));
  1897. $result = explode(':', $result);
  1898. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1899. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1900. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1901. $result = $this->Form->input('prueba', array(
  1902. 'type' => 'time', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
  1903. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1904. ));
  1905. $result = explode(':', $result);
  1906. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1907. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1908. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1909. $this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
  1910. $result = $this->Form->input('Random.start_time', array(
  1911. 'type' => 'time',
  1912. 'selected' => '18:15'
  1913. ));
  1914. $this->assertPattern('#<option value="06"[^>]*>6</option>#', $result);
  1915. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result);
  1916. $this->assertPattern('#<option value="pm"[^>]*>pm</option>#', $result);
  1917. }
  1918. /**
  1919. * test form->input() with datetime, date and time types
  1920. *
  1921. * @return void
  1922. */
  1923. function testInputDatetime() {
  1924. extract($this->dateRegex);
  1925. $result = $this->Form->input('prueba', array(
  1926. 'type' => 'datetime', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
  1927. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1928. ));
  1929. $result = explode(':', $result);
  1930. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1931. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1932. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1933. $this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
  1934. //related to ticket #5013
  1935. $result = $this->Form->input('Contact.date', array(
  1936. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  1937. ));
  1938. $this->assertPattern('/class="customClass"/', $result);
  1939. $this->assertPattern('/onChange="function\(\)\{\}"/', $result);
  1940. $result = $this->Form->input('Contact.date', array(
  1941. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  1942. ));
  1943. $this->assertPattern('/id="customIdDay"/', $result);
  1944. $this->assertPattern('/id="customIdMonth"/', $result);
  1945. $this->assertPattern('/onChange="function\(\)\{\}"/', $result);
  1946. $result = $this->Form->input('Model.field', array(
  1947. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  1948. ));
  1949. $this->assertPattern('/id="customIDDay"/', $result);
  1950. $this->assertPattern('/id="customIDHour"/', $result);
  1951. $result = explode('</select><select', $result);
  1952. $result = explode(':', $result[1]);
  1953. $this->assertPattern('/option value="23"/', $result[0]);
  1954. $this->assertNoPattern('/option value="24"/', $result[0]);
  1955. $result = $this->Form->input('Model.field', array(
  1956. 'type' => 'datetime', 'timeFormat' => 12
  1957. ));
  1958. $result = explode('</select><select', $result);
  1959. $result = explode(':', $result[1]);
  1960. $this->assertPattern('/option value="12"/', $result[0]);
  1961. $this->assertNoPattern('/option value="13"/', $result[0]);
  1962. $this->Form->request->data = array('Contact' => array('created' => null));
  1963. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  1964. $expected = array(
  1965. 'div' => array('class' => 'input date'),
  1966. 'label' => array('for' => 'ContactCreatedMonth'),
  1967. 'Created',
  1968. '/label',
  1969. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  1970. array('option' => array('value' => '')), 'Date Unknown', '/option',
  1971. $monthsRegex,
  1972. '/select', '-',
  1973. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  1974. array('option' => array('value' => '')), 'Date Unknown', '/option',
  1975. $daysRegex,
  1976. '/select', '-',
  1977. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  1978. array('option' => array('value' => '')), 'Date Unknown', '/option',
  1979. $yearsRegex,
  1980. '/select',
  1981. '/div'
  1982. );
  1983. $this->assertTags($result, $expected);
  1984. $this->Form->request->data = array('Contact' => array('created' => null));
  1985. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  1986. $this->assertPattern('/for\="ContactCreatedHour"/', $result);
  1987. $this->Form->request->data = array('Contact' => array('created' => null));
  1988. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  1989. $this->assertPattern('/for\="ContactCreatedMonth"/', $result);
  1990. $result = $this->Form->input('Contact.created', array(
  1991. 'type' => 'date',
  1992. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  1993. 'timeFormat' => 'NONE'
  1994. ));
  1995. $this->assertPattern('/for\="created-month"/', $result);
  1996. }
  1997. /**
  1998. * Test generating checkboxes in a loop.
  1999. *
  2000. * @return void
  2001. */
  2002. public function testInputCheckboxesInLoop() {
  2003. for ($i = 1; $i < 5; $i++) {
  2004. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2005. $expected = array(
  2006. 'div' => array('class' => 'input checkbox'),
  2007. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2008. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2009. 'label' => array('for' => "Contact{$i}Email"),
  2010. 'Email',
  2011. '/label',
  2012. '/div'
  2013. );
  2014. $this->assertTags($result, $expected);
  2015. }
  2016. }
  2017. /**
  2018. * test input name with leading integer, ensure attributes are generated correctly.
  2019. *
  2020. * @return void
  2021. */
  2022. public function testInputWithLeadingInteger() {
  2023. $result = $this->Form->text('0.Node.title');
  2024. $expected = array(
  2025. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2026. );
  2027. $this->assertTags($result, $expected);
  2028. }
  2029. /**
  2030. * test form->input() with select type inputs.
  2031. *
  2032. * @return void
  2033. */
  2034. public function testInputSelectType() {
  2035. $result = $this->Form->input('email', array(
  2036. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2037. );
  2038. $expected = array(
  2039. 'div' => array('class' => 'input select'),
  2040. 'label' => array('for' => 'email'),
  2041. 'Email',
  2042. '/label',
  2043. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2044. array('option' => array('value' => '')),
  2045. '/option',
  2046. array('option' => array('value' => 'è')),
  2047. 'Firést',
  2048. '/option',
  2049. array('option' => array('value' => 'é')),
  2050. 'Secoènd',
  2051. '/option',
  2052. '/select',
  2053. '/div'
  2054. );
  2055. $this->assertTags($result, $expected);
  2056. $result = $this->Form->input('email', array(
  2057. 'options' => array('First', 'Second'), 'empty' => true)
  2058. );
  2059. $expected = array(
  2060. 'div' => array('class' => 'input select'),
  2061. 'label' => array('for' => 'email'),
  2062. 'Email',
  2063. '/label',
  2064. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2065. array('option' => array('value' => '')),
  2066. '/option',
  2067. array('option' => array('value' => '0')),
  2068. 'First',
  2069. '/option',
  2070. array('option' => array('value' => '1')),
  2071. 'Second',
  2072. '/option',
  2073. '/select',
  2074. '/div'
  2075. );
  2076. $this->assertTags($result, $expected);
  2077. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2078. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2079. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2080. $expected = array(
  2081. 'div' => array('class' => 'input select'),
  2082. 'label' => array('for' => 'ModelUserId'),
  2083. 'User',
  2084. '/label',
  2085. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2086. array('option' => array('value' => '')),
  2087. '/option',
  2088. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2089. 'good',
  2090. '/option',
  2091. array('option' => array('value' => 'other')),
  2092. 'bad',
  2093. '/option',
  2094. '/select',
  2095. '/div'
  2096. );
  2097. $this->assertTags($result, $expected);
  2098. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2099. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2100. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2101. $expected = array(
  2102. 'div' => array('class' => 'input select'),
  2103. 'label' => array('for' => 'ThingUserId'),
  2104. 'User',
  2105. '/label',
  2106. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2107. array('option' => array('value' => '')),
  2108. 'Some Empty',
  2109. '/option',
  2110. array('option' => array('value' => 'value')),
  2111. 'good',
  2112. '/option',
  2113. array('option' => array('value' => 'other')),
  2114. 'bad',
  2115. '/option',
  2116. '/select',
  2117. '/div'
  2118. );
  2119. $this->assertTags($result, $expected);
  2120. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2121. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2122. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2123. $expected = array(
  2124. 'div' => array('class' => 'input select'),
  2125. 'label' => array('for' => 'ThingUserId'),
  2126. 'User',
  2127. '/label',
  2128. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2129. array('option' => array('value' => '')),
  2130. 'Some Empty',
  2131. '/option',
  2132. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2133. 'good',
  2134. '/option',
  2135. array('option' => array('value' => 'other')),
  2136. 'bad',
  2137. '/option',
  2138. '/select',
  2139. '/div'
  2140. );
  2141. $this->assertTags($result, $expected);
  2142. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2143. $this->Form->request->data = array('User' => array('User' => array('value')));
  2144. $result = $this->Form->input('User.User', array('empty' => true));
  2145. $expected = array(
  2146. 'div' => array('class' => 'input select'),
  2147. 'label' => array('for' => 'UserUser'),
  2148. 'User',
  2149. '/label',
  2150. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2151. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2152. array('option' => array('value' => '')),
  2153. '/option',
  2154. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2155. 'good',
  2156. '/option',
  2157. array('option' => array('value' => 'other')),
  2158. 'bad',
  2159. '/option',
  2160. '/select',
  2161. '/div'
  2162. );
  2163. $this->assertTags($result, $expected);
  2164. $this->Form->data = array();
  2165. $result = $this->Form->input('Publisher.id', array(
  2166. 'label' => 'Publisher',
  2167. 'type' => 'select',
  2168. 'multiple' => 'checkbox',
  2169. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2170. ));
  2171. $expected = array(
  2172. array('div' => array('class' => 'input select')),
  2173. array('label' => array('for' => 'PublisherId')),
  2174. 'Publisher',
  2175. '/label',
  2176. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2177. array('div' => array('class' => 'checkbox')),
  2178. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2179. array('label' => array('for' => 'PublisherIdValue1')),
  2180. 'Label 1',
  2181. '/label',
  2182. '/div',
  2183. array('div' => array('class' => 'checkbox')),
  2184. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2185. array('label' => array('for' => 'PublisherIdValue2')),
  2186. 'Label 2',
  2187. '/label',
  2188. '/div',
  2189. '/div'
  2190. );
  2191. $this->assertTags($result, $expected);
  2192. }
  2193. /**
  2194. * test that input() and a non standard primary key makes a hidden input by default.
  2195. *
  2196. * @return void
  2197. */
  2198. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2199. $this->Form->create('User');
  2200. $this->Form->fieldset = array(
  2201. 'User' => array(
  2202. 'fields' => array(
  2203. 'model_id' => array('type' => 'integer')
  2204. ),
  2205. 'validates' => array(),
  2206. 'key' => 'model_id'
  2207. )
  2208. );
  2209. $result = $this->Form->input('model_id');
  2210. $expected = array(
  2211. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2212. );
  2213. $this->assertTags($result, $expected);
  2214. }
  2215. /**
  2216. * test that overriding the magic select type widget is possible
  2217. *
  2218. * @return void
  2219. */
  2220. public function testInputOverridingMagicSelectType() {
  2221. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2222. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2223. $expected = array(
  2224. 'div' => array('class' => 'input text'),
  2225. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2226. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2227. '/div'
  2228. );
  2229. $this->assertTags($result, $expected);
  2230. //Check that magic types still work for plural/singular vars
  2231. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2232. $result = $this->Form->input('Model.type');
  2233. $expected = array(
  2234. 'div' => array('class' => 'input select'),
  2235. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2236. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2237. array('option' => array('value' => 'value')), 'good', '/option',
  2238. array('option' => array('value' => 'other')), 'bad', '/option',
  2239. '/select',
  2240. '/div'
  2241. );
  2242. $this->assertTags($result, $expected);
  2243. }
  2244. /**
  2245. * Test that magic input() selects can easily be converted into radio types without error.
  2246. *
  2247. * @return void
  2248. */
  2249. public function testInputMagicSelectChangeToRadio() {
  2250. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2251. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2252. $this->assertPattern('/input type="radio"/', $result);
  2253. }
  2254. /**
  2255. * fields with the same name as the model should work.
  2256. *
  2257. * @return void
  2258. */
  2259. public function testInputWithMatchingFieldAndModelName() {
  2260. $this->Form->create('User');
  2261. $this->Form->fieldset = array(
  2262. 'User' => array(
  2263. 'fields' => array(
  2264. 'User' => array('type' => 'text')
  2265. ),
  2266. 'validates' => array(),
  2267. 'key' => 'id'
  2268. )
  2269. );
  2270. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2271. $result = $this->Form->input('User', array('type' => 'text'));
  2272. $expected = array(
  2273. 'div' => array('class' => 'input text'),
  2274. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2275. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2276. '/div'
  2277. );
  2278. $this->assertTags($result, $expected);
  2279. }
  2280. /**
  2281. * testFormInputs method
  2282. *
  2283. * test correct results from form::inputs().
  2284. *
  2285. * @return void
  2286. */
  2287. public function testFormInputs() {
  2288. $this->Form->create('Contact');
  2289. $result = $this->Form->inputs('The Legend');
  2290. $expected = array(
  2291. '<fieldset',
  2292. '<legend',
  2293. 'The Legend',
  2294. '/legend',
  2295. '*/fieldset',
  2296. );
  2297. $this->assertTags($result, $expected);
  2298. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2299. $expected = array(
  2300. 'fieldset' => array('class' => 'classy-stuff'),
  2301. '<legend',
  2302. 'Field of Dreams',
  2303. '/legend',
  2304. '*/fieldset'
  2305. );
  2306. $this->assertTags($result, $expected);
  2307. $this->Form->create('Contact');
  2308. $this->Form->request['prefix'] = 'admin';
  2309. $this->Form->request['action'] = 'admin_edit';
  2310. $result = $this->Form->inputs();
  2311. $expected = array(
  2312. '<fieldset',
  2313. '<legend',
  2314. 'Edit Contact',
  2315. '/legend',
  2316. '*/fieldset',
  2317. );
  2318. $this->assertTags($result, $expected);
  2319. $this->Form->create('Contact');
  2320. $result = $this->Form->inputs(false);
  2321. $expected = array(
  2322. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2323. array('div' => array('class' => 'input text')),
  2324. '*/div',
  2325. array('div' => array('class' => 'input text')),
  2326. '*/div',
  2327. array('div' => array('class' => 'input text')),
  2328. '*/div',
  2329. array('div' => array('class' => 'input password')),
  2330. '*/div',
  2331. array('div' => array('class' => 'input date')),
  2332. '*/div',
  2333. array('div' => array('class' => 'input date')),
  2334. '*/div',
  2335. array('div' => array('class' => 'input datetime')),
  2336. '*/div',
  2337. array('div' => array('class' => 'input number')),
  2338. '*/div',
  2339. array('div' => array('class' => 'input select')),
  2340. '*/div',
  2341. );
  2342. $this->assertTags($result, $expected);
  2343. $this->Form->create('Contact');
  2344. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2345. $expected = array(
  2346. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2347. array('div' => array('class' => 'input text')),
  2348. '*/div',
  2349. array('div' => array('class' => 'input text')),
  2350. '*/div',
  2351. array('div' => array('class' => 'input text')),
  2352. '*/div',
  2353. array('div' => array('class' => 'input password')),
  2354. '*/div',
  2355. array('div' => array('class' => 'input date')),
  2356. '*/div',
  2357. array('div' => array('class' => 'input date')),
  2358. '*/div',
  2359. array('div' => array('class' => 'input datetime')),
  2360. '*/div',
  2361. array('div' => array('class' => 'input number')),
  2362. '*/div',
  2363. array('div' => array('class' => 'input select')),
  2364. '*/div',
  2365. );
  2366. $this->assertTags($result, $expected);
  2367. $this->Form->create('Contact');
  2368. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2369. $expected = array(
  2370. 'fieldset' => array(),
  2371. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2372. array('div' => array('class' => 'input text')),
  2373. '*/div',
  2374. array('div' => array('class' => 'input text')),
  2375. '*/div',
  2376. array('div' => array('class' => 'input text')),
  2377. '*/div',
  2378. array('div' => array('class' => 'input password')),
  2379. '*/div',
  2380. array('div' => array('class' => 'input date')),
  2381. '*/div',
  2382. array('div' => array('class' => 'input date')),
  2383. '*/div',
  2384. array('div' => array('class' => 'input datetime')),
  2385. '*/div',
  2386. array('div' => array('class' => 'input number')),
  2387. '*/div',
  2388. array('div' => array('class' => 'input select')),
  2389. '*/div',
  2390. '/fieldset'
  2391. );
  2392. $this->assertTags($result, $expected);
  2393. $this->Form->create('Contact');
  2394. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2395. $expected = array(
  2396. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2397. array('div' => array('class' => 'input text')),
  2398. '*/div',
  2399. array('div' => array('class' => 'input text')),
  2400. '*/div',
  2401. array('div' => array('class' => 'input text')),
  2402. '*/div',
  2403. array('div' => array('class' => 'input password')),
  2404. '*/div',
  2405. array('div' => array('class' => 'input date')),
  2406. '*/div',
  2407. array('div' => array('class' => 'input date')),
  2408. '*/div',
  2409. array('div' => array('class' => 'input datetime')),
  2410. '*/div',
  2411. array('div' => array('class' => 'input number')),
  2412. '*/div',
  2413. array('div' => array('class' => 'input select')),
  2414. '*/div',
  2415. );
  2416. $this->assertTags($result, $expected);
  2417. $this->Form->create('Contact');
  2418. $result = $this->Form->inputs('Hello');
  2419. $expected = array(
  2420. 'fieldset' => array(),
  2421. 'legend' => array(),
  2422. 'Hello',
  2423. '/legend',
  2424. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2425. array('div' => array('class' => 'input text')),
  2426. '*/div',
  2427. array('div' => array('class' => 'input text')),
  2428. '*/div',
  2429. array('div' => array('class' => 'input text')),
  2430. '*/div',
  2431. array('div' => array('class' => 'input password')),
  2432. '*/div',
  2433. array('div' => array('class' => 'input date')),
  2434. '*/div',
  2435. array('div' => array('class' => 'input date')),
  2436. '*/div',
  2437. array('div' => array('class' => 'input datetime')),
  2438. '*/div',
  2439. array('div' => array('class' => 'input number')),
  2440. '*/div',
  2441. array('div' => array('class' => 'input select')),
  2442. '*/div',
  2443. '/fieldset'
  2444. );
  2445. $this->assertTags($result, $expected);
  2446. $this->Form->create('Contact');
  2447. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2448. $expected = array(
  2449. 'fieldset' => array(),
  2450. 'legend' => array(),
  2451. 'Hello',
  2452. '/legend',
  2453. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2454. array('div' => array('class' => 'input text')),
  2455. '*/div',
  2456. array('div' => array('class' => 'input text')),
  2457. '*/div',
  2458. array('div' => array('class' => 'input text')),
  2459. '*/div',
  2460. array('div' => array('class' => 'input password')),
  2461. '*/div',
  2462. array('div' => array('class' => 'input date')),
  2463. '*/div',
  2464. array('div' => array('class' => 'input date')),
  2465. '*/div',
  2466. array('div' => array('class' => 'input datetime')),
  2467. '*/div',
  2468. array('div' => array('class' => 'input number')),
  2469. '*/div',
  2470. array('div' => array('class' => 'input select')),
  2471. '*/div',
  2472. '/fieldset'
  2473. );
  2474. $this->assertTags($result, $expected);
  2475. }
  2476. /**
  2477. * testSelectAsCheckbox method
  2478. *
  2479. * test multi-select widget with checkbox formatting.
  2480. *
  2481. * @return void
  2482. */
  2483. public function testSelectAsCheckbox() {
  2484. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2485. $expected = array(
  2486. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2487. array('div' => array('class' => 'checkbox')),
  2488. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2489. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2490. 'first',
  2491. '/label',
  2492. '/div',
  2493. array('div' => array('class' => 'checkbox')),
  2494. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2495. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2496. 'second',
  2497. '/label',
  2498. '/div',
  2499. array('div' => array('class' => 'checkbox')),
  2500. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2501. array('label' => array('for' => 'ModelMultiField2')),
  2502. 'third',
  2503. '/label',
  2504. '/div',
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2508. $expected = array(
  2509. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2510. array('div' => array('class' => 'checkbox')),
  2511. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2512. array('label' => array('for' => 'ModelMultiField12')),
  2513. 'half',
  2514. '/label',
  2515. '/div',
  2516. );
  2517. $this->assertTags($result, $expected);
  2518. }
  2519. /**
  2520. * testLabel method
  2521. *
  2522. * test label generation.
  2523. *
  2524. * @return void
  2525. */
  2526. public function testLabel() {
  2527. $this->Form->text('Person.name');
  2528. $result = $this->Form->label();
  2529. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2530. $this->Form->text('Person.name');
  2531. $result = $this->Form->label();
  2532. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2533. $result = $this->Form->label('Person.first_name');
  2534. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2535. $result = $this->Form->label('Person.first_name', 'Your first name');
  2536. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2537. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2538. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2539. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2540. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2541. $result = $this->Form->label('Person.first_name', '');
  2542. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2543. $result = $this->Form->label('Person.2.name', '');
  2544. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2545. }
  2546. /**
  2547. * testTextbox method
  2548. *
  2549. * test textbox element generation
  2550. *
  2551. * @return void
  2552. */
  2553. public function testTextbox() {
  2554. $result = $this->Form->text('Model.field');
  2555. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2556. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2557. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2558. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2559. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2560. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2561. $result = $this->Form->text('Model.text');
  2562. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2563. $Contact = ClassRegistry::getObject('Contact');
  2564. $Contact->validationErrors['text'] = array(true);
  2565. $this->Form->request->data['Contact']['text'] = 'test';
  2566. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2567. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2568. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2569. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2570. $expected = array(
  2571. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2572. );
  2573. $this->assertTags($result, $expected);
  2574. }
  2575. /**
  2576. * testDefaultValue method
  2577. *
  2578. * Test default value setting
  2579. *
  2580. * @return void
  2581. */
  2582. public function testDefaultValue() {
  2583. $this->Form->request->data['Model']['field'] = 'test';
  2584. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2585. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2586. unset($this->Form->request->data['Model']['field']);
  2587. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2588. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2589. }
  2590. /**
  2591. * testCheckboxDefaultValue method
  2592. *
  2593. * Test default value setting on checkbox() method
  2594. *
  2595. * @return void
  2596. */
  2597. public function testCheckboxDefaultValue() {
  2598. $this->Form->request->data['Model']['field'] = false;
  2599. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2600. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2601. unset($this->Form->request->data['Model']['field']);
  2602. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2603. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2604. $this->Form->request->data['Model']['field'] = true;
  2605. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2606. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2607. unset($this->Form->request->data['Model']['field']);
  2608. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2609. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2610. }
  2611. /**
  2612. * testError method
  2613. *
  2614. * Test field error generation
  2615. *
  2616. * @return void
  2617. */
  2618. public function testError() {
  2619. $Contact = ClassRegistry::getObject('Contact');
  2620. $Contact->validationErrors['field'] = array(1);
  2621. $result = $this->Form->error('Contact.field');
  2622. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2623. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2624. $this->assertEqual($result, 'Error in field Field');
  2625. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2626. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2627. $this->assertEqual($result, 'This field contains invalid input');
  2628. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2629. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2630. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2631. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2632. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2633. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2634. $this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
  2635. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2636. $this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
  2637. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2638. $this->assertEqual($result, '<strong>Badness!</strong>');
  2639. $Contact->validationErrors['field'] = array("email");
  2640. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2641. $expected = array(
  2642. 'div' => array('class' => 'field-error'),
  2643. 'No good!',
  2644. '/div'
  2645. );
  2646. $this->assertTags($result, $expected);
  2647. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2648. $result = $this->Form->error('Contact.field', array(
  2649. 'notEmpty' => 'Cannot be empty',
  2650. 'email' => 'No good!'
  2651. ));
  2652. $expected = array(
  2653. 'div' => array('class' => 'error-message'),
  2654. 'ul' => array(),
  2655. '<li', 'Cannot be empty', '/li',
  2656. '<li', 'No good!', '/li',
  2657. '<li', 'Something else', '/li',
  2658. '/ul',
  2659. '/div'
  2660. );
  2661. $this->assertTags($result, $expected);
  2662. /** Testing error messages list options **/
  2663. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2664. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2665. $expected = array(
  2666. 'div' => array('class' => 'error-message'),
  2667. 'ol' => array(),
  2668. '<li', 'notEmpty', '/li',
  2669. '<li', 'email', '/li',
  2670. '/ol',
  2671. '/div'
  2672. );
  2673. $this->assertTags($result, $expected);
  2674. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2675. $expected = array(
  2676. 'div' => array('class' => 'error-message'),
  2677. 'ol' => array(),
  2678. '<li', 'notEmpty', '/li',
  2679. '<li', 'email', '/li',
  2680. '/ol',
  2681. '/div'
  2682. );
  2683. $this->assertTags($result, $expected);
  2684. $result = $this->Form->error('Contact.field', null, array(
  2685. 'listOptions' => array(
  2686. 'class' => 'ul-class',
  2687. 'itemOptions' => array(
  2688. 'class' => 'li-class'
  2689. )
  2690. )
  2691. ));
  2692. $expected = array(
  2693. 'div' => array('class' => 'error-message'),
  2694. 'ul' => array('class' => 'ul-class'),
  2695. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2696. array('li' => array('class' => 'li-class')), 'email', '/li',
  2697. '/ul',
  2698. '/div'
  2699. );
  2700. $this->assertTags($result, $expected);
  2701. }
  2702. /**
  2703. * test error options when using form->input();
  2704. *
  2705. * @return void
  2706. */
  2707. public function testInputErrorEscape() {
  2708. $this->Form->create('ValidateProfile');
  2709. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2710. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2711. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2712. $this->assertPattern('/required&lt;br&gt;/', $result);
  2713. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2714. $this->assertPattern('/required<br>/', $result);
  2715. }
  2716. /**
  2717. * testPassword method
  2718. *
  2719. * Test password element generation
  2720. *
  2721. * @return void
  2722. */
  2723. public function testPassword() {
  2724. $Contact = ClassRegistry::getObject('Contact');
  2725. $result = $this->Form->password('Contact.field');
  2726. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2727. $Contact->validationErrors['passwd'] = 1;
  2728. $this->Form->request->data['Contact']['passwd'] = 'test';
  2729. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2730. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2731. }
  2732. /**
  2733. * testRadio method
  2734. *
  2735. * Test radio element set generation
  2736. *
  2737. * @return void
  2738. */
  2739. public function testRadio() {
  2740. $result = $this->Form->radio('Model.field', array('option A'));
  2741. $expected = array(
  2742. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2743. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2744. 'label' => array('for' => 'ModelField0'),
  2745. 'option A',
  2746. '/label'
  2747. );
  2748. $this->assertTags($result, $expected);
  2749. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2750. $expected = array(
  2751. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2752. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2753. 'label' => array('for' => 'ModelField12'),
  2754. 'half',
  2755. '/label'
  2756. );
  2757. $this->assertTags($result, $expected);
  2758. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2759. $expected = array(
  2760. 'fieldset' => array(),
  2761. 'legend' => array(),
  2762. 'Field',
  2763. '/legend',
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2765. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2766. array('label' => array('for' => 'ModelField0')),
  2767. 'option A',
  2768. '/label',
  2769. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2770. array('label' => array('for' => 'ModelField1')),
  2771. 'option B',
  2772. '/label',
  2773. '/fieldset'
  2774. );
  2775. $this->assertTags($result, $expected);
  2776. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2777. $expected = array(
  2778. 'fieldset' => array(),
  2779. 'legend' => array(),
  2780. 'Field',
  2781. '/legend',
  2782. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2783. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2784. array('label' => array('for' => 'ModelField0')),
  2785. 'option A',
  2786. '/label',
  2787. 'br' => array(),
  2788. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2789. array('label' => array('for' => 'ModelField1')),
  2790. 'option B',
  2791. '/label',
  2792. '/fieldset'
  2793. );
  2794. $this->assertTags($result, $expected);
  2795. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2796. $expected = array(
  2797. 'fieldset' => array(),
  2798. 'legend' => array(),
  2799. 'Field',
  2800. '/legend',
  2801. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2802. array('label' => array('for' => 'ModelField1')),
  2803. 'Yes',
  2804. '/label',
  2805. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2806. array('label' => array('for' => 'ModelField0')),
  2807. 'No',
  2808. '/label',
  2809. '/fieldset'
  2810. );
  2811. $this->assertTags($result, $expected);
  2812. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2813. $expected = array(
  2814. 'fieldset' => array(),
  2815. 'legend' => array(),
  2816. 'Field',
  2817. '/legend',
  2818. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2819. array('label' => array('for' => 'ModelField1')),
  2820. 'Yes',
  2821. '/label',
  2822. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2823. array('label' => array('for' => 'ModelField0')),
  2824. 'No',
  2825. '/label',
  2826. '/fieldset'
  2827. );
  2828. $this->assertTags($result, $expected);
  2829. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2830. $expected = array(
  2831. 'fieldset' => array(),
  2832. 'legend' => array(),
  2833. 'Field',
  2834. '/legend',
  2835. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2836. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2837. array('label' => array('for' => 'ModelField1')),
  2838. 'Yes',
  2839. '/label',
  2840. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2841. array('label' => array('for' => 'ModelField0')),
  2842. 'No',
  2843. '/label',
  2844. '/fieldset'
  2845. );
  2846. $this->assertTags($result, $expected);
  2847. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  2848. $expected = array(
  2849. 'fieldset' => array(),
  2850. 'legend' => array(),
  2851. 'Field',
  2852. '/legend',
  2853. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2854. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2855. array('label' => array('for' => 'ModelField1')),
  2856. 'Yes',
  2857. '/label',
  2858. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2859. array('label' => array('for' => 'ModelField0')),
  2860. 'No',
  2861. '/label',
  2862. '/fieldset'
  2863. );
  2864. $this->assertTags($result, $expected);
  2865. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2866. $expected = array(
  2867. 'div' => array('class' => 'input radio'),
  2868. 'fieldset' => array(),
  2869. 'legend' => array(),
  2870. 'Legend title',
  2871. '/legend',
  2872. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2873. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2874. array('label' => array('for' => 'NewsletterSubscribe0')),
  2875. 'Unsubscribe',
  2876. '/label',
  2877. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2878. array('label' => array('for' => 'NewsletterSubscribe1')),
  2879. 'Subscribe',
  2880. '/label',
  2881. '/fieldset',
  2882. '/div'
  2883. );
  2884. $this->assertTags($result, $expected);
  2885. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2886. $expected = array(
  2887. 'div' => array('class' => 'input radio'),
  2888. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2889. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2890. array('label' => array('for' => 'NewsletterSubscribe0')),
  2891. 'Unsubscribe',
  2892. '/label',
  2893. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2894. array('label' => array('for' => 'NewsletterSubscribe1')),
  2895. 'Subscribe',
  2896. '/label',
  2897. '/div'
  2898. );
  2899. $this->assertTags($result, $expected);
  2900. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2901. $expected = array(
  2902. 'div' => array('class' => 'input radio'),
  2903. 'fieldset' => array(),
  2904. 'legend' => array(),
  2905. 'Legend title',
  2906. '/legend',
  2907. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2908. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2909. 'Unsubscribe',
  2910. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2911. 'Subscribe',
  2912. '/fieldset',
  2913. '/div'
  2914. );
  2915. $this->assertTags($result, $expected);
  2916. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2917. $expected = array(
  2918. 'div' => array('class' => 'input radio'),
  2919. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2920. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2921. 'Unsubscribe',
  2922. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2923. 'Subscribe',
  2924. '/div'
  2925. );
  2926. $this->assertTags($result, $expected);
  2927. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2928. $expected = array(
  2929. 'div' => array('class' => 'input radio'),
  2930. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2931. 'Unsubscribe',
  2932. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  2933. 'Subscribe',
  2934. '/div'
  2935. );
  2936. $this->assertTags($result, $expected);
  2937. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2938. $expected = array(
  2939. 'fieldset' => array(),
  2940. 'legend' => array(),
  2941. 'Gender',
  2942. '/legend',
  2943. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  2944. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  2945. array('label' => array('for' => 'EmployeeGenderMale')),
  2946. 'Male',
  2947. '/label',
  2948. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  2949. array('label' => array('for' => 'EmployeeGenderFemale')),
  2950. 'Female',
  2951. '/label',
  2952. '/fieldset',
  2953. );
  2954. $this->assertTags($result, $expected);
  2955. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  2956. $expected = array(
  2957. 'fieldset' => array(),
  2958. 'legend' => array(),
  2959. 'Gender',
  2960. '/legend',
  2961. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  2962. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  2963. array('label' => array('for' => 'OfficerGenderMale')),
  2964. 'Male',
  2965. '/label',
  2966. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  2967. array('label' => array('for' => 'OfficerGenderFemale')),
  2968. 'Female',
  2969. '/label',
  2970. '/fieldset',
  2971. );
  2972. $this->assertTags($result, $expected);
  2973. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  2974. $expected = array(
  2975. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  2976. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  2977. 'label' => array('for' => 'Contact1Imrequired0'),
  2978. 'option A',
  2979. '/label'
  2980. );
  2981. $this->assertTags($result, $expected);
  2982. $result = $this->Form->radio('Model.1.field', array('option A'));
  2983. $expected = array(
  2984. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  2985. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  2986. 'label' => array('for' => 'Model1Field0'),
  2987. 'option A',
  2988. '/label'
  2989. );
  2990. $this->assertTags($result, $expected);
  2991. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  2992. $expected = array(
  2993. 'fieldset' => array(),
  2994. 'legend' => array(),
  2995. 'Field',
  2996. '/legend',
  2997. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  2998. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  2999. array('label' => array('for' => 'ModelField0')),
  3000. 'option A',
  3001. '/label',
  3002. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3003. array('label' => array('for' => 'ModelField1')),
  3004. 'option B',
  3005. '/label',
  3006. '/fieldset'
  3007. );
  3008. $this->assertTags($result, $expected);
  3009. }
  3010. /**
  3011. * test disabled radio options
  3012. *
  3013. * @return void
  3014. */
  3015. public function testRadioDisabled() {
  3016. $result = $this->Form->radio(
  3017. 'Model.field',
  3018. array('option A', 'option B'),
  3019. array('disabled' => array('option A'), 'value' => 'option A')
  3020. );
  3021. $expected = array(
  3022. 'fieldset' => array(),
  3023. 'legend' => array(),
  3024. 'Field',
  3025. '/legend',
  3026. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3027. array('label' => array('for' => 'ModelField0')),
  3028. 'option A',
  3029. '/label',
  3030. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3031. array('label' => array('for' => 'ModelField1')),
  3032. 'option B',
  3033. '/label',
  3034. '/fieldset'
  3035. );
  3036. $this->assertTags($result, $expected);
  3037. }
  3038. /**
  3039. * test disabling the hidden input for radio buttons
  3040. *
  3041. * @return void
  3042. */
  3043. public function testRadioHiddenInputDisabling() {
  3044. $result = $this->Form->input('Model.1.field', array(
  3045. 'type' => 'radio',
  3046. 'options' => array('option A'),
  3047. 'hiddenField' => false
  3048. )
  3049. );
  3050. $expected = array(
  3051. 'div' => array('class' => 'input radio'),
  3052. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3053. 'label' => array('for' => 'Model1Field0'),
  3054. 'option A',
  3055. '/label',
  3056. '/div'
  3057. );
  3058. $this->assertTags($result, $expected);
  3059. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3060. $expected = array(
  3061. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3062. 'label' => array('for' => 'Model1Field0'),
  3063. 'option A',
  3064. '/label'
  3065. );
  3066. $this->assertTags($result, $expected);
  3067. }
  3068. /**
  3069. * testSelect method
  3070. *
  3071. * Test select element generation.
  3072. *
  3073. * @return void
  3074. */
  3075. public function testSelect() {
  3076. $result = $this->Form->select('Model.field', array());
  3077. $expected = array(
  3078. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3079. array('option' => array('value' => '')),
  3080. '/option',
  3081. '/select'
  3082. );
  3083. $this->assertTags($result, $expected);
  3084. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3085. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3086. $expected = array(
  3087. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3088. array('option' => array('value' => '')),
  3089. '/option',
  3090. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3091. 'good',
  3092. '/option',
  3093. array('option' => array('value' => 'other')),
  3094. 'bad',
  3095. '/option',
  3096. '/select'
  3097. );
  3098. $this->assertTags($result, $expected);
  3099. $this->Form->request->data = array();
  3100. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3101. $expected = array(
  3102. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3103. array('option' => array('value' => '')),
  3104. '/option',
  3105. array('option' => array('value' => 'value')),
  3106. 'good',
  3107. '/option',
  3108. array('option' => array('value' => 'other')),
  3109. 'bad',
  3110. '/option',
  3111. '/select'
  3112. );
  3113. $this->assertTags($result, $expected);
  3114. $result = $this->Form->select(
  3115. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3116. array('empty' => false)
  3117. );
  3118. $expected = array(
  3119. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3120. array('option' => array('value' => 'first')),
  3121. 'first &quot;html&quot; &lt;chars&gt;',
  3122. '/option',
  3123. array('option' => array('value' => 'second')),
  3124. 'value',
  3125. '/option',
  3126. '/select'
  3127. );
  3128. $this->assertTags($result, $expected);
  3129. $result = $this->Form->select(
  3130. 'Model.field',
  3131. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3132. array('escape' => false, 'empty' => false)
  3133. );
  3134. $expected = array(
  3135. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3136. array('option' => array('value' => 'first')),
  3137. 'first "html" <chars>',
  3138. '/option',
  3139. array('option' => array('value' => 'second')),
  3140. 'value',
  3141. '/option',
  3142. '/select'
  3143. );
  3144. $this->assertTags($result, $expected);
  3145. $options = array(
  3146. array('value' => 'first', 'name' => 'First'),
  3147. array('value' => 'first', 'name' => 'Another First'),
  3148. );
  3149. $result = $this->Form->select(
  3150. 'Model.field',
  3151. $options,
  3152. array('escape' => false, 'empty' => false)
  3153. );
  3154. $expected = array(
  3155. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3156. array('option' => array('value' => 'first')),
  3157. 'First',
  3158. '/option',
  3159. array('option' => array('value' => 'first')),
  3160. 'Another First',
  3161. '/option',
  3162. '/select'
  3163. );
  3164. $this->assertTags($result, $expected);
  3165. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3166. $result = $this->Form->select(
  3167. 'Model.contact_id',
  3168. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3169. array('escape' => false, 'empty' => 'pick something')
  3170. );
  3171. $expected = array(
  3172. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3173. array('option' => array('value' => '')), 'pick something', '/option',
  3174. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3175. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3176. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3177. '/select'
  3178. );
  3179. $this->assertTags($result, $expected);
  3180. }
  3181. /**
  3182. * test that select() with optiongroups listens to the escape param.
  3183. *
  3184. * @return void
  3185. */
  3186. public function testSelectOptionGroupEscaping() {
  3187. $options = array(
  3188. '>< Key' => array(
  3189. 1 => 'One',
  3190. 2 => 'Two'
  3191. )
  3192. );
  3193. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3194. $expected = array(
  3195. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3196. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3197. array('option' => array('value' => '1')), 'One', '/option',
  3198. array('option' => array('value' => '2')), 'Two', '/option',
  3199. '/optgroup',
  3200. '/select'
  3201. );
  3202. $this->assertTags($result, $expected);
  3203. $options = array(
  3204. '>< Key' => array(
  3205. 1 => 'One',
  3206. 2 => 'Two'
  3207. )
  3208. );
  3209. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3210. $expected = array(
  3211. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3212. 'optgroup' => array('label' => '>< Key'),
  3213. array('option' => array('value' => '1')), 'One', '/option',
  3214. array('option' => array('value' => '2')), 'Two', '/option',
  3215. '/optgroup',
  3216. '/select'
  3217. );
  3218. $this->assertTags($result, $expected);
  3219. }
  3220. /**
  3221. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3222. *
  3223. * @return void
  3224. */
  3225. public function testSelectWithNullAttributes() {
  3226. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3227. $expected = array(
  3228. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3229. array('option' => array('value' => '0')),
  3230. 'first',
  3231. '/option',
  3232. array('option' => array('value' => '1')),
  3233. 'second',
  3234. '/option',
  3235. '/select'
  3236. );
  3237. $this->assertTags($result, $expected);
  3238. }
  3239. /**
  3240. * testNestedSelect method
  3241. *
  3242. * test select element generation with optgroups
  3243. *
  3244. * @return void
  3245. */
  3246. public function testNestedSelect() {
  3247. $result = $this->Form->select(
  3248. 'Model.field',
  3249. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3250. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3251. )), array('empty' => false)
  3252. );
  3253. $expected = array(
  3254. 'select' => array('name' => 'data[Model][field]',
  3255. 'id' => 'ModelField'),
  3256. array('option' => array('value' => 1)),
  3257. 'One',
  3258. '/option',
  3259. array('option' => array('value' => 2)),
  3260. 'Two',
  3261. '/option',
  3262. array('optgroup' => array('label' => 'Three')),
  3263. array('option' => array('value' => 4)),
  3264. 'Four',
  3265. '/option',
  3266. array('option' => array('value' => 5)),
  3267. 'Five',
  3268. '/option',
  3269. '/optgroup',
  3270. '/select'
  3271. );
  3272. $this->assertTags($result, $expected);
  3273. $result = $this->Form->select(
  3274. 'Model.field',
  3275. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3276. array('showParents' => true, 'empty' => false)
  3277. );
  3278. $expected = array(
  3279. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3280. array('option' => array('value' => 1)),
  3281. 'One',
  3282. '/option',
  3283. array('option' => array('value' => 2)),
  3284. 'Two',
  3285. '/option',
  3286. array('optgroup' => array('label' => 'Three')),
  3287. array('option' => array('value' => 3)),
  3288. 'Three',
  3289. '/option',
  3290. array('option' => array('value' => 4)),
  3291. 'Four',
  3292. '/option',
  3293. '/optgroup',
  3294. '/select'
  3295. );
  3296. $this->assertTags($result, $expected);
  3297. }
  3298. /**
  3299. * testSelectMultiple method
  3300. *
  3301. * test generation of multiple select elements
  3302. *
  3303. * @return void
  3304. */
  3305. public function testSelectMultiple() {
  3306. $options = array('first', 'second', 'third');
  3307. $result = $this->Form->select(
  3308. 'Model.multi_field', $options, array('multiple' => true)
  3309. );
  3310. $expected = array(
  3311. 'input' => array(
  3312. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3313. ),
  3314. 'select' => array(
  3315. 'name' => 'data[Model][multi_field][]',
  3316. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3317. ),
  3318. array('option' => array('value' => '0')),
  3319. 'first',
  3320. '/option',
  3321. array('option' => array('value' => '1')),
  3322. 'second',
  3323. '/option',
  3324. array('option' => array('value' => '2')),
  3325. 'third',
  3326. '/option',
  3327. '/select'
  3328. );
  3329. $this->assertTags($result, $expected);
  3330. $result = $this->Form->select(
  3331. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3332. );
  3333. $expected = array(
  3334. 'input' => array(
  3335. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3336. ),
  3337. 'select' => array(
  3338. 'name' => 'data[Model][multi_field][]',
  3339. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3340. ),
  3341. array('option' => array('value' => '0')),
  3342. 'first',
  3343. '/option',
  3344. array('option' => array('value' => '1')),
  3345. 'second',
  3346. '/option',
  3347. array('option' => array('value' => '2')),
  3348. 'third',
  3349. '/option',
  3350. '/select'
  3351. );
  3352. $this->assertTags($result, $expected);
  3353. $result = $this->Form->select(
  3354. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3355. );
  3356. $expected = array(
  3357. 'input' => array(
  3358. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3359. ),
  3360. 'select' => array(
  3361. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3362. 'multiple' => 'multiple'
  3363. ),
  3364. array('option' => array('value' => '0', 'selected' => 'selected')),
  3365. 'first',
  3366. '/option',
  3367. array('option' => array('value' => '1', 'selected' => 'selected')),
  3368. 'second',
  3369. '/option',
  3370. array('option' => array('value' => '2')),
  3371. 'third',
  3372. '/option',
  3373. '/select'
  3374. );
  3375. $this->assertTags($result, $expected);
  3376. $result = $this->Form->select(
  3377. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3378. );
  3379. $expected = array(
  3380. 'select' => array(
  3381. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3382. ),
  3383. array('option' => array('value' => '0', 'selected' => 'selected')),
  3384. 'first',
  3385. '/option',
  3386. array('option' => array('value' => '1', 'selected' => 'selected')),
  3387. 'second',
  3388. '/option',
  3389. array('option' => array('value' => '2')),
  3390. 'third',
  3391. '/option',
  3392. '/select'
  3393. );
  3394. $this->assertTags($result, $expected);
  3395. }
  3396. /**
  3397. * test generation of habtm select boxes.
  3398. *
  3399. * @return void
  3400. */
  3401. public function testHabtmSelectBox() {
  3402. $this->View->viewVars['contactTags'] = array(
  3403. 1 => 'blue',
  3404. 2 => 'red',
  3405. 3 => 'green'
  3406. );
  3407. $this->Form->request->data = array(
  3408. 'Contact' => array(),
  3409. 'ContactTag' => array(
  3410. array(
  3411. 'id' => 1,
  3412. 'name' => 'blue'
  3413. ),
  3414. array(
  3415. 'id' => 3,
  3416. 'name' => 'green'
  3417. )
  3418. )
  3419. );
  3420. $this->Form->create('Contact');
  3421. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3422. $expected = array(
  3423. 'input' => array(
  3424. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3425. ),
  3426. 'select' => array(
  3427. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3428. 'multiple' => 'multiple'
  3429. ),
  3430. array('option' => array('value' => '1', 'selected' => 'selected')),
  3431. 'blue',
  3432. '/option',
  3433. array('option' => array('value' => '2')),
  3434. 'red',
  3435. '/option',
  3436. array('option' => array('value' => '3', 'selected' => 'selected')),
  3437. 'green',
  3438. '/option',
  3439. '/select'
  3440. );
  3441. $this->assertTags($result, $expected);
  3442. }
  3443. /**
  3444. * test generation of multi select elements in checkbox format
  3445. *
  3446. * @return void
  3447. */
  3448. public function testSelectMultipleCheckboxes() {
  3449. $result = $this->Form->select(
  3450. 'Model.multi_field',
  3451. array('first', 'second', 'third'),
  3452. array('multiple' => 'checkbox')
  3453. );
  3454. $expected = array(
  3455. 'input' => array(
  3456. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3457. ),
  3458. array('div' => array('class' => 'checkbox')),
  3459. array('input' => array(
  3460. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3461. 'value' => '0', 'id' => 'ModelMultiField0'
  3462. )),
  3463. array('label' => array('for' => 'ModelMultiField0')),
  3464. 'first',
  3465. '/label',
  3466. '/div',
  3467. array('div' => array('class' => 'checkbox')),
  3468. array('input' => array(
  3469. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3470. 'value' => '1', 'id' => 'ModelMultiField1'
  3471. )),
  3472. array('label' => array('for' => 'ModelMultiField1')),
  3473. 'second',
  3474. '/label',
  3475. '/div',
  3476. array('div' => array('class' => 'checkbox')),
  3477. array('input' => array(
  3478. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3479. 'value' => '2', 'id' => 'ModelMultiField2'
  3480. )),
  3481. array('label' => array('for' => 'ModelMultiField2')),
  3482. 'third',
  3483. '/label',
  3484. '/div'
  3485. );
  3486. $this->assertTags($result, $expected);
  3487. $result = $this->Form->select(
  3488. 'Model.multi_field',
  3489. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3490. array('multiple' => 'checkbox')
  3491. );
  3492. $expected = array(
  3493. 'input' => array(
  3494. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3495. ),
  3496. array('div' => array('class' => 'checkbox')),
  3497. array('input' => array(
  3498. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3499. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3500. )),
  3501. array('label' => array('for' => 'ModelMultiFieldA')),
  3502. 'first',
  3503. '/label',
  3504. '/div',
  3505. array('div' => array('class' => 'checkbox')),
  3506. array('input' => array(
  3507. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3508. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3509. )),
  3510. array('label' => array('for' => 'ModelMultiFieldB')),
  3511. 'second',
  3512. '/label',
  3513. '/div',
  3514. array('div' => array('class' => 'checkbox')),
  3515. array('input' => array(
  3516. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3517. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3518. )),
  3519. array('label' => array('for' => 'ModelMultiFieldC')),
  3520. 'third',
  3521. '/label',
  3522. '/div'
  3523. );
  3524. $this->assertTags($result, $expected);
  3525. $result = $this->Form->select(
  3526. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3527. );
  3528. $expected = array(
  3529. 'input' => array(
  3530. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3531. ),
  3532. array('div' => array('class' => 'checkbox')),
  3533. array('input' => array(
  3534. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3535. 'value' => '1', 'id' => 'ModelMultiField1'
  3536. )),
  3537. array('label' => array('for' => 'ModelMultiField1')),
  3538. 'first',
  3539. '/label',
  3540. '/div'
  3541. );
  3542. $this->assertTags($result, $expected);
  3543. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3544. $result = $this->Form->select(
  3545. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3546. );
  3547. $expected = array(
  3548. 'input' => array(
  3549. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3550. ),
  3551. array('div' => array('class' => 'checkbox')),
  3552. array('input' => array(
  3553. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3554. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3555. )),
  3556. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3557. 'first',
  3558. '/label',
  3559. '/div',
  3560. array('div' => array('class' => 'checkbox')),
  3561. array('input' => array(
  3562. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3563. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3564. )),
  3565. array('label' => array('for' => 'ModelTagsArray')),
  3566. 'Array',
  3567. '/label',
  3568. '/div'
  3569. );
  3570. $this->assertTags($result, $expected);
  3571. }
  3572. /**
  3573. * test multiple checkboxes with div styles.
  3574. *
  3575. * @return void
  3576. */
  3577. public function testSelectMultipleCheckboxDiv() {
  3578. $result = $this->Form->select(
  3579. 'Model.tags',
  3580. array('first', 'second'),
  3581. array('multiple' => 'checkbox', 'class' => 'my-class')
  3582. );
  3583. $expected = array(
  3584. 'input' => array(
  3585. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3586. ),
  3587. array('div' => array('class' => 'my-class')),
  3588. array('input' => array(
  3589. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3590. 'value' => '0', 'id' => 'ModelTags0'
  3591. )),
  3592. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3593. '/div',
  3594. array('div' => array('class' => 'my-class')),
  3595. array('input' => array(
  3596. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3597. 'value' => '1', 'id' => 'ModelTags1'
  3598. )),
  3599. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3600. '/div'
  3601. );
  3602. $this->assertTags($result, $expected);
  3603. $result = $this->Form->input('Model.tags', array(
  3604. 'options' => array('first', 'second'),
  3605. 'multiple' => 'checkbox',
  3606. 'class' => 'my-class',
  3607. 'div' => false,
  3608. 'label' => false
  3609. ));
  3610. $this->assertTags($result, $expected);
  3611. $Contact = ClassRegistry::getObject('Contact');
  3612. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3613. $result = $this->Form->input('Contact.tags', array(
  3614. 'options' => array('one'),
  3615. 'multiple' => 'checkbox',
  3616. 'label' => false,
  3617. 'div' => false
  3618. ));
  3619. $expected = array(
  3620. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3621. array('div' => array('class' => 'checkbox form-error')),
  3622. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3623. array('label' => array('for' => 'ContactTags0')),
  3624. 'one',
  3625. '/label',
  3626. '/div'
  3627. );
  3628. $this->assertTags($result, $expected);
  3629. $result = $this->Form->input('Contact.tags', array(
  3630. 'options' => array('one'),
  3631. 'multiple' => 'checkbox',
  3632. 'class' => 'mycheckbox',
  3633. 'label' => false,
  3634. 'div' => false
  3635. ));
  3636. $expected = array(
  3637. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3638. array('div' => array('class' => 'mycheckbox form-error')),
  3639. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3640. array('label' => array('for' => 'ContactTags0')),
  3641. 'one',
  3642. '/label',
  3643. '/div'
  3644. );
  3645. $this->assertTags($result, $expected);
  3646. }
  3647. /**
  3648. * Checks the security hash array generated for multiple-input checkbox elements
  3649. *
  3650. * @return void
  3651. */
  3652. public function testSelectMultipleCheckboxSecurity() {
  3653. $this->Form->request['_Token'] = array('key' => 'testKey');
  3654. $this->assertEqual($this->Form->fields, array());
  3655. $result = $this->Form->select(
  3656. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3657. array('multiple' => 'checkbox')
  3658. );
  3659. $this->assertEqual($this->Form->fields, array('Model.multi_field'));
  3660. $result = $this->Form->secure($this->Form->fields);
  3661. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3662. $this->assertPattern('/"' . $key . '"/', $result);
  3663. }
  3664. /**
  3665. * testInputMultipleCheckboxes method
  3666. *
  3667. * test input() resulting in multi select elements being generated.
  3668. *
  3669. * @return void
  3670. */
  3671. public function testInputMultipleCheckboxes() {
  3672. $result = $this->Form->input('Model.multi_field', array(
  3673. 'options' => array('first', 'second', 'third'),
  3674. 'multiple' => 'checkbox'
  3675. ));
  3676. $expected = array(
  3677. array('div' => array('class' => 'input select')),
  3678. array('label' => array('for' => 'ModelMultiField')),
  3679. 'Multi Field',
  3680. '/label',
  3681. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3682. array('div' => array('class' => 'checkbox')),
  3683. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3684. array('label' => array('for' => 'ModelMultiField0')),
  3685. 'first',
  3686. '/label',
  3687. '/div',
  3688. array('div' => array('class' => 'checkbox')),
  3689. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3690. array('label' => array('for' => 'ModelMultiField1')),
  3691. 'second',
  3692. '/label',
  3693. '/div',
  3694. array('div' => array('class' => 'checkbox')),
  3695. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3696. array('label' => array('for' => 'ModelMultiField2')),
  3697. 'third',
  3698. '/label',
  3699. '/div',
  3700. '/div'
  3701. );
  3702. $this->assertTags($result, $expected);
  3703. $result = $this->Form->input('Model.multi_field', array(
  3704. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3705. 'multiple' => 'checkbox'
  3706. ));
  3707. $expected = array(
  3708. array('div' => array('class' => 'input select')),
  3709. array('label' => array('for' => 'ModelMultiField')),
  3710. 'Multi Field',
  3711. '/label',
  3712. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3713. array('div' => array('class' => 'checkbox')),
  3714. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3715. array('label' => array('for' => 'ModelMultiFieldA')),
  3716. 'first',
  3717. '/label',
  3718. '/div',
  3719. array('div' => array('class' => 'checkbox')),
  3720. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3721. array('label' => array('for' => 'ModelMultiFieldB')),
  3722. 'second',
  3723. '/label',
  3724. '/div',
  3725. array('div' => array('class' => 'checkbox')),
  3726. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3727. array('label' => array('for' => 'ModelMultiFieldC')),
  3728. 'third',
  3729. '/label',
  3730. '/div',
  3731. '/div'
  3732. );
  3733. $this->assertTags($result, $expected);
  3734. $result = $this->Form->input('Model.multi_field', array(
  3735. 'options' => array('1' => 'first'),
  3736. 'multiple' => 'checkbox',
  3737. 'label' => false,
  3738. 'div' => false
  3739. ));
  3740. $expected = array(
  3741. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3742. array('div' => array('class' => 'checkbox')),
  3743. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3744. array('label' => array('for' => 'ModelMultiField1')),
  3745. 'first',
  3746. '/label',
  3747. '/div'
  3748. );
  3749. $this->assertTags($result, $expected);
  3750. $result = $this->Form->input('Model.multi_field', array(
  3751. 'options' => array('2' => 'second'),
  3752. 'multiple' => 'checkbox',
  3753. 'label' => false,
  3754. 'div' => false
  3755. ));
  3756. $expected = array(
  3757. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3758. array('div' => array('class' => 'checkbox')),
  3759. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3760. array('label' => array('for' => 'ModelMultiField2')),
  3761. 'second',
  3762. '/label',
  3763. '/div'
  3764. );
  3765. $this->assertTags($result, $expected);
  3766. }
  3767. /**
  3768. * testSelectHiddenFieldOmission method
  3769. *
  3770. * test that select() with 'hiddenField' => false omits the hidden field
  3771. *
  3772. * @return void
  3773. */
  3774. public function testSelectHiddenFieldOmission() {
  3775. $result = $this->Form->select('Model.multi_field',
  3776. array('first', 'second'),
  3777. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3778. );
  3779. $expected = array(
  3780. array('div' => array('class' => 'checkbox')),
  3781. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3782. array('label' => array('for' => 'ModelMultiField0')),
  3783. 'first',
  3784. '/label',
  3785. '/div',
  3786. array('div' => array('class' => 'checkbox')),
  3787. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3788. array('label' => array('for' => 'ModelMultiField1')),
  3789. 'second',
  3790. '/label',
  3791. '/div'
  3792. );
  3793. $this->assertTags($result, $expected);
  3794. $result = $this->Form->input('Model.multi_field', array(
  3795. 'options' => array('first', 'second'),
  3796. 'multiple' => 'checkbox',
  3797. 'hiddenField' => false
  3798. ));
  3799. $expected = array(
  3800. array('div' => array('class' => 'input select')),
  3801. array('label' => array('for' => 'ModelMultiField')),
  3802. 'Multi Field',
  3803. '/label',
  3804. array('div' => array('class' => 'checkbox')),
  3805. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3806. array('label' => array('for' => 'ModelMultiField0')),
  3807. 'first',
  3808. '/label',
  3809. '/div',
  3810. array('div' => array('class' => 'checkbox')),
  3811. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3812. array('label' => array('for' => 'ModelMultiField1')),
  3813. 'second',
  3814. '/label',
  3815. '/div',
  3816. '/div'
  3817. );
  3818. $this->assertTags($result, $expected);
  3819. }
  3820. /**
  3821. * test that select() with multiple = checkbox works with overriding name attribute.
  3822. *
  3823. * @return void
  3824. */
  3825. public function testSelectCheckboxMultipleOverrideName() {
  3826. $result = $this->Form->input('category', array(
  3827. 'type' => 'select',
  3828. 'multiple' => 'checkbox',
  3829. 'name' => 'data[fish]',
  3830. 'options' => array('1', '2'),
  3831. 'div' => false,
  3832. 'label' => false,
  3833. ));
  3834. $expected = array(
  3835. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  3836. array('div' => array('class' => 'checkbox')),
  3837. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  3838. array('label' => array('for' => 'Category0')), '1', '/label',
  3839. '/div',
  3840. array('div' => array('class' => 'checkbox')),
  3841. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  3842. array('label' => array('for' => 'Category1')), '2', '/label',
  3843. '/div'
  3844. );
  3845. $this->assertTags($result, $expected);
  3846. }
  3847. /**
  3848. * Test that 'id' overrides all the checkbox id's as well.
  3849. *
  3850. * @return void
  3851. */
  3852. public function testSelectCheckboxMultipleId() {
  3853. $result = $this->Form->select(
  3854. 'Model.multi_field',
  3855. array('first', 'second', 'third'),
  3856. array('multiple' => 'checkbox', 'id' => 'CustomId')
  3857. );
  3858. $expected = array(
  3859. 'input' => array(
  3860. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  3861. ),
  3862. array('div' => array('class' => 'checkbox')),
  3863. array('input' => array(
  3864. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3865. 'value' => '0', 'id' => 'CustomId0'
  3866. )),
  3867. array('label' => array('for' => 'CustomId0')),
  3868. 'first',
  3869. '/label',
  3870. '/div',
  3871. array('div' => array('class' => 'checkbox')),
  3872. array('input' => array(
  3873. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3874. 'value' => '1', 'id' => 'CustomId1'
  3875. )),
  3876. array('label' => array('for' => 'CustomId1')),
  3877. 'second',
  3878. '/label',
  3879. '/div',
  3880. array('div' => array('class' => 'checkbox')),
  3881. array('input' => array(
  3882. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3883. 'value' => '2', 'id' => 'CustomId2'
  3884. )),
  3885. array('label' => array('for' => 'CustomId2')),
  3886. 'third',
  3887. '/label',
  3888. '/div'
  3889. );
  3890. $this->assertTags($result, $expected);
  3891. }
  3892. /**
  3893. * testCheckbox method
  3894. *
  3895. * Test generation of checkboxes
  3896. *
  3897. * @return void
  3898. */
  3899. public function testCheckbox() {
  3900. $result = $this->Form->checkbox('Model.field');
  3901. $expected = array(
  3902. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3903. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  3904. );
  3905. $this->assertTags($result, $expected);
  3906. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  3907. $expected = array(
  3908. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  3909. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  3910. );
  3911. $this->assertTags($result, $expected);
  3912. $Contact = ClassRegistry::getObject('Contact');
  3913. $Contact->validationErrors['field'] = 1;
  3914. $this->Form->request->data['Contact']['field'] = 'myvalue';
  3915. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  3916. $expected = array(
  3917. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  3918. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  3919. );
  3920. $this->assertTags($result, $expected);
  3921. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  3922. $expected = array(
  3923. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  3924. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  3925. );
  3926. $this->assertTags($result, $expected);
  3927. $this->Form->request->data['Contact']['field'] = '';
  3928. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  3929. $expected = array(
  3930. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  3931. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  3932. );
  3933. $this->assertTags($result, $expected);
  3934. $Contact->validationErrors = array();
  3935. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  3936. $expected = array(
  3937. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  3938. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  3939. );
  3940. $this->assertTags($result, $expected);
  3941. $this->Form->request->data['Contact']['published'] = 1;
  3942. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  3943. $expected = array(
  3944. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  3945. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  3946. );
  3947. $this->assertTags($result, $expected);
  3948. $this->Form->request->data['Contact']['published'] = 0;
  3949. $result = $this->Form->checkbox('Contact.published', array('id'=>'theID'));
  3950. $expected = array(
  3951. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  3952. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  3953. );
  3954. $this->assertTags($result, $expected);
  3955. $result = $this->Form->checkbox('Model.CustomField.1.value');
  3956. $expected = array(
  3957. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  3958. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  3959. );
  3960. $this->assertTags($result, $expected);
  3961. $result = $this->Form->checkbox('CustomField.1.value');
  3962. $expected = array(
  3963. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  3964. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  3965. );
  3966. $this->assertTags($result, $expected);
  3967. }
  3968. /**
  3969. * test checkbox() with a custom name attribute
  3970. *
  3971. * @return void
  3972. */
  3973. public function testCheckboxCustomNameAttribute() {
  3974. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3975. $expected = array(
  3976. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  3977. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  3978. );
  3979. $this->assertTags($result, $expected);
  3980. }
  3981. /**
  3982. * test the checked option for checkboxes.
  3983. *
  3984. * @return void
  3985. */
  3986. public function testCheckboxCheckedOption() {
  3987. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  3988. $expected = array(
  3989. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3990. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  3991. );
  3992. $this->assertTags($result, $expected);
  3993. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  3994. $expected = array(
  3995. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3996. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  3997. );
  3998. $this->assertTags($result, $expected);
  3999. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4000. $expected = array(
  4001. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4002. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4003. );
  4004. $this->assertTags($result, $expected);
  4005. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4006. $expected = array(
  4007. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4008. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4009. );
  4010. $this->assertTags($result, $expected);
  4011. $this->Form->request->data['Model']['field'] = 1;
  4012. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4013. $expected = array(
  4014. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4015. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4016. );
  4017. $this->assertTags($result, $expected);
  4018. }
  4019. /**
  4020. * Test that disabled attribute works on both the checkbox and hidden input.
  4021. *
  4022. * @return void
  4023. */
  4024. public function testCheckboxDisabling() {
  4025. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4026. $expected = array(
  4027. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4028. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4029. );
  4030. $this->assertTags($result, $expected);
  4031. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4032. $expected = array(
  4033. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4034. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4035. );
  4036. $this->assertTags($result, $expected);
  4037. }
  4038. /**
  4039. * Test that the hidden input for checkboxes can be removed/omitted from the output.
  4040. *
  4041. * @return void
  4042. */
  4043. public function testCheckboxHiddenFieldOmission() {
  4044. $result = $this->Form->input('UserForm.something', array(
  4045. 'type' => 'checkbox',
  4046. 'hiddenField' => false
  4047. )
  4048. );
  4049. $expected = array(
  4050. 'div' => array('class' => 'input checkbox'),
  4051. array('input' => array(
  4052. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4053. 'value' => '1', 'id' => 'UserFormSomething'
  4054. )),
  4055. 'label' => array('for' => 'UserFormSomething'),
  4056. 'Something',
  4057. '/label',
  4058. '/div'
  4059. );
  4060. $this->assertTags($result, $expected);
  4061. }
  4062. /**
  4063. * testDateTime method
  4064. *
  4065. * Test generation of date/time select elements
  4066. *
  4067. * @return void
  4068. */
  4069. public function testDateTime() {
  4070. extract($this->dateRegex);
  4071. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4072. $now = strtotime('now');
  4073. $expected = array(
  4074. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4075. $daysRegex,
  4076. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4077. date('j', $now),
  4078. '/option',
  4079. '*/select',
  4080. '-',
  4081. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4082. $monthsRegex,
  4083. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4084. date('F', $now),
  4085. '/option',
  4086. '*/select',
  4087. '-',
  4088. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4089. $yearsRegex,
  4090. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4091. date('Y', $now),
  4092. '/option',
  4093. '*/select',
  4094. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4095. $hoursRegex,
  4096. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4097. date('g', $now),
  4098. '/option',
  4099. '*/select',
  4100. ':',
  4101. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4102. $minutesRegex,
  4103. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4104. date('i', $now),
  4105. '/option',
  4106. '*/select',
  4107. ' ',
  4108. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4109. $meridianRegex,
  4110. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4111. date('a', $now),
  4112. '/option',
  4113. '*/select'
  4114. );
  4115. $this->assertTags($result, $expected);
  4116. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4117. $expected = array(
  4118. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4119. $daysRegex,
  4120. array('option' => array('value' => '')),
  4121. '/option',
  4122. '*/select',
  4123. '-',
  4124. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4125. $monthsRegex,
  4126. array('option' => array('value' => '')),
  4127. '/option',
  4128. '*/select',
  4129. '-',
  4130. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4131. $yearsRegex,
  4132. array('option' => array('value' => '')),
  4133. '/option',
  4134. '*/select',
  4135. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4136. $hoursRegex,
  4137. array('option' => array('value' => '')),
  4138. '/option',
  4139. '*/select',
  4140. ':',
  4141. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4142. $minutesRegex,
  4143. array('option' => array('value' => '')),
  4144. '/option',
  4145. '*/select',
  4146. ' ',
  4147. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4148. $meridianRegex,
  4149. array('option' => array('value' => '')),
  4150. '/option',
  4151. '*/select'
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4155. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4156. $expected = array(
  4157. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4158. $daysRegex,
  4159. array('option' => array('value' => '')),
  4160. '/option',
  4161. '*/select',
  4162. '-',
  4163. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4164. $monthsRegex,
  4165. array('option' => array('value' => '')),
  4166. '/option',
  4167. '*/select',
  4168. '-',
  4169. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4170. $yearsRegex,
  4171. array('option' => array('value' => '')),
  4172. '/option',
  4173. '*/select',
  4174. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4175. $hoursRegex,
  4176. array('option' => array('value' => '')),
  4177. '/option',
  4178. '*/select',
  4179. ':',
  4180. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4181. $minutesRegex,
  4182. array('option' => array('value' => '')),
  4183. '/option',
  4184. '*/select',
  4185. ' ',
  4186. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4187. $meridianRegex,
  4188. array('option' => array('value' => '')),
  4189. '/option',
  4190. '*/select'
  4191. );
  4192. $this->assertTags($result, $expected);
  4193. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4194. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4195. $expected = array(
  4196. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4197. $daysRegex,
  4198. array('option' => array('value' => '')),
  4199. '/option',
  4200. '*/select',
  4201. '-',
  4202. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4203. $monthsRegex,
  4204. array('option' => array('value' => '')),
  4205. '/option',
  4206. '*/select',
  4207. '-',
  4208. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4209. $yearsRegex,
  4210. array('option' => array('value' => '')),
  4211. '/option',
  4212. '*/select',
  4213. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4214. $hoursRegex,
  4215. array('option' => array('value' => '')),
  4216. '/option',
  4217. '*/select',
  4218. ':',
  4219. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4220. $minutesRegex,
  4221. array('option' => array('value' => '')),
  4222. '/option',
  4223. '*/select',
  4224. ' ',
  4225. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4226. $meridianRegex,
  4227. array('option' => array('value' => '')),
  4228. '/option',
  4229. '*/select'
  4230. );
  4231. $this->assertTags($result, $expected);
  4232. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4233. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4234. $expected = array(
  4235. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4236. $daysRegex,
  4237. array('option' => array('value' => '')),
  4238. '/option',
  4239. '*/select',
  4240. '-',
  4241. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4242. $monthsRegex,
  4243. array('option' => array('value' => '')),
  4244. '/option',
  4245. '*/select',
  4246. '-',
  4247. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4248. $yearsRegex,
  4249. array('option' => array('value' => '')),
  4250. '/option',
  4251. '*/select',
  4252. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4253. $hoursRegex,
  4254. array('option' => array('value' => '')),
  4255. '/option',
  4256. '*/select',
  4257. ':',
  4258. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4259. $minutesRegex,
  4260. array('option' => array('value' => '')),
  4261. '/option',
  4262. array('option' => array('value' => '00')),
  4263. '00',
  4264. '/option',
  4265. array('option' => array('value' => '05')),
  4266. '05',
  4267. '/option',
  4268. array('option' => array('value' => '10')),
  4269. '10',
  4270. '/option',
  4271. '*/select',
  4272. ' ',
  4273. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4274. $meridianRegex,
  4275. array('option' => array('value' => '')),
  4276. '/option',
  4277. '*/select'
  4278. );
  4279. $this->assertTags($result, $expected);
  4280. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4281. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4282. $expected = array(
  4283. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4284. $daysRegex,
  4285. array('option' => array('value' => '')),
  4286. '/option',
  4287. '*/select',
  4288. '-',
  4289. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4290. $monthsRegex,
  4291. array('option' => array('value' => '')),
  4292. '/option',
  4293. '*/select',
  4294. '-',
  4295. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4296. $yearsRegex,
  4297. array('option' => array('value' => '')),
  4298. '/option',
  4299. '*/select',
  4300. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4301. $hoursRegex,
  4302. array('option' => array('value' => '')),
  4303. '/option',
  4304. '*/select',
  4305. ':',
  4306. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4307. $minutesRegex,
  4308. array('option' => array('value' => '')),
  4309. '/option',
  4310. array('option' => array('value' => '00')),
  4311. '00',
  4312. '/option',
  4313. array('option' => array('value' => '05')),
  4314. '05',
  4315. '/option',
  4316. array('option' => array('value' => '10')),
  4317. '10',
  4318. '/option',
  4319. '*/select',
  4320. ' ',
  4321. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4322. $meridianRegex,
  4323. array('option' => array('value' => '')),
  4324. '/option',
  4325. '*/select'
  4326. );
  4327. $this->assertTags($result, $expected);
  4328. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4329. $this->Form->request->data['Contact']['data'] = null;
  4330. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4331. $expected = array(
  4332. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4333. $daysRegex,
  4334. array('option' => array('value' => '')),
  4335. '/option',
  4336. '*/select',
  4337. '-',
  4338. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4339. $monthsRegex,
  4340. array('option' => array('value' => '')),
  4341. '/option',
  4342. '*/select',
  4343. '-',
  4344. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4345. $yearsRegex,
  4346. array('option' => array('value' => '')),
  4347. '/option',
  4348. '*/select',
  4349. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4350. $hoursRegex,
  4351. array('option' => array('value' => '')),
  4352. '/option',
  4353. '*/select',
  4354. ':',
  4355. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4356. $minutesRegex,
  4357. array('option' => array('value' => '')),
  4358. '/option',
  4359. '*/select',
  4360. ' ',
  4361. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4362. $meridianRegex,
  4363. array('option' => array('value' => '')),
  4364. '/option',
  4365. '*/select'
  4366. );
  4367. $this->assertTags($result, $expected);
  4368. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4369. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4370. $now = strtotime($this->Form->data['Model']['field']);
  4371. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4372. $expected = array(
  4373. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4374. $daysRegex,
  4375. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4376. date('j', $now),
  4377. '/option',
  4378. '*/select',
  4379. '-',
  4380. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4381. $monthsRegex,
  4382. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4383. date('F', $now),
  4384. '/option',
  4385. '*/select',
  4386. '-',
  4387. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4388. $yearsRegex,
  4389. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4390. date('Y', $now),
  4391. '/option',
  4392. '*/select',
  4393. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4394. $hoursRegex,
  4395. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4396. date('g', $now),
  4397. '/option',
  4398. '*/select',
  4399. ':',
  4400. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4401. $minutesRegex,
  4402. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4403. date('i', $now),
  4404. '/option',
  4405. '*/select',
  4406. ' ',
  4407. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4408. $meridianRegex,
  4409. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4410. date('a', $now),
  4411. '/option',
  4412. '*/select'
  4413. );
  4414. $this->assertTags($result, $expected);
  4415. $selected = strtotime('2008-10-26 12:33:00');
  4416. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4417. $this->assertPattern('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4418. $this->assertPattern('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4419. $this->assertPattern('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4420. $this->assertPattern('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4421. $this->assertPattern('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4422. $this->assertPattern('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4423. $this->Form->create('Contact');
  4424. $result = $this->Form->input('published');
  4425. $now = strtotime('now');
  4426. $expected = array(
  4427. 'div' => array('class' => 'input date'),
  4428. 'label' => array('for' => 'ContactPublishedMonth'),
  4429. 'Published',
  4430. '/label',
  4431. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4432. $monthsRegex,
  4433. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4434. date('F', $now),
  4435. '/option',
  4436. '*/select',
  4437. '-',
  4438. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4439. $daysRegex,
  4440. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4441. date('j', $now),
  4442. '/option',
  4443. '*/select',
  4444. '-',
  4445. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4446. $yearsRegex,
  4447. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4448. date('Y', $now),
  4449. '/option',
  4450. '*/select',
  4451. '/div'
  4452. );
  4453. $this->assertTags($result, $expected);
  4454. $result = $this->Form->input('published2', array('type' => 'date'));
  4455. $now = strtotime('now');
  4456. $expected = array(
  4457. 'div' => array('class' => 'input date'),
  4458. 'label' => array('for' => 'ContactPublished2Month'),
  4459. 'Published2',
  4460. '/label',
  4461. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4462. $monthsRegex,
  4463. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4464. date('F', $now),
  4465. '/option',
  4466. '*/select',
  4467. '-',
  4468. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4469. $daysRegex,
  4470. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4471. date('j', $now),
  4472. '/option',
  4473. '*/select',
  4474. '-',
  4475. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4476. $yearsRegex,
  4477. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4478. date('Y', $now),
  4479. '/option',
  4480. '*/select',
  4481. '/div'
  4482. );
  4483. $this->assertTags($result, $expected);
  4484. $this->Form->create('Contact');
  4485. $result = $this->Form->input('published', array('monthNames' => false));
  4486. $now = strtotime('now');
  4487. $expected = array(
  4488. 'div' => array('class' => 'input date'),
  4489. 'label' => array('for' => 'ContactPublishedMonth'),
  4490. 'Published',
  4491. '/label',
  4492. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4493. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4494. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4495. date('m', $now),
  4496. '/option',
  4497. '*/select',
  4498. '-',
  4499. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4500. $daysRegex,
  4501. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4502. date('j', $now),
  4503. '/option',
  4504. '*/select',
  4505. '-',
  4506. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4507. $yearsRegex,
  4508. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4509. date('Y', $now),
  4510. '/option',
  4511. '*/select',
  4512. '/div'
  4513. );
  4514. $this->assertTags($result, $expected);
  4515. $result = $this->Form->input('published', array('type' => 'time'));
  4516. $now = strtotime('now');
  4517. $expected = array(
  4518. 'div' => array('class' => 'input time'),
  4519. 'label' => array('for' => 'ContactPublishedHour'),
  4520. 'Published',
  4521. '/label',
  4522. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4523. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4524. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4525. date('g', $now),
  4526. '/option',
  4527. '*/select',
  4528. ':',
  4529. );
  4530. $this->assertTags($result, $expected);
  4531. $result = $this->Form->input('published', array(
  4532. 'timeFormat' => 24,
  4533. 'interval' => 5,
  4534. 'selected' => strtotime('2009-09-03 13:37:00'),
  4535. 'type' => 'datetime'
  4536. ));
  4537. $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4538. $this->assertPattern('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4539. $this->assertPattern('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4540. $this->assertPattern('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4541. $this->assertPattern('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4542. $this->assertNoErrors();
  4543. $this->Form->request->data['Contact'] = array(
  4544. 'date' => array(
  4545. 'day' => '',
  4546. 'month' => '',
  4547. 'year' => '',
  4548. 'hour' => '',
  4549. 'min' => '',
  4550. 'meridian' => ''
  4551. )
  4552. );
  4553. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4554. }
  4555. /**
  4556. * test that datetime() and default values work.
  4557. *
  4558. * @return void
  4559. */
  4560. public function testDatetimeWithDefault() {
  4561. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4562. $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4563. $this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4564. $this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4565. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4566. 'default' => '2009-06-01 11:15:30'
  4567. ));
  4568. $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4569. $this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4570. $this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4571. }
  4572. /**
  4573. * test that bogus non-date time data doesn't cause errors.
  4574. *
  4575. * @return void
  4576. */
  4577. public function testDateTimeWithBogusData() {
  4578. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4579. $this->assertNoPattern('/selected="selected">\d/', $result);
  4580. }
  4581. /**
  4582. * testFormDateTimeMulti method
  4583. *
  4584. * test multiple datetime element generation
  4585. *
  4586. * @return void
  4587. */
  4588. public function testFormDateTimeMulti() {
  4589. extract($this->dateRegex);
  4590. $result = $this->Form->dateTime('Contact.1.updated');
  4591. $expected = array(
  4592. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4593. $daysRegex,
  4594. array('option' => array('value' => '')),
  4595. '/option',
  4596. '*/select',
  4597. '-',
  4598. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4599. $monthsRegex,
  4600. array('option' => array('value' => '')),
  4601. '/option',
  4602. '*/select',
  4603. '-',
  4604. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4605. $yearsRegex,
  4606. array('option' => array('value' => '')),
  4607. '/option',
  4608. '*/select',
  4609. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4610. $hoursRegex,
  4611. array('option' => array('value' => '')),
  4612. '/option',
  4613. '*/select',
  4614. ':',
  4615. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4616. $minutesRegex,
  4617. array('option' => array('value' => '')),
  4618. '/option',
  4619. '*/select',
  4620. ' ',
  4621. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4622. $meridianRegex,
  4623. array('option' => array('value' => '')),
  4624. '/option',
  4625. '*/select'
  4626. );
  4627. $this->assertTags($result, $expected);
  4628. $result = $this->Form->dateTime('Contact.2.updated');
  4629. $expected = array(
  4630. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4631. $daysRegex,
  4632. array('option' => array('value' => '')),
  4633. '/option',
  4634. '*/select',
  4635. '-',
  4636. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4637. $monthsRegex,
  4638. array('option' => array('value' => '')),
  4639. '/option',
  4640. '*/select',
  4641. '-',
  4642. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4643. $yearsRegex,
  4644. array('option' => array('value' => '')),
  4645. '/option',
  4646. '*/select',
  4647. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4648. $hoursRegex,
  4649. array('option' => array('value' => '')),
  4650. '/option',
  4651. '*/select',
  4652. ':',
  4653. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4654. $minutesRegex,
  4655. array('option' => array('value' => '')),
  4656. '/option',
  4657. '*/select',
  4658. ' ',
  4659. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4660. $meridianRegex,
  4661. array('option' => array('value' => '')),
  4662. '/option',
  4663. '*/select'
  4664. );
  4665. $this->assertTags($result, $expected);
  4666. }
  4667. /**
  4668. * testMonth method
  4669. *
  4670. * @return void
  4671. */
  4672. public function testMonth() {
  4673. $result = $this->Form->month('Model.field');
  4674. $expected = array(
  4675. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4676. array('option' => array('value' => '')),
  4677. '/option',
  4678. array('option' => array('value' => '01')),
  4679. date('F', strtotime('2008-01-01 00:00:00')),
  4680. '/option',
  4681. array('option' => array('value' => '02')),
  4682. date('F', strtotime('2008-02-01 00:00:00')),
  4683. '/option',
  4684. '*/select',
  4685. );
  4686. $this->assertTags($result, $expected);
  4687. $result = $this->Form->month('Model.field', array('empty' => true));
  4688. $expected = array(
  4689. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4690. array('option' => array('value' => '')),
  4691. '/option',
  4692. array('option' => array('value' => '01')),
  4693. date('F', strtotime('2008-01-01 00:00:00')),
  4694. '/option',
  4695. array('option' => array('value' => '02')),
  4696. date('F', strtotime('2008-02-01 00:00:00')),
  4697. '/option',
  4698. '*/select',
  4699. );
  4700. $this->assertTags($result, $expected);
  4701. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4702. $expected = array(
  4703. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4704. array('option' => array('value' => '')),
  4705. '/option',
  4706. array('option' => array('value' => '01')),
  4707. '01',
  4708. '/option',
  4709. array('option' => array('value' => '02')),
  4710. '02',
  4711. '/option',
  4712. '*/select',
  4713. );
  4714. $this->assertTags($result, $expected);
  4715. $monthNames = array(
  4716. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4717. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4718. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4719. $expected = array(
  4720. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4721. array('option' => array('value' => '')),
  4722. '/option',
  4723. array('option' => array('value' => '01')),
  4724. 'Jan',
  4725. '/option',
  4726. array('option' => array('value' => '02')),
  4727. 'Feb',
  4728. '/option',
  4729. '*/select',
  4730. );
  4731. $this->assertTags($result, $expected);
  4732. }
  4733. /**
  4734. * testDay method
  4735. *
  4736. * @return void
  4737. */
  4738. public function testDay() {
  4739. extract($this->dateRegex);
  4740. $result = $this->Form->day('Model.field', array('value' => false));
  4741. $expected = array(
  4742. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4743. array('option' => array('value' => '')),
  4744. '/option',
  4745. array('option' => array('value' => '01')),
  4746. '1',
  4747. '/option',
  4748. array('option' => array('value' => '02')),
  4749. '2',
  4750. '/option',
  4751. $daysRegex,
  4752. '/select',
  4753. );
  4754. $this->assertTags($result, $expected);
  4755. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4756. $result = $this->Form->day('Model.field');
  4757. $expected = array(
  4758. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4759. array('option' => array('value' => '')),
  4760. '/option',
  4761. array('option' => array('value' => '01')),
  4762. '1',
  4763. '/option',
  4764. array('option' => array('value' => '02')),
  4765. '2',
  4766. '/option',
  4767. $daysRegex,
  4768. array('option' => array('value' => '10', 'selected' => 'selected')),
  4769. '10',
  4770. '/option',
  4771. $daysRegex,
  4772. '/select',
  4773. );
  4774. $this->assertTags($result, $expected);
  4775. $this->Form->request->data['Model']['field'] = '';
  4776. $result = $this->Form->day('Model.field', array('value' => '10'));
  4777. $expected = array(
  4778. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4779. array('option' => array('value' => '')),
  4780. '/option',
  4781. array('option' => array('value' => '01')),
  4782. '1',
  4783. '/option',
  4784. array('option' => array('value' => '02')),
  4785. '2',
  4786. '/option',
  4787. $daysRegex,
  4788. array('option' => array('value' => '10', 'selected' => 'selected')),
  4789. '10',
  4790. '/option',
  4791. $daysRegex,
  4792. '/select',
  4793. );
  4794. $this->assertTags($result, $expected);
  4795. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4796. $result = $this->Form->day('Model.field', array('value' => true));
  4797. $expected = array(
  4798. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4799. array('option' => array('value' => '')),
  4800. '/option',
  4801. array('option' => array('value' => '01')),
  4802. '1',
  4803. '/option',
  4804. array('option' => array('value' => '02')),
  4805. '2',
  4806. '/option',
  4807. $daysRegex,
  4808. array('option' => array('value' => '10', 'selected' => 'selected')),
  4809. '10',
  4810. '/option',
  4811. $daysRegex,
  4812. '/select',
  4813. );
  4814. $this->assertTags($result, $expected);
  4815. }
  4816. /**
  4817. * testMinute method
  4818. *
  4819. * @return void
  4820. */
  4821. public function testMinute() {
  4822. extract($this->dateRegex);
  4823. $result = $this->Form->minute('Model.field');
  4824. $expected = array(
  4825. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4826. array('option' => array('value' => '')),
  4827. '/option',
  4828. array('option' => array('value' => '00')),
  4829. '00',
  4830. '/option',
  4831. array('option' => array('value' => '01')),
  4832. '01',
  4833. '/option',
  4834. array('option' => array('value' => '02')),
  4835. '02',
  4836. '/option',
  4837. $minutesRegex,
  4838. '/select',
  4839. );
  4840. $this->assertTags($result, $expected);
  4841. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4842. $result = $this->Form->minute('Model.field');
  4843. $expected = array(
  4844. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4845. array('option' => array('value' => '')),
  4846. '/option',
  4847. array('option' => array('value' => '00')),
  4848. '00',
  4849. '/option',
  4850. array('option' => array('value' => '01')),
  4851. '01',
  4852. '/option',
  4853. array('option' => array('value' => '02')),
  4854. '02',
  4855. '/option',
  4856. $minutesRegex,
  4857. array('option' => array('value' => '12', 'selected' => 'selected')),
  4858. '12',
  4859. '/option',
  4860. $minutesRegex,
  4861. '/select',
  4862. );
  4863. $this->assertTags($result, $expected);
  4864. $this->Form->request->data['Model']['field'] = '';
  4865. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4866. $expected = array(
  4867. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4868. array('option' => array('value' => '')),
  4869. '/option',
  4870. array('option' => array('value' => '00')),
  4871. '00',
  4872. '/option',
  4873. array('option' => array('value' => '05')),
  4874. '05',
  4875. '/option',
  4876. array('option' => array('value' => '10')),
  4877. '10',
  4878. '/option',
  4879. $minutesRegex,
  4880. '/select',
  4881. );
  4882. $this->assertTags($result, $expected);
  4883. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4884. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4885. $expected = array(
  4886. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4887. array('option' => array('value' => '')),
  4888. '/option',
  4889. array('option' => array('value' => '00')),
  4890. '00',
  4891. '/option',
  4892. array('option' => array('value' => '05')),
  4893. '05',
  4894. '/option',
  4895. array('option' => array('value' => '10', 'selected' => 'selected')),
  4896. '10',
  4897. '/option',
  4898. $minutesRegex,
  4899. '/select',
  4900. );
  4901. $this->assertTags($result, $expected);
  4902. }
  4903. /**
  4904. * testHour method
  4905. *
  4906. * @return void
  4907. */
  4908. public function testHour() {
  4909. extract($this->dateRegex);
  4910. $result = $this->Form->hour('Model.field', false);
  4911. $expected = array(
  4912. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4913. array('option' => array('value' => '')),
  4914. '/option',
  4915. array('option' => array('value' => '01')),
  4916. '1',
  4917. '/option',
  4918. array('option' => array('value' => '02')),
  4919. '2',
  4920. '/option',
  4921. $hoursRegex,
  4922. '/select',
  4923. );
  4924. $this->assertTags($result, $expected);
  4925. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4926. $result = $this->Form->hour('Model.field', false);
  4927. $expected = array(
  4928. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4929. array('option' => array('value' => '')),
  4930. '/option',
  4931. array('option' => array('value' => '01')),
  4932. '1',
  4933. '/option',
  4934. array('option' => array('value' => '02')),
  4935. '2',
  4936. '/option',
  4937. $hoursRegex,
  4938. array('option' => array('value' => '12', 'selected' => 'selected')),
  4939. '12',
  4940. '/option',
  4941. '/select',
  4942. );
  4943. $this->assertTags($result, $expected);
  4944. $this->Form->request->data['Model']['field'] = '';
  4945. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  4946. $expected = array(
  4947. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4948. array('option' => array('value' => '')),
  4949. '/option',
  4950. array('option' => array('value' => '00')),
  4951. '0',
  4952. '/option',
  4953. array('option' => array('value' => '01')),
  4954. '1',
  4955. '/option',
  4956. array('option' => array('value' => '02')),
  4957. '2',
  4958. '/option',
  4959. $hoursRegex,
  4960. array('option' => array('value' => '23', 'selected' => 'selected')),
  4961. '23',
  4962. '/option',
  4963. '/select',
  4964. );
  4965. $this->assertTags($result, $expected);
  4966. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4967. $result = $this->Form->hour('Model.field', true);
  4968. $expected = array(
  4969. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4970. array('option' => array('value' => '')),
  4971. '/option',
  4972. array('option' => array('value' => '00', 'selected' => 'selected')),
  4973. '0',
  4974. '/option',
  4975. array('option' => array('value' => '01')),
  4976. '1',
  4977. '/option',
  4978. array('option' => array('value' => '02')),
  4979. '2',
  4980. '/option',
  4981. $hoursRegex,
  4982. '/select',
  4983. );
  4984. $this->assertTags($result, $expected);
  4985. unset($this->Form->request->data['Model']['field']);
  4986. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  4987. $thisHour = date('H');
  4988. $optValue = date('G');
  4989. $this->assertPattern('/<option value="' . $thisHour . '" selected="selected">'. $optValue .'<\/option>/', $result);
  4990. }
  4991. /**
  4992. * testYear method
  4993. *
  4994. * @return void
  4995. */
  4996. public function testYear() {
  4997. $result = $this->Form->year('Model.field', 2006, 2007);
  4998. $expected = array(
  4999. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5000. array('option' => array('value' => '')),
  5001. '/option',
  5002. array('option' => array('value' => '2007')),
  5003. '2007',
  5004. '/option',
  5005. array('option' => array('value' => '2006')),
  5006. '2006',
  5007. '/option',
  5008. '/select',
  5009. );
  5010. $this->assertTags($result, $expected);
  5011. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5012. $expected = array(
  5013. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5014. array('option' => array('value' => '')),
  5015. '/option',
  5016. array('option' => array('value' => '2006')),
  5017. '2006',
  5018. '/option',
  5019. array('option' => array('value' => '2007')),
  5020. '2007',
  5021. '/option',
  5022. '/select',
  5023. );
  5024. $this->assertTags($result, $expected);
  5025. $this->request->data['Contact']['published'] = '';
  5026. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5027. $expected = array(
  5028. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5029. array('option' => array('value' => '')),
  5030. '/option',
  5031. array('option' => array('value' => '2007')),
  5032. '2007',
  5033. '/option',
  5034. array('option' => array('value' => '2006')),
  5035. '2006',
  5036. '/option',
  5037. '/select',
  5038. );
  5039. $this->assertTags($result, $expected);
  5040. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5041. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5042. $expected = array(
  5043. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5044. array('option' => array('value' => '2007')),
  5045. '2007',
  5046. '/option',
  5047. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5048. '2006',
  5049. '/option',
  5050. '/select',
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. $this->Form->request->data['Contact']['published'] = '';
  5054. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5055. $expected = array(
  5056. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5057. array('option' => array('value' => '')),
  5058. '/option',
  5059. array('option' => array('value' => '2007')),
  5060. '2007',
  5061. '/option',
  5062. array('option' => array('value' => '2006')),
  5063. '2006',
  5064. '/option',
  5065. '/select',
  5066. );
  5067. $this->assertTags($result, $expected);
  5068. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5069. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5070. $expected = array(
  5071. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5072. array('option' => array('value' => '2007')),
  5073. '2007',
  5074. '/option',
  5075. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5076. '2006',
  5077. '/option',
  5078. '/select',
  5079. );
  5080. $this->assertTags($result, $expected);
  5081. $this->Form->request->data['Contact']['published'] = '';
  5082. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5083. $expected = array(
  5084. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5085. array('option' => array('value' => '')),
  5086. '/option',
  5087. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5088. '2007',
  5089. '/option',
  5090. array('option' => array('value' => '2006')),
  5091. '2006',
  5092. '/option',
  5093. '/select',
  5094. );
  5095. $this->assertTags($result, $expected);
  5096. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5097. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5098. $expected = array(
  5099. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5100. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5101. '2007',
  5102. '/option',
  5103. array('option' => array('value' => '2006')),
  5104. '2006',
  5105. '/option',
  5106. '/select',
  5107. );
  5108. $this->assertTags($result, $expected);
  5109. $this->Form->request->data['Contact']['published'] = '';
  5110. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5111. $expected = array(
  5112. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5113. array('option' => array('value' => '2008')),
  5114. '2008',
  5115. '/option',
  5116. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5117. '2007',
  5118. '/option',
  5119. array('option' => array('value' => '2006')),
  5120. '2006',
  5121. '/option',
  5122. '/select',
  5123. );
  5124. $this->assertTags($result, $expected);
  5125. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5126. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5127. $expected = array(
  5128. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5129. array('option' => array('value' => '2008')),
  5130. '2008',
  5131. '/option',
  5132. array('option' => array('value' => '2007')),
  5133. '2007',
  5134. '/option',
  5135. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5136. '2006',
  5137. '/option',
  5138. '/select',
  5139. );
  5140. $this->assertTags($result, $expected);
  5141. $this->Form->request->data = array();
  5142. $this->Form->create('Contact');
  5143. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5144. $expected = array(
  5145. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5146. array('option' => array('value' => '2008')),
  5147. '2008',
  5148. '/option',
  5149. array('option' => array('value' => '2007')),
  5150. '2007',
  5151. '/option',
  5152. array('option' => array('value' => '2006')),
  5153. '2006',
  5154. '/option',
  5155. '/select',
  5156. );
  5157. $this->assertTags($result, $expected);
  5158. }
  5159. /**
  5160. * testTextArea method
  5161. *
  5162. * @return void
  5163. */
  5164. public function testTextArea() {
  5165. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5166. $result = $this->Form->textarea('Model.field');
  5167. $expected = array(
  5168. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5169. 'some test data',
  5170. '/textarea',
  5171. );
  5172. $this->assertTags($result, $expected);
  5173. $result = $this->Form->textarea('Model.tmp');
  5174. $expected = array(
  5175. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5176. '/textarea',
  5177. );
  5178. $this->assertTags($result, $expected);
  5179. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5180. $result = $this->Form->textarea('Model.field');
  5181. $expected = array(
  5182. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5183. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5184. '/textarea',
  5185. );
  5186. $this->assertTags($result, $expected);
  5187. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5188. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5189. $expected = array(
  5190. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5191. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5192. '/textarea',
  5193. );
  5194. $this->assertTags($result, $expected);
  5195. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5196. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5197. $expected = array(
  5198. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5199. '/textarea'
  5200. );
  5201. $this->assertTags($result, $expected);
  5202. }
  5203. /**
  5204. * testTextAreaWithStupidCharacters method
  5205. *
  5206. * test text area with non-ascii characters
  5207. *
  5208. * @return void
  5209. */
  5210. public function testTextAreaWithStupidCharacters() {
  5211. $this->loadFixtures('Post');
  5212. $result = $this->Form->input('Post.content', array(
  5213. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5214. ));
  5215. $expected = array(
  5216. 'div' => array('class' => 'input text'),
  5217. 'label' => array('for' => 'PostContent'),
  5218. 'Current Text',
  5219. '/label',
  5220. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5221. 'GREAT®',
  5222. '/textarea',
  5223. '/div'
  5224. );
  5225. $this->assertTags($result, $expected);
  5226. }
  5227. /**
  5228. * testHiddenField method
  5229. *
  5230. * @return void
  5231. */
  5232. public function testHiddenField() {
  5233. $Contact = ClassRegistry::getObject('Contact');
  5234. $Contact->validationErrors['field'] = 1;
  5235. $this->Form->request->data['Contact']['field'] = 'test';
  5236. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5237. $this->assertTags($result, array(
  5238. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5239. );
  5240. }
  5241. /**
  5242. * testFileUploadField method
  5243. *
  5244. * @return void
  5245. */
  5246. public function testFileUploadField() {
  5247. $result = $this->Form->file('Model.upload');
  5248. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5249. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5250. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5251. $expected = array(
  5252. 'div' => array('class' => 'input file'),
  5253. 'label' => array('for' => 'ModelUpload'),
  5254. 'Upload',
  5255. '/label',
  5256. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5257. '/div'
  5258. );
  5259. $this->assertTags($result, $expected);
  5260. }
  5261. /**
  5262. * test File upload input on a model not used in create();
  5263. *
  5264. * @return void
  5265. */
  5266. public function testFileUploadOnOtherModel() {
  5267. $this->Form->create('ValidateUser', array('type' => 'file'));
  5268. $result = $this->Form->file('ValidateProfile.city');
  5269. $expected = array(
  5270. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. }
  5274. /**
  5275. * testButton method
  5276. *
  5277. * @return void
  5278. */
  5279. public function testButton() {
  5280. $result = $this->Form->button('Hi');
  5281. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5282. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5283. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5284. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5285. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5286. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5287. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5288. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5289. $this->assertNoPattern('/\&039/', $result);
  5290. }
  5291. /**
  5292. * Test that button() makes unlocked fields by default.
  5293. *
  5294. * @return void
  5295. */
  5296. public function testButtonUnlockedByDefault() {
  5297. $this->Form->request->params['_Token']['key'] = 'secured';
  5298. $this->Form->button('Save', array('name' => 'save'));
  5299. $this->Form->button('Clear');
  5300. $result = $this->Form->unlockField();
  5301. $this->assertEquals(array('save'), $result);
  5302. }
  5303. /**
  5304. * testPostButton method
  5305. *
  5306. * @return void
  5307. */
  5308. public function testPostButton() {
  5309. $result = $this->Form->postButton('Hi', '/controller/action');
  5310. $this->assertTags($result, array(
  5311. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'style' => 'display:none;'),
  5312. 'div' => array('style' => 'display:none;'),
  5313. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5314. '/div',
  5315. 'button' => array('type' => 'submit'),
  5316. 'Hi',
  5317. '/button',
  5318. '/form'
  5319. ));
  5320. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5321. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5322. }
  5323. /**
  5324. * Test that postButton adds _Token fields.
  5325. *
  5326. * @return void
  5327. */
  5328. public function testSecurePostButton() {
  5329. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5330. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5331. $expected = array(
  5332. 'form' => array(
  5333. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5334. 'style' => 'display:none;'
  5335. ),
  5336. array('div' => array('style' => 'display:none;')),
  5337. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5338. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5339. '/div',
  5340. 'button' => array('type' => 'submit'),
  5341. 'Delete',
  5342. '/button',
  5343. array('div' => array('style' => 'display:none;')),
  5344. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5345. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5346. '/div',
  5347. '/form',
  5348. );
  5349. $this->assertTags($result, $expected);
  5350. }
  5351. /**
  5352. * testPostLink method
  5353. *
  5354. * @return void
  5355. */
  5356. public function testPostLink() {
  5357. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5358. $this->assertTags($result, array(
  5359. 'form' => array(
  5360. 'method' => 'post', 'action' => '/posts/delete/1',
  5361. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5362. ),
  5363. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5364. '/form',
  5365. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5366. 'Delete',
  5367. '/a'
  5368. ));
  5369. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5370. $this->assertTags($result, array(
  5371. 'form' => array(
  5372. 'method' => 'post', 'action' => '/posts/delete/1',
  5373. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5374. ),
  5375. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5376. '/form',
  5377. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5378. 'Delete',
  5379. '/a'
  5380. ));
  5381. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5382. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5383. }
  5384. /**
  5385. * Test that postLink adds _Token fields.
  5386. *
  5387. * @return void
  5388. */
  5389. public function testSecurePostLink() {
  5390. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5391. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5392. $expected = array(
  5393. 'form' => array(
  5394. 'method' => 'post', 'action' => '/posts/delete/1',
  5395. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5396. ),
  5397. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5398. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5399. 'div' => array('style' => 'display:none;'),
  5400. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5401. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5402. '/div',
  5403. '/form',
  5404. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5405. 'Delete',
  5406. '/a'
  5407. );
  5408. $this->assertTags($result, $expected);
  5409. }
  5410. /**
  5411. * testSubmitButton method
  5412. *
  5413. * @return void
  5414. */
  5415. public function testSubmitButton() {
  5416. $result = $this->Form->submit('');
  5417. $expected = array(
  5418. 'div' => array('class' => 'submit'),
  5419. 'input' => array('type' => 'submit', 'value' => ''),
  5420. '/div'
  5421. );
  5422. $this->assertTags($result, $expected);
  5423. $result = $this->Form->submit('Test Submit');
  5424. $expected = array(
  5425. 'div' => array('class' => 'submit'),
  5426. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5427. '/div'
  5428. );
  5429. $this->assertTags($result, $expected);
  5430. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5431. $expected = array(
  5432. 'span' => array('class' => 'submit'),
  5433. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5434. '/span'
  5435. );
  5436. $this->assertTags($result, $expected);
  5437. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5438. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5439. $this->assertTags($result, $expected);
  5440. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5441. $expected = array(
  5442. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5443. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5444. '/div'
  5445. );
  5446. $this->assertTags($result, $expected);
  5447. $result = $this->Form->submit('Next >');
  5448. $expected = array(
  5449. 'div' => array('class' => 'submit'),
  5450. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5451. '/div'
  5452. );
  5453. $this->assertTags($result, $expected);
  5454. $result = $this->Form->submit('Next >', array('escape' => false));
  5455. $expected = array(
  5456. 'div' => array('class' => 'submit'),
  5457. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5458. '/div'
  5459. );
  5460. $this->assertTags($result, $expected);
  5461. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5462. $expected = array(
  5463. 'div' => array('class' => 'submit'),
  5464. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5465. '/div'
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. $before = '--before--';
  5469. $after = '--after--';
  5470. $result = $this->Form->submit('Test', array('before' => $before));
  5471. $expected = array(
  5472. 'div' => array('class' => 'submit'),
  5473. '--before--',
  5474. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5475. '/div'
  5476. );
  5477. $this->assertTags($result, $expected);
  5478. $result = $this->Form->submit('Test', array('after' => $after));
  5479. $expected = array(
  5480. 'div' => array('class' => 'submit'),
  5481. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5482. '--after--',
  5483. '/div'
  5484. );
  5485. $this->assertTags($result, $expected);
  5486. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5487. $expected = array(
  5488. 'div' => array('class' => 'submit'),
  5489. '--before--',
  5490. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5491. '--after--',
  5492. '/div'
  5493. );
  5494. $this->assertTags($result, $expected);
  5495. }
  5496. /**
  5497. * test image submit types.
  5498. *
  5499. * @return void
  5500. */
  5501. public function testSubmitImage() {
  5502. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5503. $expected = array(
  5504. 'div' => array('class' => 'submit'),
  5505. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5506. '/div'
  5507. );
  5508. $this->assertTags($result, $expected);
  5509. $result = $this->Form->submit('/relative/cake.power.gif');
  5510. $expected = array(
  5511. 'div' => array('class' => 'submit'),
  5512. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5513. '/div'
  5514. );
  5515. $this->assertTags($result, $expected);
  5516. $result = $this->Form->submit('cake.power.gif');
  5517. $expected = array(
  5518. 'div' => array('class' => 'submit'),
  5519. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5520. '/div'
  5521. );
  5522. $this->assertTags($result, $expected);
  5523. $result = $this->Form->submit('Not.an.image');
  5524. $expected = array(
  5525. 'div' => array('class' => 'submit'),
  5526. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5527. '/div'
  5528. );
  5529. $this->assertTags($result, $expected);
  5530. $after = '--after--';
  5531. $before = '--before--';
  5532. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5533. $expected = array(
  5534. 'div' => array('class' => 'submit'),
  5535. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5536. '--after--',
  5537. '/div'
  5538. );
  5539. $this->assertTags($result, $expected);
  5540. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5541. $expected = array(
  5542. 'div' => array('class' => 'submit'),
  5543. '--before--',
  5544. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5545. '/div'
  5546. );
  5547. $this->assertTags($result, $expected);
  5548. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5549. $expected = array(
  5550. 'div' => array('class' => 'submit'),
  5551. '--before--',
  5552. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5553. '--after--',
  5554. '/div'
  5555. );
  5556. $this->assertTags($result, $expected);
  5557. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5558. $expected = array(
  5559. 'div' => array('class' => 'submit'),
  5560. '--before--',
  5561. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5562. '--after--',
  5563. '/div'
  5564. );
  5565. $this->assertTags($result, $expected);
  5566. }
  5567. /**
  5568. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5569. * be submitted at a time.
  5570. *
  5571. * @return void
  5572. */
  5573. public function testSubmitUnlockedByDefault() {
  5574. $this->Form->request->params['_Token']['key'] = 'secured';
  5575. $this->Form->submit('Go go');
  5576. $this->Form->submit('Save', array('name' => 'save'));
  5577. $result = $this->Form->unlockField();
  5578. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5579. }
  5580. /**
  5581. * Test submit image with timestamps.
  5582. *
  5583. * @return void
  5584. */
  5585. function testSubmitImageTimestamp() {
  5586. Configure::write('Asset.timestamp', 'force');
  5587. $result = $this->Form->submit('cake.power.gif');
  5588. $expected = array(
  5589. 'div' => array('class' => 'submit'),
  5590. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5591. '/div'
  5592. );
  5593. $this->assertTags($result, $expected);
  5594. }
  5595. /**
  5596. * test the create() method
  5597. *
  5598. * @return void
  5599. */
  5600. public function testCreate() {
  5601. $result = $this->Form->create('Contact');
  5602. $encoding = strtolower(Configure::read('App.encoding'));
  5603. $expected = array(
  5604. 'form' => array(
  5605. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5606. 'accept-charset' => $encoding
  5607. ),
  5608. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5609. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5610. '/div'
  5611. );
  5612. $this->assertTags($result, $expected);
  5613. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5614. $expected = array('form' => array(
  5615. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5616. 'accept-charset' => $encoding
  5617. ));
  5618. $this->assertTags($result, $expected);
  5619. $result = $this->Form->create('Contact', array('type' => 'get'));
  5620. $expected = array('form' => array(
  5621. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5622. 'accept-charset' => $encoding
  5623. ));
  5624. $this->assertTags($result, $expected);
  5625. $result = $this->Form->create('Contact', array('type' => 'put'));
  5626. $expected = array(
  5627. 'form' => array(
  5628. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5629. 'accept-charset' => $encoding
  5630. ),
  5631. 'div' => array('style' => 'display:none;'),
  5632. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5633. '/div'
  5634. );
  5635. $this->assertTags($result, $expected);
  5636. $result = $this->Form->create('Contact', array('type' => 'file'));
  5637. $expected = array(
  5638. 'form' => array(
  5639. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5640. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5641. ),
  5642. 'div' => array('style' => 'display:none;'),
  5643. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5644. '/div'
  5645. );
  5646. $this->assertTags($result, $expected);
  5647. $this->Form->request->data['Contact']['id'] = 1;
  5648. $this->Form->request->here = '/contacts/edit/1';
  5649. $this->Form->request['action'] = 'edit';
  5650. $result = $this->Form->create('Contact');
  5651. $expected = array(
  5652. 'form' => array(
  5653. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5654. 'accept-charset' => $encoding
  5655. ),
  5656. 'div' => array('style' => 'display:none;'),
  5657. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5658. '/div'
  5659. );
  5660. $this->assertTags($result, $expected);
  5661. $this->Form->request->data['Contact']['id'] = 1;
  5662. $this->Form->request->here = '/contacts/edit/1';
  5663. $this->Form->request['action'] = 'edit';
  5664. $result = $this->Form->create('Contact', array('type' => 'file'));
  5665. $expected = array(
  5666. 'form' => array(
  5667. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5668. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5669. ),
  5670. 'div' => array('style' => 'display:none;'),
  5671. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5672. '/div'
  5673. );
  5674. $this->assertTags($result, $expected);
  5675. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5676. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5677. $expected = array(
  5678. 'form' => array(
  5679. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5680. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5681. ),
  5682. 'div' => array('style' => 'display:none;'),
  5683. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5684. '/div'
  5685. );
  5686. $this->assertTags($result, $expected);
  5687. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5688. $expected = array(
  5689. 'form' => array(
  5690. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5691. 'accept-charset' => $encoding
  5692. ),
  5693. 'div' => array('style' => 'display:none;'),
  5694. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5695. '/div'
  5696. );
  5697. $this->assertTags($result, $expected);
  5698. $this->Form->request['action'] = 'add';
  5699. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5700. $expected = array(
  5701. 'form' => array(
  5702. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5703. 'accept-charset' => $encoding
  5704. ),
  5705. 'div' => array('style' => 'display:none;'),
  5706. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5707. '/div'
  5708. );
  5709. $this->assertTags($result, $expected);
  5710. $result = $this->Form->create('User', array('action' => 'login'));
  5711. $expected = array(
  5712. 'form' => array(
  5713. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5714. 'accept-charset' => $encoding
  5715. ),
  5716. 'div' => array('style' => 'display:none;'),
  5717. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5718. '/div'
  5719. );
  5720. $this->assertTags($result, $expected);
  5721. $result = $this->Form->create('User', array('url' => '/users/login'));
  5722. $expected = array(
  5723. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5724. 'div' => array('style' => 'display:none;'),
  5725. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5726. '/div'
  5727. );
  5728. $this->assertTags($result, $expected);
  5729. $this->Form->request['controller'] = 'pages';
  5730. $result = $this->Form->create('User', array('action' => 'signup'));
  5731. $expected = array(
  5732. 'form' => array(
  5733. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5734. 'accept-charset' => $encoding
  5735. ),
  5736. 'div' => array('style' => 'display:none;'),
  5737. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5738. '/div'
  5739. );
  5740. $this->assertTags($result, $expected);
  5741. $this->Form->request->data = array();
  5742. $this->Form->request['controller'] = 'contacts';
  5743. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5744. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5745. $expected = array(
  5746. 'form' => array(
  5747. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5748. 'accept-charset' => 'utf-8'
  5749. ),
  5750. 'div' => array('style' => 'display:none;'),
  5751. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5752. '/div'
  5753. );
  5754. $this->assertTags($result, $expected);
  5755. }
  5756. /**
  5757. * Test the onsubmit option for create()
  5758. *
  5759. * @return void
  5760. */
  5761. public function testCreateOnSubmit() {
  5762. $this->Form->request->data = array();
  5763. $this->Form->request['controller'] = 'contacts';
  5764. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5765. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5766. $expected = array(
  5767. 'form' => array(
  5768. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5769. 'accept-charset' => 'utf-8'
  5770. ),
  5771. 'div' => array('style' => 'display:none;'),
  5772. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5773. '/div'
  5774. );
  5775. $this->assertTags($result, $expected);
  5776. $this->Form->request->data = array();
  5777. $this->Form->request['controller'] = 'contacts';
  5778. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5779. $result = $this->Form->create(array(
  5780. 'url' => array('action' => 'index', 'param'),
  5781. 'default' => false,
  5782. 'onsubmit' => 'someFunction();'
  5783. ));
  5784. $expected = array(
  5785. 'form' => array(
  5786. 'id' => 'ContactAddForm', 'method' => 'post',
  5787. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  5788. 'action' => '/contacts/index/param',
  5789. 'accept-charset' => 'utf-8'
  5790. ),
  5791. 'div' => array('style' => 'display:none;'),
  5792. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5793. '/div'
  5794. );
  5795. $this->assertTags($result, $expected);
  5796. }
  5797. /**
  5798. * test create() with automatic url generation
  5799. *
  5800. * @return void
  5801. */
  5802. public function testCreateAutoUrl() {
  5803. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  5804. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  5805. $this->Form->request->base = '/base_url';
  5806. $result = $this->Form->create('Contact');
  5807. $expected = array(
  5808. 'form' => array(
  5809. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  5810. 'accept-charset' => 'utf-8'
  5811. ),
  5812. 'div' => array('style' => 'display:none;'),
  5813. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5814. '/div'
  5815. );
  5816. $this->assertTags($result, $expected);
  5817. $this->Form->request['action'] = 'delete';
  5818. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  5819. $this->Form->request->base = '/base_url';
  5820. $result = $this->Form->create('Contact');
  5821. $expected = array(
  5822. 'form' => array(
  5823. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  5824. 'accept-charset' => 'utf-8'
  5825. ),
  5826. 'div' => array('style' => 'display:none;'),
  5827. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5828. '/div'
  5829. );
  5830. $this->assertTags($result, $expected);
  5831. }
  5832. /**
  5833. * test create() with a custom route
  5834. *
  5835. * @return void
  5836. */
  5837. public function testCreateCustomRoute() {
  5838. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  5839. $encoding = strtolower(Configure::read('App.encoding'));
  5840. $result = $this->Form->create('User', array('action' => 'login'));
  5841. $expected = array(
  5842. 'form' => array(
  5843. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  5844. 'accept-charset' => $encoding
  5845. ),
  5846. 'div' => array('style' => 'display:none;'),
  5847. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5848. '/div'
  5849. );
  5850. $this->assertTags($result, $expected);
  5851. }
  5852. /**
  5853. * test that inputDefaults are stored and used.
  5854. *
  5855. * @return void
  5856. */
  5857. public function testCreateWithInputDefaults() {
  5858. $this->Form->create('User', array(
  5859. 'inputDefaults' => array('div' => false, 'label' => false)
  5860. ));
  5861. $result = $this->Form->input('username');
  5862. $expected = array(
  5863. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  5864. );
  5865. $this->assertTags($result, $expected);
  5866. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  5867. $expected = array(
  5868. 'div' => array('class' => 'input text'),
  5869. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  5870. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  5871. '/div'
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. }
  5875. /**
  5876. * test automatic accept-charset overriding
  5877. *
  5878. * @return void
  5879. */
  5880. public function testCreateWithAcceptCharset() {
  5881. $result = $this->Form->create('UserForm', array(
  5882. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  5883. )
  5884. );
  5885. $expected = array(
  5886. 'form' => array(
  5887. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  5888. 'accept-charset' => 'iso-8859-1'
  5889. ),
  5890. 'div' => array('style' => 'display:none;'),
  5891. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5892. '/div'
  5893. );
  5894. $this->assertTags($result, $expected);
  5895. }
  5896. /**
  5897. * Test base form url when url param is passed with multiple parameters (&)
  5898. *
  5899. */
  5900. public function testCreateQuerystringrequest() {
  5901. $encoding = strtolower(Configure::read('App.encoding'));
  5902. $result = $this->Form->create('Contact', array(
  5903. 'type' => 'post',
  5904. 'escape' => false,
  5905. 'url' => array(
  5906. 'controller' => 'controller',
  5907. 'action' => 'action',
  5908. '?' => array('param1' => 'value1', 'param2' => 'value2')
  5909. )
  5910. ));
  5911. $expected = array(
  5912. 'form' => array(
  5913. 'id' => 'ContactAddForm',
  5914. 'method' => 'post',
  5915. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  5916. 'accept-charset' => $encoding
  5917. ),
  5918. 'div' => array('style' => 'display:none;'),
  5919. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5920. '/div'
  5921. );
  5922. $this->assertTags($result, $expected);
  5923. $result = $this->Form->create('Contact', array(
  5924. 'type' => 'post',
  5925. 'url' => array(
  5926. 'controller' => 'controller',
  5927. 'action' => 'action',
  5928. '?' => array('param1' => 'value1', 'param2' => 'value2')
  5929. )
  5930. ));
  5931. $expected = array(
  5932. 'form' => array(
  5933. 'id' => 'ContactAddForm',
  5934. 'method' => 'post',
  5935. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  5936. 'accept-charset' => $encoding
  5937. ),
  5938. 'div' => array('style' => 'display:none;'),
  5939. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5940. '/div'
  5941. );
  5942. $this->assertTags($result, $expected);
  5943. }
  5944. /**
  5945. * test that create() doesn't cause errors by multiple id's being in the primary key
  5946. * as could happen with multiple select or checkboxes.
  5947. *
  5948. * @return void
  5949. */
  5950. public function testCreateWithMultipleIdInData() {
  5951. $encoding = strtolower(Configure::read('App.encoding'));
  5952. $this->Form->request->data['Contact']['id'] = array(1, 2);
  5953. $result = $this->Form->create('Contact');
  5954. $expected = array(
  5955. 'form' => array(
  5956. 'id' => 'ContactAddForm',
  5957. 'method' => 'post',
  5958. 'action' => '/contacts/add',
  5959. 'accept-charset' => $encoding
  5960. ),
  5961. 'div' => array('style' => 'display:none;'),
  5962. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5963. '/div'
  5964. );
  5965. $this->assertTags($result, $expected);
  5966. }
  5967. /**
  5968. * test that create() doesn't add in extra passed params.
  5969. *
  5970. * @return void
  5971. */
  5972. public function testCreatePassedArgs() {
  5973. $encoding = strtolower(Configure::read('App.encoding'));
  5974. $this->Form->request->data['Contact']['id'] = 1;
  5975. $result = $this->Form->create('Contact', array(
  5976. 'type' => 'post',
  5977. 'escape' => false,
  5978. 'url' => array(
  5979. 'action' => 'edit',
  5980. 'myparam'
  5981. )
  5982. ));
  5983. $expected = array(
  5984. 'form' => array(
  5985. 'id' => 'ContactAddForm',
  5986. 'method' => 'post',
  5987. 'action' => '/contacts/edit/myparam',
  5988. 'accept-charset' => $encoding
  5989. ),
  5990. 'div' => array('style' => 'display:none;'),
  5991. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5992. '/div'
  5993. );
  5994. $this->assertTags($result, $expected);
  5995. }
  5996. /**
  5997. * test creating a get form, and get form inputs.
  5998. *
  5999. * @return void
  6000. */
  6001. public function testGetFormCreate() {
  6002. $encoding = strtolower(Configure::read('App.encoding'));
  6003. $result = $this->Form->create('Contact', array('type' => 'get'));
  6004. $this->assertTags($result, array('form' => array(
  6005. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6006. 'accept-charset' => $encoding
  6007. )));
  6008. $result = $this->Form->text('Contact.name');
  6009. $this->assertTags($result, array('input' => array(
  6010. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6011. )));
  6012. $result = $this->Form->password('password');
  6013. $this->assertTags($result, array('input' => array(
  6014. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6015. )));
  6016. $this->assertNoPattern('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6017. $result = $this->Form->text('user_form');
  6018. $this->assertTags($result, array('input' => array(
  6019. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6020. )));
  6021. }
  6022. /**
  6023. * test get form, and inputs when the model param is false
  6024. *
  6025. * @return void
  6026. */
  6027. public function testGetFormWithFalseModel() {
  6028. $encoding = strtolower(Configure::read('App.encoding'));
  6029. $this->Form->request['controller'] = 'contact_test';
  6030. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6031. $expected = array('form' => array(
  6032. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6033. 'accept-charset' => $encoding
  6034. ));
  6035. $this->assertTags($result, $expected);
  6036. $result = $this->Form->text('reason');
  6037. $expected = array(
  6038. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6039. );
  6040. $this->assertTags($result, $expected);
  6041. }
  6042. /**
  6043. * test that datetime() works with GET style forms.
  6044. *
  6045. * @return void
  6046. */
  6047. public function testDateTimeWithGetForms() {
  6048. extract($this->dateRegex);
  6049. $this->Form->create('Contact', array('type' => 'get'));
  6050. $result = $this->Form->datetime('created');
  6051. $this->assertPattern('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6052. $this->assertPattern('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6053. $this->assertPattern('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6054. $this->assertPattern('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6055. $this->assertPattern('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6056. $this->assertPattern('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6057. }
  6058. /**
  6059. * testEditFormWithData method
  6060. *
  6061. * test auto populating form elements from submitted data.
  6062. *
  6063. * @return void
  6064. */
  6065. public function testEditFormWithData() {
  6066. $this->Form->request->data = array('Person' => array(
  6067. 'id' => 1,
  6068. 'first_name' => 'Nate',
  6069. 'last_name' => 'Abele',
  6070. 'email' => 'nate@example.com'
  6071. ));
  6072. $this->Form->request->addParams(array(
  6073. 'models' => array('Person'),
  6074. 'controller' => 'people',
  6075. 'action' => 'add'
  6076. ));
  6077. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6078. $this->Form->create();
  6079. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6080. $expected = array(
  6081. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6082. 'select' => array(
  6083. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6084. ),
  6085. array('option' => array('value' => 1)), 'Nate', '/option',
  6086. array('option' => array('value' => 2)), 'Garrett', '/option',
  6087. array('option' => array('value' => 3)), 'Larry', '/option',
  6088. '/select'
  6089. );
  6090. $this->assertTags($result, $expected);
  6091. }
  6092. /**
  6093. * Test that required fields are created for various types of validation.
  6094. *
  6095. * @return void
  6096. */
  6097. function testFormInputRequiredDetection() {
  6098. $this->Form->create('Contact');
  6099. $result = $this->Form->input('Contact.non_existing');
  6100. $expected = array(
  6101. 'div' => array('class' => 'input text required'),
  6102. 'label' => array('for' => 'ContactNonExisting'),
  6103. 'Non Existing',
  6104. '/label',
  6105. 'input' => array(
  6106. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6107. 'id' => 'ContactNonExisting'
  6108. ),
  6109. '/div'
  6110. );
  6111. $this->assertTags($result, $expected);
  6112. $result = $this->Form->input('Contact.imrequired');
  6113. $expected = array(
  6114. 'div' => array('class' => 'input text required'),
  6115. 'label' => array('for' => 'ContactImrequired'),
  6116. 'Imrequired',
  6117. '/label',
  6118. 'input' => array(
  6119. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6120. 'id' => 'ContactImrequired'
  6121. ),
  6122. '/div'
  6123. );
  6124. $this->assertTags($result, $expected);
  6125. $result = $this->Form->input('Contact.imalsorequired');
  6126. $expected = array(
  6127. 'div' => array('class' => 'input text required'),
  6128. 'label' => array('for' => 'ContactImalsorequired'),
  6129. 'Imalsorequired',
  6130. '/label',
  6131. 'input' => array(
  6132. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6133. 'id' => 'ContactImalsorequired'
  6134. ),
  6135. '/div'
  6136. );
  6137. $this->assertTags($result, $expected);
  6138. $result = $this->Form->input('Contact.imrequiredtoo');
  6139. $expected = array(
  6140. 'div' => array('class' => 'input text required'),
  6141. 'label' => array('for' => 'ContactImrequiredtoo'),
  6142. 'Imrequiredtoo',
  6143. '/label',
  6144. 'input' => array(
  6145. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6146. 'id' => 'ContactImrequiredtoo'
  6147. ),
  6148. '/div'
  6149. );
  6150. $this->assertTags($result, $expected);
  6151. $result = $this->Form->input('Contact.required_one');
  6152. $expected = array(
  6153. 'div' => array('class' => 'input text required'),
  6154. 'label' => array('for' => 'ContactRequiredOne'),
  6155. 'Required One',
  6156. '/label',
  6157. 'input' => array(
  6158. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6159. 'id' => 'ContactRequiredOne'
  6160. ),
  6161. '/div'
  6162. );
  6163. $this->assertTags($result, $expected);
  6164. $result = $this->Form->input('Contact.string_required');
  6165. $expected = array(
  6166. 'div' => array('class' => 'input text required'),
  6167. 'label' => array('for' => 'ContactStringRequired'),
  6168. 'String Required',
  6169. '/label',
  6170. 'input' => array(
  6171. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6172. 'id' => 'ContactStringRequired'
  6173. ),
  6174. '/div'
  6175. );
  6176. $this->assertTags($result, $expected);
  6177. $result = $this->Form->input('Contact.imnotrequired');
  6178. $expected = array(
  6179. 'div' => array('class' => 'input text'),
  6180. 'label' => array('for' => 'ContactImnotrequired'),
  6181. 'Imnotrequired',
  6182. '/label',
  6183. 'input' => array(
  6184. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6185. 'id' => 'ContactImnotrequired'
  6186. ),
  6187. '/div'
  6188. );
  6189. $this->assertTags($result, $expected);
  6190. $result = $this->Form->input('Contact.imalsonotrequired');
  6191. $expected = array(
  6192. 'div' => array('class' => 'input text'),
  6193. 'label' => array('for' => 'ContactImalsonotrequired'),
  6194. 'Imalsonotrequired',
  6195. '/label',
  6196. 'input' => array(
  6197. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6198. 'id' => 'ContactImalsonotrequired'
  6199. ),
  6200. '/div'
  6201. );
  6202. $this->assertTags($result, $expected);
  6203. $result = $this->Form->input('Contact.imnotrequiredeither');
  6204. $expected = array(
  6205. 'div' => array('class' => 'input text'),
  6206. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6207. 'Imnotrequiredeither',
  6208. '/label',
  6209. 'input' => array(
  6210. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6211. 'id' => 'ContactImnotrequiredeither'
  6212. ),
  6213. '/div'
  6214. );
  6215. $this->assertTags($result, $expected);
  6216. }
  6217. /**
  6218. * testFormMagicInput method
  6219. *
  6220. * @return void
  6221. */
  6222. public function testFormMagicInput() {
  6223. $encoding = strtolower(Configure::read('App.encoding'));
  6224. $result = $this->Form->create('Contact');
  6225. $expected = array(
  6226. 'form' => array(
  6227. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6228. 'accept-charset' => $encoding
  6229. ),
  6230. 'div' => array('style' => 'display:none;'),
  6231. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6232. '/div'
  6233. );
  6234. $this->assertTags($result, $expected);
  6235. $result = $this->Form->input('name');
  6236. $expected = array(
  6237. 'div' => array('class' => 'input text'),
  6238. 'label' => array('for' => 'ContactName'),
  6239. 'Name',
  6240. '/label',
  6241. 'input' => array(
  6242. 'type' => 'text', 'name' => 'data[Contact][name]',
  6243. 'id' => 'ContactName', 'maxlength' => '255'
  6244. ),
  6245. '/div'
  6246. );
  6247. $this->assertTags($result, $expected);
  6248. $result = $this->Form->input('non_existing_field_in_contact_model');
  6249. $expected = array(
  6250. 'div' => array('class' => 'input text'),
  6251. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6252. 'Non Existing Field In Contact Model',
  6253. '/label',
  6254. 'input' => array(
  6255. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6256. 'id' => 'ContactNonExistingFieldInContactModel'
  6257. ),
  6258. '/div'
  6259. );
  6260. $this->assertTags($result, $expected);
  6261. $result = $this->Form->input('Address.street');
  6262. $expected = array(
  6263. 'div' => array('class' => 'input text'),
  6264. 'label' => array('for' => 'AddressStreet'),
  6265. 'Street',
  6266. '/label',
  6267. 'input' => array(
  6268. 'type' => 'text', 'name' => 'data[Address][street]',
  6269. 'id' => 'AddressStreet'
  6270. ),
  6271. '/div'
  6272. );
  6273. $this->assertTags($result, $expected);
  6274. $result = $this->Form->input('Address.non_existing_field_in_model');
  6275. $expected = array(
  6276. 'div' => array('class' => 'input text'),
  6277. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6278. 'Non Existing Field In Model',
  6279. '/label',
  6280. 'input' => array(
  6281. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6282. 'id' => 'AddressNonExistingFieldInModel'
  6283. ),
  6284. '/div'
  6285. );
  6286. $this->assertTags($result, $expected);
  6287. $result = $this->Form->input('name', array('div' => false));
  6288. $expected = array(
  6289. 'label' => array('for' => 'ContactName'),
  6290. 'Name',
  6291. '/label',
  6292. 'input' => array(
  6293. 'type' => 'text', 'name' => 'data[Contact][name]',
  6294. 'id' => 'ContactName', 'maxlength' => '255'
  6295. )
  6296. );
  6297. $this->assertTags($result, $expected);
  6298. extract($this->dateRegex);
  6299. $now = strtotime('now');
  6300. $result = $this->Form->input('Contact.published', array('div' => false));
  6301. $expected = array(
  6302. 'label' => array('for' => 'ContactPublishedMonth'),
  6303. 'Published',
  6304. '/label',
  6305. array('select' => array(
  6306. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6307. )),
  6308. $monthsRegex,
  6309. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6310. date('F', $now),
  6311. '/option',
  6312. '*/select',
  6313. '-',
  6314. array('select' => array(
  6315. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6316. )),
  6317. $daysRegex,
  6318. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6319. date('j', $now),
  6320. '/option',
  6321. '*/select',
  6322. '-',
  6323. array('select' => array(
  6324. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6325. )),
  6326. $yearsRegex,
  6327. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6328. date('Y', $now),
  6329. '*/select'
  6330. );
  6331. $this->assertTags($result, $expected);
  6332. $result = $this->Form->input('Contact.updated', array('div' => false));
  6333. $expected = array(
  6334. 'label' => array('for' => 'ContactUpdatedMonth'),
  6335. 'Updated',
  6336. '/label',
  6337. array('select' => array(
  6338. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6339. )),
  6340. $monthsRegex,
  6341. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6342. date('F', $now),
  6343. '/option',
  6344. '*/select',
  6345. '-',
  6346. array('select' => array(
  6347. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6348. )),
  6349. $daysRegex,
  6350. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6351. date('j', $now),
  6352. '/option',
  6353. '*/select',
  6354. '-',
  6355. array('select' => array(
  6356. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6357. )),
  6358. $yearsRegex,
  6359. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6360. date('Y', $now),
  6361. '*/select'
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. $result = $this->Form->input('UserForm.stuff');
  6365. $expected = array(
  6366. 'div' => array('class' => 'input text'),
  6367. 'label' => array('for' => 'UserFormStuff'),
  6368. 'Stuff',
  6369. '/label',
  6370. 'input' => array(
  6371. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6372. 'id' => 'UserFormStuff', 'maxlength' => 10
  6373. ),
  6374. '/div'
  6375. );
  6376. $this->assertTags($result, $expected);
  6377. }
  6378. /**
  6379. * testForMagicInputNonExistingNorValidated method
  6380. *
  6381. * @return void
  6382. */
  6383. public function testForMagicInputNonExistingNorValidated() {
  6384. $encoding = strtolower(Configure::read('App.encoding'));
  6385. $result = $this->Form->create('Contact');
  6386. $expected = array(
  6387. 'form' => array(
  6388. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6389. 'accept-charset' => $encoding
  6390. ),
  6391. 'div' => array('style' => 'display:none;'),
  6392. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6393. '/div'
  6394. );
  6395. $this->assertTags($result, $expected);
  6396. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6397. $expected = array(
  6398. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6399. 'Non Existing Nor Validated',
  6400. '/label',
  6401. 'input' => array(
  6402. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6403. 'id' => 'ContactNonExistingNorValidated'
  6404. )
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6408. 'div' => false, 'value' => 'my value'
  6409. ));
  6410. $expected = array(
  6411. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6412. 'Non Existing Nor Validated',
  6413. '/label',
  6414. 'input' => array(
  6415. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6416. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6417. )
  6418. );
  6419. $this->assertTags($result, $expected);
  6420. $this->Form->request->data = array(
  6421. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6422. ));
  6423. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6424. $expected = array(
  6425. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6426. 'Non Existing Nor Validated',
  6427. '/label',
  6428. 'input' => array(
  6429. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6430. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6431. )
  6432. );
  6433. $this->assertTags($result, $expected);
  6434. }
  6435. /**
  6436. * testFormMagicInputLabel method
  6437. *
  6438. * @return void
  6439. */
  6440. public function testFormMagicInputLabel() {
  6441. $encoding = strtolower(Configure::read('App.encoding'));
  6442. $result = $this->Form->create('Contact');
  6443. $expected = array(
  6444. 'form' => array(
  6445. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6446. 'accept-charset' => $encoding
  6447. ),
  6448. 'div' => array('style' => 'display:none;'),
  6449. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6450. '/div'
  6451. );
  6452. $this->assertTags($result, $expected);
  6453. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6454. $this->assertTags($result, array('input' => array(
  6455. 'name' => 'data[Contact][name]', 'type' => 'text',
  6456. 'id' => 'ContactName', 'maxlength' => '255')
  6457. ));
  6458. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6459. $expected = array(
  6460. 'label' => array('for' => 'ContactName'),
  6461. 'My label',
  6462. '/label',
  6463. 'input' => array(
  6464. 'type' => 'text', 'name' => 'data[Contact][name]',
  6465. 'id' => 'ContactName', 'maxlength' => '255'
  6466. )
  6467. );
  6468. $this->assertTags($result, $expected);
  6469. $result = $this->Form->input('Contact.name', array(
  6470. 'div' => false, 'label' => array('class' => 'mandatory')
  6471. ));
  6472. $expected = array(
  6473. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6474. 'Name',
  6475. '/label',
  6476. 'input' => array(
  6477. 'type' => 'text', 'name' => 'data[Contact][name]',
  6478. 'id' => 'ContactName', 'maxlength' => '255'
  6479. )
  6480. );
  6481. $this->assertTags($result, $expected);
  6482. $result = $this->Form->input('Contact.name', array(
  6483. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6484. ));
  6485. $expected = array(
  6486. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6487. 'My label',
  6488. '/label',
  6489. 'input' => array(
  6490. 'type' => 'text', 'name' => 'data[Contact][name]',
  6491. 'id' => 'ContactName', 'maxlength' => '255'
  6492. )
  6493. );
  6494. $this->assertTags($result, $expected);
  6495. $result = $this->Form->input('Contact.name', array(
  6496. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6497. ));
  6498. $expected = array(
  6499. 'label' => array('for' => 'my_id'),
  6500. 'Name',
  6501. '/label',
  6502. 'input' => array(
  6503. 'type' => 'text', 'name' => 'data[Contact][name]',
  6504. 'id' => 'my_id', 'maxlength' => '255'
  6505. )
  6506. );
  6507. $this->assertTags($result, $expected);
  6508. $result = $this->Form->input('1.id');
  6509. $this->assertTags($result, array('input' => array(
  6510. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6511. 'id' => 'Contact1Id'
  6512. )));
  6513. $result = $this->Form->input("1.name");
  6514. $expected = array(
  6515. 'div' => array('class' => 'input text'),
  6516. 'label' => array('for' => 'Contact1Name'),
  6517. 'Name',
  6518. '/label',
  6519. 'input' => array(
  6520. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6521. 'id' => 'Contact1Name', 'maxlength' => '255'
  6522. ),
  6523. '/div'
  6524. );
  6525. $this->assertTags($result, $expected);
  6526. $result = $this->Form->input('Contact.1.id');
  6527. $this->assertTags($result, array(
  6528. 'input' => array(
  6529. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6530. 'id' => 'Contact1Id'
  6531. )
  6532. ));
  6533. $result = $this->Form->input("Model.1.name");
  6534. $expected = array(
  6535. 'div' => array('class' => 'input text'),
  6536. 'label' => array('for' => 'Model1Name'),
  6537. 'Name',
  6538. '/label',
  6539. 'input' => array(
  6540. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6541. 'id' => 'Model1Name'
  6542. ),
  6543. '/div'
  6544. );
  6545. $this->assertTags($result, $expected);
  6546. }
  6547. /**
  6548. * testFormEnd method
  6549. *
  6550. * @return void
  6551. */
  6552. public function testFormEnd() {
  6553. $this->assertEqual($this->Form->end(), '</form>');
  6554. $result = $this->Form->end('');
  6555. $expected = array(
  6556. 'div' => array('class' => 'submit'),
  6557. 'input' => array('type' => 'submit', 'value' => ''),
  6558. '/div',
  6559. '/form'
  6560. );
  6561. $this->assertTags($result, $expected);
  6562. $result = $this->Form->end(array('label' => ''));
  6563. $expected = array(
  6564. 'div' => array('class' => 'submit'),
  6565. 'input' => array('type' => 'submit', 'value' => ''),
  6566. '/div',
  6567. '/form'
  6568. );
  6569. $this->assertTags($result, $expected);
  6570. $result = $this->Form->end('save');
  6571. $expected = array(
  6572. 'div' => array('class' => 'submit'),
  6573. 'input' => array('type' => 'submit', 'value' => 'save'),
  6574. '/div',
  6575. '/form'
  6576. );
  6577. $this->assertTags($result, $expected);
  6578. $result = $this->Form->end(array('label' => 'save'));
  6579. $expected = array(
  6580. 'div' => array('class' => 'submit'),
  6581. 'input' => array('type' => 'submit', 'value' => 'save'),
  6582. '/div',
  6583. '/form'
  6584. );
  6585. $this->assertTags($result, $expected);
  6586. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6587. $expected = array(
  6588. 'div' => array('class' => 'submit'),
  6589. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6590. '/div',
  6591. '/form'
  6592. );
  6593. $this->assertTags($result, $expected);
  6594. $result = $this->Form->end(array('name' => 'Whatever'));
  6595. $expected = array(
  6596. 'div' => array('class' => 'submit'),
  6597. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6598. '/div',
  6599. '/form'
  6600. );
  6601. $this->assertTags($result, $expected);
  6602. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6603. $expected = array(
  6604. 'div' => array('class' => 'good'),
  6605. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6606. '/div',
  6607. '/form'
  6608. );
  6609. $this->assertTags($result, $expected);
  6610. $result = $this->Form->end(array(
  6611. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6612. ));
  6613. $expected = array(
  6614. 'div' => array('class' => 'good'),
  6615. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6616. '/div',
  6617. '/form'
  6618. );
  6619. $this->assertTags($result, $expected);
  6620. }
  6621. /**
  6622. * testMultipleFormWithIdFields method
  6623. *
  6624. * @return void
  6625. */
  6626. public function testMultipleFormWithIdFields() {
  6627. $this->Form->create('UserForm');
  6628. $result = $this->Form->input('id');
  6629. $this->assertTags($result, array('input' => array(
  6630. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6631. )));
  6632. $result = $this->Form->input('ValidateItem.id');
  6633. $this->assertTags($result, array('input' => array(
  6634. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6635. 'id' => 'ValidateItemId'
  6636. )));
  6637. $result = $this->Form->input('ValidateUser.id');
  6638. $this->assertTags($result, array('input' => array(
  6639. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6640. 'id' => 'ValidateUserId'
  6641. )));
  6642. }
  6643. /**
  6644. * testDbLessModel method
  6645. *
  6646. * @return void
  6647. */
  6648. public function testDbLessModel() {
  6649. $this->Form->create('TestMail');
  6650. $result = $this->Form->input('name');
  6651. $expected = array(
  6652. 'div' => array('class' => 'input text'),
  6653. 'label' => array('for' => 'TestMailName'),
  6654. 'Name',
  6655. '/label',
  6656. 'input' => array(
  6657. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6658. 'id' => 'TestMailName'
  6659. ),
  6660. '/div'
  6661. );
  6662. $this->assertTags($result, $expected);
  6663. ClassRegistry::init('TestMail');
  6664. $this->Form->create('TestMail');
  6665. $result = $this->Form->input('name');
  6666. $expected = array(
  6667. 'div' => array('class' => 'input text'),
  6668. 'label' => array('for' => 'TestMailName'),
  6669. 'Name',
  6670. '/label',
  6671. 'input' => array(
  6672. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6673. 'id' => 'TestMailName'
  6674. ),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. }
  6679. /**
  6680. * testBrokenness method
  6681. *
  6682. * @return void
  6683. */
  6684. public function testBrokenness() {
  6685. /*
  6686. * #4 This test has two parents and four children. By default (as of r7117) both
  6687. * parents are show but the first parent is missing a child. This is the inconsistency
  6688. * in the default behaviour - one parent has all children, the other does not - dependent
  6689. * on the data values.
  6690. */
  6691. $result = $this->Form->select('Model.field', array(
  6692. 'Fred' => array(
  6693. 'freds_son_1' => 'Fred',
  6694. 'freds_son_2' => 'Freddie'
  6695. ),
  6696. 'Bert' => array(
  6697. 'berts_son_1' => 'Albert',
  6698. 'berts_son_2' => 'Bertie')
  6699. ),
  6700. array('showParents' => true, 'empty' => false)
  6701. );
  6702. $expected = array(
  6703. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6704. array('optgroup' => array('label' => 'Fred')),
  6705. array('option' => array('value' => 'freds_son_1')),
  6706. 'Fred',
  6707. '/option',
  6708. array('option' => array('value' => 'freds_son_2')),
  6709. 'Freddie',
  6710. '/option',
  6711. '/optgroup',
  6712. array('optgroup' => array('label' => 'Bert')),
  6713. array('option' => array('value' => 'berts_son_1')),
  6714. 'Albert',
  6715. '/option',
  6716. array('option' => array('value' => 'berts_son_2')),
  6717. 'Bertie',
  6718. '/option',
  6719. '/optgroup',
  6720. '/select'
  6721. );
  6722. $this->assertTags($result, $expected);
  6723. /*
  6724. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6725. * changed, so we should expect the same select list data, just with a different name
  6726. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6727. * This is where data corruption can occur, because when a select value is missing from
  6728. * a list a form will substitute the first value in the list - without the user knowing.
  6729. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6730. * affect the availability of 3 => 'Three' as a valid option.
  6731. */
  6732. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6733. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6734. ));
  6735. $result = $this->Form->select(
  6736. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6737. );
  6738. $expected = array(
  6739. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6740. array('option' => array('value' => 1)),
  6741. 'One',
  6742. '/option',
  6743. array('option' => array('value' => 2)),
  6744. 'Two',
  6745. '/option',
  6746. array('optgroup' => array('label' => 'Three')),
  6747. array('option' => array('value' => 3)),
  6748. 'Three',
  6749. '/option',
  6750. array('option' => array('value' => 4)),
  6751. 'Four',
  6752. '/option',
  6753. array('option' => array('value' => 5)),
  6754. 'Five',
  6755. '/option',
  6756. '/optgroup',
  6757. '/select'
  6758. );
  6759. $this->assertTags($result, $expected);
  6760. }
  6761. /**
  6762. * Test the generation of fields for a multi record form.
  6763. *
  6764. * @return void
  6765. */
  6766. public function testMultiRecordForm() {
  6767. $this->Form->create('ValidateProfile');
  6768. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6769. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6770. $expected = array(
  6771. 'div' => array('class' => 'input textarea'),
  6772. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6773. 'Name',
  6774. '/label',
  6775. 'textarea' => array(
  6776. 'id' => 'ValidateProfile1ValidateItem2Name',
  6777. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  6778. 'cols' => 30,
  6779. 'rows' => 6
  6780. ),
  6781. 'Value',
  6782. '/textarea',
  6783. '/div'
  6784. );
  6785. $this->assertTags($result, $expected);
  6786. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  6787. $expected = array(
  6788. 'div' => array('class' => 'input date'),
  6789. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6790. 'Created',
  6791. '/label',
  6792. array('select' => array(
  6793. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  6794. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6795. )
  6796. ),
  6797. array('option' => array('value' => '')), '/option',
  6798. $this->dateRegex['monthsRegex'],
  6799. '/select', '-',
  6800. array('select' => array(
  6801. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  6802. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6803. )
  6804. ),
  6805. array('option' => array('value' => '')), '/option',
  6806. $this->dateRegex['daysRegex'],
  6807. '/select', '-',
  6808. array('select' => array(
  6809. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  6810. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6811. )
  6812. ),
  6813. array('option' => array('value' => '')), '/option',
  6814. $this->dateRegex['yearsRegex'],
  6815. '/select',
  6816. '/div'
  6817. );
  6818. $this->assertTags($result, $expected);
  6819. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  6820. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6821. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6822. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6823. $expected = array(
  6824. 'div' => array('class' => 'input select error'),
  6825. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6826. 'Profile',
  6827. '/label',
  6828. 'select' => array(
  6829. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  6830. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6831. 'class' => 'form-error'
  6832. ),
  6833. '/select',
  6834. array('div' => array('class' => 'error-message')),
  6835. 'Error',
  6836. '/div',
  6837. '/div'
  6838. );
  6839. $this->assertTags($result, $expected);
  6840. }
  6841. /**
  6842. * test the correct display of multi-record form validation errors.
  6843. *
  6844. * @return void
  6845. */
  6846. public function testMultiRecordFormValidationErrors() {
  6847. $this->Form->create('ValidateProfile');
  6848. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  6849. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6850. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6851. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6852. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6853. $result = $this->Form->error('ValidateProfile.2.city');
  6854. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6855. $result = $this->Form->error('2.city');
  6856. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6857. }
  6858. /**
  6859. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  6860. *
  6861. * @return void
  6862. */
  6863. public function testInputTemplate() {
  6864. $result = $this->Form->input('Contact.email', array(
  6865. 'type' => 'text', 'format' => array('input')
  6866. ));
  6867. $expected = array(
  6868. 'div' => array('class' => 'input text'),
  6869. 'input' => array(
  6870. 'type' => 'text', 'name' => 'data[Contact][email]',
  6871. 'id' => 'ContactEmail'
  6872. ),
  6873. '/div'
  6874. );
  6875. $this->assertTags($result, $expected);
  6876. $result = $this->Form->input('Contact.email', array(
  6877. 'type' => 'text', 'format' => array('input', 'label'),
  6878. 'label' => '<em>Email (required)</em>'
  6879. ));
  6880. $expected = array(
  6881. 'div' => array('class' => 'input text'),
  6882. array('input' => array(
  6883. 'type' => 'text', 'name' => 'data[Contact][email]',
  6884. 'id' => 'ContactEmail'
  6885. )),
  6886. 'label' => array('for' => 'ContactEmail'),
  6887. 'em' => array(),
  6888. 'Email (required)',
  6889. '/em',
  6890. '/label',
  6891. '/div'
  6892. );
  6893. $this->assertTags($result, $expected);
  6894. $result = $this->Form->input('Contact.email', array(
  6895. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  6896. 'between' => '<div>Something in the middle</div>',
  6897. 'after' => '<span>Some text at the end</span>'
  6898. ));
  6899. $expected = array(
  6900. 'div' => array('class' => 'input text'),
  6901. array('input' => array(
  6902. 'type' => 'text', 'name' => 'data[Contact][email]',
  6903. 'id' => 'ContactEmail'
  6904. )),
  6905. array('div' => array()),
  6906. 'Something in the middle',
  6907. '/div',
  6908. 'label' => array('for' => 'ContactEmail'),
  6909. 'Email',
  6910. '/label',
  6911. 'span' => array(),
  6912. 'Some text at the end',
  6913. '/span',
  6914. '/div'
  6915. );
  6916. $this->assertTags($result, $expected);
  6917. }
  6918. /**
  6919. * test that some html5 inputs + FormHelper::__call() work
  6920. *
  6921. * @return void
  6922. */
  6923. public function testHtml5Inputs() {
  6924. $result = $this->Form->email('User.email');
  6925. $expected = array(
  6926. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  6927. );
  6928. $this->assertTags($result, $expected);
  6929. $result = $this->Form->search('User.query');
  6930. $expected = array(
  6931. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  6932. );
  6933. $this->assertTags($result, $expected);
  6934. $result = $this->Form->search('User.query', array('value' => 'test'));
  6935. $expected = array(
  6936. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  6940. $expected = array(
  6941. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  6942. );
  6943. $this->assertTags($result, $expected);
  6944. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  6945. $expected = array(
  6946. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  6947. );
  6948. $this->assertTags($result, $expected);
  6949. }
  6950. /**
  6951. *
  6952. * @expectedException CakeException
  6953. * @return void
  6954. */
  6955. public function testHtml5InputException() {
  6956. $this->Form->email();
  6957. }
  6958. /**
  6959. * Tests that a model can be loaded from the model names passed in the request object
  6960. *
  6961. * @return void
  6962. */
  6963. public function testIntrospectModelFromRequest() {
  6964. $this->loadFixtures('Post');
  6965. App::build(array(
  6966. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  6967. ));
  6968. CakePlugin::load('TestPlugin');
  6969. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  6970. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  6971. $this->Form->create('TestPluginPost');
  6972. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  6973. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  6974. CakePlugin::unload();
  6975. App::build();
  6976. }
  6977. /**
  6978. * Tests that it is possible to set the validation errors directly in the helper for a field
  6979. *
  6980. * @return void
  6981. */
  6982. public function testCustomValidationErrors() {
  6983. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  6984. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  6985. $this->assertEquals('Badness!', $result);
  6986. }
  6987. }