FormHelperTest.php 319 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  319. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  320. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  321. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  322. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  323. );
  324. /**
  325. * beforeValidate method
  326. *
  327. * @return void
  328. */
  329. public function beforeValidate($options = array()) {
  330. $this->invalidate('email');
  331. return false;
  332. }
  333. }
  334. /**
  335. * ValidateProfile class
  336. *
  337. * @package Cake.Test.Case.View.Helper
  338. */
  339. class ValidateProfile extends CakeTestModel {
  340. /**
  341. * useTable property
  342. *
  343. * @var boolean
  344. */
  345. public $useTable = false;
  346. /**
  347. * schema property
  348. *
  349. * @var array
  350. */
  351. protected $_schema = array(
  352. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  354. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  356. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  357. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  358. );
  359. /**
  360. * hasOne property
  361. *
  362. * @var array
  363. */
  364. public $hasOne = array('ValidateItem' => array(
  365. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  366. ));
  367. /**
  368. * belongsTo property
  369. *
  370. * @var array
  371. */
  372. public $belongsTo = array('ValidateUser' => array(
  373. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  374. ));
  375. /**
  376. * beforeValidate method
  377. *
  378. * @return void
  379. */
  380. public function beforeValidate($options = array()) {
  381. $this->invalidate('full_name');
  382. $this->invalidate('city');
  383. return false;
  384. }
  385. }
  386. /**
  387. * ValidateItem class
  388. *
  389. * @package Cake.Test.Case.View.Helper
  390. */
  391. class ValidateItem extends CakeTestModel {
  392. /**
  393. * useTable property
  394. *
  395. * @var boolean
  396. */
  397. public $useTable = false;
  398. /**
  399. * schema property
  400. *
  401. * @var array
  402. */
  403. protected $_schema = array(
  404. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  406. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  407. 'description' => array(
  408. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  409. ),
  410. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  411. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  412. );
  413. /**
  414. * belongsTo property
  415. *
  416. * @var array
  417. */
  418. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  419. /**
  420. * beforeValidate method
  421. *
  422. * @return void
  423. */
  424. public function beforeValidate($options = array()) {
  425. $this->invalidate('description');
  426. return false;
  427. }
  428. }
  429. /**
  430. * TestMail class
  431. *
  432. * @package Cake.Test.Case.View.Helper
  433. */
  434. class TestMail extends CakeTestModel {
  435. /**
  436. * useTable property
  437. *
  438. * @var boolean
  439. */
  440. public $useTable = false;
  441. }
  442. /**
  443. * FormHelperTest class
  444. *
  445. * @package Cake.Test.Case.View.Helper
  446. * @property FormHelper $Form
  447. */
  448. class FormHelperTest extends CakeTestCase {
  449. /**
  450. * Fixtures to be used
  451. *
  452. * @var array
  453. */
  454. public $fixtures = array('core.post');
  455. /**
  456. * Do not load the fixtures by default
  457. *
  458. * @var boolean
  459. */
  460. public $autoFixtures = false;
  461. /**
  462. * setUp method
  463. *
  464. * @return void
  465. */
  466. public function setUp() {
  467. parent::setUp();
  468. Configure::write('Config.language', 'eng');
  469. Configure::write('App.base', '');
  470. Configure::delete('Asset');
  471. $this->Controller = new ContactTestController();
  472. $this->View = new View($this->Controller);
  473. $this->Form = new FormHelper($this->View);
  474. $this->Form->Html = new HtmlHelper($this->View);
  475. $this->Form->request = new CakeRequest('contacts/add', false);
  476. $this->Form->request->here = '/contacts/add';
  477. $this->Form->request['action'] = 'add';
  478. $this->Form->request->webroot = '';
  479. $this->Form->request->base = '';
  480. ClassRegistry::addObject('Contact', new Contact());
  481. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  482. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  483. ClassRegistry::addObject('User', new UserForm());
  484. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  485. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  486. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  487. $this->oldSalt = Configure::read('Security.salt');
  488. $this->dateRegex = array(
  489. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  491. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  493. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  494. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  495. );
  496. Configure::write('Security.salt', 'foo!');
  497. }
  498. /**
  499. * tearDown method
  500. *
  501. * @return void
  502. */
  503. public function tearDown() {
  504. parent::tearDown();
  505. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  506. Configure::write('Security.salt', $this->oldSalt);
  507. }
  508. /**
  509. * testFormCreateWithSecurity method
  510. *
  511. * Test form->create() with security key.
  512. *
  513. * @return void
  514. */
  515. public function testCreateWithSecurity() {
  516. $this->Form->request['_Token'] = array('key' => 'testKey');
  517. $encoding = strtolower(Configure::read('App.encoding'));
  518. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  519. $expected = array(
  520. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  521. 'div' => array('style' => 'display:none;'),
  522. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  523. array('input' => array(
  524. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  525. )),
  526. '/div'
  527. );
  528. $this->assertTags($result, $expected);
  529. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  530. $expected['form']['id'] = 'MyForm';
  531. $this->assertTags($result, $expected);
  532. }
  533. /**
  534. * testFormCreateGetNoSecurity method
  535. *
  536. * Test form->create() with no security key as its a get form
  537. *
  538. * @return void
  539. */
  540. public function testCreateEndGetNoSecurity() {
  541. $this->Form->request['_Token'] = array('key' => 'testKey');
  542. $encoding = strtolower(Configure::read('App.encoding'));
  543. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  544. $this->assertNotContains('Token', $result);
  545. $result = $this->Form->end('Save');
  546. $this->assertNotContains('Token', $result);
  547. }
  548. /**
  549. * test that create() clears the fields property so it starts fresh
  550. *
  551. * @return void
  552. */
  553. public function testCreateClearingFields() {
  554. $this->Form->fields = array('model_id');
  555. $this->Form->create('Contact');
  556. $this->assertEquals(array(), $this->Form->fields);
  557. }
  558. /**
  559. * Tests form hash generation with model-less data
  560. *
  561. * @return void
  562. */
  563. public function testValidateHashNoModel() {
  564. $this->Form->request['_Token'] = array('key' => 'foo');
  565. $result = $this->Form->secure(array('anything'));
  566. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  567. }
  568. /**
  569. * Tests that models with identical field names get resolved properly
  570. *
  571. * @return void
  572. */
  573. public function testDuplicateFieldNameResolution() {
  574. $result = $this->Form->create('ValidateUser');
  575. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateItem.name');
  577. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  578. $result = $this->Form->input('ValidateUser.name');
  579. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  580. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  581. $this->assertRegExp('/type="text"/', $result);
  582. $result = $this->Form->input('ValidateItem.name');
  583. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  584. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  585. $this->assertRegExp('/<textarea/', $result);
  586. $result = $this->Form->input('name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. }
  591. /**
  592. * Tests that hidden fields generated for checkboxes don't get locked
  593. *
  594. * @return void
  595. */
  596. public function testNoCheckboxLocking() {
  597. $this->Form->request['_Token'] = array('key' => 'foo');
  598. $this->assertSame(array(), $this->Form->fields);
  599. $this->Form->checkbox('check', array('value' => '1'));
  600. $this->assertSame($this->Form->fields, array('check'));
  601. }
  602. /**
  603. * testFormSecurityFields method
  604. *
  605. * Test generation of secure form hash generation.
  606. *
  607. * @return void
  608. */
  609. public function testFormSecurityFields() {
  610. $key = 'testKey';
  611. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  612. $secureAttributes = array('form' => 'MyTestForm');
  613. $this->Form->request['_Token'] = array('key' => $key);
  614. $result = $this->Form->secure($fields, $secureAttributes);
  615. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  616. $hash .= ':' . 'Model.valid';
  617. $hash = urlencode($hash);
  618. $expected = array(
  619. 'div' => array('style' => 'display:none;'),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  622. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/',
  623. 'form' => 'MyTestForm',
  624. )),
  625. array('input' => array(
  626. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  627. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/',
  628. 'form' => 'MyTestForm',
  629. )),
  630. '/div'
  631. );
  632. $this->assertTags($result, $expected);
  633. }
  634. /**
  635. * Tests correct generation of number fields for double and float fields
  636. *
  637. * @return void
  638. */
  639. public function testTextFieldGenerationForFloats() {
  640. $model = ClassRegistry::getObject('Contact');
  641. $model->setSchema(array('foo' => array(
  642. 'type' => 'float',
  643. 'null' => false,
  644. 'default' => null,
  645. 'length' => 10
  646. )));
  647. $this->Form->create('Contact');
  648. $result = $this->Form->input('foo');
  649. $expected = array(
  650. 'div' => array('class' => 'input number'),
  651. 'label' => array('for' => 'ContactFoo'),
  652. 'Foo',
  653. '/label',
  654. array('input' => array(
  655. 'type' => 'number',
  656. 'name' => 'data[Contact][foo]',
  657. 'id' => 'ContactFoo',
  658. 'step' => 'any'
  659. )),
  660. '/div'
  661. );
  662. $this->assertTags($result, $expected);
  663. $result = $this->Form->input('foo', array('step' => 0.5));
  664. $expected = array(
  665. 'div' => array('class' => 'input number'),
  666. 'label' => array('for' => 'ContactFoo'),
  667. 'Foo',
  668. '/label',
  669. array('input' => array(
  670. 'type' => 'number',
  671. 'name' => 'data[Contact][foo]',
  672. 'id' => 'ContactFoo',
  673. 'step' => '0.5'
  674. )),
  675. '/div'
  676. );
  677. $this->assertTags($result, $expected);
  678. }
  679. /**
  680. * Tests correct generation of number fields for integer fields
  681. *
  682. * @return void
  683. */
  684. public function testTextFieldTypeNumberGenerationForIntegers() {
  685. $model = ClassRegistry::getObject('Contact');
  686. $model->setSchema(array('foo' => array(
  687. 'type' => 'integer',
  688. 'null' => false,
  689. 'default' => null,
  690. 'length' => null
  691. )));
  692. $this->Form->create('Contact');
  693. $result = $this->Form->input('foo');
  694. $expected = array(
  695. 'div' => array('class' => 'input number'),
  696. 'label' => array('for' => 'ContactFoo'),
  697. 'Foo',
  698. '/label',
  699. array('input' => array(
  700. 'type' => 'number', 'name' => 'data[Contact][foo]',
  701. 'id' => 'ContactFoo'
  702. )),
  703. '/div'
  704. );
  705. $this->assertTags($result, $expected);
  706. }
  707. /**
  708. * Tests correct generation of file upload fields for binary fields
  709. *
  710. * @return void
  711. */
  712. public function testFileUploadFieldTypeGenerationForBinaries() {
  713. $model = ClassRegistry::getObject('Contact');
  714. $model->setSchema(array('foo' => array(
  715. 'type' => 'binary',
  716. 'null' => false,
  717. 'default' => null,
  718. 'length' => 1024
  719. )));
  720. $this->Form->create('Contact');
  721. $result = $this->Form->input('foo');
  722. $expected = array(
  723. 'div' => array('class' => 'input file'),
  724. 'label' => array('for' => 'ContactFoo'),
  725. 'Foo',
  726. '/label',
  727. array('input' => array(
  728. 'type' => 'file', 'name' => 'data[Contact][foo]',
  729. 'id' => 'ContactFoo'
  730. )),
  731. '/div'
  732. );
  733. $this->assertTags($result, $expected);
  734. }
  735. /**
  736. * testFormSecurityMultipleFields method
  737. *
  738. * Test secure() with multiple row form. Ensure hash is correct.
  739. *
  740. * @return void
  741. */
  742. public function testFormSecurityMultipleFields() {
  743. $key = 'testKey';
  744. $fields = array(
  745. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  746. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  747. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  748. );
  749. $this->Form->request['_Token'] = array('key' => $key);
  750. $result = $this->Form->secure($fields);
  751. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  752. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  753. $expected = array(
  754. 'div' => array('style' => 'display:none;'),
  755. array('input' => array(
  756. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  757. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  758. )),
  759. array('input' => array(
  760. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  761. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  762. )),
  763. '/div'
  764. );
  765. $this->assertTags($result, $expected);
  766. }
  767. /**
  768. * testFormSecurityMultipleSubmitButtons
  769. *
  770. * test form submit generation and ensure that _Token is only created on end()
  771. *
  772. * @return void
  773. */
  774. public function testFormSecurityMultipleSubmitButtons() {
  775. $key = 'testKey';
  776. $this->Form->request['_Token'] = array('key' => $key);
  777. $this->Form->create('Addresses');
  778. $this->Form->input('Address.title');
  779. $this->Form->input('Address.first_name');
  780. $result = $this->Form->submit('Save', array('name' => 'save'));
  781. $expected = array(
  782. 'div' => array('class' => 'submit'),
  783. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  784. '/div',
  785. );
  786. $this->assertTags($result, $expected);
  787. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  788. $expected = array(
  789. 'div' => array('class' => 'submit'),
  790. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  791. '/div',
  792. );
  793. $this->assertTags($result, $expected);
  794. $result = $this->Form->end(null);
  795. $expected = array(
  796. 'div' => array('style' => 'display:none;'),
  797. array('input' => array(
  798. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  799. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  800. )),
  801. array('input' => array(
  802. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  803. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  804. )),
  805. '/div'
  806. );
  807. $this->assertTags($result, $expected);
  808. }
  809. /**
  810. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  811. *
  812. * @return void
  813. */
  814. public function testSecurityButtonNestedNamed() {
  815. $key = 'testKey';
  816. $this->Form->request['_Token'] = array('key' => $key);
  817. $this->Form->create('Addresses');
  818. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  819. $result = $this->Form->unlockField();
  820. $this->assertEquals(array('Address.button'), $result);
  821. }
  822. /**
  823. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  824. *
  825. * @return void
  826. */
  827. public function testSecuritySubmitNestedNamed() {
  828. $key = 'testKey';
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $this->Form->create('Addresses');
  831. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  832. $result = $this->Form->unlockField();
  833. $this->assertEquals(array('Address.button'), $result);
  834. }
  835. /**
  836. * Test that the correct fields are unlocked for image submits with no names.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitImageNoName() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('User');
  844. $result = $this->Form->submit('save.png');
  845. $expected = array(
  846. 'div' => array('class' => 'submit'),
  847. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  848. '/div'
  849. );
  850. $this->assertTags($result, $expected);
  851. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  852. }
  853. /**
  854. * Test that the correct fields are unlocked for image submits with names.
  855. *
  856. * @return void
  857. */
  858. public function testSecuritySubmitImageName() {
  859. $key = 'testKey';
  860. $this->Form->request['_Token'] = array('key' => $key);
  861. $this->Form->create('User');
  862. $result = $this->Form->submit('save.png', array('name' => 'test'));
  863. $expected = array(
  864. 'div' => array('class' => 'submit'),
  865. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  866. '/div'
  867. );
  868. $this->assertTags($result, $expected);
  869. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  870. }
  871. /**
  872. * testFormSecurityMultipleInputFields method
  873. *
  874. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  875. *
  876. * @return void
  877. */
  878. public function testFormSecurityMultipleInputFields() {
  879. $key = 'testKey';
  880. $this->Form->request['_Token'] = array('key' => $key);
  881. $this->Form->create('Addresses');
  882. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  883. $this->Form->input('Addresses.0.title');
  884. $this->Form->input('Addresses.0.first_name');
  885. $this->Form->input('Addresses.0.last_name');
  886. $this->Form->input('Addresses.0.address');
  887. $this->Form->input('Addresses.0.city');
  888. $this->Form->input('Addresses.0.phone');
  889. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  890. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  891. $this->Form->input('Addresses.1.title');
  892. $this->Form->input('Addresses.1.first_name');
  893. $this->Form->input('Addresses.1.last_name');
  894. $this->Form->input('Addresses.1.address');
  895. $this->Form->input('Addresses.1.city');
  896. $this->Form->input('Addresses.1.phone');
  897. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  898. $result = $this->Form->secure($this->Form->fields);
  899. $hash = 'a3b9b2ba1cb688838f92818a5970e17dd7943a78%3AAddresses.0.id%7CAddresses.1.id';
  900. $expected = array(
  901. 'div' => array('style' => 'display:none;'),
  902. array('input' => array(
  903. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  904. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  905. )),
  906. array('input' => array(
  907. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  908. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  909. )),
  910. '/div'
  911. );
  912. $this->assertTags($result, $expected);
  913. }
  914. /**
  915. * Test form security with Model.field.0 style inputs
  916. *
  917. * @return void
  918. */
  919. public function testFormSecurityArrayFields() {
  920. $key = 'testKey';
  921. $this->Form->request->params['_Token']['key'] = $key;
  922. $this->Form->create('Address');
  923. $this->Form->input('Address.primary.1');
  924. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  925. $this->Form->input('Address.secondary.1.0');
  926. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  927. }
  928. /**
  929. * testFormSecurityMultipleInputDisabledFields method
  930. *
  931. * test secure form generation with multiple records and disabled fields.
  932. *
  933. * @return void
  934. */
  935. public function testFormSecurityMultipleInputDisabledFields() {
  936. $key = 'testKey';
  937. $this->Form->request->params['_Token'] = array(
  938. 'key' => $key,
  939. 'unlockedFields' => array('first_name', 'address')
  940. );
  941. $this->Form->create();
  942. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  943. $this->Form->input('Addresses.0.title');
  944. $this->Form->input('Addresses.0.first_name');
  945. $this->Form->input('Addresses.0.last_name');
  946. $this->Form->input('Addresses.0.address');
  947. $this->Form->input('Addresses.0.city');
  948. $this->Form->input('Addresses.0.phone');
  949. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  950. $this->Form->input('Addresses.1.title');
  951. $this->Form->input('Addresses.1.first_name');
  952. $this->Form->input('Addresses.1.last_name');
  953. $this->Form->input('Addresses.1.address');
  954. $this->Form->input('Addresses.1.city');
  955. $this->Form->input('Addresses.1.phone');
  956. $result = $this->Form->secure($this->Form->fields);
  957. $hash = '5c9cadf9da008cc444d3960b481391a425a5d979%3AAddresses.0.id%7CAddresses.1.id';
  958. $expected = array(
  959. 'div' => array('style' => 'display:none;'),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  962. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  963. )),
  964. array('input' => array(
  965. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  966. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  967. )),
  968. '/div'
  969. );
  970. $this->assertTags($result, $expected);
  971. }
  972. /**
  973. * testFormSecurityInputDisabledFields method
  974. *
  975. * Test single record form with disabled fields.
  976. *
  977. * @return void
  978. */
  979. public function testFormSecurityInputUnlockedFields() {
  980. $key = 'testKey';
  981. $this->Form->request['_Token'] = array(
  982. 'key' => $key,
  983. 'unlockedFields' => array('first_name', 'address')
  984. );
  985. $this->Form->create();
  986. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  987. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  988. $this->Form->input('Addresses.title');
  989. $this->Form->input('Addresses.first_name');
  990. $this->Form->input('Addresses.last_name');
  991. $this->Form->input('Addresses.address');
  992. $this->Form->input('Addresses.city');
  993. $this->Form->input('Addresses.phone');
  994. $result = $this->Form->fields;
  995. $expected = array(
  996. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  997. 'Addresses.city', 'Addresses.phone'
  998. );
  999. $this->assertEquals($expected, $result);
  1000. $result = $this->Form->secure($expected);
  1001. $hash = '40289bd07811587887ff56585a8526ff9da59d7a%3AAddresses.id';
  1002. $expected = array(
  1003. 'div' => array('style' => 'display:none;'),
  1004. array('input' => array(
  1005. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1006. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1007. )),
  1008. array('input' => array(
  1009. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1010. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1011. )),
  1012. '/div'
  1013. );
  1014. $this->assertTags($result, $expected);
  1015. }
  1016. /**
  1017. * test securing inputs with custom name attributes.
  1018. *
  1019. * @return void
  1020. */
  1021. public function testFormSecureWithCustomNameAttribute() {
  1022. $this->Form->request->params['_Token']['key'] = 'testKey';
  1023. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1024. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1025. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1026. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1027. }
  1028. /**
  1029. * testFormSecuredInput method
  1030. *
  1031. * Test generation of entire secure form, assertions made on input() output.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testFormSecuredInput() {
  1036. $this->Form->request['_Token'] = array('key' => 'testKey');
  1037. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1038. $encoding = strtolower(Configure::read('App.encoding'));
  1039. $expected = array(
  1040. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1041. 'div' => array('style' => 'display:none;'),
  1042. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1043. array('input' => array(
  1044. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1045. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1046. )),
  1047. '/div'
  1048. );
  1049. $this->assertTags($result, $expected);
  1050. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1051. $expected = array(
  1052. 'div' => array('class' => 'input text'),
  1053. 'label' => array('for' => 'UserFormPublished'),
  1054. 'Published',
  1055. '/label',
  1056. array('input' => array(
  1057. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1058. 'id' => 'UserFormPublished'
  1059. )),
  1060. '/div'
  1061. );
  1062. $this->assertTags($result, $expected);
  1063. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1064. $expected = array(
  1065. 'div' => array('class' => 'input text'),
  1066. 'label' => array('for' => 'UserFormOther'),
  1067. 'Other',
  1068. '/label',
  1069. array('input' => array(
  1070. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1071. 'id' => 'UserFormOther'
  1072. )),
  1073. '/div'
  1074. );
  1075. $this->assertTags($result, $expected);
  1076. $result = $this->Form->hidden('UserForm.stuff');
  1077. $expected = array(
  1078. 'input' => array(
  1079. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1080. 'id' => 'UserFormStuff'
  1081. ));
  1082. $this->assertTags($result, $expected);
  1083. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1084. $expected = array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1086. 'value' => '0', 'id' => 'UserFormHidden'
  1087. ));
  1088. $this->assertTags($result, $expected);
  1089. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1090. $expected = array(
  1091. 'div' => array('class' => 'input checkbox'),
  1092. array('input' => array(
  1093. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1094. 'value' => '0', 'id' => 'UserFormSomething_'
  1095. )),
  1096. array('input' => array(
  1097. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1098. 'value' => '1', 'id' => 'UserFormSomething'
  1099. )),
  1100. 'label' => array('for' => 'UserFormSomething'),
  1101. 'Something',
  1102. '/label',
  1103. '/div'
  1104. );
  1105. $this->assertTags($result, $expected);
  1106. $result = $this->Form->fields;
  1107. $expected = array(
  1108. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1109. 'UserForm.hidden' => '0', 'UserForm.something'
  1110. );
  1111. $this->assertEquals($expected, $result);
  1112. $hash = '6014b4e1c4f39eb62389712111dbe6435bec66cb%3AUserForm.hidden%7CUserForm.stuff';
  1113. $result = $this->Form->secure($this->Form->fields);
  1114. $expected = array(
  1115. 'div' => array('style' => 'display:none;'),
  1116. array('input' => array(
  1117. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1118. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1119. )),
  1120. array('input' => array(
  1121. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1122. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertTags($result, $expected);
  1127. }
  1128. /**
  1129. * Test secured inputs with custom names.
  1130. *
  1131. * @return void
  1132. */
  1133. public function testSecuredInputCustomName() {
  1134. $this->Form->request['_Token'] = array('key' => 'testKey');
  1135. $this->assertEquals(array(), $this->Form->fields);
  1136. $this->Form->input('text_input', array(
  1137. 'name' => 'data[Option][General.default_role]',
  1138. ));
  1139. $expected = array('Option.General.default_role');
  1140. $this->assertEquals($expected, $this->Form->fields);
  1141. $this->Form->input('select_box', array(
  1142. 'name' => 'data[Option][General.select_role]',
  1143. 'type' => 'select',
  1144. 'options' => array(1, 2),
  1145. ));
  1146. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. }
  1149. /**
  1150. * Tests that the correct keys are added to the field hash index
  1151. *
  1152. * @return void
  1153. */
  1154. public function testSecuredFileInput() {
  1155. $this->Form->request['_Token'] = array('key' => 'testKey');
  1156. $this->assertEquals(array(), $this->Form->fields);
  1157. $this->Form->file('Attachment.file');
  1158. $expected = array(
  1159. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1160. 'Attachment.file.error', 'Attachment.file.size'
  1161. );
  1162. $this->assertEquals($expected, $this->Form->fields);
  1163. }
  1164. /**
  1165. * test that multiple selects keys are added to field hash
  1166. *
  1167. * @return void
  1168. */
  1169. public function testSecuredMultipleSelect() {
  1170. $this->Form->request['_Token'] = array('key' => 'testKey');
  1171. $this->assertEquals(array(), $this->Form->fields);
  1172. $options = array('1' => 'one', '2' => 'two');
  1173. $this->Form->select('Model.select', $options);
  1174. $expected = array('Model.select');
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. $this->Form->fields = array();
  1177. $this->Form->select('Model.select', $options, array('multiple' => true));
  1178. $this->assertEquals($expected, $this->Form->fields);
  1179. }
  1180. /**
  1181. * testFormSecuredRadio method
  1182. *
  1183. * @return void
  1184. */
  1185. public function testSecuredRadio() {
  1186. $this->Form->request['_Token'] = array('key' => 'testKey');
  1187. $this->assertEquals(array(), $this->Form->fields);
  1188. $options = array('1' => 'option1', '2' => 'option2');
  1189. $this->Form->radio('Test.test', $options);
  1190. $expected = array('Test.test');
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * Test that when disabled is in a list based attribute array it works.
  1195. *
  1196. * @return void
  1197. */
  1198. public function testSecuredAndDisabledNotAssoc() {
  1199. $this->Form->request['_Token'] = array('key' => 'testKey');
  1200. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1201. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1202. $this->Form->text('Model.text', array('disabled'));
  1203. $this->Form->textarea('Model.textarea', array('disabled'));
  1204. $this->Form->password('Model.password', array('disabled'));
  1205. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1206. $expected = array(
  1207. 'Model.radio' => ''
  1208. );
  1209. $this->assertEquals($expected, $this->Form->fields);
  1210. }
  1211. /**
  1212. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1213. * hashing.
  1214. *
  1215. * @return void
  1216. */
  1217. public function testSecuredAndDisabled() {
  1218. $this->Form->request['_Token'] = array('key' => 'testKey');
  1219. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1220. $this->Form->text('Model.text', array('disabled' => true));
  1221. $this->Form->password('Model.text', array('disabled' => true));
  1222. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1223. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1224. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1225. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1226. $this->Form->month('Model.month', array('disabled' => true));
  1227. $this->Form->day('Model.day', array('disabled' => true));
  1228. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1229. $this->Form->minute('Model.minute', array('disabled' => true));
  1230. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1231. $expected = array(
  1232. 'Model.radio' => ''
  1233. );
  1234. $this->assertEquals($expected, $this->Form->fields);
  1235. }
  1236. /**
  1237. * Test that only the path + query elements of a form's URL show up in their hash.
  1238. *
  1239. * @return void
  1240. */
  1241. public function testSecuredFormUrlIgnoresHost() {
  1242. $this->Form->request['_Token'] = array('key' => 'testKey');
  1243. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  1244. $this->Form->create('Address', array(
  1245. 'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
  1246. ));
  1247. $result = $this->Form->secure();
  1248. $this->assertContains($expected, $result);
  1249. $this->Form->create('Address', array('url' => 'http://localhost/articles/view/1?page=1'));
  1250. $result = $this->Form->secure();
  1251. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1252. $this->Form->create('Address', array('url' => '/articles/view/1?page=1'));
  1253. $result = $this->Form->secure();
  1254. $this->assertContains($expected, $result, 'URL path + query should work.');
  1255. $this->Form->create('Address', array('url' => '/articles/view/1'));
  1256. $result = $this->Form->secure();
  1257. $this->assertNotContains($expected, $result, 'URL is different');
  1258. }
  1259. /**
  1260. * Ensure named parameters work correctly with hash generation.
  1261. *
  1262. * @return void
  1263. */
  1264. public function testSecuredFormUrlWorksWithNamedParameter() {
  1265. $this->Form->request['_Token'] = array('key' => 'testKey');
  1266. $expected = 'c890c5f041b1d83d1610dee8f52cd257df7ce618%3A';
  1267. $this->Form->create('Address', array(
  1268. 'url' => array('controller' => 'articles', 'action' => 'view', 1, 'type' => 'red')
  1269. ));
  1270. $result = $this->Form->secure();
  1271. $this->assertContains($expected, $result);
  1272. }
  1273. /**
  1274. * Test that URL, HTML and identifer show up in their hashs.
  1275. *
  1276. * @return void
  1277. */
  1278. public function testSecuredFormUrlHasHtmlAndIdentifer() {
  1279. $this->Form->request['_Token'] = array('key' => 'testKey');
  1280. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  1281. $this->Form->create('Address', array(
  1282. 'url' => array(
  1283. 'controller' => 'articles',
  1284. 'action' => 'view',
  1285. '?' => array(
  1286. 'page' => 1,
  1287. 'limit' => 10,
  1288. 'html' => '<>"',
  1289. ),
  1290. '#' => 'result',
  1291. ),
  1292. ));
  1293. $result = $this->Form->secure();
  1294. $this->assertContains($expected, $result);
  1295. $this->Form->create('Address', array('url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
  1296. $result = $this->Form->secure();
  1297. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1298. $this->Form->create('Address', array('url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'));
  1299. $result = $this->Form->secure();
  1300. $this->assertContains($expected, $result, 'URL path + query should work.');
  1301. }
  1302. /**
  1303. * testDisableSecurityUsingForm method
  1304. *
  1305. * @return void
  1306. */
  1307. public function testDisableSecurityUsingForm() {
  1308. $this->Form->request['_Token'] = array(
  1309. 'key' => 'testKey',
  1310. 'disabledFields' => array()
  1311. );
  1312. $this->Form->create();
  1313. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1314. $this->Form->input('Addresses.title');
  1315. $this->Form->input('Addresses.first_name', array('secure' => false));
  1316. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1317. $this->Form->input('Addresses.zip', array(
  1318. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1319. ));
  1320. $result = $this->Form->fields;
  1321. $expected = array(
  1322. 'Addresses.id' => '123456', 'Addresses.title',
  1323. );
  1324. $this->assertEquals($expected, $result);
  1325. }
  1326. /**
  1327. * test disableField
  1328. *
  1329. * @return void
  1330. */
  1331. public function testUnlockFieldAddsToList() {
  1332. $this->Form->request['_Token'] = array(
  1333. 'key' => 'testKey',
  1334. 'unlockedFields' => array()
  1335. );
  1336. $this->Form->create('Contact');
  1337. $this->Form->unlockField('Contact.name');
  1338. $this->Form->text('Contact.name');
  1339. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1340. $this->assertEquals(array(), $this->Form->fields);
  1341. }
  1342. /**
  1343. * test unlockField removing from fields array.
  1344. *
  1345. * @return void
  1346. */
  1347. public function testUnlockFieldRemovingFromFields() {
  1348. $this->Form->request['_Token'] = array(
  1349. 'key' => 'testKey',
  1350. 'unlockedFields' => array()
  1351. );
  1352. $this->Form->create('Contact');
  1353. $this->Form->hidden('Contact.id', array('value' => 1));
  1354. $this->Form->text('Contact.name');
  1355. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1356. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1357. $this->Form->unlockField('Contact.name');
  1358. $this->Form->unlockField('Contact.id');
  1359. $this->assertEquals(array(), $this->Form->fields);
  1360. }
  1361. /**
  1362. * testTagIsInvalid method
  1363. *
  1364. * @return void
  1365. */
  1366. public function testTagIsInvalid() {
  1367. $Contact = ClassRegistry::getObject('Contact');
  1368. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1369. $this->Form->setEntity('Contact.0.email');
  1370. $result = $this->Form->tagIsInvalid();
  1371. $this->assertEquals($expected, $result);
  1372. $this->Form->setEntity('Contact.1.email');
  1373. $result = $this->Form->tagIsInvalid();
  1374. $this->assertFalse($result);
  1375. $this->Form->setEntity('Contact.0.name');
  1376. $result = $this->Form->tagIsInvalid();
  1377. $this->assertFalse($result);
  1378. }
  1379. /**
  1380. * Test tagIsInvalid with validation errors from a saveMany
  1381. *
  1382. * @return void
  1383. */
  1384. public function testTagIsInvalidSaveMany() {
  1385. $Contact = ClassRegistry::getObject('Contact');
  1386. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1387. $this->Form->create('Contact');
  1388. $this->Form->setEntity('0.email');
  1389. $result = $this->Form->tagIsInvalid();
  1390. $this->assertEquals($expected, $result);
  1391. $this->Form->setEntity('0.Contact.email');
  1392. $result = $this->Form->tagIsInvalid();
  1393. $this->assertEquals($expected, $result);
  1394. }
  1395. /**
  1396. * Test validation errors.
  1397. *
  1398. * @return void
  1399. */
  1400. public function testPasswordValidation() {
  1401. $Contact = ClassRegistry::getObject('Contact');
  1402. $Contact->validationErrors['password'] = array('Please provide a password');
  1403. $result = $this->Form->input('Contact.password');
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. array('div' => array('class' => 'error-message')),
  1414. 'Please provide a password',
  1415. '/div',
  1416. '/div'
  1417. );
  1418. $this->assertTags($result, $expected);
  1419. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1420. $expected = array(
  1421. 'div' => array('class' => 'input password error'),
  1422. 'label' => array('for' => 'ContactPassword'),
  1423. 'Password',
  1424. '/label',
  1425. 'input' => array(
  1426. 'type' => 'password', 'name' => 'data[Contact][password]',
  1427. 'id' => 'ContactPassword', 'class' => 'form-error'
  1428. ),
  1429. '/div'
  1430. );
  1431. $this->assertTags($result, $expected);
  1432. }
  1433. /**
  1434. * Test validation errors, when validation message is an empty string.
  1435. *
  1436. * @return void
  1437. */
  1438. public function testEmptyErrorValidation() {
  1439. $this->Form->validationErrors['Contact']['password'] = '';
  1440. $result = $this->Form->input('Contact.password');
  1441. $expected = array(
  1442. 'div' => array('class' => 'input password error'),
  1443. 'label' => array('for' => 'ContactPassword'),
  1444. 'Password',
  1445. '/label',
  1446. 'input' => array(
  1447. 'type' => 'password', 'name' => 'data[Contact][password]',
  1448. 'id' => 'ContactPassword', 'class' => 'form-error'
  1449. ),
  1450. array('div' => array('class' => 'error-message')),
  1451. array(),
  1452. '/div',
  1453. '/div'
  1454. );
  1455. $this->assertTags($result, $expected);
  1456. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1457. $expected = array(
  1458. 'div' => array('class' => 'input password error'),
  1459. 'label' => array('for' => 'ContactPassword'),
  1460. 'Password',
  1461. '/label',
  1462. 'input' => array(
  1463. 'type' => 'password', 'name' => 'data[Contact][password]',
  1464. 'id' => 'ContactPassword', 'class' => 'form-error'
  1465. ),
  1466. '/div'
  1467. );
  1468. $this->assertTags($result, $expected);
  1469. }
  1470. /**
  1471. * Test validation errors, when calling input() overriding validation message by an empty string.
  1472. *
  1473. * @return void
  1474. */
  1475. public function testEmptyInputErrorValidation() {
  1476. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1477. $result = $this->Form->input('Contact.password', array('error' => ''));
  1478. $expected = array(
  1479. 'div' => array('class' => 'input password error'),
  1480. 'label' => array('for' => 'ContactPassword'),
  1481. 'Password',
  1482. '/label',
  1483. 'input' => array(
  1484. 'type' => 'password', 'name' => 'data[Contact][password]',
  1485. 'id' => 'ContactPassword', 'class' => 'form-error'
  1486. ),
  1487. array('div' => array('class' => 'error-message')),
  1488. array(),
  1489. '/div',
  1490. '/div'
  1491. );
  1492. $this->assertTags($result, $expected);
  1493. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1494. $expected = array(
  1495. 'div' => array('class' => 'input password error'),
  1496. 'label' => array('for' => 'ContactPassword'),
  1497. 'Password',
  1498. '/label',
  1499. 'input' => array(
  1500. 'type' => 'password', 'name' => 'data[Contact][password]',
  1501. 'id' => 'ContactPassword', 'class' => 'form-error'
  1502. ),
  1503. '/div'
  1504. );
  1505. $this->assertTags($result, $expected);
  1506. }
  1507. /**
  1508. * testFormValidationAssociated method
  1509. *
  1510. * test display of form errors in conjunction with model::validates.
  1511. *
  1512. * @return void
  1513. */
  1514. public function testFormValidationAssociated() {
  1515. $this->UserForm = ClassRegistry::getObject('UserForm');
  1516. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1517. $data = array(
  1518. 'UserForm' => array('name' => 'user'),
  1519. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1520. );
  1521. $result = $this->UserForm->OpenidUrl->create($data);
  1522. $this->assertFalse(empty($result));
  1523. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1524. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1525. $encoding = strtolower(Configure::read('App.encoding'));
  1526. $expected = array(
  1527. 'form' => array(
  1528. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1529. 'accept-charset' => $encoding
  1530. ),
  1531. 'div' => array('style' => 'display:none;'),
  1532. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1533. '/div'
  1534. );
  1535. $this->assertTags($result, $expected);
  1536. $result = $this->Form->error(
  1537. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Error, not registered', $result);
  1540. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1541. }
  1542. /**
  1543. * testFormValidationAssociatedFirstLevel method
  1544. *
  1545. * test form error display with associated model.
  1546. *
  1547. * @return void
  1548. */
  1549. public function testFormValidationAssociatedFirstLevel() {
  1550. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1551. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1552. $data = array(
  1553. 'ValidateUser' => array('name' => 'mariano'),
  1554. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1555. );
  1556. $result = $this->ValidateUser->create($data);
  1557. $this->assertFalse(empty($result));
  1558. $this->assertFalse($this->ValidateUser->validates());
  1559. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1560. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1561. $encoding = strtolower(Configure::read('App.encoding'));
  1562. $expected = array(
  1563. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1564. 'div' => array('style' => 'display:none;'),
  1565. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1566. '/div'
  1567. );
  1568. $this->assertTags($result, $expected);
  1569. $result = $this->Form->error(
  1570. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1571. );
  1572. $this->assertEquals('Invalid email', $result);
  1573. $result = $this->Form->error(
  1574. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1575. );
  1576. $this->assertEquals('Invalid name', $result);
  1577. $result = $this->Form->error(
  1578. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1579. );
  1580. $this->assertEquals('Invalid city', $result);
  1581. unset($this->ValidateUser->ValidateProfile);
  1582. unset($this->ValidateUser);
  1583. }
  1584. /**
  1585. * testFormValidationAssociatedSecondLevel method
  1586. *
  1587. * test form error display with associated model.
  1588. *
  1589. * @return void
  1590. */
  1591. public function testFormValidationAssociatedSecondLevel() {
  1592. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1593. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1594. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1595. $data = array(
  1596. 'ValidateUser' => array('name' => 'mariano'),
  1597. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1598. 'ValidateItem' => array('name' => 'Item')
  1599. );
  1600. $result = $this->ValidateUser->create($data);
  1601. $this->assertFalse(empty($result));
  1602. $this->assertFalse($this->ValidateUser->validates());
  1603. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1604. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1605. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1606. $encoding = strtolower(Configure::read('App.encoding'));
  1607. $expected = array(
  1608. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1609. 'div' => array('style' => 'display:none;'),
  1610. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1611. '/div'
  1612. );
  1613. $this->assertTags($result, $expected);
  1614. $result = $this->Form->error(
  1615. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1616. );
  1617. $this->assertEquals('Invalid email', $result);
  1618. $result = $this->Form->error(
  1619. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1620. );
  1621. $this->assertEquals('Invalid name', $result);
  1622. $result = $this->Form->error(
  1623. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1624. );
  1625. $result = $this->Form->error(
  1626. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1627. );
  1628. $this->assertEquals('Invalid description', $result);
  1629. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1630. unset($this->ValidateUser->ValidateProfile);
  1631. unset($this->ValidateUser);
  1632. }
  1633. /**
  1634. * testFormValidationMultiRecord method
  1635. *
  1636. * test form error display with multiple records.
  1637. *
  1638. * @return void
  1639. */
  1640. public function testFormValidationMultiRecord() {
  1641. $Contact = ClassRegistry::getObject('Contact');
  1642. $Contact->validationErrors[2] = array(
  1643. 'name' => array('This field cannot be left blank')
  1644. );
  1645. $result = $this->Form->input('Contact.2.name');
  1646. $expected = array(
  1647. 'div' => array('class' => 'input text error'),
  1648. 'label' => array('for' => 'Contact2Name'),
  1649. 'Name',
  1650. '/label',
  1651. 'input' => array(
  1652. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1653. 'class' => 'form-error', 'maxlength' => 255
  1654. ),
  1655. array('div' => array('class' => 'error-message')),
  1656. 'This field cannot be left blank',
  1657. '/div',
  1658. '/div'
  1659. );
  1660. $this->assertTags($result, $expected);
  1661. }
  1662. /**
  1663. * testMultipleInputValidation method
  1664. *
  1665. * test multiple record form validation error display.
  1666. *
  1667. * @return void
  1668. */
  1669. public function testMultipleInputValidation() {
  1670. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1671. $Address->validationErrors[0] = array(
  1672. 'title' => array('This field cannot be empty'),
  1673. 'first_name' => array('This field cannot be empty')
  1674. );
  1675. $Address->validationErrors[1] = array(
  1676. 'last_name' => array('You must have a last name')
  1677. );
  1678. $this->Form->create();
  1679. $result = $this->Form->input('Address.0.title');
  1680. $expected = array(
  1681. 'div' => array('class'),
  1682. 'label' => array('for'),
  1683. 'preg:/[^<]+/',
  1684. '/label',
  1685. 'input' => array(
  1686. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1687. ),
  1688. array('div' => array('class' => 'error-message')),
  1689. 'This field cannot be empty',
  1690. '/div',
  1691. '/div'
  1692. );
  1693. $this->assertTags($result, $expected);
  1694. $result = $this->Form->input('Address.0.first_name');
  1695. $expected = array(
  1696. 'div' => array('class'),
  1697. 'label' => array('for'),
  1698. 'preg:/[^<]+/',
  1699. '/label',
  1700. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1701. array('div' => array('class' => 'error-message')),
  1702. 'This field cannot be empty',
  1703. '/div',
  1704. '/div'
  1705. );
  1706. $this->assertTags($result, $expected);
  1707. $result = $this->Form->input('Address.0.last_name');
  1708. $expected = array(
  1709. 'div' => array('class'),
  1710. 'label' => array('for'),
  1711. 'preg:/[^<]+/',
  1712. '/label',
  1713. 'input' => array('type' => 'text', 'name', 'id'),
  1714. '/div'
  1715. );
  1716. $this->assertTags($result, $expected);
  1717. $result = $this->Form->input('Address.1.last_name');
  1718. $expected = array(
  1719. 'div' => array('class'),
  1720. 'label' => array('for'),
  1721. 'preg:/[^<]+/',
  1722. '/label',
  1723. 'input' => array(
  1724. 'type' => 'text', 'name', 'id',
  1725. 'class' => 'form-error'
  1726. ),
  1727. array('div' => array('class' => 'error-message')),
  1728. 'You must have a last name',
  1729. '/div',
  1730. '/div'
  1731. );
  1732. $this->assertTags($result, $expected);
  1733. }
  1734. /**
  1735. * testInput method
  1736. *
  1737. * Test various incarnations of input().
  1738. *
  1739. * @return void
  1740. */
  1741. public function testInput() {
  1742. $result = $this->Form->input('ValidateUser.balance');
  1743. $expected = array(
  1744. 'div' => array('class'),
  1745. 'label' => array('for'),
  1746. 'Balance',
  1747. '/label',
  1748. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1749. '/div',
  1750. );
  1751. $this->assertTags($result, $expected);
  1752. $result = $this->Form->input('ValidateUser.cost_decimal');
  1753. $expected = array(
  1754. 'div' => array('class'),
  1755. 'label' => array('for'),
  1756. 'Cost Decimal',
  1757. '/label',
  1758. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1759. '/div',
  1760. );
  1761. $this->assertTags($result, $expected);
  1762. $result = $this->Form->input('ValidateUser.ratio');
  1763. $expected = array(
  1764. 'div' => array('class'),
  1765. 'label' => array('for'),
  1766. 'Ratio',
  1767. '/label',
  1768. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1769. '/div',
  1770. );
  1771. $this->assertTags($result, $expected);
  1772. $result = $this->Form->input('ValidateUser.population');
  1773. $expected = array(
  1774. 'div' => array('class'),
  1775. 'label' => array('for'),
  1776. 'Population',
  1777. '/label',
  1778. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1779. '/div',
  1780. );
  1781. $this->assertTags($result, $expected);
  1782. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1783. $expected = array(
  1784. 'div' => array('class' => 'input email'),
  1785. 'label' => array('for' => 'custom'),
  1786. 'Email',
  1787. '/label',
  1788. array('input' => array(
  1789. 'type' => 'email', 'name' => 'data[Contact][email]',
  1790. 'id' => 'custom', 'maxlength' => 255
  1791. )),
  1792. '/div'
  1793. );
  1794. $this->assertTags($result, $expected);
  1795. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1796. $expected = array(
  1797. '<div',
  1798. 'label' => array('for' => 'ContactEmail'),
  1799. 'Email',
  1800. '/label',
  1801. array('input' => array(
  1802. 'type' => 'email', 'name' => 'data[Contact][email]',
  1803. 'id' => 'ContactEmail', 'maxlength' => 255
  1804. )),
  1805. '/div'
  1806. );
  1807. $this->assertTags($result, $expected);
  1808. $result = $this->Form->hidden('Contact.idontexist');
  1809. $expected = array('input' => array(
  1810. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1811. 'id' => 'ContactIdontexist'
  1812. ));
  1813. $this->assertTags($result, $expected);
  1814. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1815. $expected = array(
  1816. 'div' => array('class' => 'input text'),
  1817. 'label' => array('for' => 'ContactEmail'),
  1818. 'Email',
  1819. '/label',
  1820. array('input' => array(
  1821. 'type' => 'text', 'name' => 'data[Contact][email]',
  1822. 'id' => 'ContactEmail'
  1823. )),
  1824. '/div'
  1825. );
  1826. $this->assertTags($result, $expected);
  1827. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1828. $expected = array(
  1829. 'div' => array('class' => 'input text'),
  1830. 'label' => array('for' => 'Contact5Email'),
  1831. 'Email',
  1832. '/label',
  1833. array('input' => array(
  1834. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1835. 'id' => 'Contact5Email'
  1836. )),
  1837. '/div'
  1838. );
  1839. $this->assertTags($result, $expected);
  1840. $result = $this->Form->input('Contact.password');
  1841. $expected = array(
  1842. 'div' => array('class' => 'input password'),
  1843. 'label' => array('for' => 'ContactPassword'),
  1844. 'Password',
  1845. '/label',
  1846. array('input' => array(
  1847. 'type' => 'password', 'name' => 'data[Contact][password]',
  1848. 'id' => 'ContactPassword'
  1849. )),
  1850. '/div'
  1851. );
  1852. $this->assertTags($result, $expected);
  1853. $result = $this->Form->input('Contact.email', array(
  1854. 'type' => 'file', 'class' => 'textbox'
  1855. ));
  1856. $expected = array(
  1857. 'div' => array('class' => 'input file'),
  1858. 'label' => array('for' => 'ContactEmail'),
  1859. 'Email',
  1860. '/label',
  1861. array('input' => array(
  1862. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1863. 'id' => 'ContactEmail'
  1864. )),
  1865. '/div'
  1866. );
  1867. $this->assertTags($result, $expected);
  1868. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1869. $result = $this->Form->input('Contact.phone');
  1870. $expected = array(
  1871. 'div' => array('class' => 'input tel'),
  1872. 'label' => array('for' => 'ContactPhone'),
  1873. 'Phone',
  1874. '/label',
  1875. array('input' => array(
  1876. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1877. 'value' => 'Hello &amp; World &gt; weird chars',
  1878. 'id' => 'ContactPhone', 'maxlength' => 255
  1879. )),
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1884. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1885. $expected = array(
  1886. 'div' => array('class' => 'input text'),
  1887. 'label' => array('for' => 'myId'),
  1888. 'Field',
  1889. '/label',
  1890. 'input' => array(
  1891. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1892. 'value' => 'My value', 'id' => 'myId'
  1893. ),
  1894. '/div'
  1895. );
  1896. $this->assertTags($result, $expected);
  1897. unset($this->Form->request->data);
  1898. $Contact = ClassRegistry::getObject('Contact');
  1899. $Contact->validationErrors['field'] = array('Badness!');
  1900. $result = $this->Form->input('Contact.field');
  1901. $expected = array(
  1902. 'div' => array('class' => 'input text error'),
  1903. 'label' => array('for' => 'ContactField'),
  1904. 'Field',
  1905. '/label',
  1906. 'input' => array(
  1907. 'type' => 'text', 'name' => 'data[Contact][field]',
  1908. 'id' => 'ContactField', 'class' => 'form-error'
  1909. ),
  1910. array('div' => array('class' => 'error-message')),
  1911. 'Badness!',
  1912. '/div',
  1913. '/div'
  1914. );
  1915. $this->assertTags($result, $expected);
  1916. $result = $this->Form->input('Contact.field', array(
  1917. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1918. ));
  1919. $expected = array(
  1920. 'label' => array('for' => 'ContactField'),
  1921. 'Field',
  1922. '/label',
  1923. 'input' => array(
  1924. 'type' => 'text', 'name' => 'data[Contact][field]',
  1925. 'id' => 'ContactField', 'class' => 'form-error'
  1926. ),
  1927. array('span' => array('class' => 'error-message')),
  1928. 'Badness!',
  1929. '/span'
  1930. );
  1931. $this->assertTags($result, $expected);
  1932. $result = $this->Form->input('Contact.field', array(
  1933. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1934. ));
  1935. $expected = array(
  1936. 'div' => array('class' => 'input text error'),
  1937. 'label' => array('for' => 'ContactField'),
  1938. 'Field',
  1939. '/label',
  1940. 'input' => array(
  1941. 'type' => 'text', 'name' => 'data[Contact][field]',
  1942. 'id' => 'ContactField', 'class' => 'form-error'
  1943. ),
  1944. array('div' => array('class' => 'error')),
  1945. 'Badness!',
  1946. '/div'
  1947. );
  1948. $this->assertTags($result, $expected);
  1949. $result = $this->Form->input('Contact.field', array(
  1950. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1951. ));
  1952. $expected = array(
  1953. 'span' => array('class' => 'input text error'),
  1954. 'label' => array('for' => 'ContactField'),
  1955. 'Field',
  1956. '/label',
  1957. 'input' => array(
  1958. 'type' => 'text', 'name' => 'data[Contact][field]',
  1959. 'id' => 'ContactField', 'class' => 'form-error'
  1960. ),
  1961. 'Badness!',
  1962. '/span'
  1963. );
  1964. $this->assertTags($result, $expected);
  1965. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1966. $expected = array(
  1967. 'div' => array('class' => 'input text error'),
  1968. 'label' => array('for' => 'ContactField'),
  1969. 'Field',
  1970. '/label',
  1971. 'input' => array(
  1972. 'type' => 'text', 'name' => 'data[Contact][field]',
  1973. 'id' => 'ContactField', 'class' => 'form-error'
  1974. ),
  1975. 'A message to you, Rudy',
  1976. array('div' => array('class' => 'error-message')),
  1977. 'Badness!',
  1978. '/div',
  1979. '/div'
  1980. );
  1981. $this->assertTags($result, $expected);
  1982. $this->Form->setEntity(null);
  1983. $this->Form->setEntity('Contact.field');
  1984. $result = $this->Form->input('Contact.field', array(
  1985. 'after' => 'A message to you, Rudy', 'error' => false
  1986. ));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input text'),
  1989. 'label' => array('for' => 'ContactField'),
  1990. 'Field',
  1991. '/label',
  1992. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1993. 'A message to you, Rudy',
  1994. '/div'
  1995. );
  1996. $this->assertTags($result, $expected);
  1997. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1998. $expected = array(
  1999. 'div' => array('class' => 'input text'),
  2000. 'label' => array('for' => 'ObjectField'),
  2001. 'Field',
  2002. '/label',
  2003. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  2004. 'A message to you, Rudy',
  2005. '/div'
  2006. );
  2007. $this->assertTags($result, $expected);
  2008. $Contact->validationErrors['field'] = array('minLength');
  2009. $result = $this->Form->input('Contact.field', array(
  2010. 'error' => array(
  2011. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2012. 'maxLength' => 'login too large'
  2013. )
  2014. ));
  2015. $expected = array(
  2016. 'div' => array('class' => 'input text error'),
  2017. 'label' => array('for' => 'ContactField'),
  2018. 'Field',
  2019. '/label',
  2020. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2021. array('div' => array('class' => 'error-message')),
  2022. 'Le login doit contenir au moins 2 caractères',
  2023. '/div',
  2024. '/div'
  2025. );
  2026. $this->assertTags($result, $expected);
  2027. $Contact->validationErrors['field'] = array('maxLength');
  2028. $result = $this->Form->input('Contact.field', array(
  2029. 'error' => array(
  2030. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  2031. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2032. 'maxLength' => 'login too large',
  2033. )
  2034. ));
  2035. $expected = array(
  2036. 'div' => array('class' => 'input text error'),
  2037. 'label' => array('for' => 'ContactField'),
  2038. 'Field',
  2039. '/label',
  2040. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2041. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  2042. 'login too large',
  2043. '/span',
  2044. '/div'
  2045. );
  2046. $this->assertTags($result, $expected);
  2047. }
  2048. /**
  2049. * Test that inputs with 0 can be created.
  2050. *
  2051. * @return void
  2052. */
  2053. public function testInputZero() {
  2054. $this->Form->create('User');
  2055. $result = $this->Form->input('0');
  2056. $expected = array(
  2057. 'div' => array('class' => 'input text'),
  2058. 'label' => array('for' => 'User0'), '/label',
  2059. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  2060. '/div'
  2061. );
  2062. $this->assertTags($result, $expected);
  2063. }
  2064. /**
  2065. * test input() with checkbox creation
  2066. *
  2067. * @return void
  2068. */
  2069. public function testInputCheckbox() {
  2070. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2071. $expected = array(
  2072. 'div' => array('class' => 'input checkbox'),
  2073. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2074. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2075. '/div'
  2076. );
  2077. $this->assertTags($result, $expected);
  2078. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2079. $expected = array(
  2080. 'div' => array('class' => 'input checkbox'),
  2081. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2082. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2083. '/div'
  2084. );
  2085. $this->assertTags($result, $expected);
  2086. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2087. $expected = array(
  2088. 'div' => array('class' => 'input checkbox'),
  2089. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2090. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2091. '/div'
  2092. );
  2093. $this->assertTags($result, $expected);
  2094. $result = $this->Form->input('User.disabled', array(
  2095. 'label' => 'Disabled',
  2096. 'type' => 'checkbox',
  2097. 'data-foo' => 'disabled'
  2098. ));
  2099. $expected = array(
  2100. 'div' => array('class' => 'input checkbox'),
  2101. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2102. array('input' => array(
  2103. 'type' => 'checkbox',
  2104. 'name' => 'data[User][disabled]',
  2105. 'value' => '1',
  2106. 'id' => 'UserDisabled',
  2107. 'data-foo' => 'disabled'
  2108. )),
  2109. 'label' => array('for' => 'UserDisabled'),
  2110. 'Disabled',
  2111. '/label',
  2112. '/div'
  2113. );
  2114. $this->assertTags($result, $expected);
  2115. }
  2116. /**
  2117. * test form->input() with time types.
  2118. *
  2119. * @return void
  2120. */
  2121. public function testInputTime() {
  2122. extract($this->dateRegex);
  2123. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2124. $result = explode(':', $result);
  2125. $this->assertRegExp('/option value="23"/', $result[0]);
  2126. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2127. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2128. $result = explode(':', $result);
  2129. $this->assertRegExp('/option value="23"/', $result[0]);
  2130. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2131. $result = $this->Form->input('Model.field', array(
  2132. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2133. ));
  2134. $result = explode(':', $result);
  2135. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2136. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2137. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2138. $result = $this->Form->input('Model.field', array(
  2139. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2140. ));
  2141. $result = explode(':', $result);
  2142. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2143. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2144. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2145. $result = $this->Form->input('prueba', array(
  2146. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2147. 'maxYear' => date('Y') + 1, 'interval' => 15
  2148. ));
  2149. $result = explode(':', $result);
  2150. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2151. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2152. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2153. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2154. $result = $this->Form->input('Random.start_time', array(
  2155. 'type' => 'time',
  2156. 'selected' => '18:15'
  2157. ));
  2158. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2159. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2160. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2161. $result = $this->Form->input('published', array('type' => 'time'));
  2162. $now = strtotime('now');
  2163. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2164. $now = strtotime('2013-03-09 00:42:21');
  2165. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2166. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2167. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2168. }
  2169. /**
  2170. * Test interval + selected near the hour roll over.
  2171. *
  2172. * @return void
  2173. */
  2174. public function testTimeSelectedWithInterval() {
  2175. $result = $this->Form->input('Model.start_time', array(
  2176. 'type' => 'time',
  2177. 'interval' => 15,
  2178. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2179. ));
  2180. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2181. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2182. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2183. $result = $this->Form->input('Model.start_time', array(
  2184. 'type' => 'time',
  2185. 'interval' => 15,
  2186. 'selected' => '2012-10-23 15:57:00'
  2187. ));
  2188. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2189. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2190. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2191. $result = $this->Form->input('Model.start_time', array(
  2192. 'timeFormat' => 24,
  2193. 'type' => 'time',
  2194. 'interval' => 15,
  2195. 'selected' => '15:57'
  2196. ));
  2197. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2198. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2199. $result = $this->Form->input('Model.start_time', array(
  2200. 'timeFormat' => 24,
  2201. 'type' => 'time',
  2202. 'interval' => 15,
  2203. 'selected' => '23:57'
  2204. ));
  2205. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2206. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2207. $result = $this->Form->input('Model.created', array(
  2208. 'timeFormat' => 24,
  2209. 'type' => 'datetime',
  2210. 'interval' => 15,
  2211. 'selected' => '2012-09-30 23:56'
  2212. ));
  2213. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2214. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2215. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2216. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2217. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2218. }
  2219. /**
  2220. * Test time with selected values around 12:xx:xx
  2221. *
  2222. * @return void
  2223. */
  2224. public function testTimeSelectedWithIntervalTwelve() {
  2225. $result = $this->Form->input('Model.start_time', array(
  2226. 'type' => 'time',
  2227. 'timeFormat' => 12,
  2228. 'interval' => 15,
  2229. 'selected' => '00:00:00'
  2230. ));
  2231. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2232. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2233. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2234. $result = $this->Form->input('Model.start_time', array(
  2235. 'type' => 'time',
  2236. 'timeFormat' => 12,
  2237. 'interval' => 15,
  2238. 'selected' => '12:00:00'
  2239. ));
  2240. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2241. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2242. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2243. $result = $this->Form->input('Model.start_time', array(
  2244. 'type' => 'time',
  2245. 'timeFormat' => 12,
  2246. 'interval' => 15,
  2247. 'selected' => '12:15:00'
  2248. ));
  2249. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2250. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2251. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2252. }
  2253. /**
  2254. * Test interval & timeFormat = 12
  2255. *
  2256. * @return void
  2257. */
  2258. public function testInputTimeWithIntervalAnd12HourFormat() {
  2259. $result = $this->Form->input('Model.start_time', array(
  2260. 'type' => 'time',
  2261. 'timeFormat' => 12,
  2262. 'interval' => 5,
  2263. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2264. ));
  2265. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2266. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2267. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2268. $result = $this->Form->input('Model.start_time', array(
  2269. 'type' => 'time',
  2270. 'timeFormat' => '12',
  2271. 'interval' => 5,
  2272. 'selected' => '2013-04-19 16:30:00'
  2273. ));
  2274. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2275. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2276. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2277. $result = $this->Form->input('Model.start_time', array(
  2278. 'type' => 'time',
  2279. 'timeFormat' => '12',
  2280. 'interval' => 10,
  2281. 'selected' => '2013-05-19 00:33:00'
  2282. ));
  2283. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2284. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2285. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2286. $result = $this->Form->input('Model.start_time', array(
  2287. 'type' => 'time',
  2288. 'timeFormat' => '12',
  2289. 'interval' => 10,
  2290. 'selected' => '2013-05-19 13:33:00'
  2291. ));
  2292. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2293. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2294. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2295. $result = $this->Form->input('Model.start_time', array(
  2296. 'type' => 'time',
  2297. 'timeFormat' => '12',
  2298. 'interval' => 10,
  2299. 'selected' => '2013-05-19 01:33:00'
  2300. ));
  2301. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2302. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2303. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2304. }
  2305. /**
  2306. * test form->input() with datetime, date and time types
  2307. *
  2308. * @return void
  2309. */
  2310. public function testInputDatetime() {
  2311. extract($this->dateRegex);
  2312. $result = $this->Form->input('prueba', array(
  2313. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2314. 'maxYear' => date('Y') + 1, 'interval' => 15
  2315. ));
  2316. $result = explode(':', $result);
  2317. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2318. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2319. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2320. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2321. //related to ticket #5013
  2322. $result = $this->Form->input('Contact.date', array(
  2323. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2324. ));
  2325. $this->assertRegExp('/class="customClass"/', $result);
  2326. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2327. $result = $this->Form->input('Contact.date', array(
  2328. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2329. ));
  2330. $this->assertRegExp('/id="customIdDay"/', $result);
  2331. $this->assertRegExp('/id="customIdMonth"/', $result);
  2332. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2333. $result = $this->Form->input('Model.field', array(
  2334. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2335. ));
  2336. $this->assertRegExp('/id="customIDDay"/', $result);
  2337. $this->assertRegExp('/id="customIDHour"/', $result);
  2338. $result = explode('</select><select', $result);
  2339. $result = explode(':', $result[1]);
  2340. $this->assertRegExp('/option value="23"/', $result[0]);
  2341. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2342. $result = $this->Form->input('Model.field', array(
  2343. 'type' => 'datetime', 'timeFormat' => 12
  2344. ));
  2345. $result = explode('</select><select', $result);
  2346. $result = explode(':', $result[1]);
  2347. $this->assertRegExp('/option value="12"/', $result[0]);
  2348. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2349. $this->Form->request->data = array('Contact' => array('created' => null));
  2350. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2351. $expected = array(
  2352. 'div' => array('class' => 'input date'),
  2353. 'label' => array('for' => 'ContactCreatedMonth'),
  2354. 'Created',
  2355. '/label',
  2356. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2357. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2358. $monthsRegex,
  2359. '/select', '-',
  2360. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2361. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2362. $daysRegex,
  2363. '/select', '-',
  2364. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2365. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2366. $yearsRegex,
  2367. '/select',
  2368. '/div'
  2369. );
  2370. $this->assertTags($result, $expected);
  2371. $this->Form->request->data = array('Contact' => array('created' => null));
  2372. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2373. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2374. $this->Form->request->data = array('Contact' => array('created' => null));
  2375. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2376. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2377. $result = $this->Form->input('Contact.created', array(
  2378. 'type' => 'date',
  2379. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2380. 'timeFormat' => 'NONE'
  2381. ));
  2382. $this->assertRegExp('/for\="created-month"/', $result);
  2383. }
  2384. /**
  2385. * Test generating checkboxes in a loop.
  2386. *
  2387. * @return void
  2388. */
  2389. public function testInputCheckboxesInLoop() {
  2390. for ($i = 1; $i < 5; $i++) {
  2391. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2392. $expected = array(
  2393. 'div' => array('class' => 'input checkbox'),
  2394. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2395. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2396. 'label' => array('for' => "Contact{$i}Email"),
  2397. 'Email',
  2398. '/label',
  2399. '/div'
  2400. );
  2401. $this->assertTags($result, $expected);
  2402. }
  2403. }
  2404. /**
  2405. * Test generating checkboxes with disabled elements.
  2406. *
  2407. * @return void
  2408. */
  2409. public function testInputCheckboxWithDisabledElements() {
  2410. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2411. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2412. $expected = array(
  2413. array('div' => array('class' => 'input select')),
  2414. array('label' => array('for' => "ContactMultiple")),
  2415. 'Multiple',
  2416. '/label',
  2417. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2418. array('div' => array('class' => 'checkbox')),
  2419. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2420. array('label' => array('for' => "ContactMultiple1")),
  2421. 'One',
  2422. '/label',
  2423. '/div',
  2424. array('div' => array('class' => 'checkbox')),
  2425. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2426. array('label' => array('for' => "ContactMultiple2")),
  2427. 'Two',
  2428. '/label',
  2429. '/div',
  2430. array('div' => array('class' => 'checkbox')),
  2431. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2432. array('label' => array('for' => "ContactMultiple3")),
  2433. 'Three',
  2434. '/label',
  2435. '/div',
  2436. '/div'
  2437. );
  2438. $this->assertTags($result, $expected);
  2439. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2440. $this->assertTags($result, $expected);
  2441. $disabled = array('2', 3);
  2442. $expected = array(
  2443. array('div' => array('class' => 'input select')),
  2444. array('label' => array('for' => "ContactMultiple")),
  2445. 'Multiple',
  2446. '/label',
  2447. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2448. array('div' => array('class' => 'checkbox')),
  2449. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2450. array('label' => array('for' => "ContactMultiple1")),
  2451. 'One',
  2452. '/label',
  2453. '/div',
  2454. array('div' => array('class' => 'checkbox')),
  2455. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2456. array('label' => array('for' => "ContactMultiple2")),
  2457. 'Two',
  2458. '/label',
  2459. '/div',
  2460. array('div' => array('class' => 'checkbox')),
  2461. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2462. array('label' => array('for' => "ContactMultiple3")),
  2463. 'Three',
  2464. '/label',
  2465. '/div',
  2466. '/div'
  2467. );
  2468. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2469. $this->assertTags($result, $expected);
  2470. // make sure 50 does only disable 50, and not 50f5c0cf
  2471. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2472. $disabled = array(50);
  2473. $expected = array(
  2474. array('div' => array('class' => 'input select')),
  2475. array('label' => array('for' => "ContactMultiple")),
  2476. 'Multiple',
  2477. '/label',
  2478. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2479. array('div' => array('class' => 'checkbox')),
  2480. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2481. array('label' => array('for' => "ContactMultiple50")),
  2482. 'Fifty',
  2483. '/label',
  2484. '/div',
  2485. array('div' => array('class' => 'checkbox')),
  2486. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2487. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2488. 'Stringy',
  2489. '/label',
  2490. '/div',
  2491. '/div'
  2492. );
  2493. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2494. $this->assertTags($result, $expected);
  2495. }
  2496. /**
  2497. * test input name with leading integer, ensure attributes are generated correctly.
  2498. *
  2499. * @return void
  2500. */
  2501. public function testInputWithLeadingInteger() {
  2502. $result = $this->Form->text('0.Node.title');
  2503. $expected = array(
  2504. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. }
  2508. /**
  2509. * test form->input() with select type inputs.
  2510. *
  2511. * @return void
  2512. */
  2513. public function testInputSelectType() {
  2514. $result = $this->Form->input('email', array(
  2515. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2516. );
  2517. $expected = array(
  2518. 'div' => array('class' => 'input select'),
  2519. 'label' => array('for' => 'email'),
  2520. 'Email',
  2521. '/label',
  2522. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2523. array('option' => array('value' => '')),
  2524. '/option',
  2525. array('option' => array('value' => 'è')),
  2526. 'Firést',
  2527. '/option',
  2528. array('option' => array('value' => 'é')),
  2529. 'Secoènd',
  2530. '/option',
  2531. '/select',
  2532. '/div'
  2533. );
  2534. $this->assertTags($result, $expected);
  2535. $result = $this->Form->input('email', array(
  2536. 'options' => array('First', 'Second'), 'empty' => true)
  2537. );
  2538. $expected = array(
  2539. 'div' => array('class' => 'input select'),
  2540. 'label' => array('for' => 'email'),
  2541. 'Email',
  2542. '/label',
  2543. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2544. array('option' => array('value' => '')),
  2545. '/option',
  2546. array('option' => array('value' => '0')),
  2547. 'First',
  2548. '/option',
  2549. array('option' => array('value' => '1')),
  2550. 'Second',
  2551. '/option',
  2552. '/select',
  2553. '/div'
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2557. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2558. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2559. $expected = array(
  2560. 'div' => array('class' => 'input select'),
  2561. 'label' => array('for' => 'ModelUserId'),
  2562. 'User',
  2563. '/label',
  2564. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2565. array('option' => array('value' => '')),
  2566. '/option',
  2567. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2568. 'good',
  2569. '/option',
  2570. array('option' => array('value' => 'other')),
  2571. 'bad',
  2572. '/option',
  2573. '/select',
  2574. '/div'
  2575. );
  2576. $this->assertTags($result, $expected);
  2577. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2578. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2579. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2580. $expected = array(
  2581. 'div' => array('class' => 'input select'),
  2582. 'label' => array('for' => 'ThingUserId'),
  2583. 'User',
  2584. '/label',
  2585. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2586. array('option' => array('value' => '')),
  2587. 'Some Empty',
  2588. '/option',
  2589. array('option' => array('value' => 'value')),
  2590. 'good',
  2591. '/option',
  2592. array('option' => array('value' => 'other')),
  2593. 'bad',
  2594. '/option',
  2595. '/select',
  2596. '/div'
  2597. );
  2598. $this->assertTags($result, $expected);
  2599. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2600. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2601. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2602. $expected = array(
  2603. 'div' => array('class' => 'input select'),
  2604. 'label' => array('for' => 'ThingUserId'),
  2605. 'User',
  2606. '/label',
  2607. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2608. array('option' => array('value' => '')),
  2609. 'Some Empty',
  2610. '/option',
  2611. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2612. 'good',
  2613. '/option',
  2614. array('option' => array('value' => 'other')),
  2615. 'bad',
  2616. '/option',
  2617. '/select',
  2618. '/div'
  2619. );
  2620. $this->assertTags($result, $expected);
  2621. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2622. $this->Form->request->data = array('User' => array('User' => array('value')));
  2623. $result = $this->Form->input('User.User', array('empty' => true));
  2624. $expected = array(
  2625. 'div' => array('class' => 'input select'),
  2626. 'label' => array('for' => 'UserUser'),
  2627. 'User',
  2628. '/label',
  2629. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2630. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2631. array('option' => array('value' => '')),
  2632. '/option',
  2633. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2634. 'good',
  2635. '/option',
  2636. array('option' => array('value' => 'other')),
  2637. 'bad',
  2638. '/option',
  2639. '/select',
  2640. '/div'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. $this->Form->data = array();
  2644. $result = $this->Form->input('Publisher.id', array(
  2645. 'label' => 'Publisher',
  2646. 'type' => 'select',
  2647. 'multiple' => 'checkbox',
  2648. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2649. ));
  2650. $expected = array(
  2651. array('div' => array('class' => 'input select')),
  2652. array('label' => array('for' => 'PublisherId')),
  2653. 'Publisher',
  2654. '/label',
  2655. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2656. array('div' => array('class' => 'checkbox')),
  2657. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2658. array('label' => array('for' => 'PublisherIdValue1')),
  2659. 'Label 1',
  2660. '/label',
  2661. '/div',
  2662. array('div' => array('class' => 'checkbox')),
  2663. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2664. array('label' => array('for' => 'PublisherIdValue2')),
  2665. 'Label 2',
  2666. '/label',
  2667. '/div',
  2668. '/div'
  2669. );
  2670. $this->assertTags($result, $expected);
  2671. }
  2672. /**
  2673. * test that input() and a non standard primary key makes a hidden input by default.
  2674. *
  2675. * @return void
  2676. */
  2677. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2678. $this->Form->create('User');
  2679. $this->Form->fieldset = array(
  2680. 'User' => array(
  2681. 'fields' => array(
  2682. 'model_id' => array('type' => 'integer')
  2683. ),
  2684. 'validates' => array(),
  2685. 'key' => 'model_id'
  2686. )
  2687. );
  2688. $result = $this->Form->input('model_id');
  2689. $expected = array(
  2690. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2691. );
  2692. $this->assertTags($result, $expected);
  2693. }
  2694. /**
  2695. * test that overriding the magic select type widget is possible
  2696. *
  2697. * @return void
  2698. */
  2699. public function testInputOverridingMagicSelectType() {
  2700. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2701. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2702. $expected = array(
  2703. 'div' => array('class' => 'input text'),
  2704. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2705. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2706. '/div'
  2707. );
  2708. $this->assertTags($result, $expected);
  2709. //Check that magic types still work for plural/singular vars
  2710. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2711. $result = $this->Form->input('Model.type');
  2712. $expected = array(
  2713. 'div' => array('class' => 'input select'),
  2714. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2715. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2716. array('option' => array('value' => 'value')), 'good', '/option',
  2717. array('option' => array('value' => 'other')), 'bad', '/option',
  2718. '/select',
  2719. '/div'
  2720. );
  2721. $this->assertTags($result, $expected);
  2722. }
  2723. /**
  2724. * Test that inferred types do not override developer input
  2725. *
  2726. * @return void
  2727. */
  2728. public function testInputMagicTypeDoesNotOverride() {
  2729. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2730. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2731. $expected = array(
  2732. 'div' => array('class' => 'input checkbox'),
  2733. array('input' => array(
  2734. 'type' => 'hidden',
  2735. 'name' => 'data[Model][user]',
  2736. 'id' => 'ModelUser_',
  2737. 'value' => 0,
  2738. )),
  2739. array('input' => array(
  2740. 'name' => 'data[Model][user]',
  2741. 'type' => 'checkbox',
  2742. 'id' => 'ModelUser',
  2743. 'value' => 1
  2744. )),
  2745. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2746. '/div'
  2747. );
  2748. $this->assertTags($result, $expected);
  2749. }
  2750. /**
  2751. * Test that magic input() selects are created for type=number
  2752. *
  2753. * @return void
  2754. */
  2755. public function testInputMagicSelectForTypeNumber() {
  2756. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2757. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2758. $result = $this->Form->input('ValidateUser.balance');
  2759. $expected = array(
  2760. 'div' => array('class' => 'input select'),
  2761. 'label' => array('for' => 'ValidateUserBalance'),
  2762. 'Balance',
  2763. '/label',
  2764. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2765. array('option' => array('value' => '0')),
  2766. 'nothing',
  2767. '/option',
  2768. array('option' => array('value' => '1', 'selected' => 'selected')),
  2769. 'some',
  2770. '/option',
  2771. array('option' => array('value' => '100')),
  2772. 'a lot',
  2773. '/option',
  2774. '/select',
  2775. '/div'
  2776. );
  2777. $this->assertTags($result, $expected);
  2778. }
  2779. /**
  2780. * Test that magic input() selects can easily be converted into radio types without error.
  2781. *
  2782. * @return void
  2783. */
  2784. public function testInputMagicSelectChangeToRadio() {
  2785. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2786. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2787. $this->assertRegExp('/input type="radio"/', $result);
  2788. }
  2789. /**
  2790. * fields with the same name as the model should work.
  2791. *
  2792. * @return void
  2793. */
  2794. public function testInputWithMatchingFieldAndModelName() {
  2795. $this->Form->create('User');
  2796. $this->Form->fieldset = array(
  2797. 'User' => array(
  2798. 'fields' => array(
  2799. 'User' => array('type' => 'text')
  2800. ),
  2801. 'validates' => array(),
  2802. 'key' => 'id'
  2803. )
  2804. );
  2805. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2806. $result = $this->Form->input('User', array('type' => 'text'));
  2807. $expected = array(
  2808. 'div' => array('class' => 'input text'),
  2809. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2810. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2811. '/div'
  2812. );
  2813. $this->assertTags($result, $expected);
  2814. }
  2815. /**
  2816. * testFormInputs method
  2817. *
  2818. * test correct results from form::inputs().
  2819. *
  2820. * @return void
  2821. */
  2822. public function testFormInputs() {
  2823. $this->Form->create('Contact');
  2824. $result = $this->Form->inputs('The Legend');
  2825. $expected = array(
  2826. '<fieldset',
  2827. '<legend',
  2828. 'The Legend',
  2829. '/legend',
  2830. '*/fieldset',
  2831. );
  2832. $this->assertTags($result, $expected);
  2833. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2834. $expected = array(
  2835. 'fieldset' => array('class' => 'classy-stuff'),
  2836. '<legend',
  2837. 'Field of Dreams',
  2838. '/legend',
  2839. '*/fieldset'
  2840. );
  2841. $this->assertTags($result, $expected);
  2842. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2843. $this->assertTags($result, $expected);
  2844. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2845. $this->assertTags($result, $expected);
  2846. $this->Form->create('Contact');
  2847. $this->Form->request['prefix'] = 'admin';
  2848. $this->Form->request['action'] = 'admin_edit';
  2849. $result = $this->Form->inputs();
  2850. $expected = array(
  2851. '<fieldset',
  2852. '<legend',
  2853. 'Edit Contact',
  2854. '/legend',
  2855. '*/fieldset',
  2856. );
  2857. $this->assertTags($result, $expected);
  2858. $this->Form->create('Contact');
  2859. $result = $this->Form->inputs(false);
  2860. $expected = array(
  2861. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2862. array('div' => array('class' => 'input text')),
  2863. '*/div',
  2864. array('div' => array('class' => 'input email')),
  2865. '*/div',
  2866. array('div' => array('class' => 'input tel')),
  2867. '*/div',
  2868. array('div' => array('class' => 'input password')),
  2869. '*/div',
  2870. array('div' => array('class' => 'input date')),
  2871. '*/div',
  2872. array('div' => array('class' => 'input date')),
  2873. '*/div',
  2874. array('div' => array('class' => 'input datetime')),
  2875. '*/div',
  2876. array('div' => array('class' => 'input number')),
  2877. '*/div',
  2878. array('div' => array('class' => 'input select')),
  2879. '*/div',
  2880. );
  2881. $this->assertTags($result, $expected);
  2882. $this->Form->create('Contact');
  2883. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2884. $expected = array(
  2885. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2886. array('div' => array('class' => 'input text')),
  2887. '*/div',
  2888. array('div' => array('class' => 'input email')),
  2889. '*/div',
  2890. array('div' => array('class' => 'input tel')),
  2891. '*/div',
  2892. array('div' => array('class' => 'input password')),
  2893. '*/div',
  2894. array('div' => array('class' => 'input date')),
  2895. '*/div',
  2896. array('div' => array('class' => 'input date')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input datetime')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input number')),
  2901. '*/div',
  2902. array('div' => array('class' => 'input select')),
  2903. '*/div',
  2904. );
  2905. $this->assertTags($result, $expected);
  2906. $this->Form->create('Contact');
  2907. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2908. $this->assertTags($result, $expected);
  2909. $this->Form->create('Contact');
  2910. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2911. $expected = array(
  2912. 'fieldset' => array(),
  2913. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2914. array('div' => array('class' => 'input text')),
  2915. '*/div',
  2916. array('div' => array('class' => 'input email')),
  2917. '*/div',
  2918. array('div' => array('class' => 'input tel')),
  2919. '*/div',
  2920. array('div' => array('class' => 'input password')),
  2921. '*/div',
  2922. array('div' => array('class' => 'input date')),
  2923. '*/div',
  2924. array('div' => array('class' => 'input date')),
  2925. '*/div',
  2926. array('div' => array('class' => 'input datetime')),
  2927. '*/div',
  2928. array('div' => array('class' => 'input number')),
  2929. '*/div',
  2930. array('div' => array('class' => 'input select')),
  2931. '*/div',
  2932. '/fieldset'
  2933. );
  2934. $this->assertTags($result, $expected);
  2935. $this->Form->create('Contact');
  2936. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2937. $expected = array(
  2938. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2939. array('div' => array('class' => 'input text')),
  2940. '*/div',
  2941. array('div' => array('class' => 'input email')),
  2942. '*/div',
  2943. array('div' => array('class' => 'input tel')),
  2944. '*/div',
  2945. array('div' => array('class' => 'input password')),
  2946. '*/div',
  2947. array('div' => array('class' => 'input date')),
  2948. '*/div',
  2949. array('div' => array('class' => 'input date')),
  2950. '*/div',
  2951. array('div' => array('class' => 'input datetime')),
  2952. '*/div',
  2953. array('div' => array('class' => 'input number')),
  2954. '*/div',
  2955. array('div' => array('class' => 'input select')),
  2956. '*/div',
  2957. );
  2958. $this->assertTags($result, $expected);
  2959. $this->Form->create('Contact');
  2960. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2961. $this->assertTags($result, $expected);
  2962. $this->Form->create('Contact');
  2963. $result = $this->Form->inputs('Hello');
  2964. $expected = array(
  2965. 'fieldset' => array(),
  2966. 'legend' => array(),
  2967. 'Hello',
  2968. '/legend',
  2969. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2970. array('div' => array('class' => 'input text')),
  2971. '*/div',
  2972. array('div' => array('class' => 'input email')),
  2973. '*/div',
  2974. array('div' => array('class' => 'input tel')),
  2975. '*/div',
  2976. array('div' => array('class' => 'input password')),
  2977. '*/div',
  2978. array('div' => array('class' => 'input date')),
  2979. '*/div',
  2980. array('div' => array('class' => 'input date')),
  2981. '*/div',
  2982. array('div' => array('class' => 'input datetime')),
  2983. '*/div',
  2984. array('div' => array('class' => 'input number')),
  2985. '*/div',
  2986. array('div' => array('class' => 'input select')),
  2987. '*/div',
  2988. '/fieldset'
  2989. );
  2990. $this->assertTags($result, $expected);
  2991. $this->Form->create('Contact');
  2992. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2993. $expected = array(
  2994. 'fieldset' => array(),
  2995. 'legend' => array(),
  2996. 'Hello',
  2997. '/legend',
  2998. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2999. array('div' => array('class' => 'input text')),
  3000. '*/div',
  3001. array('div' => array('class' => 'input email')),
  3002. '*/div',
  3003. array('div' => array('class' => 'input tel')),
  3004. '*/div',
  3005. array('div' => array('class' => 'input password')),
  3006. '*/div',
  3007. array('div' => array('class' => 'input date')),
  3008. '*/div',
  3009. array('div' => array('class' => 'input date')),
  3010. '*/div',
  3011. array('div' => array('class' => 'input datetime')),
  3012. '*/div',
  3013. array('div' => array('class' => 'input number')),
  3014. '*/div',
  3015. array('div' => array('class' => 'input select')),
  3016. '*/div',
  3017. '/fieldset'
  3018. );
  3019. $this->assertTags($result, $expected);
  3020. $this->Form->create('Contact');
  3021. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3022. $this->assertTags($result, $expected);
  3023. $this->Form->end();
  3024. $this->Form->create(false);
  3025. $expected = array(
  3026. 'fieldset' => array(),
  3027. array('div' => array('class' => 'input text')),
  3028. 'label' => array('for' => 'foo'),
  3029. 'Foo',
  3030. '/label',
  3031. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  3032. '*/div',
  3033. '/fieldset'
  3034. );
  3035. $result = $this->Form->inputs(
  3036. array('foo' => array('type' => 'text')),
  3037. array(),
  3038. array('legend' => false)
  3039. );
  3040. $this->assertTags($result, $expected);
  3041. }
  3042. /**
  3043. * Tests inputs() works with plugin models
  3044. *
  3045. * @return void
  3046. */
  3047. public function testInputsPluginModel() {
  3048. $this->loadFixtures('Post');
  3049. App::build(array(
  3050. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  3051. ));
  3052. CakePlugin::load('TestPlugin');
  3053. $this->Form->request['models'] = array(
  3054. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3055. );
  3056. $this->Form->create('TestPlugin.TestPluginPost');
  3057. $result = $this->Form->inputs();
  3058. $this->assertContains('data[TestPluginPost][id]', $result);
  3059. $this->assertContains('data[TestPluginPost][author_id]', $result);
  3060. $this->assertContains('data[TestPluginPost][title]', $result);
  3061. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  3062. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  3063. $this->assertEquals('TestPluginPost', $this->Form->model());
  3064. }
  3065. /**
  3066. * testSelectAsCheckbox method
  3067. *
  3068. * test multi-select widget with checkbox formatting.
  3069. *
  3070. * @return void
  3071. */
  3072. public function testSelectAsCheckbox() {
  3073. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  3074. $expected = array(
  3075. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3076. array('div' => array('class' => 'checkbox')),
  3077. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  3078. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  3079. 'first',
  3080. '/label',
  3081. '/div',
  3082. array('div' => array('class' => 'checkbox')),
  3083. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  3084. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  3085. 'second',
  3086. '/label',
  3087. '/div',
  3088. array('div' => array('class' => 'checkbox')),
  3089. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3090. array('label' => array('for' => 'ModelMultiField2')),
  3091. 'third',
  3092. '/label',
  3093. '/div',
  3094. );
  3095. $this->assertTags($result, $expected);
  3096. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  3097. $expected = array(
  3098. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3099. array('div' => array('class' => 'checkbox')),
  3100. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  3101. array('label' => array('for' => 'ModelMultiField12')),
  3102. 'half',
  3103. '/label',
  3104. '/div',
  3105. );
  3106. $this->assertTags($result, $expected);
  3107. }
  3108. /**
  3109. * testLabel method
  3110. *
  3111. * test label generation.
  3112. *
  3113. * @return void
  3114. */
  3115. public function testLabel() {
  3116. $this->Form->text('Person.name');
  3117. $result = $this->Form->label();
  3118. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3119. $this->Form->text('Person.name');
  3120. $result = $this->Form->label();
  3121. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3122. $result = $this->Form->label('Person.first_name');
  3123. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3124. $result = $this->Form->label('Person.first_name', 'Your first name');
  3125. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3126. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3127. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3128. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3129. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3130. $result = $this->Form->label('Person.first_name', '');
  3131. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3132. $result = $this->Form->label('Person.2.name', '');
  3133. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3134. }
  3135. /**
  3136. * testTextbox method
  3137. *
  3138. * test textbox element generation
  3139. *
  3140. * @return void
  3141. */
  3142. public function testTextbox() {
  3143. $result = $this->Form->text('Model.field');
  3144. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3145. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3146. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3147. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3148. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3149. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3150. $result = $this->Form->text('Model.text');
  3151. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3152. $Contact = ClassRegistry::getObject('Contact');
  3153. $Contact->validationErrors['text'] = array(true);
  3154. $this->Form->request->data['Contact']['text'] = 'test';
  3155. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3156. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3157. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3158. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3159. $expected = array(
  3160. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3161. );
  3162. $this->assertTags($result, $expected);
  3163. }
  3164. /**
  3165. * testDefaultValue method
  3166. *
  3167. * Test default value setting
  3168. *
  3169. * @return void
  3170. */
  3171. public function testDefaultValue() {
  3172. $this->Form->request->data['Model']['field'] = 'test';
  3173. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3174. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3175. unset($this->Form->request->data['Model']['field']);
  3176. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3177. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3178. }
  3179. /**
  3180. * testCheckboxDefaultValue method
  3181. *
  3182. * Test default value setting on checkbox() method
  3183. *
  3184. * @return void
  3185. */
  3186. public function testCheckboxDefaultValue() {
  3187. $this->Form->request->data['Model']['field'] = false;
  3188. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3189. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3190. unset($this->Form->request->data['Model']['field']);
  3191. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3192. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3193. $this->Form->request->data['Model']['field'] = true;
  3194. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3195. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3196. unset($this->Form->request->data['Model']['field']);
  3197. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3198. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3199. }
  3200. /**
  3201. * testError method
  3202. *
  3203. * Test field error generation
  3204. *
  3205. * @return void
  3206. */
  3207. public function testError() {
  3208. $Contact = ClassRegistry::getObject('Contact');
  3209. $Contact->validationErrors['field'] = array(1);
  3210. $result = $this->Form->error('Contact.field');
  3211. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3212. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3213. $this->assertEquals('Error in field Field', $result);
  3214. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3215. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3216. $this->assertEquals('This field contains invalid input', $result);
  3217. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3218. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3219. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3220. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3221. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3222. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3223. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3224. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3225. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3226. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3227. $this->assertEquals('<strong>Badness!</strong>', $result);
  3228. $Contact->validationErrors['field'] = array("email");
  3229. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3230. $expected = array(
  3231. 'div' => array('class' => 'field-error'),
  3232. 'No good!',
  3233. '/div'
  3234. );
  3235. $this->assertTags($result, $expected);
  3236. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3237. $result = $this->Form->error('Contact.field', array(
  3238. 'notEmpty' => 'Cannot be empty',
  3239. 'email' => 'No good!'
  3240. ));
  3241. $expected = array(
  3242. 'div' => array('class' => 'error-message'),
  3243. 'ul' => array(),
  3244. '<li', 'Cannot be empty', '/li',
  3245. '<li', 'No good!', '/li',
  3246. '<li', 'Something else', '/li',
  3247. '/ul',
  3248. '/div'
  3249. );
  3250. $this->assertTags($result, $expected);
  3251. // Testing error messages list options
  3252. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3253. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3254. $expected = array(
  3255. 'div' => array('class' => 'error-message'),
  3256. 'ol' => array(),
  3257. '<li', 'notEmpty', '/li',
  3258. '<li', 'email', '/li',
  3259. '/ol',
  3260. '/div'
  3261. );
  3262. $this->assertTags($result, $expected);
  3263. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3264. $expected = array(
  3265. 'div' => array('class' => 'error-message'),
  3266. 'ol' => array(),
  3267. '<li', 'notEmpty', '/li',
  3268. '<li', 'email', '/li',
  3269. '/ol',
  3270. '/div'
  3271. );
  3272. $this->assertTags($result, $expected);
  3273. $result = $this->Form->error('Contact.field', null, array(
  3274. 'listOptions' => array(
  3275. 'class' => 'ul-class',
  3276. 'itemOptions' => array(
  3277. 'class' => 'li-class'
  3278. )
  3279. )
  3280. ));
  3281. $expected = array(
  3282. 'div' => array('class' => 'error-message'),
  3283. 'ul' => array('class' => 'ul-class'),
  3284. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3285. array('li' => array('class' => 'li-class')), 'email', '/li',
  3286. '/ul',
  3287. '/div'
  3288. );
  3289. $this->assertTags($result, $expected);
  3290. }
  3291. /**
  3292. * test error options when using form->input();
  3293. *
  3294. * @return void
  3295. */
  3296. public function testInputErrorEscape() {
  3297. $this->Form->create('ValidateProfile');
  3298. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3299. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3300. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3301. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3302. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3303. $this->assertRegExp('/required<br>/', $result);
  3304. }
  3305. /**
  3306. * testPassword method
  3307. *
  3308. * Test password element generation
  3309. *
  3310. * @return void
  3311. */
  3312. public function testPassword() {
  3313. $Contact = ClassRegistry::getObject('Contact');
  3314. $result = $this->Form->password('Contact.field');
  3315. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3316. $Contact->validationErrors['passwd'] = 1;
  3317. $this->Form->request->data['Contact']['passwd'] = 'test';
  3318. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3319. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3320. }
  3321. /**
  3322. * testRadio method
  3323. *
  3324. * Test radio element set generation
  3325. *
  3326. * @return void
  3327. */
  3328. public function testRadio() {
  3329. $result = $this->Form->radio('Model.field', array('option A'));
  3330. $expected = array(
  3331. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3332. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3333. 'label' => array('for' => 'ModelField0'),
  3334. 'option A',
  3335. '/label'
  3336. );
  3337. $this->assertTags($result, $expected);
  3338. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3339. $expected = array(
  3340. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3341. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3342. 'label' => array('for' => 'ModelField12'),
  3343. 'half',
  3344. '/label'
  3345. );
  3346. $this->assertTags($result, $expected);
  3347. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3348. $expected = array(
  3349. 'fieldset' => array(),
  3350. 'legend' => array(),
  3351. 'Field',
  3352. '/legend',
  3353. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3354. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3355. array('label' => array('for' => 'ModelField0')),
  3356. 'option A',
  3357. '/label',
  3358. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3359. array('label' => array('for' => 'ModelField1')),
  3360. 'option B',
  3361. '/label',
  3362. '/fieldset'
  3363. );
  3364. $this->assertTags($result, $expected);
  3365. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3366. $expected = array(
  3367. 'fieldset' => array(),
  3368. 'legend' => array(),
  3369. 'Field',
  3370. '/legend',
  3371. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3372. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3373. array('label' => array('for' => 'ModelField0')),
  3374. 'option A',
  3375. '/label',
  3376. 'br' => array(),
  3377. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3378. array('label' => array('for' => 'ModelField1')),
  3379. 'option B',
  3380. '/label',
  3381. '/fieldset'
  3382. );
  3383. $this->assertTags($result, $expected);
  3384. $result = $this->Form->radio(
  3385. 'Employee.gender',
  3386. array('male' => 'Male', 'female' => 'Female'),
  3387. array('form' => 'my-form')
  3388. );
  3389. $expected = array(
  3390. 'fieldset' => array(),
  3391. 'legend' => array(),
  3392. 'Gender',
  3393. '/legend',
  3394. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'),
  3395. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
  3396. array('label' => array('for' => 'EmployeeGenderMale')),
  3397. 'Male',
  3398. '/label',
  3399. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')),
  3400. array('label' => array('for' => 'EmployeeGenderFemale')),
  3401. 'Female',
  3402. '/label',
  3403. '/fieldset',
  3404. );
  3405. $this->assertTags($result, $expected);
  3406. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3407. $expected = array(
  3408. 'fieldset' => array(),
  3409. 'legend' => array(),
  3410. 'Gender',
  3411. '/legend',
  3412. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3413. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3414. array('label' => array('for' => 'OfficerGenderMale')),
  3415. 'Male',
  3416. '/label',
  3417. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3418. array('label' => array('for' => 'OfficerGenderFemale')),
  3419. 'Female',
  3420. '/label',
  3421. '/fieldset',
  3422. );
  3423. $this->assertTags($result, $expected);
  3424. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3425. $expected = array(
  3426. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3427. array('input' => array(
  3428. 'type' => 'radio',
  3429. 'name' => 'data[Contact][1][imrequired]',
  3430. 'value' => '0',
  3431. 'id' => 'Contact1Imrequired0',
  3432. 'required' => 'required'
  3433. )),
  3434. 'label' => array('for' => 'Contact1Imrequired0'),
  3435. 'option A',
  3436. '/label'
  3437. );
  3438. $this->assertTags($result, $expected);
  3439. $result = $this->Form->radio('Model.1.field', array('option A'));
  3440. $expected = array(
  3441. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3442. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3443. 'label' => array('for' => 'Model1Field0'),
  3444. 'option A',
  3445. '/label'
  3446. );
  3447. $this->assertTags($result, $expected);
  3448. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3449. $expected = array(
  3450. 'fieldset' => array(),
  3451. 'legend' => array(),
  3452. 'Field',
  3453. '/legend',
  3454. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3455. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3456. array('label' => array('for' => 'ModelField0')),
  3457. 'option A',
  3458. '/label',
  3459. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3460. array('label' => array('for' => 'ModelField1')),
  3461. 'option B',
  3462. '/label',
  3463. '/fieldset'
  3464. );
  3465. $this->assertTags($result, $expected);
  3466. $result = $this->Form->radio(
  3467. 'Model.field',
  3468. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3469. );
  3470. $expected = array(
  3471. 'fieldset' => array(),
  3472. 'legend' => array(),
  3473. 'Field',
  3474. '/legend',
  3475. 'input' => array(
  3476. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3477. 'id' => 'ModelField_', 'value' => '',
  3478. ),
  3479. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3480. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3481. array('label' => array('for' => 'ModelFieldAB')),
  3482. 'first',
  3483. '/label',
  3484. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3485. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3486. array('label' => array('for' => 'ModelFieldAB1')),
  3487. 'second',
  3488. '/label',
  3489. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3490. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3491. array('label' => array('for' => 'ModelFieldAB2')),
  3492. 'third',
  3493. '/label',
  3494. '/fieldset'
  3495. );
  3496. $this->assertTags($result, $expected);
  3497. }
  3498. /**
  3499. * testRadioDifferentModel
  3500. * Refs #2911
  3501. *
  3502. * @return void
  3503. */
  3504. public function testRadioDifferentModel() {
  3505. $this->Form->create('User');
  3506. $result = $this->Form->radio(
  3507. 'Model.field',
  3508. array('v1' => 'option A', 'v2' => 'option B'),
  3509. array('label' => true, 'legend' => false, 'value' => false)
  3510. );
  3511. $expected = array(
  3512. array('input' => array(
  3513. 'type' => 'radio', 'name' => 'data[Model][field]',
  3514. 'value' => 'v1', 'id' => 'ModelFieldV1'
  3515. )),
  3516. array('label' => array('for' => 'ModelFieldV1')),
  3517. 'option A',
  3518. '/label',
  3519. array('input' => array(
  3520. 'type' => 'radio', 'name' => 'data[Model][field]',
  3521. 'value' => 'v2', 'id' => 'ModelFieldV2'
  3522. )),
  3523. array('label' => array('for' => 'ModelFieldV2')),
  3524. 'option B',
  3525. '/label'
  3526. );
  3527. $this->assertTags($result, $expected);
  3528. }
  3529. /**
  3530. * Test radio inputs with between as string or array. Also ensure
  3531. * that an array with less between elements works.
  3532. *
  3533. * @return void
  3534. */
  3535. public function testRadioBetween() {
  3536. $result = $this->Form->radio(
  3537. 'Model.field',
  3538. array('option A', 'option B'),
  3539. array('between' => 'I am between')
  3540. );
  3541. $expected = array(
  3542. 'fieldset' => array(),
  3543. 'legend' => array(),
  3544. 'Field',
  3545. '/legend',
  3546. 'I am between',
  3547. 'input' => array(
  3548. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3549. 'value' => '', 'id' => 'ModelField_'
  3550. ),
  3551. array('input' => array(
  3552. 'type' => 'radio', 'name' => 'data[Model][field]',
  3553. 'value' => '0', 'id' => 'ModelField0'
  3554. )),
  3555. array('label' => array('for' => 'ModelField0')),
  3556. 'option A',
  3557. '/label',
  3558. array('input' => array(
  3559. 'type' => 'radio', 'name' => 'data[Model][field]',
  3560. 'value' => '1', 'id' => 'ModelField1'
  3561. )),
  3562. array('label' => array('for' => 'ModelField1')),
  3563. 'option B',
  3564. '/label',
  3565. '/fieldset'
  3566. );
  3567. $this->assertTags($result, $expected);
  3568. $result = $this->Form->radio(
  3569. 'Model.field',
  3570. array('option A', 'option B', 'option C'),
  3571. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3572. );
  3573. $expected = array(
  3574. 'fieldset' => array(),
  3575. 'legend' => array(),
  3576. 'Field',
  3577. '/legend',
  3578. 'input' => array(
  3579. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3580. 'value' => '', 'id' => 'ModelField_'
  3581. ),
  3582. array('input' => array(
  3583. 'type' => 'radio', 'name' => 'data[Model][field]',
  3584. 'value' => '0', 'id' => 'ModelField0'
  3585. )),
  3586. array('label' => array('for' => 'ModelField0')),
  3587. 'option A',
  3588. '/label',
  3589. 'between A',
  3590. '--separator--',
  3591. array('input' => array(
  3592. 'type' => 'radio', 'name' => 'data[Model][field]',
  3593. 'value' => '1', 'id' => 'ModelField1'
  3594. )),
  3595. array('label' => array('for' => 'ModelField1')),
  3596. 'option B',
  3597. '/label',
  3598. 'between B',
  3599. '--separator--',
  3600. array('input' => array(
  3601. 'type' => 'radio', 'name' => 'data[Model][field]',
  3602. 'value' => '2', 'id' => 'ModelField2'
  3603. )),
  3604. array('label' => array('for' => 'ModelField2')),
  3605. 'option C',
  3606. '/label',
  3607. 'between C',
  3608. '/fieldset'
  3609. );
  3610. $this->assertTags($result, $expected);
  3611. $result = $this->Form->input('Model.field', array(
  3612. 'options' => array('1' => 'first', '2' => 'second'),
  3613. 'type' => 'radio',
  3614. 'before' => '--before--',
  3615. 'after' => '--after--',
  3616. 'separator' => '--separator--',
  3617. 'between' => array('--between first--', '--between second--')
  3618. ));
  3619. $expected = array(
  3620. 'div' => array('class' => 'input radio'),
  3621. '--before--',
  3622. 'fieldset' => array(),
  3623. 'legend' => array(),
  3624. 'Field',
  3625. '/legend',
  3626. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3627. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3628. array('label' => array('for' => 'ModelField1')),
  3629. 'first',
  3630. '/label',
  3631. '--between first--',
  3632. '--separator--',
  3633. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3634. array('label' => array('for' => 'ModelField2')),
  3635. 'second',
  3636. '/label',
  3637. '--between second--',
  3638. '/fieldset',
  3639. '--after--',
  3640. '/div'
  3641. );
  3642. $this->assertTags($result, $expected);
  3643. $result = $this->Form->input('Model.field', array(
  3644. 'options' => array('1' => 'first', '2' => 'second'),
  3645. 'type' => 'radio',
  3646. 'before' => '--before--',
  3647. 'after' => '--after--',
  3648. 'separator' => '--separator--',
  3649. 'between' => array('--between first--')
  3650. ));
  3651. $expected = array(
  3652. 'div' => array('class' => 'input radio'),
  3653. '--before--',
  3654. 'fieldset' => array(),
  3655. 'legend' => array(),
  3656. 'Field',
  3657. '/legend',
  3658. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3659. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3660. array('label' => array('for' => 'ModelField1')),
  3661. 'first',
  3662. '/label',
  3663. '--between first--',
  3664. '--separator--',
  3665. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3666. array('label' => array('for' => 'ModelField2')),
  3667. 'second',
  3668. '/label',
  3669. '/fieldset',
  3670. '--after--',
  3671. '/div'
  3672. );
  3673. $this->assertTags($result, $expected);
  3674. }
  3675. /**
  3676. * Test that radios with a 0 value are selected under the correct conditions.
  3677. * Also ensure that values that are booleanish are handled correctly.
  3678. *
  3679. * @return void
  3680. */
  3681. public function testRadioOptionWithBooleanishValues() {
  3682. $expected = array(
  3683. 'fieldset' => array(),
  3684. 'legend' => array(),
  3685. 'Field',
  3686. '/legend',
  3687. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3688. array('label' => array('for' => 'ModelField1')),
  3689. 'Yes',
  3690. '/label',
  3691. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3692. array('label' => array('for' => 'ModelField0')),
  3693. 'No',
  3694. '/label',
  3695. '/fieldset'
  3696. );
  3697. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3698. $this->assertTags($result, $expected);
  3699. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3700. $this->assertTags($result, $expected);
  3701. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3702. $this->assertTags($result, $expected);
  3703. $expected = array(
  3704. 'fieldset' => array(),
  3705. 'legend' => array(),
  3706. 'Field',
  3707. '/legend',
  3708. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3709. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3710. array('label' => array('for' => 'ModelField1')),
  3711. 'Yes',
  3712. '/label',
  3713. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3714. array('label' => array('for' => 'ModelField0')),
  3715. 'No',
  3716. '/label',
  3717. '/fieldset'
  3718. );
  3719. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3720. $this->assertTags($result, $expected);
  3721. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3722. $this->assertTags($result, $expected);
  3723. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3724. $this->assertTags($result, $expected);
  3725. $expected = array(
  3726. 'fieldset' => array(),
  3727. 'legend' => array(),
  3728. 'Field',
  3729. '/legend',
  3730. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3731. array('label' => array('for' => 'ModelField1')),
  3732. 'Yes',
  3733. '/label',
  3734. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3735. array('label' => array('for' => 'ModelField0')),
  3736. 'No',
  3737. '/label',
  3738. '/fieldset'
  3739. );
  3740. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3741. $this->assertTags($result, $expected);
  3742. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3743. $this->assertTags($result, $expected);
  3744. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3745. $this->assertTags($result, $expected);
  3746. }
  3747. /**
  3748. * test disabled radio options
  3749. *
  3750. * @return void
  3751. */
  3752. public function testRadioDisabled() {
  3753. $result = $this->Form->radio(
  3754. 'Model.field',
  3755. array('option A', 'option B'),
  3756. array('disabled' => array(0), 'value' => '0')
  3757. );
  3758. $expected = array(
  3759. 'fieldset' => array(),
  3760. 'legend' => array(),
  3761. 'Field',
  3762. '/legend',
  3763. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3764. array('label' => array('for' => 'ModelField0')),
  3765. 'option A',
  3766. '/label',
  3767. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3768. array('label' => array('for' => 'ModelField1')),
  3769. 'option B',
  3770. '/label',
  3771. '/fieldset'
  3772. );
  3773. $this->assertTags($result, $expected);
  3774. $result = $this->Form->radio(
  3775. 'Model.field',
  3776. array('option A', 'option B'),
  3777. array('disabled' => true, 'value' => '0')
  3778. );
  3779. $expected = array(
  3780. 'fieldset' => array(),
  3781. 'legend' => array(),
  3782. 'Field',
  3783. '/legend',
  3784. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3785. array('label' => array('for' => 'ModelField0')),
  3786. 'option A',
  3787. '/label',
  3788. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3789. array('label' => array('for' => 'ModelField1')),
  3790. 'option B',
  3791. '/label',
  3792. '/fieldset'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $result = $this->Form->radio(
  3796. 'Model.field',
  3797. array('option A', 'option B'),
  3798. array('disabled' => 'disabled', 'value' => '0')
  3799. );
  3800. $expected = array(
  3801. 'fieldset' => array(),
  3802. 'legend' => array(),
  3803. 'Field',
  3804. '/legend',
  3805. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3806. array('label' => array('for' => 'ModelField0')),
  3807. 'option A',
  3808. '/label',
  3809. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3810. array('label' => array('for' => 'ModelField1')),
  3811. 'option B',
  3812. '/label',
  3813. '/fieldset'
  3814. );
  3815. $this->assertTags($result, $expected);
  3816. $result = $this->Form->input('Model.field', array(
  3817. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3818. 'type' => 'radio',
  3819. 'disabled' => array(2, '3x'),
  3820. ));
  3821. $expected = array(
  3822. 'div' => array('class' => 'input radio'),
  3823. 'fieldset' => array(),
  3824. 'legend' => array(),
  3825. 'Field',
  3826. '/legend',
  3827. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3828. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3829. array('label' => array('for' => 'ModelField1')),
  3830. 'first',
  3831. '/label',
  3832. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3833. array('label' => array('for' => 'ModelField2')),
  3834. 'second',
  3835. '/label',
  3836. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3837. array('label' => array('for' => 'ModelField2x')),
  3838. '2x',
  3839. '/label',
  3840. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3841. array('label' => array('for' => 'ModelField3')),
  3842. 'third',
  3843. '/label',
  3844. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3845. array('label' => array('for' => 'ModelField3x')),
  3846. '3x',
  3847. '/label',
  3848. '/fieldset',
  3849. '/div'
  3850. );
  3851. $this->assertTags($result, $expected);
  3852. $result = $this->Form->input('Model.field', array(
  3853. 'type' => 'radio',
  3854. 'options' => array(
  3855. 1 => 'A',
  3856. 2 => 'B',
  3857. 3 => 'C'
  3858. ),
  3859. 'disabled' => array(1)
  3860. ));
  3861. $expected = array(
  3862. 'div' => array('class' => 'input radio'),
  3863. 'fieldset' => array(),
  3864. 'legend' => array(),
  3865. 'Field',
  3866. '/legend',
  3867. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3868. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3869. array('label' => array('for' => 'ModelField1')),
  3870. 'A',
  3871. '/label',
  3872. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3873. array('label' => array('for' => 'ModelField2')),
  3874. 'B',
  3875. '/label',
  3876. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3877. array('label' => array('for' => 'ModelField3')),
  3878. 'C',
  3879. '/label',
  3880. '/fieldset',
  3881. '/div'
  3882. );
  3883. $this->assertTags($result, $expected);
  3884. }
  3885. /**
  3886. * test disabling the hidden input for radio buttons
  3887. *
  3888. * @return void
  3889. */
  3890. public function testRadioHiddenInputDisabling() {
  3891. $result = $this->Form->input('Model.1.field', array(
  3892. 'type' => 'radio',
  3893. 'options' => array('option A'),
  3894. 'hiddenField' => false
  3895. )
  3896. );
  3897. $expected = array(
  3898. 'div' => array('class' => 'input radio'),
  3899. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3900. 'label' => array('for' => 'Model1Field0'),
  3901. 'option A',
  3902. '/label',
  3903. '/div'
  3904. );
  3905. $this->assertTags($result, $expected);
  3906. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3907. $expected = array(
  3908. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3909. 'label' => array('for' => 'Model1Field0'),
  3910. 'option A',
  3911. '/label'
  3912. );
  3913. $this->assertTags($result, $expected);
  3914. }
  3915. /**
  3916. * test adding an empty option for radio buttons
  3917. *
  3918. * @return void
  3919. */
  3920. public function testRadioAddEmptyOption() {
  3921. $result = $this->Form->input('Model.1.field', array(
  3922. 'type' => 'radio',
  3923. 'options' => array('option A'),
  3924. 'empty' => true,
  3925. 'hiddenField' => false
  3926. ));
  3927. $expected = array(
  3928. 'div' => array('class' => 'input radio'),
  3929. 'fieldset' => array(),
  3930. 'legend' => array(),
  3931. 'Field',
  3932. '/legend',
  3933. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3934. array('label' => array('for' => 'Model1Field')),
  3935. __('empty'),
  3936. '/label',
  3937. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3938. array('label' => array('for' => 'Model1Field0')),
  3939. 'option A',
  3940. '/label',
  3941. '/fieldset',
  3942. '/div'
  3943. );
  3944. $this->assertTags($result, $expected);
  3945. $result = $this->Form->input('Model.1.field', array(
  3946. 'type' => 'radio',
  3947. 'options' => array('option A'),
  3948. 'empty' => 'CustomEmptyLabel',
  3949. 'hiddenField' => false
  3950. ));
  3951. $expected = array(
  3952. 'div' => array('class' => 'input radio'),
  3953. 'fieldset' => array(),
  3954. 'legend' => array(),
  3955. 'Field',
  3956. '/legend',
  3957. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3958. array('label' => array('for' => 'Model1Field')),
  3959. 'CustomEmptyLabel',
  3960. '/label',
  3961. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3962. array('label' => array('for' => 'Model1Field0')),
  3963. 'option A',
  3964. '/label',
  3965. '/fieldset',
  3966. '/div'
  3967. );
  3968. $this->assertTags($result, $expected);
  3969. $result = $this->Form->input('Model.1.field', array(
  3970. 'type' => 'radio',
  3971. 'options' => array('option A'),
  3972. 'empty' => false,
  3973. 'hiddenField' => false
  3974. ));
  3975. $this->assertTextNotContains('"Model1Field"', $result);
  3976. }
  3977. /**
  3978. * Test that radio() accepts an array for label
  3979. *
  3980. * @return void
  3981. */
  3982. public function testRadioLabelArray() {
  3983. $result = $this->Form->input('Model.field', array(
  3984. 'type' => 'radio',
  3985. 'legend' => false,
  3986. 'label' => array(
  3987. 'class' => 'checkbox float-left',
  3988. ),
  3989. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3990. ));
  3991. $this->assertTextContains(
  3992. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3993. $result
  3994. );
  3995. }
  3996. /**
  3997. * Test that label id's match the input element id's when radio is called after create().
  3998. *
  3999. * @return void
  4000. */
  4001. public function testRadioWithCreate() {
  4002. $this->Form->create('Model');
  4003. $result = $this->Form->radio('recipient',
  4004. array('1' => '1', '2' => '2', '3' => '3'),
  4005. array('legend' => false, 'value' => '1')
  4006. );
  4007. $this->assertTextNotContains(
  4008. '<label for="ModelModelRecipient1">1</label>',
  4009. $result
  4010. );
  4011. $this->assertTextContains(
  4012. '<label for="ModelRecipient1">1</label>',
  4013. $result
  4014. );
  4015. }
  4016. /**
  4017. * testDomIdSuffix method
  4018. *
  4019. * @return void
  4020. */
  4021. public function testDomIdSuffix() {
  4022. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  4023. $this->assertEquals('1StringWith1DollarSigns', $result);
  4024. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  4025. $this->assertEquals('AbcXFooYBar', $result);
  4026. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
  4027. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  4028. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
  4029. $this->assertEquals('AbcX=FooY=Bar', $result);
  4030. }
  4031. /**
  4032. * testDomIdSuffixCollisionResolvement()
  4033. *
  4034. * @return void
  4035. */
  4036. public function testDomIdSuffixCollisionResolvement() {
  4037. $result = $this->Form->domIdSuffix('a>b');
  4038. $this->assertEquals('AB', $result);
  4039. $result = $this->Form->domIdSuffix('a<b');
  4040. $this->assertEquals('AB1', $result);
  4041. $result = $this->Form->domIdSuffix('a\'b');
  4042. $this->assertEquals('AB2', $result);
  4043. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  4044. $this->assertEquals('1StringWith1Dollar', $result);
  4045. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  4046. $this->assertEquals('1StringWith1Dollar1', $result);
  4047. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  4048. $this->assertEquals('1StringWith1Dollar2', $result);
  4049. }
  4050. /**
  4051. * testSelect method
  4052. *
  4053. * Test select element generation.
  4054. *
  4055. * @return void
  4056. */
  4057. public function testSelect() {
  4058. $result = $this->Form->select('Model.field', array());
  4059. $expected = array(
  4060. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4061. array('option' => array('value' => '')),
  4062. '/option',
  4063. '/select'
  4064. );
  4065. $this->assertTags($result, $expected);
  4066. $this->Form->request->data = array('Model' => array('field' => 'value'));
  4067. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  4068. $expected = array(
  4069. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4070. array('option' => array('value' => '')),
  4071. '/option',
  4072. array('option' => array('value' => 'value', 'selected' => 'selected')),
  4073. 'good',
  4074. '/option',
  4075. array('option' => array('value' => 'other')),
  4076. 'bad',
  4077. '/option',
  4078. '/select'
  4079. );
  4080. $this->assertTags($result, $expected);
  4081. $this->Form->request->data = array();
  4082. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  4083. $expected = array(
  4084. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4085. array('option' => array('value' => '')),
  4086. '/option',
  4087. array('option' => array('value' => 'value')),
  4088. 'good',
  4089. '/option',
  4090. array('option' => array('value' => 'other')),
  4091. 'bad',
  4092. '/option',
  4093. '/select'
  4094. );
  4095. $this->assertTags($result, $expected);
  4096. $result = $this->Form->select(
  4097. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  4098. array('empty' => false)
  4099. );
  4100. $expected = array(
  4101. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4102. array('option' => array('value' => 'first')),
  4103. 'first &quot;html&quot; &lt;chars&gt;',
  4104. '/option',
  4105. array('option' => array('value' => 'second')),
  4106. 'value',
  4107. '/option',
  4108. '/select'
  4109. );
  4110. $this->assertTags($result, $expected);
  4111. $result = $this->Form->select(
  4112. 'Model.field',
  4113. array('first' => 'first "html" <chars>', 'second' => 'value'),
  4114. array('escape' => false, 'empty' => false)
  4115. );
  4116. $expected = array(
  4117. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4118. array('option' => array('value' => 'first')),
  4119. 'first "html" <chars>',
  4120. '/option',
  4121. array('option' => array('value' => 'second')),
  4122. 'value',
  4123. '/option',
  4124. '/select'
  4125. );
  4126. $this->assertTags($result, $expected);
  4127. $options = array(
  4128. array('value' => 'first', 'name' => 'First'),
  4129. array('value' => 'first', 'name' => 'Another First'),
  4130. );
  4131. $result = $this->Form->select(
  4132. 'Model.field',
  4133. $options,
  4134. array('escape' => false, 'empty' => false)
  4135. );
  4136. $expected = array(
  4137. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4138. array('option' => array('value' => 'first')),
  4139. 'First',
  4140. '/option',
  4141. array('option' => array('value' => 'first')),
  4142. 'Another First',
  4143. '/option',
  4144. '/select'
  4145. );
  4146. $this->assertTags($result, $expected);
  4147. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  4148. $result = $this->Form->select(
  4149. 'Model.contact_id',
  4150. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4151. array('escape' => false, 'empty' => 'pick something')
  4152. );
  4153. $expected = array(
  4154. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4155. array('option' => array('value' => '')), 'pick something', '/option',
  4156. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4157. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4158. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4159. '/select'
  4160. );
  4161. $this->assertTags($result, $expected);
  4162. $this->Form->request->data['Model']['field'] = 0;
  4163. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4164. $expected = array(
  4165. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4166. array('option' => array('value' => '')), '/option',
  4167. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4168. array('option' => array('value' => '1')), 'Yes', '/option',
  4169. '/select'
  4170. );
  4171. $this->assertTags($result, $expected);
  4172. $this->Form->request->data['Model']['field'] = 50;
  4173. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4174. $expected = array(
  4175. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4176. array('option' => array('value' => '')), '/option',
  4177. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4178. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4179. '/select'
  4180. );
  4181. $this->assertTags($result, $expected);
  4182. $result = $this->Form->select('Contact.required_one', array('option A'));
  4183. $expected = array(
  4184. 'select' => array(
  4185. 'name' => 'data[Contact][required_one]',
  4186. 'id' => 'ContactRequiredOne',
  4187. 'required' => 'required'
  4188. ),
  4189. array('option' => array('value' => '')), '/option',
  4190. array('option' => array('value' => '0')), 'option A', '/option',
  4191. '/select'
  4192. );
  4193. $this->assertTags($result, $expected);
  4194. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4195. $expected = array(
  4196. 'select' => array(
  4197. 'name' => 'data[Contact][required_one]',
  4198. 'id' => 'ContactRequiredOne',
  4199. 'disabled' => 'disabled'
  4200. ),
  4201. array('option' => array('value' => '')), '/option',
  4202. array('option' => array('value' => '0')), 'option A', '/option',
  4203. '/select'
  4204. );
  4205. $this->assertTags($result, $expected);
  4206. }
  4207. /**
  4208. * test that select() with optiongroups listens to the escape param.
  4209. *
  4210. * @return void
  4211. */
  4212. public function testSelectOptionGroupEscaping() {
  4213. $options = array(
  4214. '>< Key' => array(
  4215. 1 => 'One',
  4216. 2 => 'Two'
  4217. )
  4218. );
  4219. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4220. $expected = array(
  4221. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4222. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4223. array('option' => array('value' => '1')), 'One', '/option',
  4224. array('option' => array('value' => '2')), 'Two', '/option',
  4225. '/optgroup',
  4226. '/select'
  4227. );
  4228. $this->assertTags($result, $expected);
  4229. $options = array(
  4230. '>< Key' => array(
  4231. 1 => 'One',
  4232. 2 => 'Two'
  4233. )
  4234. );
  4235. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4236. $expected = array(
  4237. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4238. 'optgroup' => array('label' => '>< Key'),
  4239. array('option' => array('value' => '1')), 'One', '/option',
  4240. array('option' => array('value' => '2')), 'Two', '/option',
  4241. '/optgroup',
  4242. '/select'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. }
  4246. /**
  4247. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4248. *
  4249. * @return void
  4250. */
  4251. public function testSelectWithNullAttributes() {
  4252. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4253. $expected = array(
  4254. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4255. array('option' => array('value' => '0')),
  4256. 'first',
  4257. '/option',
  4258. array('option' => array('value' => '1')),
  4259. 'second',
  4260. '/option',
  4261. '/select'
  4262. );
  4263. $this->assertTags($result, $expected);
  4264. }
  4265. /**
  4266. * testNestedSelect method
  4267. *
  4268. * test select element generation with optgroups
  4269. *
  4270. * @return void
  4271. */
  4272. public function testNestedSelect() {
  4273. $result = $this->Form->select(
  4274. 'Model.field',
  4275. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4276. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4277. )), array('empty' => false)
  4278. );
  4279. $expected = array(
  4280. 'select' => array('name' => 'data[Model][field]',
  4281. 'id' => 'ModelField'),
  4282. array('option' => array('value' => 1)),
  4283. 'One',
  4284. '/option',
  4285. array('option' => array('value' => 2)),
  4286. 'Two',
  4287. '/option',
  4288. array('optgroup' => array('label' => 'Three')),
  4289. array('option' => array('value' => 4)),
  4290. 'Four',
  4291. '/option',
  4292. array('option' => array('value' => 5)),
  4293. 'Five',
  4294. '/option',
  4295. '/optgroup',
  4296. '/select'
  4297. );
  4298. $this->assertTags($result, $expected);
  4299. $result = $this->Form->select(
  4300. 'Model.field',
  4301. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4302. array('showParents' => true, 'empty' => false)
  4303. );
  4304. $expected = array(
  4305. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4306. array('option' => array('value' => 1)),
  4307. 'One',
  4308. '/option',
  4309. array('option' => array('value' => 2)),
  4310. 'Two',
  4311. '/option',
  4312. array('optgroup' => array('label' => 'Three')),
  4313. array('option' => array('value' => 3)),
  4314. 'Three',
  4315. '/option',
  4316. array('option' => array('value' => 4)),
  4317. 'Four',
  4318. '/option',
  4319. '/optgroup',
  4320. '/select'
  4321. );
  4322. $this->assertTags($result, $expected);
  4323. }
  4324. /**
  4325. * testSelectMultiple method
  4326. *
  4327. * test generation of multiple select elements
  4328. *
  4329. * @return void
  4330. */
  4331. public function testSelectMultiple() {
  4332. $options = array('first', 'second', 'third');
  4333. $result = $this->Form->select(
  4334. 'Model.multi_field', $options, array('multiple' => true)
  4335. );
  4336. $expected = array(
  4337. 'input' => array(
  4338. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4339. ),
  4340. 'select' => array(
  4341. 'name' => 'data[Model][multi_field][]',
  4342. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4343. ),
  4344. array('option' => array('value' => '0')),
  4345. 'first',
  4346. '/option',
  4347. array('option' => array('value' => '1')),
  4348. 'second',
  4349. '/option',
  4350. array('option' => array('value' => '2')),
  4351. 'third',
  4352. '/option',
  4353. '/select'
  4354. );
  4355. $this->assertTags($result, $expected);
  4356. $result = $this->Form->select(
  4357. 'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
  4358. );
  4359. $expected = array(
  4360. 'input' => array(
  4361. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_',
  4362. 'form' => 'my-form',
  4363. ),
  4364. 'select' => array(
  4365. 'name' => 'data[Model][multi_field][]',
  4366. 'id' => 'ModelMultiField',
  4367. 'multiple' => 'multiple',
  4368. 'form' => 'my-form',
  4369. ),
  4370. array('option' => array('value' => '0')),
  4371. 'first',
  4372. '/option',
  4373. array('option' => array('value' => '1')),
  4374. 'second',
  4375. '/option',
  4376. array('option' => array('value' => '2')),
  4377. 'third',
  4378. '/option',
  4379. '/select'
  4380. );
  4381. $this->assertTags($result, $expected);
  4382. $result = $this->Form->select(
  4383. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4384. );
  4385. $expected = array(
  4386. 'input' => array(
  4387. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4388. ),
  4389. 'select' => array(
  4390. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4391. 'multiple' => 'multiple'
  4392. ),
  4393. array('option' => array('value' => '0', 'selected' => 'selected')),
  4394. 'first',
  4395. '/option',
  4396. array('option' => array('value' => '1', 'selected' => 'selected')),
  4397. 'second',
  4398. '/option',
  4399. array('option' => array('value' => '2')),
  4400. 'third',
  4401. '/option',
  4402. '/select'
  4403. );
  4404. $this->assertTags($result, $expected);
  4405. $result = $this->Form->select(
  4406. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4407. );
  4408. $expected = array(
  4409. 'select' => array(
  4410. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4411. ),
  4412. array('option' => array('value' => '0', 'selected' => 'selected')),
  4413. 'first',
  4414. '/option',
  4415. array('option' => array('value' => '1', 'selected' => 'selected')),
  4416. 'second',
  4417. '/option',
  4418. array('option' => array('value' => '2')),
  4419. 'third',
  4420. '/option',
  4421. '/select'
  4422. );
  4423. $this->assertTags($result, $expected);
  4424. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4425. $selected = array('2', '3x');
  4426. $result = $this->Form->select(
  4427. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4428. );
  4429. $expected = array(
  4430. 'input' => array(
  4431. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4432. ),
  4433. 'select' => array(
  4434. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4435. ),
  4436. array('option' => array('value' => '1')),
  4437. 'One',
  4438. '/option',
  4439. array('option' => array('value' => '2', 'selected' => 'selected')),
  4440. 'Two',
  4441. '/option',
  4442. array('option' => array('value' => '3')),
  4443. 'Three',
  4444. '/option',
  4445. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4446. 'Stringy',
  4447. '/option',
  4448. '/select'
  4449. );
  4450. $this->assertTags($result, $expected);
  4451. $result = $this->Form->select('Contact.required_one', array(
  4452. '1' => 'option A',
  4453. '2' => 'option B'
  4454. ), array('multiple' => true));
  4455. $expected = array(
  4456. 'input' => array(
  4457. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4458. ),
  4459. 'select' => array(
  4460. 'name' => 'data[Contact][required_one][]',
  4461. 'id' => 'ContactRequiredOne',
  4462. 'required' => 'required',
  4463. 'multiple' => 'multiple'
  4464. ),
  4465. array('option' => array('value' => '1')), 'option A', '/option',
  4466. array('option' => array('value' => '2')), 'option B', '/option',
  4467. '/select'
  4468. );
  4469. $this->assertTags($result, $expected);
  4470. $result = $this->Form->select(
  4471. 'Model.multi_field',
  4472. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4473. array('multiple' => true)
  4474. );
  4475. $expected = array(
  4476. 'input' => array(
  4477. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4478. 'id' => 'ModelMultiField_'
  4479. ),
  4480. array('select' => array('name' => 'data[Model][multi_field][]',
  4481. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4482. )),
  4483. array('option' => array('value' => 'a&gt;b')),
  4484. 'first',
  4485. '/option',
  4486. array('option' => array('value' => 'a&lt;b')),
  4487. 'second',
  4488. '/option',
  4489. array('option' => array(
  4490. 'value' => 'a&quot;b'
  4491. )),
  4492. 'third',
  4493. '/option',
  4494. '/select'
  4495. );
  4496. $this->assertTags($result, $expected);
  4497. }
  4498. /**
  4499. * Test generating multiple select with disabled elements.
  4500. *
  4501. * @return void
  4502. */
  4503. public function testSelectMultipleWithDisabledElements() {
  4504. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4505. $disabled = array(2, 3);
  4506. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4507. $expected = array(
  4508. 'input' => array(
  4509. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4510. ),
  4511. 'select' => array(
  4512. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4513. ),
  4514. array('option' => array('value' => '1')),
  4515. 'One',
  4516. '/option',
  4517. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4518. 'Two',
  4519. '/option',
  4520. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4521. 'Three',
  4522. '/option',
  4523. array('option' => array('value' => '3x')),
  4524. 'Stringy',
  4525. '/option',
  4526. '/select'
  4527. );
  4528. $this->assertTags($result, $expected);
  4529. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4530. $disabled = array('2', '3x');
  4531. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4532. $expected = array(
  4533. array('div' => array('class' => 'input select')),
  4534. array('label' => array('for' => 'ContactMultiple')),
  4535. 'Multiple',
  4536. '/label',
  4537. 'input' => array(
  4538. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4539. ),
  4540. 'select' => array(
  4541. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4542. ),
  4543. array('option' => array('value' => '1')),
  4544. 'One',
  4545. '/option',
  4546. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4547. 'Two',
  4548. '/option',
  4549. array('option' => array('value' => '3')),
  4550. 'Three',
  4551. '/option',
  4552. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4553. 'Stringy',
  4554. '/option',
  4555. '/select',
  4556. '/div'
  4557. );
  4558. $this->assertTags($result, $expected);
  4559. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4560. $disabled = true;
  4561. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4562. $expected = array(
  4563. array('div' => array('class' => 'input select')),
  4564. array('label' => array('for' => 'ContactMultiple')),
  4565. 'Multiple',
  4566. '/label',
  4567. 'input' => array(
  4568. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4569. ),
  4570. 'select' => array(
  4571. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4572. ),
  4573. array('option' => array('value' => '1')),
  4574. 'One',
  4575. '/option',
  4576. array('option' => array('value' => '2')),
  4577. 'Two',
  4578. '/option',
  4579. array('option' => array('value' => '3')),
  4580. 'Three',
  4581. '/option',
  4582. array('option' => array('value' => '3x')),
  4583. 'Stringy',
  4584. '/option',
  4585. '/select',
  4586. '/div'
  4587. );
  4588. $this->assertTags($result, $expected);
  4589. }
  4590. /**
  4591. * Test generating select with disabled elements.
  4592. *
  4593. * @return void
  4594. */
  4595. public function testSelectWithDisabledElements() {
  4596. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4597. $disabled = array(2, 3);
  4598. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4599. $expected = array(
  4600. 'select' => array(
  4601. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4602. ),
  4603. array('option' => array('value' => '')),
  4604. '/option',
  4605. array('option' => array('value' => '1')),
  4606. 'One',
  4607. '/option',
  4608. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4609. 'Two',
  4610. '/option',
  4611. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4612. 'Three',
  4613. '/option',
  4614. array('option' => array('value' => '3x')),
  4615. 'Stringy',
  4616. '/option',
  4617. '/select'
  4618. );
  4619. $this->assertTags($result, $expected);
  4620. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4621. $disabled = array('2', '3x');
  4622. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4623. $expected = array(
  4624. array('div' => array('class' => 'input select')),
  4625. array('label' => array('for' => 'ModelField')),
  4626. 'Field',
  4627. '/label',
  4628. 'select' => array(
  4629. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4630. ),
  4631. array('option' => array('value' => '1')),
  4632. 'One',
  4633. '/option',
  4634. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4635. 'Two',
  4636. '/option',
  4637. array('option' => array('value' => '3')),
  4638. 'Three',
  4639. '/option',
  4640. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4641. 'Stringy',
  4642. '/option',
  4643. '/select',
  4644. '/div'
  4645. );
  4646. $this->assertTags($result, $expected);
  4647. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4648. $disabled = true;
  4649. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4650. $expected = array(
  4651. array('div' => array('class' => 'input select')),
  4652. array('label' => array('for' => 'ModelField')),
  4653. 'Field',
  4654. '/label',
  4655. 'select' => array(
  4656. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4657. ),
  4658. array('option' => array('value' => '1')),
  4659. 'One',
  4660. '/option',
  4661. array('option' => array('value' => '2')),
  4662. 'Two',
  4663. '/option',
  4664. array('option' => array('value' => '3')),
  4665. 'Three',
  4666. '/option',
  4667. array('option' => array('value' => '3x')),
  4668. 'Stringy',
  4669. '/option',
  4670. '/select',
  4671. '/div'
  4672. );
  4673. $this->assertTags($result, $expected);
  4674. }
  4675. /**
  4676. * test generation of habtm select boxes.
  4677. *
  4678. * @return void
  4679. */
  4680. public function testHabtmSelectBox() {
  4681. $this->View->viewVars['contactTags'] = array(
  4682. 1 => 'blue',
  4683. 2 => 'red',
  4684. 3 => 'green'
  4685. );
  4686. $this->Form->request->data = array(
  4687. 'Contact' => array(),
  4688. 'ContactTag' => array(
  4689. array(
  4690. 'id' => '1',
  4691. 'name' => 'blue'
  4692. ),
  4693. array(
  4694. 'id' => 3,
  4695. 'name' => 'green'
  4696. )
  4697. )
  4698. );
  4699. $this->Form->create('Contact');
  4700. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4701. $expected = array(
  4702. 'input' => array(
  4703. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4704. ),
  4705. 'select' => array(
  4706. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4707. 'multiple' => 'multiple'
  4708. ),
  4709. array('option' => array('value' => '1', 'selected' => 'selected')),
  4710. 'blue',
  4711. '/option',
  4712. array('option' => array('value' => '2')),
  4713. 'red',
  4714. '/option',
  4715. array('option' => array('value' => '3', 'selected' => 'selected')),
  4716. 'green',
  4717. '/option',
  4718. '/select'
  4719. );
  4720. $this->assertTags($result, $expected);
  4721. // make sure only 50 is selected, and not 50f5c0cf
  4722. $this->View->viewVars['contactTags'] = array(
  4723. '1' => 'blue',
  4724. '50f5c0cf' => 'red',
  4725. '50' => 'green'
  4726. );
  4727. $this->Form->request->data = array(
  4728. 'Contact' => array(),
  4729. 'ContactTag' => array(
  4730. array(
  4731. 'id' => 1,
  4732. 'name' => 'blue'
  4733. ),
  4734. array(
  4735. 'id' => 50,
  4736. 'name' => 'green'
  4737. )
  4738. )
  4739. );
  4740. $this->Form->create('Contact');
  4741. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4742. $expected = array(
  4743. 'input' => array(
  4744. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4745. ),
  4746. 'select' => array(
  4747. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4748. 'multiple' => 'multiple'
  4749. ),
  4750. array('option' => array('value' => '1', 'selected' => 'selected')),
  4751. 'blue',
  4752. '/option',
  4753. array('option' => array('value' => '50f5c0cf')),
  4754. 'red',
  4755. '/option',
  4756. array('option' => array('value' => '50', 'selected' => 'selected')),
  4757. 'green',
  4758. '/option',
  4759. '/select'
  4760. );
  4761. $this->assertTags($result, $expected);
  4762. }
  4763. /**
  4764. * test generation of multi select elements in checkbox format
  4765. *
  4766. * @return void
  4767. */
  4768. public function testSelectMultipleCheckboxes() {
  4769. $result = $this->Form->select(
  4770. 'Model.multi_field',
  4771. array('first', 'second', 'third'),
  4772. array('multiple' => 'checkbox')
  4773. );
  4774. $expected = array(
  4775. 'input' => array(
  4776. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4777. ),
  4778. array('div' => array('class' => 'checkbox')),
  4779. array('input' => array(
  4780. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4781. 'value' => '0', 'id' => 'ModelMultiField0'
  4782. )),
  4783. array('label' => array('for' => 'ModelMultiField0')),
  4784. 'first',
  4785. '/label',
  4786. '/div',
  4787. array('div' => array('class' => 'checkbox')),
  4788. array('input' => array(
  4789. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4790. 'value' => '1', 'id' => 'ModelMultiField1'
  4791. )),
  4792. array('label' => array('for' => 'ModelMultiField1')),
  4793. 'second',
  4794. '/label',
  4795. '/div',
  4796. array('div' => array('class' => 'checkbox')),
  4797. array('input' => array(
  4798. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4799. 'value' => '2', 'id' => 'ModelMultiField2'
  4800. )),
  4801. array('label' => array('for' => 'ModelMultiField2')),
  4802. 'third',
  4803. '/label',
  4804. '/div'
  4805. );
  4806. $this->assertTags($result, $expected);
  4807. $result = $this->Form->select(
  4808. 'Model.multi_field',
  4809. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4810. array('multiple' => 'checkbox')
  4811. );
  4812. $expected = array(
  4813. 'input' => array(
  4814. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4815. ),
  4816. array('div' => array('class' => 'checkbox')),
  4817. array('input' => array(
  4818. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4819. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4820. )),
  4821. array('label' => array('for' => 'ModelMultiFieldA')),
  4822. 'first',
  4823. '/label',
  4824. '/div',
  4825. array('div' => array('class' => 'checkbox')),
  4826. array('input' => array(
  4827. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4828. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4829. )),
  4830. array('label' => array('for' => 'ModelMultiFieldB')),
  4831. 'second',
  4832. '/label',
  4833. '/div',
  4834. array('div' => array('class' => 'checkbox')),
  4835. array('input' => array(
  4836. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4837. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4838. )),
  4839. array('label' => array('for' => 'ModelMultiFieldC')),
  4840. 'third',
  4841. '/label',
  4842. '/div'
  4843. );
  4844. $this->assertTags($result, $expected);
  4845. $result = $this->Form->select(
  4846. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4847. );
  4848. $expected = array(
  4849. 'input' => array(
  4850. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4851. ),
  4852. array('div' => array('class' => 'checkbox')),
  4853. array('input' => array(
  4854. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4855. 'value' => '1', 'id' => 'ModelMultiField1'
  4856. )),
  4857. array('label' => array('for' => 'ModelMultiField1')),
  4858. 'first',
  4859. '/label',
  4860. '/div'
  4861. );
  4862. $this->assertTags($result, $expected);
  4863. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4864. $result = $this->Form->select(
  4865. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4866. );
  4867. $expected = array(
  4868. 'input' => array(
  4869. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4870. ),
  4871. array('div' => array('class' => 'checkbox')),
  4872. array('input' => array(
  4873. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4874. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4875. )),
  4876. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4877. 'first',
  4878. '/label',
  4879. '/div',
  4880. array('div' => array('class' => 'checkbox')),
  4881. array('input' => array(
  4882. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4883. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4884. )),
  4885. array('label' => array('for' => 'ModelTagsArray')),
  4886. 'Array',
  4887. '/label',
  4888. '/div'
  4889. );
  4890. $this->assertTags($result, $expected);
  4891. $result = $this->Form->select(
  4892. 'Model.multi_field',
  4893. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4894. array('multiple' => 'checkbox')
  4895. );
  4896. $expected = array(
  4897. 'input' => array(
  4898. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4899. ),
  4900. array('div' => array('class' => 'checkbox')),
  4901. array('input' => array(
  4902. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4903. 'value' => 'a+', 'id' => 'ModelMultiFieldA2'
  4904. )),
  4905. array('label' => array('for' => 'ModelMultiFieldA2')),
  4906. 'first',
  4907. '/label',
  4908. '/div',
  4909. array('div' => array('class' => 'checkbox')),
  4910. array('input' => array(
  4911. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4912. 'value' => 'a++', 'id' => 'ModelMultiFieldA1'
  4913. )),
  4914. array('label' => array('for' => 'ModelMultiFieldA1')),
  4915. 'second',
  4916. '/label',
  4917. '/div',
  4918. array('div' => array('class' => 'checkbox')),
  4919. array('input' => array(
  4920. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4921. 'value' => 'a+++', 'id' => 'ModelMultiFieldA'
  4922. )),
  4923. array('label' => array('for' => 'ModelMultiFieldA')),
  4924. 'third',
  4925. '/label',
  4926. '/div'
  4927. );
  4928. $this->assertTags($result, $expected);
  4929. $result = $this->Form->select(
  4930. 'Model.multi_field',
  4931. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4932. array('multiple' => 'checkbox')
  4933. );
  4934. $expected = array(
  4935. 'input' => array(
  4936. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4937. ),
  4938. array('div' => array('class' => 'checkbox')),
  4939. array('input' => array(
  4940. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4941. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4942. )),
  4943. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4944. 'first',
  4945. '/label',
  4946. '/div',
  4947. array('div' => array('class' => 'checkbox')),
  4948. array('input' => array(
  4949. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4950. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4951. )),
  4952. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4953. 'second',
  4954. '/label',
  4955. '/div',
  4956. array('div' => array('class' => 'checkbox')),
  4957. array('input' => array(
  4958. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4959. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4960. )),
  4961. array('label' => array('for' => 'ModelMultiFieldAB')),
  4962. 'third',
  4963. '/label',
  4964. '/div'
  4965. );
  4966. $this->assertTags($result, $expected);
  4967. }
  4968. /**
  4969. * test multiple checkboxes with div styles.
  4970. *
  4971. * @return void
  4972. */
  4973. public function testSelectMultipleCheckboxDiv() {
  4974. $result = $this->Form->select(
  4975. 'Model.tags',
  4976. array('first', 'second'),
  4977. array('multiple' => 'checkbox', 'class' => 'my-class')
  4978. );
  4979. $expected = array(
  4980. 'input' => array(
  4981. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4982. ),
  4983. array('div' => array('class' => 'my-class')),
  4984. array('input' => array(
  4985. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4986. 'value' => '0', 'id' => 'ModelTags0'
  4987. )),
  4988. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4989. '/div',
  4990. array('div' => array('class' => 'my-class')),
  4991. array('input' => array(
  4992. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4993. 'value' => '1', 'id' => 'ModelTags1'
  4994. )),
  4995. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4996. '/div'
  4997. );
  4998. $this->assertTags($result, $expected);
  4999. $result = $this->Form->input('Model.tags', array(
  5000. 'options' => array('first', 'second'),
  5001. 'multiple' => 'checkbox',
  5002. 'class' => 'my-class',
  5003. 'div' => false,
  5004. 'label' => false
  5005. ));
  5006. $this->assertTags($result, $expected);
  5007. $Contact = ClassRegistry::getObject('Contact');
  5008. $Contact->validationErrors['tags'] = 'Select atleast one option';
  5009. $result = $this->Form->input('Contact.tags', array(
  5010. 'options' => array('one'),
  5011. 'multiple' => 'checkbox',
  5012. 'label' => false,
  5013. 'div' => false
  5014. ));
  5015. $expected = array(
  5016. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  5017. array('div' => array('class' => 'checkbox form-error')),
  5018. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  5019. array('label' => array('for' => 'ContactTags0')),
  5020. 'one',
  5021. '/label',
  5022. '/div'
  5023. );
  5024. $this->assertTags($result, $expected);
  5025. $result = $this->Form->input('Contact.tags', array(
  5026. 'options' => array('one'),
  5027. 'multiple' => 'checkbox',
  5028. 'class' => 'mycheckbox',
  5029. 'label' => false,
  5030. 'div' => false
  5031. ));
  5032. $expected = array(
  5033. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  5034. array('div' => array('class' => 'mycheckbox form-error')),
  5035. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  5036. array('label' => array('for' => 'ContactTags0')),
  5037. 'one',
  5038. '/label',
  5039. '/div'
  5040. );
  5041. $this->assertTags($result, $expected);
  5042. }
  5043. /**
  5044. * Checks the security hash array generated for multiple-input checkbox elements
  5045. *
  5046. * @return void
  5047. */
  5048. public function testSelectMultipleCheckboxSecurity() {
  5049. $this->Form->request['_Token'] = array('key' => 'testKey');
  5050. $this->assertEquals(array(), $this->Form->fields);
  5051. $result = $this->Form->select(
  5052. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  5053. array('multiple' => 'checkbox')
  5054. );
  5055. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  5056. $result = $this->Form->secure($this->Form->fields);
  5057. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  5058. $this->assertRegExp('/"' . $key . '"/', $result);
  5059. }
  5060. /**
  5061. * Multiple select elements should always be secured as they always participate
  5062. * in the POST data.
  5063. *
  5064. * @return void
  5065. */
  5066. public function testSelectMultipleSecureWithNoOptions() {
  5067. $this->Form->request['_Token'] = array('key' => 'testkey');
  5068. $this->assertEquals(array(), $this->Form->fields);
  5069. $this->Form->select(
  5070. 'Model.select',
  5071. array(),
  5072. array('multiple' => true)
  5073. );
  5074. $this->assertEquals(array('Model.select'), $this->Form->fields);
  5075. }
  5076. /**
  5077. * When a select box has no options it should not be added to the fields list
  5078. * as it always fail post validation.
  5079. *
  5080. * @return void
  5081. */
  5082. public function testSelectNoSecureWithNoOptions() {
  5083. $this->Form->request['_Token'] = array('key' => 'testkey');
  5084. $this->assertEquals(array(), $this->Form->fields);
  5085. $this->Form->select(
  5086. 'Model.select',
  5087. array()
  5088. );
  5089. $this->assertEquals(array(), $this->Form->fields);
  5090. $this->Form->select(
  5091. 'Model.select',
  5092. array(),
  5093. array('empty' => true)
  5094. );
  5095. $this->assertEquals(array('Model.select'), $this->Form->fields);
  5096. }
  5097. /**
  5098. * testInputMultipleCheckboxes method
  5099. *
  5100. * test input() resulting in multi select elements being generated.
  5101. *
  5102. * @return void
  5103. */
  5104. public function testInputMultipleCheckboxes() {
  5105. $result = $this->Form->input('Model.multi_field', array(
  5106. 'options' => array('first', 'second', 'third'),
  5107. 'multiple' => 'checkbox'
  5108. ));
  5109. $expected = array(
  5110. array('div' => array('class' => 'input select')),
  5111. array('label' => array('for' => 'ModelMultiField')),
  5112. 'Multi Field',
  5113. '/label',
  5114. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5115. array('div' => array('class' => 'checkbox')),
  5116. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5117. array('label' => array('for' => 'ModelMultiField0')),
  5118. 'first',
  5119. '/label',
  5120. '/div',
  5121. array('div' => array('class' => 'checkbox')),
  5122. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5123. array('label' => array('for' => 'ModelMultiField1')),
  5124. 'second',
  5125. '/label',
  5126. '/div',
  5127. array('div' => array('class' => 'checkbox')),
  5128. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5129. array('label' => array('for' => 'ModelMultiField2')),
  5130. 'third',
  5131. '/label',
  5132. '/div',
  5133. '/div'
  5134. );
  5135. $this->assertTags($result, $expected);
  5136. $result = $this->Form->input('Model.multi_field', array(
  5137. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  5138. 'multiple' => 'checkbox'
  5139. ));
  5140. $expected = array(
  5141. array('div' => array('class' => 'input select')),
  5142. array('label' => array('for' => 'ModelMultiField')),
  5143. 'Multi Field',
  5144. '/label',
  5145. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5146. array('div' => array('class' => 'checkbox')),
  5147. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  5148. array('label' => array('for' => 'ModelMultiFieldA')),
  5149. 'first',
  5150. '/label',
  5151. '/div',
  5152. array('div' => array('class' => 'checkbox')),
  5153. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5154. array('label' => array('for' => 'ModelMultiFieldB')),
  5155. 'second',
  5156. '/label',
  5157. '/div',
  5158. array('div' => array('class' => 'checkbox')),
  5159. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5160. array('label' => array('for' => 'ModelMultiFieldC')),
  5161. 'third',
  5162. '/label',
  5163. '/div',
  5164. '/div'
  5165. );
  5166. $this->assertTags($result, $expected);
  5167. $result = $this->Form->input('Model.multi_field', array(
  5168. 'options' => array('1' => 'first'),
  5169. 'multiple' => 'checkbox',
  5170. 'label' => false,
  5171. 'div' => false
  5172. ));
  5173. $expected = array(
  5174. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5175. array('div' => array('class' => 'checkbox')),
  5176. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5177. array('label' => array('for' => 'ModelMultiField1')),
  5178. 'first',
  5179. '/label',
  5180. '/div'
  5181. );
  5182. $this->assertTags($result, $expected);
  5183. $result = $this->Form->input('Model.multi_field', array(
  5184. 'options' => array('2' => 'second'),
  5185. 'multiple' => 'checkbox',
  5186. 'label' => false,
  5187. 'div' => false
  5188. ));
  5189. $expected = array(
  5190. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5191. array('div' => array('class' => 'checkbox')),
  5192. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5193. array('label' => array('for' => 'ModelMultiField2')),
  5194. 'second',
  5195. '/label',
  5196. '/div'
  5197. );
  5198. $this->assertTags($result, $expected);
  5199. }
  5200. /**
  5201. * testSelectHiddenFieldOmission method
  5202. *
  5203. * test that select() with 'hiddenField' => false omits the hidden field
  5204. *
  5205. * @return void
  5206. */
  5207. public function testSelectHiddenFieldOmission() {
  5208. $result = $this->Form->select('Model.multi_field',
  5209. array('first', 'second'),
  5210. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5211. );
  5212. $expected = array(
  5213. array('div' => array('class' => 'checkbox')),
  5214. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5215. array('label' => array('for' => 'ModelMultiField0')),
  5216. 'first',
  5217. '/label',
  5218. '/div',
  5219. array('div' => array('class' => 'checkbox')),
  5220. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5221. array('label' => array('for' => 'ModelMultiField1')),
  5222. 'second',
  5223. '/label',
  5224. '/div'
  5225. );
  5226. $this->assertTags($result, $expected);
  5227. $result = $this->Form->input('Model.multi_field', array(
  5228. 'options' => array('first', 'second'),
  5229. 'multiple' => 'checkbox',
  5230. 'hiddenField' => false
  5231. ));
  5232. $expected = array(
  5233. array('div' => array('class' => 'input select')),
  5234. array('label' => array('for' => 'ModelMultiField')),
  5235. 'Multi Field',
  5236. '/label',
  5237. array('div' => array('class' => 'checkbox')),
  5238. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5239. array('label' => array('for' => 'ModelMultiField0')),
  5240. 'first',
  5241. '/label',
  5242. '/div',
  5243. array('div' => array('class' => 'checkbox')),
  5244. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5245. array('label' => array('for' => 'ModelMultiField1')),
  5246. 'second',
  5247. '/label',
  5248. '/div',
  5249. '/div'
  5250. );
  5251. $this->assertTags($result, $expected);
  5252. }
  5253. /**
  5254. * test that select() with multiple = checkbox works with overriding name attribute.
  5255. *
  5256. * @return void
  5257. */
  5258. public function testSelectCheckboxMultipleOverrideName() {
  5259. $result = $this->Form->input('category', array(
  5260. 'type' => 'select',
  5261. 'multiple' => 'checkbox',
  5262. 'name' => 'data[fish]',
  5263. 'options' => array('1', '2'),
  5264. 'div' => false,
  5265. 'label' => false,
  5266. ));
  5267. $expected = array(
  5268. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5269. array('div' => array('class' => 'checkbox')),
  5270. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5271. array('label' => array('for' => 'Category0')), '1', '/label',
  5272. '/div',
  5273. array('div' => array('class' => 'checkbox')),
  5274. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5275. array('label' => array('for' => 'Category1')), '2', '/label',
  5276. '/div'
  5277. );
  5278. $this->assertTags($result, $expected);
  5279. }
  5280. /**
  5281. * Test that 'id' overrides all the checkbox id's as well.
  5282. *
  5283. * @return void
  5284. */
  5285. public function testSelectCheckboxMultipleId() {
  5286. $result = $this->Form->select(
  5287. 'Model.multi_field',
  5288. array('first', 'second', 'third'),
  5289. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5290. );
  5291. $expected = array(
  5292. 'input' => array(
  5293. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5294. ),
  5295. array('div' => array('class' => 'checkbox')),
  5296. array('input' => array(
  5297. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5298. 'value' => '0', 'id' => 'CustomId0'
  5299. )),
  5300. array('label' => array('for' => 'CustomId0')),
  5301. 'first',
  5302. '/label',
  5303. '/div',
  5304. array('div' => array('class' => 'checkbox')),
  5305. array('input' => array(
  5306. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5307. 'value' => '1', 'id' => 'CustomId1'
  5308. )),
  5309. array('label' => array('for' => 'CustomId1')),
  5310. 'second',
  5311. '/label',
  5312. '/div',
  5313. array('div' => array('class' => 'checkbox')),
  5314. array('input' => array(
  5315. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5316. 'value' => '2', 'id' => 'CustomId2'
  5317. )),
  5318. array('label' => array('for' => 'CustomId2')),
  5319. 'third',
  5320. '/label',
  5321. '/div'
  5322. );
  5323. $this->assertTags($result, $expected);
  5324. }
  5325. /**
  5326. * testCheckbox method
  5327. *
  5328. * Test generation of checkboxes
  5329. *
  5330. * @return void
  5331. */
  5332. public function testCheckbox() {
  5333. $result = $this->Form->checkbox('Model.field');
  5334. $expected = array(
  5335. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5336. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5337. );
  5338. $this->assertTags($result, $expected);
  5339. $result = $this->Form->checkbox('Model.field', array(
  5340. 'id' => 'theID',
  5341. 'value' => 'myvalue',
  5342. 'form' => 'my-form',
  5343. ));
  5344. $expected = array(
  5345. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'),
  5346. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
  5347. );
  5348. $this->assertTags($result, $expected);
  5349. $Contact = ClassRegistry::getObject('Contact');
  5350. $Contact->validationErrors['field'] = 1;
  5351. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5352. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5353. $expected = array(
  5354. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5355. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5356. );
  5357. $this->assertTags($result, $expected);
  5358. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5359. $expected = array(
  5360. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5361. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5362. );
  5363. $this->assertTags($result, $expected);
  5364. $this->Form->request->data['Contact']['field'] = '';
  5365. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5366. $expected = array(
  5367. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5368. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5369. );
  5370. $this->assertTags($result, $expected);
  5371. $Contact->validationErrors = array();
  5372. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5373. $expected = array(
  5374. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5375. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5376. );
  5377. $this->assertTags($result, $expected);
  5378. $this->Form->request->data['Contact']['published'] = 1;
  5379. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5380. $expected = array(
  5381. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5382. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5383. );
  5384. $this->assertTags($result, $expected);
  5385. $this->Form->request->data['Contact']['published'] = 0;
  5386. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5387. $expected = array(
  5388. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5389. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5390. );
  5391. $this->assertTags($result, $expected);
  5392. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5393. $expected = array(
  5394. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5395. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5396. );
  5397. $this->assertTags($result, $expected);
  5398. $result = $this->Form->checkbox('CustomField.1.value');
  5399. $expected = array(
  5400. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5401. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5402. );
  5403. $this->assertTags($result, $expected);
  5404. }
  5405. /**
  5406. * test checkbox() with a custom name attribute
  5407. *
  5408. * @return void
  5409. */
  5410. public function testCheckboxCustomNameAttribute() {
  5411. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5412. $expected = array(
  5413. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5414. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5415. );
  5416. $this->assertTags($result, $expected);
  5417. }
  5418. /**
  5419. * test the checked option for checkboxes.
  5420. *
  5421. * @return void
  5422. */
  5423. public function testCheckboxCheckedOption() {
  5424. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5425. $expected = array(
  5426. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5427. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5428. );
  5429. $this->assertTags($result, $expected);
  5430. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5431. $expected = array(
  5432. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5433. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5434. );
  5435. $this->assertTags($result, $expected);
  5436. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5437. $expected = array(
  5438. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5439. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5440. );
  5441. $this->assertTags($result, $expected);
  5442. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5443. $expected = array(
  5444. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5445. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5446. );
  5447. $this->assertTags($result, $expected);
  5448. $this->Form->request->data['Model']['field'] = 1;
  5449. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5450. $expected = array(
  5451. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5452. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5453. );
  5454. $this->assertTags($result, $expected);
  5455. }
  5456. /**
  5457. * Test that disabled attribute works on both the checkbox and hidden input.
  5458. *
  5459. * @return void
  5460. */
  5461. public function testCheckboxDisabling() {
  5462. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5463. $expected = array(
  5464. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5465. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5469. $expected = array(
  5470. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5471. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5472. );
  5473. $this->assertTags($result, $expected);
  5474. }
  5475. /**
  5476. * Test that the hidden input for checkboxes can be omitted or set to a
  5477. * specific value.
  5478. *
  5479. * @return void
  5480. */
  5481. public function testCheckboxHiddenField() {
  5482. $result = $this->Form->input('UserForm.something', array(
  5483. 'type' => 'checkbox',
  5484. 'hiddenField' => false
  5485. ));
  5486. $expected = array(
  5487. 'div' => array('class' => 'input checkbox'),
  5488. array('input' => array(
  5489. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5490. 'value' => '1', 'id' => 'UserFormSomething'
  5491. )),
  5492. 'label' => array('for' => 'UserFormSomething'),
  5493. 'Something',
  5494. '/label',
  5495. '/div'
  5496. );
  5497. $this->assertTags($result, $expected);
  5498. $result = $this->Form->input('UserForm.something', array(
  5499. 'type' => 'checkbox',
  5500. 'value' => 'Y',
  5501. 'hiddenField' => 'N',
  5502. ));
  5503. $expected = array(
  5504. 'div' => array('class' => 'input checkbox'),
  5505. array('input' => array(
  5506. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5507. 'value' => 'N', 'id' => 'UserFormSomething_'
  5508. )),
  5509. array('input' => array(
  5510. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5511. 'value' => 'Y', 'id' => 'UserFormSomething'
  5512. )),
  5513. 'label' => array('for' => 'UserFormSomething'),
  5514. 'Something',
  5515. '/label',
  5516. '/div'
  5517. );
  5518. $this->assertTags($result, $expected);
  5519. }
  5520. /**
  5521. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  5522. *
  5523. * @return void
  5524. */
  5525. public function testCheckboxZeroValue() {
  5526. $result = $this->Form->input('User.get_spam', array(
  5527. 'type' => 'checkbox',
  5528. 'value' => '0',
  5529. 'hiddenField' => '1',
  5530. ));
  5531. $expected = array(
  5532. 'div' => array('class' => 'input checkbox'),
  5533. array('input' => array(
  5534. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  5535. 'value' => '1', 'id' => 'UserGetSpam_'
  5536. )),
  5537. array('input' => array(
  5538. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  5539. 'value' => '0', 'id' => 'UserGetSpam'
  5540. )),
  5541. 'label' => array('for' => 'UserGetSpam'),
  5542. 'Get Spam',
  5543. '/label',
  5544. '/div'
  5545. );
  5546. $this->assertTags($result, $expected);
  5547. }
  5548. /**
  5549. * testDateTime method
  5550. *
  5551. * Test generation of date/time select elements
  5552. *
  5553. * @return void
  5554. */
  5555. public function testDateTime() {
  5556. extract($this->dateRegex);
  5557. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5558. $now = strtotime('now');
  5559. $expected = array(
  5560. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5561. $daysRegex,
  5562. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5563. date('j', $now),
  5564. '/option',
  5565. '*/select',
  5566. '-',
  5567. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5568. $monthsRegex,
  5569. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5570. date('F', $now),
  5571. '/option',
  5572. '*/select',
  5573. '-',
  5574. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5575. $yearsRegex,
  5576. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5577. date('Y', $now),
  5578. '/option',
  5579. '*/select',
  5580. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5581. $hoursRegex,
  5582. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5583. date('g', $now),
  5584. '/option',
  5585. '*/select',
  5586. ':',
  5587. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5588. $minutesRegex,
  5589. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5590. date('i', $now),
  5591. '/option',
  5592. '*/select',
  5593. ' ',
  5594. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5595. $meridianRegex,
  5596. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5597. date('a', $now),
  5598. '/option',
  5599. '*/select'
  5600. );
  5601. $this->assertTags($result, $expected);
  5602. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5603. $expected = array(
  5604. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5605. $daysRegex,
  5606. array('option' => array('value' => '')),
  5607. '/option',
  5608. '*/select',
  5609. '-',
  5610. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5611. $monthsRegex,
  5612. array('option' => array('value' => '')),
  5613. '/option',
  5614. '*/select',
  5615. '-',
  5616. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5617. $yearsRegex,
  5618. array('option' => array('value' => '')),
  5619. '/option',
  5620. '*/select',
  5621. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5622. $hoursRegex,
  5623. array('option' => array('value' => '')),
  5624. '/option',
  5625. '*/select',
  5626. ':',
  5627. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5628. $minutesRegex,
  5629. array('option' => array('value' => '')),
  5630. '/option',
  5631. '*/select',
  5632. ' ',
  5633. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5634. $meridianRegex,
  5635. array('option' => array('value' => '')),
  5636. '/option',
  5637. '*/select'
  5638. );
  5639. $this->assertTags($result, $expected);
  5640. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5641. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5642. $this->assertTags($result, $expected);
  5643. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5644. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5645. $this->assertTags($result, $expected);
  5646. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5647. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5648. $expected = array(
  5649. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5650. $daysRegex,
  5651. array('option' => array('value' => '')),
  5652. '/option',
  5653. '*/select',
  5654. '-',
  5655. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5656. $monthsRegex,
  5657. array('option' => array('value' => '')),
  5658. '/option',
  5659. '*/select',
  5660. '-',
  5661. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5662. $yearsRegex,
  5663. array('option' => array('value' => '')),
  5664. '/option',
  5665. '*/select',
  5666. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5667. $hoursRegex,
  5668. array('option' => array('value' => '')),
  5669. '/option',
  5670. '*/select',
  5671. ':',
  5672. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5673. $minutesRegex,
  5674. array('option' => array('value' => '')),
  5675. '/option',
  5676. array('option' => array('value' => '00')),
  5677. '00',
  5678. '/option',
  5679. array('option' => array('value' => '05')),
  5680. '05',
  5681. '/option',
  5682. array('option' => array('value' => '10')),
  5683. '10',
  5684. '/option',
  5685. '*/select',
  5686. ' ',
  5687. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5688. $meridianRegex,
  5689. array('option' => array('value' => '')),
  5690. '/option',
  5691. '*/select'
  5692. );
  5693. $this->assertTags($result, $expected);
  5694. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5695. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5696. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5697. $this->Form->request->data['Contact']['data'] = null;
  5698. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5699. $expected = array(
  5700. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5701. $daysRegex,
  5702. array('option' => array('value' => '')),
  5703. '/option',
  5704. '*/select',
  5705. '-',
  5706. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5707. $monthsRegex,
  5708. array('option' => array('value' => '')),
  5709. '/option',
  5710. '*/select',
  5711. '-',
  5712. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5713. $yearsRegex,
  5714. array('option' => array('value' => '')),
  5715. '/option',
  5716. '*/select',
  5717. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5718. $hoursRegex,
  5719. array('option' => array('value' => '')),
  5720. '/option',
  5721. '*/select',
  5722. ':',
  5723. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5724. $minutesRegex,
  5725. array('option' => array('value' => '')),
  5726. '/option',
  5727. '*/select',
  5728. ' ',
  5729. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5730. $meridianRegex,
  5731. array('option' => array('value' => '')),
  5732. '/option',
  5733. '*/select'
  5734. );
  5735. $this->assertTags($result, $expected);
  5736. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5737. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5738. $now = strtotime($this->Form->data['Model']['field']);
  5739. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5740. $expected = array(
  5741. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5742. $daysRegex,
  5743. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5744. date('j', $now),
  5745. '/option',
  5746. '*/select',
  5747. '-',
  5748. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5749. $monthsRegex,
  5750. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5751. date('F', $now),
  5752. '/option',
  5753. '*/select',
  5754. '-',
  5755. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5756. $yearsRegex,
  5757. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5758. date('Y', $now),
  5759. '/option',
  5760. '*/select',
  5761. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5762. $hoursRegex,
  5763. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5764. date('g', $now),
  5765. '/option',
  5766. '*/select',
  5767. ':',
  5768. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5769. $minutesRegex,
  5770. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5771. date('i', $now),
  5772. '/option',
  5773. '*/select',
  5774. ' ',
  5775. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5776. $meridianRegex,
  5777. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5778. date('a', $now),
  5779. '/option',
  5780. '*/select'
  5781. );
  5782. $this->assertTags($result, $expected);
  5783. $selected = strtotime('2008-10-26 12:33:00');
  5784. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5785. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5786. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5787. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5788. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5789. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5790. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5791. $this->Form->create('Contact');
  5792. $result = $this->Form->input('published');
  5793. $now = strtotime('now');
  5794. $expected = array(
  5795. 'div' => array('class' => 'input date'),
  5796. 'label' => array('for' => 'ContactPublishedMonth'),
  5797. 'Published',
  5798. '/label',
  5799. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5800. $monthsRegex,
  5801. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5802. date('F', $now),
  5803. '/option',
  5804. '*/select',
  5805. '-',
  5806. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5807. $daysRegex,
  5808. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5809. date('j', $now),
  5810. '/option',
  5811. '*/select',
  5812. '-',
  5813. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5814. $yearsRegex,
  5815. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5816. date('Y', $now),
  5817. '/option',
  5818. '*/select',
  5819. '/div'
  5820. );
  5821. $this->assertTags($result, $expected);
  5822. $result = $this->Form->input('published2', array('type' => 'date'));
  5823. $now = strtotime('now');
  5824. $expected = array(
  5825. 'div' => array('class' => 'input date'),
  5826. 'label' => array('for' => 'ContactPublished2Month'),
  5827. 'Published2',
  5828. '/label',
  5829. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5830. $monthsRegex,
  5831. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5832. date('F', $now),
  5833. '/option',
  5834. '*/select',
  5835. '-',
  5836. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5837. $daysRegex,
  5838. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5839. date('j', $now),
  5840. '/option',
  5841. '*/select',
  5842. '-',
  5843. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5844. $yearsRegex,
  5845. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5846. date('Y', $now),
  5847. '/option',
  5848. '*/select',
  5849. '/div'
  5850. );
  5851. $this->assertTags($result, $expected);
  5852. $this->Form->create('Contact');
  5853. $result = $this->Form->input('published', array('monthNames' => false));
  5854. $now = strtotime('now');
  5855. $expected = array(
  5856. 'div' => array('class' => 'input date'),
  5857. 'label' => array('for' => 'ContactPublishedMonth'),
  5858. 'Published',
  5859. '/label',
  5860. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5861. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5862. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5863. date('m', $now),
  5864. '/option',
  5865. '*/select',
  5866. '-',
  5867. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5868. $daysRegex,
  5869. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5870. date('j', $now),
  5871. '/option',
  5872. '*/select',
  5873. '-',
  5874. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5875. $yearsRegex,
  5876. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5877. date('Y', $now),
  5878. '/option',
  5879. '*/select',
  5880. '/div'
  5881. );
  5882. $this->assertTags($result, $expected);
  5883. $result = $this->Form->input('published', array(
  5884. 'timeFormat' => 24,
  5885. 'interval' => 5,
  5886. 'selected' => strtotime('2009-09-03 13:37:00'),
  5887. 'type' => 'datetime'
  5888. ));
  5889. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5890. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5891. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5892. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5893. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5894. }
  5895. /**
  5896. * Test dateTime with rounding
  5897. *
  5898. * @return void
  5899. */
  5900. public function testDateTimeRounding() {
  5901. $this->Form->request->data['Contact'] = array(
  5902. 'date' => array(
  5903. 'day' => '13',
  5904. 'month' => '12',
  5905. 'year' => '2010',
  5906. 'hour' => '04',
  5907. 'min' => '19',
  5908. 'meridian' => 'AM'
  5909. )
  5910. );
  5911. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5912. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5913. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5914. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5915. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5916. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5917. }
  5918. /**
  5919. * Test that empty values don't trigger errors.
  5920. *
  5921. * @return void
  5922. */
  5923. public function testDateTimeNoErrorsOnEmptyData() {
  5924. $this->Form->request->data['Contact'] = array(
  5925. 'date' => array(
  5926. 'day' => '',
  5927. 'month' => '',
  5928. 'year' => '',
  5929. 'hour' => '',
  5930. 'min' => '',
  5931. 'meridian' => ''
  5932. )
  5933. );
  5934. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5935. $this->assertNotEmpty($result);
  5936. }
  5937. /**
  5938. * test that datetime() and default values work.
  5939. *
  5940. * @return void
  5941. */
  5942. public function testDatetimeWithDefault() {
  5943. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5944. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5945. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5946. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5947. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5948. 'default' => '2009-06-01 11:15:30'
  5949. ));
  5950. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5951. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5952. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5953. }
  5954. /**
  5955. * test that bogus non-date time data doesn't cause errors.
  5956. *
  5957. * @return void
  5958. */
  5959. public function testDateTimeWithBogusData() {
  5960. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5961. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5962. }
  5963. /**
  5964. * testDateTime all zeros
  5965. *
  5966. * @return void
  5967. */
  5968. public function testDateTimeAllZeros() {
  5969. $result = $this->Form->dateTime('Contact.date',
  5970. 'DMY',
  5971. false,
  5972. array(
  5973. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5974. 'value' => '0000-00-00'
  5975. )
  5976. );
  5977. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5978. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5979. }
  5980. /**
  5981. * testDateTimeEmptyAsArray
  5982. *
  5983. * @return void
  5984. */
  5985. public function testDateTimeEmptyAsArray() {
  5986. $result = $this->Form->dateTime('Contact.date',
  5987. 'DMY',
  5988. '12',
  5989. array(
  5990. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5991. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5992. )
  5993. )
  5994. );
  5995. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5996. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5997. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5998. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5999. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  6000. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  6001. $result = $this->Form->dateTime('Contact.date',
  6002. 'DMY',
  6003. '12',
  6004. array(
  6005. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  6006. )
  6007. );
  6008. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  6009. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  6010. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  6011. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  6012. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  6013. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  6014. }
  6015. /**
  6016. * testFormDateTimeMulti method
  6017. *
  6018. * test multiple datetime element generation
  6019. *
  6020. * @return void
  6021. */
  6022. public function testFormDateTimeMulti() {
  6023. extract($this->dateRegex);
  6024. $result = $this->Form->dateTime('Contact.1.updated');
  6025. $expected = array(
  6026. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  6027. $daysRegex,
  6028. array('option' => array('value' => '')),
  6029. '/option',
  6030. '*/select',
  6031. '-',
  6032. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  6033. $monthsRegex,
  6034. array('option' => array('value' => '')),
  6035. '/option',
  6036. '*/select',
  6037. '-',
  6038. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  6039. $yearsRegex,
  6040. array('option' => array('value' => '')),
  6041. '/option',
  6042. '*/select',
  6043. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  6044. $hoursRegex,
  6045. array('option' => array('value' => '')),
  6046. '/option',
  6047. '*/select',
  6048. ':',
  6049. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  6050. $minutesRegex,
  6051. array('option' => array('value' => '')),
  6052. '/option',
  6053. '*/select',
  6054. ' ',
  6055. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  6056. $meridianRegex,
  6057. array('option' => array('value' => '')),
  6058. '/option',
  6059. '*/select'
  6060. );
  6061. $this->assertTags($result, $expected);
  6062. $result = $this->Form->dateTime('Contact.2.updated');
  6063. $expected = array(
  6064. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  6065. $daysRegex,
  6066. array('option' => array('value' => '')),
  6067. '/option',
  6068. '*/select',
  6069. '-',
  6070. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  6071. $monthsRegex,
  6072. array('option' => array('value' => '')),
  6073. '/option',
  6074. '*/select',
  6075. '-',
  6076. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  6077. $yearsRegex,
  6078. array('option' => array('value' => '')),
  6079. '/option',
  6080. '*/select',
  6081. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  6082. $hoursRegex,
  6083. array('option' => array('value' => '')),
  6084. '/option',
  6085. '*/select',
  6086. ':',
  6087. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  6088. $minutesRegex,
  6089. array('option' => array('value' => '')),
  6090. '/option',
  6091. '*/select',
  6092. ' ',
  6093. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  6094. $meridianRegex,
  6095. array('option' => array('value' => '')),
  6096. '/option',
  6097. '*/select'
  6098. );
  6099. $this->assertTags($result, $expected);
  6100. }
  6101. /**
  6102. * When changing the date format, the label should always focus the first select box when
  6103. * clicked.
  6104. *
  6105. * @return void
  6106. */
  6107. public function testDateTimeLabelIdMatchesFirstInput() {
  6108. $result = $this->Form->input('Model.date', array('type' => 'date'));
  6109. $this->assertContains('label for="ModelDateMonth"', $result);
  6110. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  6111. $this->assertContains('label for="ModelDateDay"', $result);
  6112. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  6113. $this->assertContains('label for="ModelDateYear"', $result);
  6114. }
  6115. /**
  6116. * testMonth method
  6117. *
  6118. * @return void
  6119. */
  6120. public function testMonth() {
  6121. $result = $this->Form->month('Model.field');
  6122. $expected = array(
  6123. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6124. array('option' => array('value' => '')),
  6125. '/option',
  6126. array('option' => array('value' => '01')),
  6127. date('F', strtotime('2008-01-01 00:00:00')),
  6128. '/option',
  6129. array('option' => array('value' => '02')),
  6130. date('F', strtotime('2008-02-01 00:00:00')),
  6131. '/option',
  6132. '*/select',
  6133. );
  6134. $this->assertTags($result, $expected);
  6135. $result = $this->Form->month('Model.field', array('empty' => true));
  6136. $expected = array(
  6137. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6138. array('option' => array('value' => '')),
  6139. '/option',
  6140. array('option' => array('value' => '01')),
  6141. date('F', strtotime('2008-01-01 00:00:00')),
  6142. '/option',
  6143. array('option' => array('value' => '02')),
  6144. date('F', strtotime('2008-02-01 00:00:00')),
  6145. '/option',
  6146. '*/select',
  6147. );
  6148. $this->assertTags($result, $expected);
  6149. $result = $this->Form->month('Model.field', array('monthNames' => false));
  6150. $expected = array(
  6151. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6152. array('option' => array('value' => '')),
  6153. '/option',
  6154. array('option' => array('value' => '01')),
  6155. '01',
  6156. '/option',
  6157. array('option' => array('value' => '02')),
  6158. '02',
  6159. '/option',
  6160. '*/select',
  6161. );
  6162. $this->assertTags($result, $expected);
  6163. $monthNames = array(
  6164. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6165. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  6166. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  6167. $expected = array(
  6168. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6169. array('option' => array('value' => '')),
  6170. '/option',
  6171. array('option' => array('value' => '01')),
  6172. 'Jan',
  6173. '/option',
  6174. array('option' => array('value' => '02')),
  6175. 'Feb',
  6176. '/option',
  6177. '*/select',
  6178. );
  6179. $this->assertTags($result, $expected);
  6180. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6181. $result = $this->Form->month('Project.release');
  6182. $expected = array(
  6183. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  6184. array('option' => array('value' => '')),
  6185. '/option',
  6186. array('option' => array('value' => '01')),
  6187. 'January',
  6188. '/option',
  6189. array('option' => array('value' => '02', 'selected' => 'selected')),
  6190. 'February',
  6191. '/option',
  6192. '*/select',
  6193. );
  6194. $this->assertTags($result, $expected);
  6195. $this->Form->request->data['Model']['field'] = '12a';
  6196. $result = $this->Form->month('Model.field');
  6197. $expected = array(
  6198. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6199. array('option' => array('value' => '')),
  6200. '/option',
  6201. array('option' => array('value' => '01')),
  6202. date('F', strtotime('2008-01-01 00:00:00')),
  6203. '/option',
  6204. array('option' => array('value' => '02')),
  6205. date('F', strtotime('2008-02-01 00:00:00')),
  6206. '/option',
  6207. '*/select',
  6208. );
  6209. $this->assertTags($result, $expected);
  6210. }
  6211. /**
  6212. * testDay method
  6213. *
  6214. * @return void
  6215. */
  6216. public function testDay() {
  6217. extract($this->dateRegex);
  6218. $result = $this->Form->day('Model.field', array('value' => false));
  6219. $expected = array(
  6220. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6221. array('option' => array('value' => '')),
  6222. '/option',
  6223. array('option' => array('value' => '01')),
  6224. '1',
  6225. '/option',
  6226. array('option' => array('value' => '02')),
  6227. '2',
  6228. '/option',
  6229. $daysRegex,
  6230. '/select',
  6231. );
  6232. $this->assertTags($result, $expected);
  6233. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6234. $result = $this->Form->day('Model.field');
  6235. $expected = array(
  6236. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6237. array('option' => array('value' => '')),
  6238. '/option',
  6239. array('option' => array('value' => '01')),
  6240. '1',
  6241. '/option',
  6242. array('option' => array('value' => '02')),
  6243. '2',
  6244. '/option',
  6245. $daysRegex,
  6246. array('option' => array('value' => '10', 'selected' => 'selected')),
  6247. '10',
  6248. '/option',
  6249. $daysRegex,
  6250. '/select',
  6251. );
  6252. $this->assertTags($result, $expected);
  6253. $this->Form->request->data['Model']['field'] = '';
  6254. $result = $this->Form->day('Model.field', array('value' => '10'));
  6255. $expected = array(
  6256. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6257. array('option' => array('value' => '')),
  6258. '/option',
  6259. array('option' => array('value' => '01')),
  6260. '1',
  6261. '/option',
  6262. array('option' => array('value' => '02')),
  6263. '2',
  6264. '/option',
  6265. $daysRegex,
  6266. array('option' => array('value' => '10', 'selected' => 'selected')),
  6267. '10',
  6268. '/option',
  6269. $daysRegex,
  6270. '/select',
  6271. );
  6272. $this->assertTags($result, $expected);
  6273. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6274. $result = $this->Form->day('Model.field', array('value' => true));
  6275. $expected = array(
  6276. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6277. array('option' => array('value' => '')),
  6278. '/option',
  6279. array('option' => array('value' => '01')),
  6280. '1',
  6281. '/option',
  6282. array('option' => array('value' => '02')),
  6283. '2',
  6284. '/option',
  6285. $daysRegex,
  6286. array('option' => array('value' => '10', 'selected' => 'selected')),
  6287. '10',
  6288. '/option',
  6289. $daysRegex,
  6290. '/select',
  6291. );
  6292. $this->assertTags($result, $expected);
  6293. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6294. $result = $this->Form->day('Project.release');
  6295. $expected = array(
  6296. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6297. array('option' => array('value' => '')),
  6298. '/option',
  6299. array('option' => array('value' => '01')),
  6300. '1',
  6301. '/option',
  6302. array('option' => array('value' => '02')),
  6303. '2',
  6304. '/option',
  6305. $daysRegex,
  6306. array('option' => array('value' => '10', 'selected' => 'selected')),
  6307. '10',
  6308. '/option',
  6309. $daysRegex,
  6310. '/select',
  6311. );
  6312. $this->assertTags($result, $expected);
  6313. $this->Form->request->data['Model']['field'] = '12e';
  6314. $result = $this->Form->day('Model.field');
  6315. $expected = array(
  6316. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6317. array('option' => array('value' => '')),
  6318. '/option',
  6319. array('option' => array('value' => '01')),
  6320. '1',
  6321. '/option',
  6322. array('option' => array('value' => '02')),
  6323. '2',
  6324. '/option',
  6325. $daysRegex,
  6326. '/select',
  6327. );
  6328. $this->assertTags($result, $expected);
  6329. }
  6330. /**
  6331. * testMinute method
  6332. *
  6333. * @return void
  6334. */
  6335. public function testMinute() {
  6336. extract($this->dateRegex);
  6337. $result = $this->Form->minute('Model.field');
  6338. $expected = array(
  6339. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6340. array('option' => array('value' => '')),
  6341. '/option',
  6342. array('option' => array('value' => '00')),
  6343. '00',
  6344. '/option',
  6345. array('option' => array('value' => '01')),
  6346. '01',
  6347. '/option',
  6348. array('option' => array('value' => '02')),
  6349. '02',
  6350. '/option',
  6351. $minutesRegex,
  6352. '/select',
  6353. );
  6354. $this->assertTags($result, $expected);
  6355. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6356. $result = $this->Form->minute('Model.field');
  6357. $expected = array(
  6358. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6359. array('option' => array('value' => '')),
  6360. '/option',
  6361. array('option' => array('value' => '00')),
  6362. '00',
  6363. '/option',
  6364. array('option' => array('value' => '01')),
  6365. '01',
  6366. '/option',
  6367. array('option' => array('value' => '02')),
  6368. '02',
  6369. '/option',
  6370. $minutesRegex,
  6371. array('option' => array('value' => '12', 'selected' => 'selected')),
  6372. '12',
  6373. '/option',
  6374. $minutesRegex,
  6375. '/select',
  6376. );
  6377. $this->assertTags($result, $expected);
  6378. $this->Form->request->data['Model']['field'] = '';
  6379. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6380. $expected = array(
  6381. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6382. array('option' => array('value' => '')),
  6383. '/option',
  6384. array('option' => array('value' => '00')),
  6385. '00',
  6386. '/option',
  6387. array('option' => array('value' => '05')),
  6388. '05',
  6389. '/option',
  6390. array('option' => array('value' => '10')),
  6391. '10',
  6392. '/option',
  6393. $minutesRegex,
  6394. '/select',
  6395. );
  6396. $this->assertTags($result, $expected);
  6397. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6398. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6399. $expected = array(
  6400. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6401. array('option' => array('value' => '')),
  6402. '/option',
  6403. array('option' => array('value' => '00')),
  6404. '00',
  6405. '/option',
  6406. array('option' => array('value' => '05')),
  6407. '05',
  6408. '/option',
  6409. array('option' => array('value' => '10', 'selected' => 'selected')),
  6410. '10',
  6411. '/option',
  6412. $minutesRegex,
  6413. '/select',
  6414. );
  6415. $this->assertTags($result, $expected);
  6416. $result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
  6417. $expected = array(
  6418. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6419. array('option' => array('value' => '')),
  6420. '/option',
  6421. array('option' => array('value' => '00')),
  6422. '00',
  6423. '/option',
  6424. array('option' => array('value' => '01')),
  6425. '01',
  6426. '/option',
  6427. array('option' => array('value' => '02')),
  6428. '02',
  6429. '/option',
  6430. $minutesRegex,
  6431. '/select',
  6432. );
  6433. $this->assertTags($result, $expected);
  6434. }
  6435. /**
  6436. * testHour method
  6437. *
  6438. * @return void
  6439. */
  6440. public function testHour() {
  6441. extract($this->dateRegex);
  6442. $result = $this->Form->hour('Model.field', false);
  6443. $expected = array(
  6444. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6445. array('option' => array('value' => '')),
  6446. '/option',
  6447. array('option' => array('value' => '01')),
  6448. '1',
  6449. '/option',
  6450. array('option' => array('value' => '02')),
  6451. '2',
  6452. '/option',
  6453. $hoursRegex,
  6454. '/select',
  6455. );
  6456. $this->assertTags($result, $expected);
  6457. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6458. $result = $this->Form->hour('Model.field', false);
  6459. $expected = array(
  6460. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6461. array('option' => array('value' => '')),
  6462. '/option',
  6463. array('option' => array('value' => '01')),
  6464. '1',
  6465. '/option',
  6466. array('option' => array('value' => '02')),
  6467. '2',
  6468. '/option',
  6469. $hoursRegex,
  6470. array('option' => array('value' => '12', 'selected' => 'selected')),
  6471. '12',
  6472. '/option',
  6473. '/select',
  6474. );
  6475. $this->assertTags($result, $expected);
  6476. $this->Form->request->data['Model']['field'] = '';
  6477. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6478. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6479. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6480. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6481. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6482. $result = $this->Form->hour('Model.field', true);
  6483. $expected = array(
  6484. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6485. array('option' => array('value' => '')),
  6486. '/option',
  6487. array('option' => array('value' => '00', 'selected' => 'selected')),
  6488. '0',
  6489. '/option',
  6490. array('option' => array('value' => '01')),
  6491. '1',
  6492. '/option',
  6493. array('option' => array('value' => '02')),
  6494. '2',
  6495. '/option',
  6496. $hoursRegex,
  6497. '/select',
  6498. );
  6499. $this->assertTags($result, $expected);
  6500. unset($this->Form->request->data['Model']['field']);
  6501. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6502. $thisHour = date('H');
  6503. $optValue = date('G');
  6504. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6505. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6506. $result = $this->Form->hour('Model.field', true);
  6507. $expected = array(
  6508. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6509. array('option' => array('value' => '')),
  6510. '/option',
  6511. array('option' => array('value' => '00')),
  6512. '0',
  6513. '/option',
  6514. array('option' => array('value' => '01', 'selected' => 'selected')),
  6515. '1',
  6516. '/option',
  6517. array('option' => array('value' => '02')),
  6518. '2',
  6519. '/option',
  6520. $hoursRegex,
  6521. '/select',
  6522. );
  6523. $this->assertTags($result, $expected);
  6524. $this->Form->request->data['Model']['field'] = '18a';
  6525. $result = $this->Form->hour('Model.field', false);
  6526. $expected = array(
  6527. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6528. array('option' => array('value' => '')),
  6529. '/option',
  6530. array('option' => array('value' => '01')),
  6531. '1',
  6532. '/option',
  6533. array('option' => array('value' => '02')),
  6534. '2',
  6535. '/option',
  6536. $hoursRegex,
  6537. '/select',
  6538. );
  6539. $this->assertTags($result, $expected);
  6540. }
  6541. /**
  6542. * testYear method
  6543. *
  6544. * @return void
  6545. */
  6546. public function testYear() {
  6547. $result = $this->Form->year('Model.field', 2006, 2007);
  6548. $expected = array(
  6549. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6550. array('option' => array('value' => '')),
  6551. '/option',
  6552. array('option' => array('value' => '2007')),
  6553. '2007',
  6554. '/option',
  6555. array('option' => array('value' => '2006')),
  6556. '2006',
  6557. '/option',
  6558. '/select',
  6559. );
  6560. $this->assertTags($result, $expected);
  6561. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6562. $expected = array(
  6563. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6564. array('option' => array('value' => '')),
  6565. '/option',
  6566. array('option' => array('value' => '2006')),
  6567. '2006',
  6568. '/option',
  6569. array('option' => array('value' => '2007')),
  6570. '2007',
  6571. '/option',
  6572. '/select',
  6573. );
  6574. $this->assertTags($result, $expected);
  6575. $this->request->data['Contact']['published'] = '';
  6576. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6577. $expected = array(
  6578. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6579. array('option' => array('value' => '')),
  6580. '/option',
  6581. array('option' => array('value' => '2007')),
  6582. '2007',
  6583. '/option',
  6584. array('option' => array('value' => '2006')),
  6585. '2006',
  6586. '/option',
  6587. '/select',
  6588. );
  6589. $this->assertTags($result, $expected);
  6590. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6591. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6592. $expected = array(
  6593. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6594. array('option' => array('value' => '2007')),
  6595. '2007',
  6596. '/option',
  6597. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6598. '2006',
  6599. '/option',
  6600. '/select',
  6601. );
  6602. $this->assertTags($result, $expected);
  6603. $this->Form->request->data['Contact']['published'] = '';
  6604. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6605. $expected = array(
  6606. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6607. array('option' => array('value' => '')),
  6608. '/option',
  6609. array('option' => array('value' => '2007')),
  6610. '2007',
  6611. '/option',
  6612. array('option' => array('value' => '2006')),
  6613. '2006',
  6614. '/option',
  6615. '/select',
  6616. );
  6617. $this->assertTags($result, $expected);
  6618. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6619. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6620. $expected = array(
  6621. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6622. array('option' => array('value' => '2007')),
  6623. '2007',
  6624. '/option',
  6625. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6626. '2006',
  6627. '/option',
  6628. '/select',
  6629. );
  6630. $this->assertTags($result, $expected);
  6631. $this->Form->request->data['Contact']['published'] = '';
  6632. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6633. $expected = array(
  6634. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6635. array('option' => array('value' => '')),
  6636. '/option',
  6637. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6638. '2007',
  6639. '/option',
  6640. array('option' => array('value' => '2006')),
  6641. '2006',
  6642. '/option',
  6643. '/select',
  6644. );
  6645. $this->assertTags($result, $expected);
  6646. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6647. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6648. $expected = array(
  6649. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6650. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6651. '2007',
  6652. '/option',
  6653. array('option' => array('value' => '2006')),
  6654. '2006',
  6655. '/option',
  6656. '/select',
  6657. );
  6658. $this->assertTags($result, $expected);
  6659. $this->Form->request->data['Contact']['published'] = '';
  6660. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6661. $expected = array(
  6662. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6663. array('option' => array('value' => '2008')),
  6664. '2008',
  6665. '/option',
  6666. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6667. '2007',
  6668. '/option',
  6669. array('option' => array('value' => '2006')),
  6670. '2006',
  6671. '/option',
  6672. '/select',
  6673. );
  6674. $this->assertTags($result, $expected);
  6675. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6676. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6677. $expected = array(
  6678. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6679. array('option' => array('value' => '2008')),
  6680. '2008',
  6681. '/option',
  6682. array('option' => array('value' => '2007')),
  6683. '2007',
  6684. '/option',
  6685. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6686. '2006',
  6687. '/option',
  6688. '/select',
  6689. );
  6690. $this->assertTags($result, $expected);
  6691. $this->Form->request->data = array();
  6692. $this->Form->create('Contact');
  6693. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6694. $expected = array(
  6695. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6696. array('option' => array('value' => '2008')),
  6697. '2008',
  6698. '/option',
  6699. array('option' => array('value' => '2007')),
  6700. '2007',
  6701. '/option',
  6702. array('option' => array('value' => '2006')),
  6703. '2006',
  6704. '/option',
  6705. '/select',
  6706. );
  6707. $this->assertTags($result, $expected);
  6708. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6709. $this->assertContains('data[Contact][published][year]', $result);
  6710. $this->Form->request->data['Contact']['published'] = '2014ee';
  6711. $result = $this->Form->year('Contact.published', 2010, 2011);
  6712. $expected = array(
  6713. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6714. array('option' => array('value' => '')),
  6715. '/option',
  6716. array('option' => array('value' => '2011')),
  6717. '2011',
  6718. '/option',
  6719. array('option' => array('value' => '2010')),
  6720. '2010',
  6721. '/option',
  6722. '/select',
  6723. );
  6724. $this->assertTags($result, $expected);
  6725. }
  6726. /**
  6727. * testYearAutoExpandRange method
  6728. *
  6729. * @return void
  6730. */
  6731. public function testYearAutoExpandRange() {
  6732. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6733. $result = $this->Form->year('User.birthday');
  6734. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6735. $result = $matches[1];
  6736. $expected = range(date('Y') + 20, 1930);
  6737. $this->assertEquals($expected, $result);
  6738. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6739. $result = $this->Form->year('Project.release');
  6740. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6741. $result = $matches[1];
  6742. $expected = range(2050, date('Y') - 20);
  6743. $this->assertEquals($expected, $result);
  6744. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6745. $result = $this->Form->year('Project.release', 1890, 1900);
  6746. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6747. $result = $matches[1];
  6748. $expected = range(1900, 1881);
  6749. $this->assertEquals($expected, $result);
  6750. }
  6751. /**
  6752. * testInputDate method
  6753. *
  6754. * Test various inputs with type date and different dateFormat values.
  6755. * Failing to provide a dateFormat key should not error.
  6756. * It should simply not pre-select any value then.
  6757. *
  6758. * @return void
  6759. */
  6760. public function testInputDate() {
  6761. $this->Form->request->data = array(
  6762. 'User' => array(
  6763. 'month_year' => array('month' => date('m')),
  6764. 'just_year' => array('month' => date('m')),
  6765. 'just_month' => array('year' => date('Y')),
  6766. 'just_day' => array('month' => date('m')),
  6767. )
  6768. );
  6769. $this->Form->create('User');
  6770. $result = $this->Form->input('month_year',
  6771. array(
  6772. 'label' => false,
  6773. 'div' => false,
  6774. 'type' => 'date',
  6775. 'dateFormat' => 'MY',
  6776. 'minYear' => 2006,
  6777. 'maxYear' => 2008
  6778. )
  6779. );
  6780. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6781. $this->assertNotContains('value="2008" selected="selected"', $result);
  6782. $result = $this->Form->input('just_year',
  6783. array(
  6784. 'type' => 'date',
  6785. 'label' => false,
  6786. 'dateFormat' => 'Y',
  6787. 'minYear' => date('Y'),
  6788. 'maxYear' => date('Y', strtotime('+20 years'))
  6789. )
  6790. );
  6791. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6792. $result = $this->Form->input('just_month',
  6793. array(
  6794. 'type' => 'date',
  6795. 'label' => false,
  6796. 'dateFormat' => 'M',
  6797. 'empty' => false,
  6798. )
  6799. );
  6800. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6801. $result = $this->Form->input('just_day',
  6802. array(
  6803. 'type' => 'date',
  6804. 'label' => false,
  6805. 'dateFormat' => 'D',
  6806. 'empty' => false,
  6807. )
  6808. );
  6809. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6810. }
  6811. /**
  6812. * testInputDateMaxYear method
  6813. *
  6814. * Let's say we want to only allow users born from 2006 to 2008 to register
  6815. * This being the first singup page, we still don't have any data
  6816. *
  6817. * @return void
  6818. */
  6819. public function testInputDateMaxYear() {
  6820. $this->Form->request->data = array();
  6821. $this->Form->create('User');
  6822. $result = $this->Form->input('birthday',
  6823. array(
  6824. 'label' => false,
  6825. 'div' => false,
  6826. 'type' => 'date',
  6827. 'dateFormat' => 'DMY',
  6828. 'minYear' => 2006,
  6829. 'maxYear' => 2008
  6830. )
  6831. );
  6832. $this->assertContains('value="2008" selected="selected"', $result);
  6833. }
  6834. /**
  6835. * testTextArea method
  6836. *
  6837. * @return void
  6838. */
  6839. public function testTextArea() {
  6840. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6841. $result = $this->Form->textarea('Model.field');
  6842. $expected = array(
  6843. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6844. 'some test data',
  6845. '/textarea',
  6846. );
  6847. $this->assertTags($result, $expected);
  6848. $result = $this->Form->textarea('Model.tmp');
  6849. $expected = array(
  6850. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6851. '/textarea',
  6852. );
  6853. $this->assertTags($result, $expected);
  6854. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6855. $result = $this->Form->textarea('Model.field');
  6856. $expected = array(
  6857. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6858. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6859. '/textarea',
  6860. );
  6861. $this->assertTags($result, $expected);
  6862. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6863. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6864. $expected = array(
  6865. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6866. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6867. '/textarea',
  6868. );
  6869. $this->assertTags($result, $expected);
  6870. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6871. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6872. $expected = array(
  6873. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6874. '/textarea'
  6875. );
  6876. $this->assertTags($result, $expected);
  6877. }
  6878. /**
  6879. * testTextAreaWithStupidCharacters method
  6880. *
  6881. * test text area with non-ascii characters
  6882. *
  6883. * @return void
  6884. */
  6885. public function testTextAreaWithStupidCharacters() {
  6886. $this->loadFixtures('Post');
  6887. $result = $this->Form->input('Post.content', array(
  6888. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6889. ));
  6890. $expected = array(
  6891. 'div' => array('class' => 'input textarea'),
  6892. 'label' => array('for' => 'PostContent'),
  6893. 'Current Text',
  6894. '/label',
  6895. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6896. 'GREAT®',
  6897. '/textarea',
  6898. '/div'
  6899. );
  6900. $this->assertTags($result, $expected);
  6901. }
  6902. /**
  6903. * testHiddenField method
  6904. *
  6905. * @return void
  6906. */
  6907. public function testHiddenField() {
  6908. $Contact = ClassRegistry::getObject('Contact');
  6909. $Contact->validationErrors['field'] = 1;
  6910. $this->Form->request->data['Contact']['field'] = 'test';
  6911. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6912. $this->assertTags($result, array(
  6913. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6914. );
  6915. }
  6916. /**
  6917. * testFileUploadField method
  6918. *
  6919. * @return void
  6920. */
  6921. public function testFileUploadField() {
  6922. $result = $this->Form->file('Model.upload');
  6923. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6924. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6925. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6926. $expected = array(
  6927. 'div' => array('class' => 'input file'),
  6928. 'label' => array('for' => 'ModelUpload'),
  6929. 'Upload',
  6930. '/label',
  6931. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6932. '/div'
  6933. );
  6934. $this->assertTags($result, $expected);
  6935. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6936. $result = $this->Form->file('Model.upload');
  6937. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6938. }
  6939. /**
  6940. * test File upload input on a model not used in create();
  6941. *
  6942. * @return void
  6943. */
  6944. public function testFileUploadOnOtherModel() {
  6945. $this->Form->create('ValidateUser', array('type' => 'file'));
  6946. $result = $this->Form->file('ValidateProfile.city');
  6947. $expected = array(
  6948. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6949. );
  6950. $this->assertTags($result, $expected);
  6951. }
  6952. /**
  6953. * testButton method
  6954. *
  6955. * @return void
  6956. */
  6957. public function testButton() {
  6958. $result = $this->Form->button('Hi');
  6959. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6960. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6961. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6962. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6963. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6964. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6965. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6966. $result = $this->Form->button('No type', array('type' => false));
  6967. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6968. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6969. $this->assertNotRegExp('/\&039/', $result);
  6970. }
  6971. /**
  6972. * Test that button() makes unlocked fields by default.
  6973. *
  6974. * @return void
  6975. */
  6976. public function testButtonUnlockedByDefault() {
  6977. $this->Form->request->params['_Token']['key'] = 'secured';
  6978. $this->Form->button('Save', array('name' => 'save'));
  6979. $this->Form->button('Clear');
  6980. $result = $this->Form->unlockField();
  6981. $this->assertEquals(array('save'), $result);
  6982. }
  6983. /**
  6984. * testPostButton method
  6985. *
  6986. * @return void
  6987. */
  6988. public function testPostButton() {
  6989. $result = $this->Form->postButton('Hi', '/controller/action');
  6990. $this->assertTags($result, array(
  6991. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6992. 'div' => array('style' => 'display:none;'),
  6993. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6994. '/div',
  6995. 'button' => array('type' => 'submit'),
  6996. 'Hi',
  6997. '/button',
  6998. '/form'
  6999. ));
  7000. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  7001. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  7002. }
  7003. /**
  7004. * Test using postButton with N dimensional data.
  7005. *
  7006. * @return void
  7007. */
  7008. public function testPostButtonNestedData() {
  7009. $data = array(
  7010. 'one' => array(
  7011. 'two' => array(
  7012. 3, 4, 5
  7013. )
  7014. )
  7015. );
  7016. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  7017. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  7018. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  7019. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  7020. }
  7021. /**
  7022. * Test that postButton adds _Token fields.
  7023. *
  7024. * @return void
  7025. */
  7026. public function testSecurePostButton() {
  7027. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  7028. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  7029. $expected = array(
  7030. 'form' => array(
  7031. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  7032. ),
  7033. array('div' => array('style' => 'display:none;')),
  7034. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7035. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7036. '/div',
  7037. 'button' => array('type' => 'submit'),
  7038. 'Delete',
  7039. '/button',
  7040. array('div' => array('style' => 'display:none;')),
  7041. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7042. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7043. '/div',
  7044. '/form',
  7045. );
  7046. $this->assertTags($result, $expected);
  7047. }
  7048. /**
  7049. * testPostLink method
  7050. *
  7051. * @return void
  7052. */
  7053. public function testPostLink() {
  7054. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7055. $this->assertTags($result, array(
  7056. 'form' => array(
  7057. 'method' => 'post', 'action' => '/posts/delete/1',
  7058. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7059. ),
  7060. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7061. '/form',
  7062. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7063. 'Delete',
  7064. '/a'
  7065. ));
  7066. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  7067. $this->assertTags($result, array(
  7068. 'form' => array(
  7069. 'method' => 'post', 'action' => '/posts/delete/1',
  7070. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7071. ),
  7072. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  7073. '/form',
  7074. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7075. 'Delete',
  7076. '/a'
  7077. ));
  7078. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  7079. $this->assertTags($result, array(
  7080. 'form' => array(
  7081. 'method' => 'post', 'action' => '/posts/delete/1',
  7082. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7083. ),
  7084. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7085. '/form',
  7086. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7087. 'Delete',
  7088. '/a'
  7089. ));
  7090. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  7091. $this->assertTags($result, array(
  7092. 'form' => array(
  7093. 'method' => 'post', 'action' => '/posts/delete/1',
  7094. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7095. ),
  7096. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7097. '/form',
  7098. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;&#039;Confirm&#039; this \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7099. 'Delete',
  7100. '/a'
  7101. ));
  7102. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  7103. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  7104. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  7105. $this->assertTags($result, array(
  7106. 'form' => array(
  7107. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7108. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7109. ),
  7110. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7111. '/form',
  7112. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7113. 'Delete',
  7114. '/a'
  7115. ));
  7116. $result = $this->Form->postLink(
  7117. '',
  7118. array('controller' => 'items', 'action' => 'delete', 10),
  7119. array('class' => 'btn btn-danger', 'escape' => false),
  7120. 'Confirm thing'
  7121. );
  7122. $this->assertTags($result, array(
  7123. 'form' => array(
  7124. 'method' => 'post', 'action' => '/items/delete/10',
  7125. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7126. ),
  7127. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7128. '/form',
  7129. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7130. '/a'
  7131. ));
  7132. }
  7133. /**
  7134. * Test that security hashes for postLink include the url.
  7135. *
  7136. * @return void
  7137. */
  7138. public function testPostLinkSecurityHash() {
  7139. $hash = Security::hash(
  7140. '/posts/delete/1' .
  7141. serialize(array()) .
  7142. '' .
  7143. Configure::read('Security.salt')
  7144. );
  7145. $hash .= '%3A';
  7146. $this->Form->request->params['_Token']['key'] = 'test';
  7147. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7148. $this->assertTags($result, array(
  7149. 'form' => array(
  7150. 'method' => 'post', 'action' => '/posts/delete/1',
  7151. 'name', 'id', 'style' => 'display:none;'
  7152. ),
  7153. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7154. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'test', 'id')),
  7155. 'div' => array('style' => 'display:none;'),
  7156. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => $hash, 'id')),
  7157. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id')),
  7158. '/div',
  7159. '/form',
  7160. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7161. 'Delete',
  7162. '/a'
  7163. ));
  7164. }
  7165. /**
  7166. * Test using postLink with N dimensional data.
  7167. *
  7168. * @return void
  7169. */
  7170. public function testPostLinkNestedData() {
  7171. $data = array(
  7172. 'one' => array(
  7173. 'two' => array(
  7174. 3, 4, 5
  7175. )
  7176. )
  7177. );
  7178. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  7179. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  7180. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  7181. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  7182. }
  7183. /**
  7184. * test creating postLinks after a GET form.
  7185. *
  7186. * @return void
  7187. */
  7188. public function testPostLinkAfterGetForm() {
  7189. $this->Form->request->params['_Token']['key'] = 'testkey';
  7190. $this->Form->create('User', array('type' => 'get'));
  7191. $this->Form->end();
  7192. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7193. $this->assertTags($result, array(
  7194. 'form' => array(
  7195. 'method' => 'post', 'action' => '/posts/delete/1',
  7196. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7197. ),
  7198. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7199. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7200. 'div' => array('style' => 'display:none;'),
  7201. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7202. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7203. '/div',
  7204. '/form',
  7205. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7206. 'Delete',
  7207. '/a'
  7208. ));
  7209. }
  7210. /**
  7211. * Test that postLink adds _Token fields.
  7212. *
  7213. * @return void
  7214. */
  7215. public function testSecurePostLink() {
  7216. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  7217. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7218. $expected = array(
  7219. 'form' => array(
  7220. 'method' => 'post', 'action' => '/posts/delete/1',
  7221. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7222. ),
  7223. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7224. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7225. 'div' => array('style' => 'display:none;'),
  7226. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7227. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7228. '/div',
  7229. '/form',
  7230. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7231. 'Delete',
  7232. '/a'
  7233. );
  7234. $this->assertTags($result, $expected);
  7235. }
  7236. /**
  7237. * Test that postLink adds form tags to view block
  7238. *
  7239. * @return void
  7240. */
  7241. public function testPostLinkFormBuffer() {
  7242. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  7243. $this->assertTags($result, array(
  7244. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7245. 'Delete',
  7246. '/a'
  7247. ));
  7248. $result = $this->View->fetch('postLink');
  7249. $this->assertTags($result, array(
  7250. 'form' => array(
  7251. 'method' => 'post', 'action' => '/posts/delete/1',
  7252. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7253. ),
  7254. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7255. '/form'
  7256. ));
  7257. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  7258. array('inline' => false, 'method' => 'DELETE')
  7259. );
  7260. $this->assertTags($result, array(
  7261. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7262. 'Delete',
  7263. '/a'
  7264. ));
  7265. $result = $this->View->fetch('postLink');
  7266. $this->assertTags($result, array(
  7267. 'form' => array(
  7268. 'method' => 'post', 'action' => '/posts/delete/1',
  7269. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7270. ),
  7271. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7272. '/form',
  7273. array(
  7274. 'form' => array(
  7275. 'method' => 'post', 'action' => '/posts/delete/2',
  7276. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7277. ),
  7278. ),
  7279. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  7280. '/form'
  7281. ));
  7282. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  7283. $this->assertTags($result, array(
  7284. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7285. 'Delete',
  7286. '/a'
  7287. ));
  7288. $result = $this->View->fetch('foobar');
  7289. $this->assertTags($result, array(
  7290. 'form' => array(
  7291. 'method' => 'post', 'action' => '/posts/delete/1',
  7292. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7293. ),
  7294. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7295. '/form'
  7296. ));
  7297. }
  7298. /**
  7299. * testSubmitButton method
  7300. *
  7301. * @return void
  7302. */
  7303. public function testSubmitButton() {
  7304. $result = $this->Form->submit('');
  7305. $expected = array(
  7306. 'div' => array('class' => 'submit'),
  7307. 'input' => array('type' => 'submit', 'value' => ''),
  7308. '/div'
  7309. );
  7310. $this->assertTags($result, $expected);
  7311. $result = $this->Form->submit('Test Submit');
  7312. $expected = array(
  7313. 'div' => array('class' => 'submit'),
  7314. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7315. '/div'
  7316. );
  7317. $this->assertTags($result, $expected);
  7318. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  7319. $expected = array(
  7320. 'span' => array('class' => 'submit'),
  7321. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7322. '/span'
  7323. );
  7324. $this->assertTags($result, $expected);
  7325. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  7326. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  7327. $this->assertTags($result, $expected);
  7328. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  7329. $expected = array(
  7330. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  7331. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7332. '/div'
  7333. );
  7334. $this->assertTags($result, $expected);
  7335. $result = $this->Form->submit('Next >');
  7336. $expected = array(
  7337. 'div' => array('class' => 'submit'),
  7338. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  7339. '/div'
  7340. );
  7341. $this->assertTags($result, $expected);
  7342. $result = $this->Form->submit('Next >', array('escape' => false));
  7343. $expected = array(
  7344. 'div' => array('class' => 'submit'),
  7345. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  7346. '/div'
  7347. );
  7348. $this->assertTags($result, $expected);
  7349. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7350. $expected = array(
  7351. 'div' => array('class' => 'submit'),
  7352. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7353. '/div'
  7354. );
  7355. $this->assertTags($result, $expected);
  7356. $before = '--before--';
  7357. $after = '--after--';
  7358. $result = $this->Form->submit('Test', array('before' => $before));
  7359. $expected = array(
  7360. 'div' => array('class' => 'submit'),
  7361. '--before--',
  7362. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7363. '/div'
  7364. );
  7365. $this->assertTags($result, $expected);
  7366. $result = $this->Form->submit('Test', array('after' => $after));
  7367. $expected = array(
  7368. 'div' => array('class' => 'submit'),
  7369. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7370. '--after--',
  7371. '/div'
  7372. );
  7373. $this->assertTags($result, $expected);
  7374. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7375. $expected = array(
  7376. 'div' => array('class' => 'submit'),
  7377. '--before--',
  7378. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7379. '--after--',
  7380. '/div'
  7381. );
  7382. $this->assertTags($result, $expected);
  7383. }
  7384. /**
  7385. * test image submit types.
  7386. *
  7387. * @return void
  7388. */
  7389. public function testSubmitImage() {
  7390. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7391. $expected = array(
  7392. 'div' => array('class' => 'submit'),
  7393. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7394. '/div'
  7395. );
  7396. $this->assertTags($result, $expected);
  7397. $result = $this->Form->submit('/relative/cake.power.gif');
  7398. $expected = array(
  7399. 'div' => array('class' => 'submit'),
  7400. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7401. '/div'
  7402. );
  7403. $this->assertTags($result, $expected);
  7404. $result = $this->Form->submit('cake.power.gif');
  7405. $expected = array(
  7406. 'div' => array('class' => 'submit'),
  7407. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7408. '/div'
  7409. );
  7410. $this->assertTags($result, $expected);
  7411. $result = $this->Form->submit('Not.an.image');
  7412. $expected = array(
  7413. 'div' => array('class' => 'submit'),
  7414. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7415. '/div'
  7416. );
  7417. $this->assertTags($result, $expected);
  7418. $after = '--after--';
  7419. $before = '--before--';
  7420. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7421. $expected = array(
  7422. 'div' => array('class' => 'submit'),
  7423. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7424. '--after--',
  7425. '/div'
  7426. );
  7427. $this->assertTags($result, $expected);
  7428. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7429. $expected = array(
  7430. 'div' => array('class' => 'submit'),
  7431. '--before--',
  7432. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7433. '/div'
  7434. );
  7435. $this->assertTags($result, $expected);
  7436. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7437. $expected = array(
  7438. 'div' => array('class' => 'submit'),
  7439. '--before--',
  7440. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7441. '--after--',
  7442. '/div'
  7443. );
  7444. $this->assertTags($result, $expected);
  7445. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7446. $expected = array(
  7447. 'div' => array('class' => 'submit'),
  7448. '--before--',
  7449. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7450. '--after--',
  7451. '/div'
  7452. );
  7453. $this->assertTags($result, $expected);
  7454. }
  7455. /**
  7456. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7457. * be submitted at a time.
  7458. *
  7459. * @return void
  7460. */
  7461. public function testSubmitUnlockedByDefault() {
  7462. $this->Form->request->params['_Token']['key'] = 'secured';
  7463. $this->Form->submit('Go go');
  7464. $this->Form->submit('Save', array('name' => 'save'));
  7465. $result = $this->Form->unlockField();
  7466. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7467. }
  7468. /**
  7469. * Test submit image with timestamps.
  7470. *
  7471. * @return void
  7472. */
  7473. public function testSubmitImageTimestamp() {
  7474. Configure::write('Asset.timestamp', 'force');
  7475. $result = $this->Form->submit('cake.power.gif');
  7476. $expected = array(
  7477. 'div' => array('class' => 'submit'),
  7478. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7479. '/div'
  7480. );
  7481. $this->assertTags($result, $expected);
  7482. }
  7483. /**
  7484. * test the create() method
  7485. *
  7486. * @return void
  7487. */
  7488. public function testCreate() {
  7489. $result = $this->Form->create('Contact');
  7490. $encoding = strtolower(Configure::read('App.encoding'));
  7491. $expected = array(
  7492. 'form' => array(
  7493. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7494. 'accept-charset' => $encoding
  7495. ),
  7496. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7497. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7498. '/div'
  7499. );
  7500. $this->assertTags($result, $expected);
  7501. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7502. $expected = array('form' => array(
  7503. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7504. 'accept-charset' => $encoding
  7505. ));
  7506. $this->assertTags($result, $expected);
  7507. $result = $this->Form->create('Contact', array('type' => 'get'));
  7508. $expected = array('form' => array(
  7509. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7510. 'accept-charset' => $encoding
  7511. ));
  7512. $this->assertTags($result, $expected);
  7513. $result = $this->Form->create('Contact', array('type' => 'put'));
  7514. $expected = array(
  7515. 'form' => array(
  7516. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7517. 'accept-charset' => $encoding
  7518. ),
  7519. 'div' => array('style' => 'display:none;'),
  7520. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7521. '/div'
  7522. );
  7523. $this->assertTags($result, $expected);
  7524. $result = $this->Form->create('Contact', array('type' => 'file'));
  7525. $expected = array(
  7526. 'form' => array(
  7527. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7528. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7529. ),
  7530. 'div' => array('style' => 'display:none;'),
  7531. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7532. '/div'
  7533. );
  7534. $this->assertTags($result, $expected);
  7535. $this->Form->request->data['Contact']['id'] = 1;
  7536. $this->Form->request->here = '/contacts/edit/1';
  7537. $this->Form->request['action'] = 'edit';
  7538. $result = $this->Form->create('Contact');
  7539. $expected = array(
  7540. 'form' => array(
  7541. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7542. 'accept-charset' => $encoding
  7543. ),
  7544. 'div' => array('style' => 'display:none;'),
  7545. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7546. '/div'
  7547. );
  7548. $this->assertTags($result, $expected);
  7549. $this->Form->request->data['Contact']['id'] = 1;
  7550. $this->Form->request->here = '/contacts/edit/1';
  7551. $this->Form->request['action'] = 'edit';
  7552. $result = $this->Form->create('Contact', array('type' => 'file'));
  7553. $expected = array(
  7554. 'form' => array(
  7555. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7556. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7557. ),
  7558. 'div' => array('style' => 'display:none;'),
  7559. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7560. '/div'
  7561. );
  7562. $this->assertTags($result, $expected);
  7563. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7564. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7565. $expected = array(
  7566. 'form' => array(
  7567. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7568. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7569. ),
  7570. 'div' => array('style' => 'display:none;'),
  7571. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7572. '/div'
  7573. );
  7574. $this->assertTags($result, $expected);
  7575. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7576. $expected = array(
  7577. 'form' => array(
  7578. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7579. 'accept-charset' => $encoding
  7580. ),
  7581. 'div' => array('style' => 'display:none;'),
  7582. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7583. '/div'
  7584. );
  7585. $this->assertTags($result, $expected);
  7586. $this->Form->request['action'] = 'add';
  7587. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7588. $expected = array(
  7589. 'form' => array(
  7590. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7591. 'accept-charset' => $encoding
  7592. ),
  7593. 'div' => array('style' => 'display:none;'),
  7594. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7595. '/div'
  7596. );
  7597. $this->assertTags($result, $expected);
  7598. $result = $this->Form->create('User', array('action' => 'login'));
  7599. $expected = array(
  7600. 'form' => array(
  7601. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7602. 'accept-charset' => $encoding
  7603. ),
  7604. 'div' => array('style' => 'display:none;'),
  7605. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7606. '/div'
  7607. );
  7608. $this->assertTags($result, $expected);
  7609. $result = $this->Form->create('User', array('url' => '/users/login'));
  7610. $expected = array(
  7611. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7612. 'div' => array('style' => 'display:none;'),
  7613. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7614. '/div'
  7615. );
  7616. $this->assertTags($result, $expected);
  7617. $this->Form->request['controller'] = 'pages';
  7618. $result = $this->Form->create('User', array('action' => 'signup'));
  7619. $expected = array(
  7620. 'form' => array(
  7621. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7622. 'accept-charset' => $encoding
  7623. ),
  7624. 'div' => array('style' => 'display:none;'),
  7625. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7626. '/div'
  7627. );
  7628. $this->assertTags($result, $expected);
  7629. $this->Form->request->data = array();
  7630. $this->Form->request['controller'] = 'contacts';
  7631. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7632. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7633. $expected = array(
  7634. 'form' => array(
  7635. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7636. 'accept-charset' => 'utf-8'
  7637. ),
  7638. 'div' => array('style' => 'display:none;'),
  7639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7640. '/div'
  7641. );
  7642. $this->assertTags($result, $expected);
  7643. }
  7644. /**
  7645. * Test the onsubmit option for create()
  7646. *
  7647. * @return void
  7648. */
  7649. public function testCreateOnSubmit() {
  7650. $this->Form->request->data = array();
  7651. $this->Form->request['controller'] = 'contacts';
  7652. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7653. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7654. $expected = array(
  7655. 'form' => array(
  7656. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7657. 'accept-charset' => 'utf-8'
  7658. ),
  7659. 'div' => array('style' => 'display:none;'),
  7660. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7661. '/div'
  7662. );
  7663. $this->assertTags($result, $expected);
  7664. $this->Form->request->data = array();
  7665. $this->Form->request['controller'] = 'contacts';
  7666. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7667. $result = $this->Form->create(array(
  7668. 'url' => array('action' => 'index', 'param'),
  7669. 'default' => false,
  7670. 'onsubmit' => 'someFunction();'
  7671. ));
  7672. $expected = array(
  7673. 'form' => array(
  7674. 'id' => 'ContactAddForm', 'method' => 'post',
  7675. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7676. 'action' => '/contacts/index/param',
  7677. 'accept-charset' => 'utf-8'
  7678. ),
  7679. 'div' => array('style' => 'display:none;'),
  7680. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7681. '/div'
  7682. );
  7683. $this->assertTags($result, $expected);
  7684. }
  7685. /**
  7686. * test create() with automatic url generation
  7687. *
  7688. * @return void
  7689. */
  7690. public function testCreateAutoUrl() {
  7691. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7692. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7693. $this->Form->request->base = '/base_url';
  7694. $result = $this->Form->create('Contact');
  7695. $expected = array(
  7696. 'form' => array(
  7697. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7698. 'accept-charset' => 'utf-8'
  7699. ),
  7700. 'div' => array('style' => 'display:none;'),
  7701. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7702. '/div'
  7703. );
  7704. $this->assertTags($result, $expected);
  7705. $this->Form->request['action'] = 'delete';
  7706. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7707. $this->Form->request->base = '/base_url';
  7708. $result = $this->Form->create('Contact');
  7709. $expected = array(
  7710. 'form' => array(
  7711. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7712. 'accept-charset' => 'utf-8'
  7713. ),
  7714. 'div' => array('style' => 'display:none;'),
  7715. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7716. '/div'
  7717. );
  7718. $this->assertTags($result, $expected);
  7719. }
  7720. /**
  7721. * test create() with a custom route
  7722. *
  7723. * @return void
  7724. */
  7725. public function testCreateCustomRoute() {
  7726. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7727. $encoding = strtolower(Configure::read('App.encoding'));
  7728. $result = $this->Form->create('User', array('action' => 'login'));
  7729. $expected = array(
  7730. 'form' => array(
  7731. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7732. 'accept-charset' => $encoding
  7733. ),
  7734. 'div' => array('style' => 'display:none;'),
  7735. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7736. '/div'
  7737. );
  7738. $this->assertTags($result, $expected);
  7739. }
  7740. /**
  7741. * test that inputDefaults are stored and used.
  7742. *
  7743. * @return void
  7744. */
  7745. public function testCreateWithInputDefaults() {
  7746. $this->Form->create('User', array(
  7747. 'inputDefaults' => array(
  7748. 'div' => false,
  7749. 'label' => false,
  7750. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7751. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7752. )
  7753. ));
  7754. $result = $this->Form->input('username');
  7755. $expected = array(
  7756. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7757. );
  7758. $this->assertTags($result, $expected);
  7759. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7760. $expected = array(
  7761. 'div' => array('class' => 'input text'),
  7762. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7763. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7764. '/div'
  7765. );
  7766. $this->assertTags($result, $expected);
  7767. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7768. $expected = array(
  7769. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7770. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7771. );
  7772. $this->assertTags($result, $expected);
  7773. $this->Form->create('User', array(
  7774. 'inputDefaults' => array(
  7775. 'div' => false,
  7776. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7777. )
  7778. ));
  7779. $result = $this->Form->input('username', array('div' => true));
  7780. $expected = array(
  7781. 'div' => array('class' => 'input text'),
  7782. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7783. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7784. '/div'
  7785. );
  7786. $this->assertTags($result, $expected);
  7787. }
  7788. /**
  7789. * test automatic accept-charset overriding
  7790. *
  7791. * @return void
  7792. */
  7793. public function testCreateWithAcceptCharset() {
  7794. $result = $this->Form->create('UserForm', array(
  7795. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7796. )
  7797. );
  7798. $expected = array(
  7799. 'form' => array(
  7800. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7801. 'accept-charset' => 'iso-8859-1'
  7802. ),
  7803. 'div' => array('style' => 'display:none;'),
  7804. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7805. '/div'
  7806. );
  7807. $this->assertTags($result, $expected);
  7808. }
  7809. /**
  7810. * Test base form URL when url param is passed with multiple parameters (&)
  7811. *
  7812. * @return void
  7813. */
  7814. public function testCreateQuerystringrequest() {
  7815. $encoding = strtolower(Configure::read('App.encoding'));
  7816. $result = $this->Form->create('Contact', array(
  7817. 'type' => 'post',
  7818. 'escape' => false,
  7819. 'url' => array(
  7820. 'controller' => 'controller',
  7821. 'action' => 'action',
  7822. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7823. )
  7824. ));
  7825. $expected = array(
  7826. 'form' => array(
  7827. 'id' => 'ContactAddForm',
  7828. 'method' => 'post',
  7829. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7830. 'accept-charset' => $encoding
  7831. ),
  7832. 'div' => array('style' => 'display:none;'),
  7833. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7834. '/div'
  7835. );
  7836. $this->assertTags($result, $expected);
  7837. $result = $this->Form->create('Contact', array(
  7838. 'type' => 'post',
  7839. 'url' => array(
  7840. 'controller' => 'controller',
  7841. 'action' => 'action',
  7842. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7843. )
  7844. ));
  7845. $expected = array(
  7846. 'form' => array(
  7847. 'id' => 'ContactAddForm',
  7848. 'method' => 'post',
  7849. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7850. 'accept-charset' => $encoding
  7851. ),
  7852. 'div' => array('style' => 'display:none;'),
  7853. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7854. '/div'
  7855. );
  7856. $this->assertTags($result, $expected);
  7857. }
  7858. /**
  7859. * test that create() doesn't cause errors by multiple id's being in the primary key
  7860. * as could happen with multiple select or checkboxes.
  7861. *
  7862. * @return void
  7863. */
  7864. public function testCreateWithMultipleIdInData() {
  7865. $encoding = strtolower(Configure::read('App.encoding'));
  7866. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7867. $result = $this->Form->create('Contact');
  7868. $expected = array(
  7869. 'form' => array(
  7870. 'id' => 'ContactAddForm',
  7871. 'method' => 'post',
  7872. 'action' => '/contacts/add',
  7873. 'accept-charset' => $encoding
  7874. ),
  7875. 'div' => array('style' => 'display:none;'),
  7876. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7877. '/div'
  7878. );
  7879. $this->assertTags($result, $expected);
  7880. }
  7881. /**
  7882. * test that create() doesn't add in extra passed params.
  7883. *
  7884. * @return void
  7885. */
  7886. public function testCreatePassedArgs() {
  7887. $encoding = strtolower(Configure::read('App.encoding'));
  7888. $this->Form->request->data['Contact']['id'] = 1;
  7889. $result = $this->Form->create('Contact', array(
  7890. 'type' => 'post',
  7891. 'escape' => false,
  7892. 'url' => array(
  7893. 'action' => 'edit',
  7894. 'myparam'
  7895. )
  7896. ));
  7897. $expected = array(
  7898. 'form' => array(
  7899. 'id' => 'ContactAddForm',
  7900. 'method' => 'post',
  7901. 'action' => '/contacts/edit/myparam',
  7902. 'accept-charset' => $encoding
  7903. ),
  7904. 'div' => array('style' => 'display:none;'),
  7905. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7906. '/div'
  7907. );
  7908. $this->assertTags($result, $expected);
  7909. }
  7910. /**
  7911. * test that create() works without raising errors with a Mock Model
  7912. *
  7913. * @return void
  7914. */
  7915. public function testCreateNoErrorsWithMockModel() {
  7916. $encoding = strtolower(Configure::read('App.encoding'));
  7917. $ContactMock = $this->getMockBuilder('Contact')
  7918. ->disableOriginalConstructor()
  7919. ->getMock();
  7920. ClassRegistry::removeObject('Contact');
  7921. ClassRegistry::addObject('Contact', $ContactMock);
  7922. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7923. $expected = array('form' => array(
  7924. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7925. 'accept-charset' => $encoding
  7926. ));
  7927. $this->assertTags($result, $expected);
  7928. }
  7929. /**
  7930. * test creating a get form, and get form inputs.
  7931. *
  7932. * @return void
  7933. */
  7934. public function testGetFormCreate() {
  7935. $encoding = strtolower(Configure::read('App.encoding'));
  7936. $result = $this->Form->create('Contact', array('type' => 'get'));
  7937. $this->assertTags($result, array('form' => array(
  7938. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7939. 'accept-charset' => $encoding
  7940. )));
  7941. $result = $this->Form->text('Contact.name');
  7942. $this->assertTags($result, array('input' => array(
  7943. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7944. )));
  7945. $result = $this->Form->password('password');
  7946. $this->assertTags($result, array('input' => array(
  7947. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7948. )));
  7949. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7950. $result = $this->Form->text('user_form');
  7951. $this->assertTags($result, array('input' => array(
  7952. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7953. )));
  7954. }
  7955. /**
  7956. * test get form, and inputs when the model param is false
  7957. *
  7958. * @return void
  7959. */
  7960. public function testGetFormWithFalseModel() {
  7961. $encoding = strtolower(Configure::read('App.encoding'));
  7962. $this->Form->request['controller'] = 'contact_test';
  7963. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7964. $expected = array('form' => array(
  7965. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7966. 'accept-charset' => $encoding
  7967. ));
  7968. $this->assertTags($result, $expected);
  7969. $result = $this->Form->text('reason');
  7970. $expected = array(
  7971. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7972. );
  7973. $this->assertTags($result, $expected);
  7974. }
  7975. /**
  7976. * test that datetime() works with GET style forms.
  7977. *
  7978. * @return void
  7979. */
  7980. public function testDateTimeWithGetForms() {
  7981. extract($this->dateRegex);
  7982. $this->Form->create('Contact', array('type' => 'get'));
  7983. $result = $this->Form->datetime('created');
  7984. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7985. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7986. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7987. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7988. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7989. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7990. }
  7991. /**
  7992. * testEditFormWithData method
  7993. *
  7994. * test auto populating form elements from submitted data.
  7995. *
  7996. * @return void
  7997. */
  7998. public function testEditFormWithData() {
  7999. $this->Form->request->data = array('Person' => array(
  8000. 'id' => 1,
  8001. 'first_name' => 'Nate',
  8002. 'last_name' => 'Abele',
  8003. 'email' => 'nate@example.com'
  8004. ));
  8005. $this->Form->request->addParams(array(
  8006. 'models' => array('Person'),
  8007. 'controller' => 'people',
  8008. 'action' => 'add'
  8009. ));
  8010. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  8011. $this->Form->create();
  8012. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  8013. $expected = array(
  8014. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  8015. 'select' => array(
  8016. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  8017. ),
  8018. array('option' => array('value' => 1)), 'Nate', '/option',
  8019. array('option' => array('value' => 2)), 'Garrett', '/option',
  8020. array('option' => array('value' => 3)), 'Larry', '/option',
  8021. '/select'
  8022. );
  8023. $this->assertTags($result, $expected);
  8024. }
  8025. /**
  8026. * Test that required fields are created for various types of validation.
  8027. *
  8028. * @return void
  8029. */
  8030. public function testFormInputRequiredDetection() {
  8031. $this->Form->create('Contact');
  8032. $result = $this->Form->input('Contact.non_existing');
  8033. $expected = array(
  8034. 'div' => array('class' => 'input text'),
  8035. 'label' => array('for' => 'ContactNonExisting'),
  8036. 'Non Existing',
  8037. '/label',
  8038. 'input' => array(
  8039. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  8040. 'id' => 'ContactNonExisting'
  8041. ),
  8042. '/div'
  8043. );
  8044. $this->assertTags($result, $expected);
  8045. $result = $this->Form->input('Contact.imrequired');
  8046. $expected = array(
  8047. 'div' => array('class' => 'input text required'),
  8048. 'label' => array('for' => 'ContactImrequired'),
  8049. 'Imrequired',
  8050. '/label',
  8051. 'input' => array(
  8052. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8053. 'id' => 'ContactImrequired',
  8054. 'required' => 'required'
  8055. ),
  8056. '/div'
  8057. );
  8058. $this->assertTags($result, $expected);
  8059. $result = $this->Form->input('Contact.imalsorequired');
  8060. $expected = array(
  8061. 'div' => array('class' => 'input text required'),
  8062. 'label' => array('for' => 'ContactImalsorequired'),
  8063. 'Imalsorequired',
  8064. '/label',
  8065. 'input' => array(
  8066. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  8067. 'id' => 'ContactImalsorequired',
  8068. 'required' => 'required'
  8069. ),
  8070. '/div'
  8071. );
  8072. $this->assertTags($result, $expected);
  8073. $result = $this->Form->input('Contact.imrequiredtoo');
  8074. $expected = array(
  8075. 'div' => array('class' => 'input text required'),
  8076. 'label' => array('for' => 'ContactImrequiredtoo'),
  8077. 'Imrequiredtoo',
  8078. '/label',
  8079. 'input' => array(
  8080. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  8081. 'id' => 'ContactImrequiredtoo',
  8082. 'required' => 'required'
  8083. ),
  8084. '/div'
  8085. );
  8086. $this->assertTags($result, $expected);
  8087. $result = $this->Form->input('Contact.required_one');
  8088. $expected = array(
  8089. 'div' => array('class' => 'input text required'),
  8090. 'label' => array('for' => 'ContactRequiredOne'),
  8091. 'Required One',
  8092. '/label',
  8093. 'input' => array(
  8094. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  8095. 'id' => 'ContactRequiredOne',
  8096. 'required' => 'required'
  8097. ),
  8098. '/div'
  8099. );
  8100. $this->assertTags($result, $expected);
  8101. $result = $this->Form->input('Contact.string_required');
  8102. $expected = array(
  8103. 'div' => array('class' => 'input text required'),
  8104. 'label' => array('for' => 'ContactStringRequired'),
  8105. 'String Required',
  8106. '/label',
  8107. 'input' => array(
  8108. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  8109. 'id' => 'ContactStringRequired',
  8110. 'required' => 'required'
  8111. ),
  8112. '/div'
  8113. );
  8114. $this->assertTags($result, $expected);
  8115. $result = $this->Form->input('Contact.imnotrequired');
  8116. $expected = array(
  8117. 'div' => array('class' => 'input text'),
  8118. 'label' => array('for' => 'ContactImnotrequired'),
  8119. 'Imnotrequired',
  8120. '/label',
  8121. 'input' => array(
  8122. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  8123. 'id' => 'ContactImnotrequired'
  8124. ),
  8125. '/div'
  8126. );
  8127. $this->assertTags($result, $expected);
  8128. $result = $this->Form->input('Contact.imalsonotrequired');
  8129. $expected = array(
  8130. 'div' => array('class' => 'input text'),
  8131. 'label' => array('for' => 'ContactImalsonotrequired'),
  8132. 'Imalsonotrequired',
  8133. '/label',
  8134. 'input' => array(
  8135. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  8136. 'id' => 'ContactImalsonotrequired'
  8137. ),
  8138. '/div'
  8139. );
  8140. $this->assertTags($result, $expected);
  8141. $result = $this->Form->input('Contact.imalsonotrequired2');
  8142. $expected = array(
  8143. 'div' => array('class' => 'input text'),
  8144. 'label' => array('for' => 'ContactImalsonotrequired2'),
  8145. 'Imalsonotrequired2',
  8146. '/label',
  8147. 'input' => array(
  8148. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  8149. 'id' => 'ContactImalsonotrequired2'
  8150. ),
  8151. '/div'
  8152. );
  8153. $this->assertTags($result, $expected);
  8154. $result = $this->Form->input('Contact.imnotrequiredeither');
  8155. $expected = array(
  8156. 'div' => array('class' => 'input text'),
  8157. 'label' => array('for' => 'ContactImnotrequiredeither'),
  8158. 'Imnotrequiredeither',
  8159. '/label',
  8160. 'input' => array(
  8161. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  8162. 'id' => 'ContactImnotrequiredeither'
  8163. ),
  8164. '/div'
  8165. );
  8166. $this->assertTags($result, $expected);
  8167. $result = $this->Form->input('Contact.iamrequiredalways');
  8168. $expected = array(
  8169. 'div' => array('class' => 'input text required'),
  8170. 'label' => array('for' => 'ContactIamrequiredalways'),
  8171. 'Iamrequiredalways',
  8172. '/label',
  8173. 'input' => array(
  8174. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  8175. 'id' => 'ContactIamrequiredalways',
  8176. 'required' => 'required'
  8177. ),
  8178. '/div'
  8179. );
  8180. $this->assertTags($result, $expected);
  8181. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  8182. $expected = array(
  8183. 'div' => array('class' => 'input checkbox required'),
  8184. array('input' => array(
  8185. 'type' => 'hidden',
  8186. 'name' => 'data[Contact][boolean_field]',
  8187. 'id' => 'ContactBooleanField_',
  8188. 'value' => '0'
  8189. )),
  8190. array('input' => array(
  8191. 'type' => 'checkbox',
  8192. 'name' => 'data[Contact][boolean_field]',
  8193. 'value' => '1',
  8194. 'id' => 'ContactBooleanField'
  8195. )),
  8196. 'label' => array('for' => 'ContactBooleanField'),
  8197. 'Boolean Field',
  8198. '/label',
  8199. '/div'
  8200. );
  8201. $this->assertTags($result, $expected);
  8202. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  8203. $expected = array(
  8204. 'div' => array('class' => 'input checkbox required'),
  8205. array('input' => array(
  8206. 'type' => 'hidden',
  8207. 'name' => 'data[Contact][boolean_field]',
  8208. 'id' => 'ContactBooleanField_',
  8209. 'value' => '0'
  8210. )),
  8211. array('input' => array(
  8212. 'type' => 'checkbox',
  8213. 'name' => 'data[Contact][boolean_field]',
  8214. 'value' => '1',
  8215. 'id' => 'ContactBooleanField',
  8216. 'required' => 'required'
  8217. )),
  8218. 'label' => array('for' => 'ContactBooleanField'),
  8219. 'Boolean Field',
  8220. '/label',
  8221. '/div'
  8222. );
  8223. $this->assertTags($result, $expected);
  8224. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  8225. $expected = array(
  8226. 'div' => array('class' => 'input file required'),
  8227. 'label' => array('for' => 'ContactIamrequiredalways'),
  8228. 'Iamrequiredalways',
  8229. '/label',
  8230. 'input' => array(
  8231. 'type' => 'file',
  8232. 'name' => 'data[Contact][iamrequiredalways]',
  8233. 'id' => 'ContactIamrequiredalways',
  8234. 'required' => 'required'
  8235. ),
  8236. '/div'
  8237. );
  8238. $this->assertTags($result, $expected);
  8239. }
  8240. /**
  8241. * Test that required fields are created when only using ModelValidator::add().
  8242. *
  8243. * @return void
  8244. */
  8245. public function testFormInputRequiredDetectionModelValidator() {
  8246. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  8247. $this->Form->create('ContactTag');
  8248. $result = $this->Form->input('ContactTag.iwillberequired');
  8249. $expected = array(
  8250. 'div' => array('class' => 'input text required'),
  8251. 'label' => array('for' => 'ContactTagIwillberequired'),
  8252. 'Iwillberequired',
  8253. '/label',
  8254. 'input' => array(
  8255. 'name' => 'data[ContactTag][iwillberequired]',
  8256. 'type' => 'text',
  8257. 'id' => 'ContactTagIwillberequired',
  8258. 'required' => 'required'
  8259. ),
  8260. '/div'
  8261. );
  8262. $this->assertTags($result, $expected);
  8263. }
  8264. /**
  8265. * testFormMagicInput method
  8266. *
  8267. * @return void
  8268. */
  8269. public function testFormMagicInput() {
  8270. $encoding = strtolower(Configure::read('App.encoding'));
  8271. $result = $this->Form->create('Contact');
  8272. $expected = array(
  8273. 'form' => array(
  8274. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8275. 'accept-charset' => $encoding
  8276. ),
  8277. 'div' => array('style' => 'display:none;'),
  8278. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8279. '/div'
  8280. );
  8281. $this->assertTags($result, $expected);
  8282. $result = $this->Form->input('name');
  8283. $expected = array(
  8284. 'div' => array('class' => 'input text'),
  8285. 'label' => array('for' => 'ContactName'),
  8286. 'Name',
  8287. '/label',
  8288. 'input' => array(
  8289. 'type' => 'text', 'name' => 'data[Contact][name]',
  8290. 'id' => 'ContactName', 'maxlength' => '255'
  8291. ),
  8292. '/div'
  8293. );
  8294. $this->assertTags($result, $expected);
  8295. $result = $this->Form->input('non_existing_field_in_contact_model');
  8296. $expected = array(
  8297. 'div' => array('class' => 'input text'),
  8298. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  8299. 'Non Existing Field In Contact Model',
  8300. '/label',
  8301. 'input' => array(
  8302. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  8303. 'id' => 'ContactNonExistingFieldInContactModel'
  8304. ),
  8305. '/div'
  8306. );
  8307. $this->assertTags($result, $expected);
  8308. $result = $this->Form->input('Address.street');
  8309. $expected = array(
  8310. 'div' => array('class' => 'input text'),
  8311. 'label' => array('for' => 'AddressStreet'),
  8312. 'Street',
  8313. '/label',
  8314. 'input' => array(
  8315. 'type' => 'text', 'name' => 'data[Address][street]',
  8316. 'id' => 'AddressStreet'
  8317. ),
  8318. '/div'
  8319. );
  8320. $this->assertTags($result, $expected);
  8321. $result = $this->Form->input('Address.non_existing_field_in_model');
  8322. $expected = array(
  8323. 'div' => array('class' => 'input text'),
  8324. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  8325. 'Non Existing Field In Model',
  8326. '/label',
  8327. 'input' => array(
  8328. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  8329. 'id' => 'AddressNonExistingFieldInModel'
  8330. ),
  8331. '/div'
  8332. );
  8333. $this->assertTags($result, $expected);
  8334. $result = $this->Form->input('name', array('div' => false));
  8335. $expected = array(
  8336. 'label' => array('for' => 'ContactName'),
  8337. 'Name',
  8338. '/label',
  8339. 'input' => array(
  8340. 'type' => 'text', 'name' => 'data[Contact][name]',
  8341. 'id' => 'ContactName', 'maxlength' => '255'
  8342. )
  8343. );
  8344. $this->assertTags($result, $expected);
  8345. extract($this->dateRegex);
  8346. $now = strtotime('now');
  8347. $result = $this->Form->input('Contact.published', array('div' => false));
  8348. $expected = array(
  8349. 'label' => array('for' => 'ContactPublishedMonth'),
  8350. 'Published',
  8351. '/label',
  8352. array('select' => array(
  8353. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  8354. )),
  8355. $monthsRegex,
  8356. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8357. date('F', $now),
  8358. '/option',
  8359. '*/select',
  8360. '-',
  8361. array('select' => array(
  8362. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  8363. )),
  8364. $daysRegex,
  8365. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8366. date('j', $now),
  8367. '/option',
  8368. '*/select',
  8369. '-',
  8370. array('select' => array(
  8371. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  8372. )),
  8373. $yearsRegex,
  8374. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8375. date('Y', $now),
  8376. '*/select'
  8377. );
  8378. $this->assertTags($result, $expected);
  8379. $result = $this->Form->input('Contact.updated', array('div' => false));
  8380. $expected = array(
  8381. 'label' => array('for' => 'ContactUpdatedMonth'),
  8382. 'Updated',
  8383. '/label',
  8384. array('select' => array(
  8385. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  8386. )),
  8387. $monthsRegex,
  8388. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8389. date('F', $now),
  8390. '/option',
  8391. '*/select',
  8392. '-',
  8393. array('select' => array(
  8394. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  8395. )),
  8396. $daysRegex,
  8397. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8398. date('j', $now),
  8399. '/option',
  8400. '*/select',
  8401. '-',
  8402. array('select' => array(
  8403. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  8404. )),
  8405. $yearsRegex,
  8406. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8407. date('Y', $now),
  8408. '*/select'
  8409. );
  8410. $this->assertTags($result, $expected);
  8411. $result = $this->Form->input('UserForm.stuff');
  8412. $expected = array(
  8413. 'div' => array('class' => 'input text'),
  8414. 'label' => array('for' => 'UserFormStuff'),
  8415. 'Stuff',
  8416. '/label',
  8417. 'input' => array(
  8418. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8419. 'id' => 'UserFormStuff', 'maxlength' => 10
  8420. ),
  8421. '/div'
  8422. );
  8423. $this->assertTags($result, $expected);
  8424. }
  8425. /**
  8426. * testForMagicInputNonExistingNorValidated method
  8427. *
  8428. * @return void
  8429. */
  8430. public function testForMagicInputNonExistingNorValidated() {
  8431. $encoding = strtolower(Configure::read('App.encoding'));
  8432. $result = $this->Form->create('Contact');
  8433. $expected = array(
  8434. 'form' => array(
  8435. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8436. 'accept-charset' => $encoding
  8437. ),
  8438. 'div' => array('style' => 'display:none;'),
  8439. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8440. '/div'
  8441. );
  8442. $this->assertTags($result, $expected);
  8443. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8444. $expected = array(
  8445. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8446. 'Non Existing Nor Validated',
  8447. '/label',
  8448. 'input' => array(
  8449. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8450. 'id' => 'ContactNonExistingNorValidated'
  8451. )
  8452. );
  8453. $this->assertTags($result, $expected);
  8454. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8455. 'div' => false, 'value' => 'my value'
  8456. ));
  8457. $expected = array(
  8458. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8459. 'Non Existing Nor Validated',
  8460. '/label',
  8461. 'input' => array(
  8462. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8463. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8464. )
  8465. );
  8466. $this->assertTags($result, $expected);
  8467. $this->Form->request->data = array(
  8468. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8469. ));
  8470. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8471. $expected = array(
  8472. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8473. 'Non Existing Nor Validated',
  8474. '/label',
  8475. 'input' => array(
  8476. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8477. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8478. )
  8479. );
  8480. $this->assertTags($result, $expected);
  8481. }
  8482. /**
  8483. * testFormMagicInputLabel method
  8484. *
  8485. * @return void
  8486. */
  8487. public function testFormMagicInputLabel() {
  8488. $encoding = strtolower(Configure::read('App.encoding'));
  8489. $result = $this->Form->create('Contact');
  8490. $expected = array(
  8491. 'form' => array(
  8492. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8493. 'accept-charset' => $encoding
  8494. ),
  8495. 'div' => array('style' => 'display:none;'),
  8496. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8497. '/div'
  8498. );
  8499. $this->assertTags($result, $expected);
  8500. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8501. $this->assertTags($result, array('input' => array(
  8502. 'name' => 'data[Contact][name]', 'type' => 'text',
  8503. 'id' => 'ContactName', 'maxlength' => '255')
  8504. ));
  8505. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8506. $expected = array(
  8507. 'label' => array('for' => 'ContactName'),
  8508. 'My label',
  8509. '/label',
  8510. 'input' => array(
  8511. 'type' => 'text', 'name' => 'data[Contact][name]',
  8512. 'id' => 'ContactName', 'maxlength' => '255'
  8513. )
  8514. );
  8515. $this->assertTags($result, $expected);
  8516. $result = $this->Form->input('Contact.name', array(
  8517. 'div' => false, 'label' => array('class' => 'mandatory')
  8518. ));
  8519. $expected = array(
  8520. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8521. 'Name',
  8522. '/label',
  8523. 'input' => array(
  8524. 'type' => 'text', 'name' => 'data[Contact][name]',
  8525. 'id' => 'ContactName', 'maxlength' => '255'
  8526. )
  8527. );
  8528. $this->assertTags($result, $expected);
  8529. $result = $this->Form->input('Contact.name', array(
  8530. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8531. ));
  8532. $expected = array(
  8533. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8534. 'My label',
  8535. '/label',
  8536. 'input' => array(
  8537. 'type' => 'text', 'name' => 'data[Contact][name]',
  8538. 'id' => 'ContactName', 'maxlength' => '255'
  8539. )
  8540. );
  8541. $this->assertTags($result, $expected);
  8542. $result = $this->Form->input('Contact.name', array(
  8543. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8544. ));
  8545. $expected = array(
  8546. 'label' => array('for' => 'my_id'),
  8547. 'Name',
  8548. '/label',
  8549. 'input' => array(
  8550. 'type' => 'text', 'name' => 'data[Contact][name]',
  8551. 'id' => 'my_id', 'maxlength' => '255'
  8552. )
  8553. );
  8554. $this->assertTags($result, $expected);
  8555. $result = $this->Form->input('1.id');
  8556. $this->assertTags($result, array('input' => array(
  8557. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8558. 'id' => 'Contact1Id'
  8559. )));
  8560. $result = $this->Form->input("1.name");
  8561. $expected = array(
  8562. 'div' => array('class' => 'input text'),
  8563. 'label' => array('for' => 'Contact1Name'),
  8564. 'Name',
  8565. '/label',
  8566. 'input' => array(
  8567. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8568. 'id' => 'Contact1Name', 'maxlength' => '255'
  8569. ),
  8570. '/div'
  8571. );
  8572. $this->assertTags($result, $expected);
  8573. $result = $this->Form->input('Contact.1.id');
  8574. $this->assertTags($result, array(
  8575. 'input' => array(
  8576. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8577. 'id' => 'Contact1Id'
  8578. )
  8579. ));
  8580. $result = $this->Form->input("Model.1.name");
  8581. $expected = array(
  8582. 'div' => array('class' => 'input text'),
  8583. 'label' => array('for' => 'Model1Name'),
  8584. 'Name',
  8585. '/label',
  8586. 'input' => array(
  8587. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8588. 'id' => 'Model1Name'
  8589. ),
  8590. '/div'
  8591. );
  8592. $this->assertTags($result, $expected);
  8593. }
  8594. /**
  8595. * testFormEnd method
  8596. *
  8597. * @return void
  8598. */
  8599. public function testFormEnd() {
  8600. $this->assertEquals('</form>', $this->Form->end());
  8601. $result = $this->Form->end('', array('form' => 'form-name'));
  8602. $expected = array(
  8603. 'div' => array('class' => 'submit'),
  8604. 'input' => array('type' => 'submit', 'value' => ''),
  8605. '/div',
  8606. '/form'
  8607. );
  8608. $this->assertTags($result, $expected);
  8609. $result = $this->Form->end('');
  8610. $expected = array(
  8611. 'div' => array('class' => 'submit'),
  8612. 'input' => array('type' => 'submit', 'value' => ''),
  8613. '/div',
  8614. '/form'
  8615. );
  8616. $this->assertTags($result, $expected);
  8617. $result = $this->Form->end(array('label' => ''));
  8618. $expected = array(
  8619. 'div' => array('class' => 'submit'),
  8620. 'input' => array('type' => 'submit', 'value' => ''),
  8621. '/div',
  8622. '/form'
  8623. );
  8624. $this->assertTags($result, $expected);
  8625. $result = $this->Form->end('save');
  8626. $expected = array(
  8627. 'div' => array('class' => 'submit'),
  8628. 'input' => array('type' => 'submit', 'value' => 'save'),
  8629. '/div',
  8630. '/form'
  8631. );
  8632. $this->assertTags($result, $expected);
  8633. $result = $this->Form->end(array('label' => 'save'));
  8634. $expected = array(
  8635. 'div' => array('class' => 'submit'),
  8636. 'input' => array('type' => 'submit', 'value' => 'save'),
  8637. '/div',
  8638. '/form'
  8639. );
  8640. $this->assertTags($result, $expected);
  8641. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8642. $expected = array(
  8643. 'div' => array('class' => 'submit'),
  8644. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8645. '/div',
  8646. '/form'
  8647. );
  8648. $this->assertTags($result, $expected);
  8649. $result = $this->Form->end(array('name' => 'Whatever'));
  8650. $expected = array(
  8651. 'div' => array('class' => 'submit'),
  8652. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8653. '/div',
  8654. '/form'
  8655. );
  8656. $this->assertTags($result, $expected);
  8657. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8658. $expected = array(
  8659. 'div' => array('class' => 'good'),
  8660. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8661. '/div',
  8662. '/form'
  8663. );
  8664. $this->assertTags($result, $expected);
  8665. $result = $this->Form->end(array(
  8666. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8667. ));
  8668. $expected = array(
  8669. 'div' => array('class' => 'good'),
  8670. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8671. '/div',
  8672. '/form'
  8673. );
  8674. $this->assertTags($result, $expected);
  8675. }
  8676. /**
  8677. * testMultipleFormWithIdFields method
  8678. *
  8679. * @return void
  8680. */
  8681. public function testMultipleFormWithIdFields() {
  8682. $this->Form->create('UserForm');
  8683. $result = $this->Form->input('id');
  8684. $this->assertTags($result, array('input' => array(
  8685. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8686. )));
  8687. $result = $this->Form->input('ValidateItem.id');
  8688. $this->assertTags($result, array('input' => array(
  8689. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8690. 'id' => 'ValidateItemId'
  8691. )));
  8692. $result = $this->Form->input('ValidateUser.id');
  8693. $this->assertTags($result, array('input' => array(
  8694. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8695. 'id' => 'ValidateUserId'
  8696. )));
  8697. }
  8698. /**
  8699. * testDbLessModel method
  8700. *
  8701. * @return void
  8702. */
  8703. public function testDbLessModel() {
  8704. $this->Form->create('TestMail');
  8705. $result = $this->Form->input('name');
  8706. $expected = array(
  8707. 'div' => array('class' => 'input text'),
  8708. 'label' => array('for' => 'TestMailName'),
  8709. 'Name',
  8710. '/label',
  8711. 'input' => array(
  8712. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8713. 'id' => 'TestMailName'
  8714. ),
  8715. '/div'
  8716. );
  8717. $this->assertTags($result, $expected);
  8718. ClassRegistry::init('TestMail');
  8719. $this->Form->create('TestMail');
  8720. $result = $this->Form->input('name');
  8721. $expected = array(
  8722. 'div' => array('class' => 'input text'),
  8723. 'label' => array('for' => 'TestMailName'),
  8724. 'Name',
  8725. '/label',
  8726. 'input' => array(
  8727. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8728. 'id' => 'TestMailName'
  8729. ),
  8730. '/div'
  8731. );
  8732. $this->assertTags($result, $expected);
  8733. }
  8734. /**
  8735. * testBrokenness method
  8736. *
  8737. * @return void
  8738. */
  8739. public function testBrokenness() {
  8740. /*
  8741. * #4 This test has two parents and four children. By default (as of r7117) both
  8742. * parents are show but the first parent is missing a child. This is the inconsistency
  8743. * in the default behaviour - one parent has all children, the other does not - dependent
  8744. * on the data values.
  8745. */
  8746. $result = $this->Form->select('Model.field', array(
  8747. 'Fred' => array(
  8748. 'freds_son_1' => 'Fred',
  8749. 'freds_son_2' => 'Freddie'
  8750. ),
  8751. 'Bert' => array(
  8752. 'berts_son_1' => 'Albert',
  8753. 'berts_son_2' => 'Bertie')
  8754. ),
  8755. array('showParents' => true, 'empty' => false)
  8756. );
  8757. $expected = array(
  8758. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8759. array('optgroup' => array('label' => 'Fred')),
  8760. array('option' => array('value' => 'freds_son_1')),
  8761. 'Fred',
  8762. '/option',
  8763. array('option' => array('value' => 'freds_son_2')),
  8764. 'Freddie',
  8765. '/option',
  8766. '/optgroup',
  8767. array('optgroup' => array('label' => 'Bert')),
  8768. array('option' => array('value' => 'berts_son_1')),
  8769. 'Albert',
  8770. '/option',
  8771. array('option' => array('value' => 'berts_son_2')),
  8772. 'Bertie',
  8773. '/option',
  8774. '/optgroup',
  8775. '/select'
  8776. );
  8777. $this->assertTags($result, $expected);
  8778. /*
  8779. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8780. * changed, so we should expect the same select list data, just with a different name
  8781. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8782. * This is where data corruption can occur, because when a select value is missing from
  8783. * a list a form will substitute the first value in the list - without the user knowing.
  8784. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8785. * affect the availability of 3 => 'Three' as a valid option.
  8786. */
  8787. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8788. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8789. ));
  8790. $result = $this->Form->select(
  8791. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8792. );
  8793. $expected = array(
  8794. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8795. array('option' => array('value' => 1)),
  8796. 'One',
  8797. '/option',
  8798. array('option' => array('value' => 2)),
  8799. 'Two',
  8800. '/option',
  8801. array('optgroup' => array('label' => 'Three')),
  8802. array('option' => array('value' => 3)),
  8803. 'Three',
  8804. '/option',
  8805. array('option' => array('value' => 4)),
  8806. 'Four',
  8807. '/option',
  8808. array('option' => array('value' => 5)),
  8809. 'Five',
  8810. '/option',
  8811. '/optgroup',
  8812. '/select'
  8813. );
  8814. $this->assertTags($result, $expected);
  8815. }
  8816. /**
  8817. * Test the generation of fields for a multi record form.
  8818. *
  8819. * @return void
  8820. */
  8821. public function testMultiRecordForm() {
  8822. $this->Form->create('ValidateProfile');
  8823. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8824. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8825. $expected = array(
  8826. 'div' => array('class' => 'input textarea'),
  8827. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8828. 'Name',
  8829. '/label',
  8830. 'textarea' => array(
  8831. 'id' => 'ValidateProfile1ValidateItem2Name',
  8832. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8833. 'cols' => 30,
  8834. 'rows' => 6
  8835. ),
  8836. 'Value',
  8837. '/textarea',
  8838. '/div'
  8839. );
  8840. $this->assertTags($result, $expected);
  8841. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8842. $expected = array(
  8843. 'div' => array('class' => 'input date'),
  8844. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8845. 'Created',
  8846. '/label',
  8847. array('select' => array(
  8848. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8849. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8850. )
  8851. ),
  8852. array('option' => array('value' => '')), '/option',
  8853. $this->dateRegex['monthsRegex'],
  8854. '/select', '-',
  8855. array('select' => array(
  8856. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8857. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8858. )
  8859. ),
  8860. array('option' => array('value' => '')), '/option',
  8861. $this->dateRegex['daysRegex'],
  8862. '/select', '-',
  8863. array('select' => array(
  8864. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8865. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8866. )
  8867. ),
  8868. array('option' => array('value' => '')), '/option',
  8869. $this->dateRegex['yearsRegex'],
  8870. '/select',
  8871. '/div'
  8872. );
  8873. $this->assertTags($result, $expected);
  8874. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8875. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8876. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8877. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8878. $expected = array(
  8879. 'div' => array('class' => 'input select error'),
  8880. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8881. 'Profile',
  8882. '/label',
  8883. 'select' => array(
  8884. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8885. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8886. 'class' => 'form-error'
  8887. ),
  8888. '/select',
  8889. array('div' => array('class' => 'error-message')),
  8890. 'Error',
  8891. '/div',
  8892. '/div'
  8893. );
  8894. $this->assertTags($result, $expected);
  8895. }
  8896. /**
  8897. * test the correct display of multi-record form validation errors.
  8898. *
  8899. * @return void
  8900. */
  8901. public function testMultiRecordFormValidationErrors() {
  8902. $this->Form->create('ValidateProfile');
  8903. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8904. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8905. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8906. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8907. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8908. $result = $this->Form->error('ValidateProfile.2.city');
  8909. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8910. $result = $this->Form->error('2.city');
  8911. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8912. }
  8913. /**
  8914. * test the correct display of multi-record form validation errors.
  8915. *
  8916. * @return void
  8917. */
  8918. public function testSaveManyRecordFormValidationErrors() {
  8919. $this->Form->create('ValidateUser');
  8920. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8921. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8922. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8923. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8924. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8925. $result = $this->Form->error('ValidateUser.0.city');
  8926. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8927. }
  8928. /**
  8929. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8930. *
  8931. * @return void
  8932. */
  8933. public function testInputTemplate() {
  8934. $result = $this->Form->input('Contact.email', array(
  8935. 'type' => 'text', 'format' => array('input')
  8936. ));
  8937. $expected = array(
  8938. 'div' => array('class' => 'input text'),
  8939. 'input' => array(
  8940. 'type' => 'text', 'name' => 'data[Contact][email]',
  8941. 'id' => 'ContactEmail'
  8942. ),
  8943. '/div'
  8944. );
  8945. $this->assertTags($result, $expected);
  8946. $result = $this->Form->input('Contact.email', array(
  8947. 'type' => 'text', 'format' => array('input', 'label'),
  8948. 'label' => '<em>Email (required)</em>'
  8949. ));
  8950. $expected = array(
  8951. 'div' => array('class' => 'input text'),
  8952. array('input' => array(
  8953. 'type' => 'text', 'name' => 'data[Contact][email]',
  8954. 'id' => 'ContactEmail'
  8955. )),
  8956. 'label' => array('for' => 'ContactEmail'),
  8957. 'em' => array(),
  8958. 'Email (required)',
  8959. '/em',
  8960. '/label',
  8961. '/div'
  8962. );
  8963. $this->assertTags($result, $expected);
  8964. $result = $this->Form->input('Contact.email', array(
  8965. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8966. 'between' => '<div>Something in the middle</div>',
  8967. 'after' => '<span>Some text at the end</span>'
  8968. ));
  8969. $expected = array(
  8970. 'div' => array('class' => 'input text'),
  8971. array('input' => array(
  8972. 'type' => 'text', 'name' => 'data[Contact][email]',
  8973. 'id' => 'ContactEmail'
  8974. )),
  8975. array('div' => array()),
  8976. 'Something in the middle',
  8977. '/div',
  8978. 'label' => array('for' => 'ContactEmail'),
  8979. 'Email',
  8980. '/label',
  8981. 'span' => array(),
  8982. 'Some text at the end',
  8983. '/span',
  8984. '/div'
  8985. );
  8986. $this->assertTags($result, $expected);
  8987. $result = $this->Form->input('Contact.method', array(
  8988. 'type' => 'radio',
  8989. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8990. 'between' => 'I am between',
  8991. ));
  8992. $expected = array(
  8993. 'div' => array('class' => 'input radio'),
  8994. 'fieldset' => array(),
  8995. 'legend' => array(),
  8996. 'Method',
  8997. '/legend',
  8998. 'I am between',
  8999. 'input' => array(
  9000. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  9001. 'value' => '', 'id' => 'ContactMethod_'
  9002. ),
  9003. array('input' => array(
  9004. 'type' => 'radio', 'name' => 'data[Contact][method]',
  9005. 'value' => 'email', 'id' => 'ContactMethodEmail'
  9006. )),
  9007. array('label' => array('for' => 'ContactMethodEmail')),
  9008. 'Email',
  9009. '/label',
  9010. array('input' => array(
  9011. 'type' => 'radio', 'name' => 'data[Contact][method]',
  9012. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  9013. )),
  9014. array('label' => array('for' => 'ContactMethodPigeon')),
  9015. 'Pigeon',
  9016. '/label',
  9017. '/fieldset',
  9018. '/div',
  9019. );
  9020. $this->assertTags($result, $expected);
  9021. }
  9022. /**
  9023. * test that some html5 inputs + FormHelper::__call() work
  9024. *
  9025. * @return void
  9026. */
  9027. public function testHtml5Inputs() {
  9028. $result = $this->Form->email('User.email');
  9029. $expected = array(
  9030. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  9031. );
  9032. $this->assertTags($result, $expected);
  9033. $result = $this->Form->search('User.query');
  9034. $expected = array(
  9035. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  9036. );
  9037. $this->assertTags($result, $expected);
  9038. $result = $this->Form->search('User.query', array('value' => 'test'));
  9039. $expected = array(
  9040. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  9041. );
  9042. $this->assertTags($result, $expected);
  9043. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  9044. $expected = array(
  9045. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  9046. );
  9047. $this->assertTags($result, $expected);
  9048. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  9049. $expected = array(
  9050. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  9051. );
  9052. $this->assertTags($result, $expected);
  9053. }
  9054. /**
  9055. *
  9056. * @expectedException CakeException
  9057. * @return void
  9058. */
  9059. public function testHtml5InputException() {
  9060. $this->Form->email();
  9061. }
  9062. /**
  9063. * Tests that a model can be loaded from the model names passed in the request object
  9064. *
  9065. * @return void
  9066. */
  9067. public function testIntrospectModelFromRequest() {
  9068. $this->loadFixtures('Post');
  9069. App::build(array(
  9070. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  9071. ));
  9072. CakePlugin::load('TestPlugin');
  9073. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  9074. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  9075. $this->Form->create('TestPluginPost');
  9076. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  9077. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  9078. CakePlugin::unload();
  9079. App::build();
  9080. }
  9081. /**
  9082. * Tests that it is possible to set the validation errors directly in the helper for a field
  9083. *
  9084. * @return void
  9085. */
  9086. public function testCustomValidationErrors() {
  9087. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  9088. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  9089. $this->assertEquals('Badness!', $result);
  9090. }
  9091. /**
  9092. * Tests that the 'on' key validates as expected on create
  9093. *
  9094. * @return void
  9095. */
  9096. public function testRequiredOnCreate() {
  9097. $this->Form->create('Contact');
  9098. $result = $this->Form->input('Contact.imrequiredonupdate');
  9099. $expected = array(
  9100. 'div' => array('class' => 'input text'),
  9101. 'label' => array('for' => 'ContactImrequiredonupdate'),
  9102. 'Imrequiredonupdate',
  9103. '/label',
  9104. 'input' => array(
  9105. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  9106. 'id' => 'ContactImrequiredonupdate'
  9107. ),
  9108. '/div'
  9109. );
  9110. $this->assertTags($result, $expected);
  9111. $result = $this->Form->input('Contact.imrequiredoncreate');
  9112. $expected = array(
  9113. 'div' => array('class' => 'input text required'),
  9114. 'label' => array('for' => 'ContactImrequiredoncreate'),
  9115. 'Imrequiredoncreate',
  9116. '/label',
  9117. 'input' => array(
  9118. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  9119. 'id' => 'ContactImrequiredoncreate',
  9120. 'required' => 'required'
  9121. ),
  9122. '/div'
  9123. );
  9124. $this->assertTags($result, $expected);
  9125. $result = $this->Form->input('Contact.imrequiredonboth');
  9126. $expected = array(
  9127. 'div' => array('class' => 'input text required'),
  9128. 'label' => array('for' => 'ContactImrequiredonboth'),
  9129. 'Imrequiredonboth',
  9130. '/label',
  9131. 'input' => array(
  9132. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  9133. 'id' => 'ContactImrequiredonboth',
  9134. 'required' => 'required'
  9135. ),
  9136. '/div'
  9137. );
  9138. $this->assertTags($result, $expected);
  9139. $this->Form->inputDefaults(array('required' => false));
  9140. $result = $this->Form->input('Contact.imrequired');
  9141. $expected = array(
  9142. 'div' => array('class' => 'input text'),
  9143. 'label' => array('for' => 'ContactImrequired'),
  9144. 'Imrequired',
  9145. '/label',
  9146. 'input' => array(
  9147. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9148. 'id' => 'ContactImrequired'
  9149. ),
  9150. '/div'
  9151. );
  9152. $this->assertTags($result, $expected);
  9153. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  9154. $this->assertTags($result, $expected);
  9155. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  9156. $expected = array(
  9157. 'div' => array('class' => 'input text required'),
  9158. 'label' => array('for' => 'ContactImrequired'),
  9159. 'Imrequired',
  9160. '/label',
  9161. 'input' => array(
  9162. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9163. 'id' => 'ContactImrequired'
  9164. ),
  9165. '/div'
  9166. );
  9167. $this->assertTags($result, $expected);
  9168. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  9169. $this->assertTags($result, $expected);
  9170. }
  9171. /**
  9172. * Tests that the 'on' key validates as expected on update
  9173. *
  9174. * @return void
  9175. */
  9176. public function testRequiredOnUpdate() {
  9177. $this->Form->request->data['Contact']['id'] = 1;
  9178. $this->Form->create('Contact');
  9179. $result = $this->Form->input('Contact.imrequiredonupdate');
  9180. $expected = array(
  9181. 'div' => array('class' => 'input text required'),
  9182. 'label' => array('for' => 'ContactImrequiredonupdate'),
  9183. 'Imrequiredonupdate',
  9184. '/label',
  9185. 'input' => array(
  9186. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  9187. 'id' => 'ContactImrequiredonupdate',
  9188. 'required' => 'required'
  9189. ),
  9190. '/div'
  9191. );
  9192. $this->assertTags($result, $expected);
  9193. $result = $this->Form->input('Contact.imrequiredoncreate');
  9194. $expected = array(
  9195. 'div' => array('class' => 'input text'),
  9196. 'label' => array('for' => 'ContactImrequiredoncreate'),
  9197. 'Imrequiredoncreate',
  9198. '/label',
  9199. 'input' => array(
  9200. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  9201. 'id' => 'ContactImrequiredoncreate'
  9202. ),
  9203. '/div'
  9204. );
  9205. $this->assertTags($result, $expected);
  9206. $result = $this->Form->input('Contact.imrequiredonboth');
  9207. $expected = array(
  9208. 'div' => array('class' => 'input text required'),
  9209. 'label' => array('for' => 'ContactImrequiredonboth'),
  9210. 'Imrequiredonboth',
  9211. '/label',
  9212. 'input' => array(
  9213. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  9214. 'id' => 'ContactImrequiredonboth',
  9215. 'required' => 'required'
  9216. ),
  9217. '/div'
  9218. );
  9219. $this->assertTags($result, $expected);
  9220. $result = $this->Form->input('Contact.imrequired');
  9221. $expected = array(
  9222. 'div' => array('class' => 'input text required'),
  9223. 'label' => array('for' => 'ContactImrequired'),
  9224. 'Imrequired',
  9225. '/label',
  9226. 'input' => array(
  9227. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9228. 'id' => 'ContactImrequired',
  9229. 'required' => 'required'
  9230. ),
  9231. '/div'
  9232. );
  9233. $this->assertTags($result, $expected);
  9234. }
  9235. /**
  9236. * Test inputDefaults setter and getter
  9237. *
  9238. * @return void
  9239. */
  9240. public function testInputDefaults() {
  9241. $this->Form->create('Contact');
  9242. $this->Form->inputDefaults(array(
  9243. 'label' => false,
  9244. 'div' => array(
  9245. 'style' => 'color: #000;'
  9246. )
  9247. ));
  9248. $result = $this->Form->input('Contact.field1');
  9249. $expected = array(
  9250. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  9251. 'input' => array(
  9252. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9253. 'id' => 'ContactField1'
  9254. ),
  9255. '/div'
  9256. );
  9257. $this->assertTags($result, $expected);
  9258. $this->Form->inputDefaults(array(
  9259. 'div' => false,
  9260. 'label' => 'Label',
  9261. ));
  9262. $result = $this->Form->input('Contact.field1');
  9263. $expected = array(
  9264. 'label' => array('for' => 'ContactField1'),
  9265. 'Label',
  9266. '/label',
  9267. 'input' => array(
  9268. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9269. 'id' => 'ContactField1'
  9270. ),
  9271. );
  9272. $this->assertTags($result, $expected);
  9273. $this->Form->inputDefaults(array(
  9274. 'label' => false,
  9275. ), true);
  9276. $result = $this->Form->input('Contact.field1');
  9277. $expected = array(
  9278. 'input' => array(
  9279. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9280. 'id' => 'ContactField1'
  9281. ),
  9282. );
  9283. $this->assertTags($result, $expected);
  9284. $result = $this->Form->inputDefaults();
  9285. $expected = array(
  9286. 'div' => false,
  9287. 'label' => false,
  9288. );
  9289. $this->assertEquals($expected, $result);
  9290. }
  9291. }