FormHelperTest.php 309 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737
  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. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  320. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  321. );
  322. /**
  323. * beforeValidate method
  324. *
  325. * @return void
  326. */
  327. public function beforeValidate($options = array()) {
  328. $this->invalidate('email');
  329. return false;
  330. }
  331. }
  332. /**
  333. * ValidateProfile class
  334. *
  335. * @package Cake.Test.Case.View.Helper
  336. */
  337. class ValidateProfile extends CakeTestModel {
  338. /**
  339. * useTable property
  340. *
  341. * @var boolean
  342. */
  343. public $useTable = false;
  344. /**
  345. * schema property
  346. *
  347. * @var array
  348. */
  349. protected $_schema = array(
  350. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  351. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  352. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  353. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  354. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  355. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  356. );
  357. /**
  358. * hasOne property
  359. *
  360. * @var array
  361. */
  362. public $hasOne = array('ValidateItem' => array(
  363. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  364. ));
  365. /**
  366. * belongsTo property
  367. *
  368. * @var array
  369. */
  370. public $belongsTo = array('ValidateUser' => array(
  371. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  372. ));
  373. /**
  374. * beforeValidate method
  375. *
  376. * @return void
  377. */
  378. public function beforeValidate($options = array()) {
  379. $this->invalidate('full_name');
  380. $this->invalidate('city');
  381. return false;
  382. }
  383. }
  384. /**
  385. * ValidateItem class
  386. *
  387. * @package Cake.Test.Case.View.Helper
  388. */
  389. class ValidateItem extends CakeTestModel {
  390. /**
  391. * useTable property
  392. *
  393. * @var boolean
  394. */
  395. public $useTable = false;
  396. /**
  397. * schema property
  398. *
  399. * @var array
  400. */
  401. protected $_schema = array(
  402. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  403. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  404. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  405. 'description' => array(
  406. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  407. ),
  408. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  409. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  410. );
  411. /**
  412. * belongsTo property
  413. *
  414. * @var array
  415. */
  416. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  417. /**
  418. * beforeValidate method
  419. *
  420. * @return void
  421. */
  422. public function beforeValidate($options = array()) {
  423. $this->invalidate('description');
  424. return false;
  425. }
  426. }
  427. /**
  428. * TestMail class
  429. *
  430. * @package Cake.Test.Case.View.Helper
  431. */
  432. class TestMail extends CakeTestModel {
  433. /**
  434. * useTable property
  435. *
  436. * @var boolean
  437. */
  438. public $useTable = false;
  439. }
  440. /**
  441. * FormHelperTest class
  442. *
  443. * @package Cake.Test.Case.View.Helper
  444. * @property FormHelper $Form
  445. */
  446. class FormHelperTest extends CakeTestCase {
  447. /**
  448. * Fixtures to be used
  449. *
  450. * @var array
  451. */
  452. public $fixtures = array('core.post');
  453. /**
  454. * Do not load the fixtures by default
  455. *
  456. * @var boolean
  457. */
  458. public $autoFixtures = false;
  459. /**
  460. * setUp method
  461. *
  462. * @return void
  463. */
  464. public function setUp() {
  465. parent::setUp();
  466. Configure::write('Config.language', 'eng');
  467. Configure::write('App.base', '');
  468. Configure::delete('Asset');
  469. $this->Controller = new ContactTestController();
  470. $this->View = new View($this->Controller);
  471. $this->Form = new FormHelper($this->View);
  472. $this->Form->Html = new HtmlHelper($this->View);
  473. $this->Form->request = new CakeRequest('contacts/add', false);
  474. $this->Form->request->here = '/contacts/add';
  475. $this->Form->request['action'] = 'add';
  476. $this->Form->request->webroot = '';
  477. $this->Form->request->base = '';
  478. ClassRegistry::addObject('Contact', new Contact());
  479. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  480. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  481. ClassRegistry::addObject('User', new UserForm());
  482. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  483. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  484. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  485. $this->oldSalt = Configure::read('Security.salt');
  486. $this->dateRegex = array(
  487. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  488. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  489. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  491. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  492. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  493. );
  494. Configure::write('Security.salt', 'foo!');
  495. }
  496. /**
  497. * tearDown method
  498. *
  499. * @return void
  500. */
  501. public function tearDown() {
  502. parent::tearDown();
  503. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  504. Configure::write('Security.salt', $this->oldSalt);
  505. }
  506. /**
  507. * testFormCreateWithSecurity method
  508. *
  509. * Test form->create() with security key.
  510. *
  511. * @return void
  512. */
  513. public function testCreateWithSecurity() {
  514. $this->Form->request['_Token'] = array('key' => 'testKey');
  515. $encoding = strtolower(Configure::read('App.encoding'));
  516. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  517. $expected = array(
  518. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  519. 'div' => array('style' => 'display:none;'),
  520. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  521. array('input' => array(
  522. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  523. )),
  524. '/div'
  525. );
  526. $this->assertTags($result, $expected);
  527. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  528. $expected['form']['id'] = 'MyForm';
  529. $this->assertTags($result, $expected);
  530. }
  531. /**
  532. * testFormCreateGetNoSecurity method
  533. *
  534. * Test form->create() with no security key as its a get form
  535. *
  536. * @return void
  537. */
  538. public function testCreateEndGetNoSecurity() {
  539. $this->Form->request['_Token'] = array('key' => 'testKey');
  540. $encoding = strtolower(Configure::read('App.encoding'));
  541. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  542. $this->assertNotContains('Token', $result);
  543. $result = $this->Form->end('Save');
  544. $this->assertNotContains('Token', $result);
  545. }
  546. /**
  547. * test that create() clears the fields property so it starts fresh
  548. *
  549. * @return void
  550. */
  551. public function testCreateClearingFields() {
  552. $this->Form->fields = array('model_id');
  553. $this->Form->create('Contact');
  554. $this->assertEquals(array(), $this->Form->fields);
  555. }
  556. /**
  557. * Tests form hash generation with model-less data
  558. *
  559. * @return void
  560. */
  561. public function testValidateHashNoModel() {
  562. $this->Form->request['_Token'] = array('key' => 'foo');
  563. $result = $this->Form->secure(array('anything'));
  564. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  565. }
  566. /**
  567. * Tests that models with identical field names get resolved properly
  568. *
  569. * @return void
  570. */
  571. public function testDuplicateFieldNameResolution() {
  572. $result = $this->Form->create('ValidateUser');
  573. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  574. $result = $this->Form->input('ValidateItem.name');
  575. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateUser.name');
  577. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  578. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  579. $this->assertRegExp('/type="text"/', $result);
  580. $result = $this->Form->input('ValidateItem.name');
  581. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  582. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  583. $this->assertRegExp('/<textarea/', $result);
  584. $result = $this->Form->input('name');
  585. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  586. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  587. $this->assertRegExp('/type="text"/', $result);
  588. }
  589. /**
  590. * Tests that hidden fields generated for checkboxes don't get locked
  591. *
  592. * @return void
  593. */
  594. public function testNoCheckboxLocking() {
  595. $this->Form->request['_Token'] = array('key' => 'foo');
  596. $this->assertSame(array(), $this->Form->fields);
  597. $this->Form->checkbox('check', array('value' => '1'));
  598. $this->assertSame($this->Form->fields, array('check'));
  599. }
  600. /**
  601. * testFormSecurityFields method
  602. *
  603. * Test generation of secure form hash generation.
  604. *
  605. * @return void
  606. */
  607. public function testFormSecurityFields() {
  608. $key = 'testKey';
  609. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  610. $this->Form->request['_Token'] = array('key' => $key);
  611. $result = $this->Form->secure($fields);
  612. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  613. $hash .= ':' . 'Model.valid';
  614. $hash = urlencode($hash);
  615. $expected = array(
  616. 'div' => array('style' => 'display:none;'),
  617. array('input' => array(
  618. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  619. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  620. )),
  621. array('input' => array(
  622. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  623. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  624. )),
  625. '/div'
  626. );
  627. $this->assertTags($result, $expected);
  628. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  629. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  630. $result = $this->Form->secure($fields);
  631. $expected = array(
  632. 'div' => array('class' => 'hidden'),
  633. array('input' => array(
  634. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  635. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  636. )),
  637. array('input' => array(
  638. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  639. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  640. )),
  641. '/div'
  642. );
  643. $this->assertTags($result, $expected);
  644. }
  645. /**
  646. * Tests correct generation of number fields for double and float fields
  647. *
  648. * @return void
  649. */
  650. public function testTextFieldGenerationForFloats() {
  651. $model = ClassRegistry::getObject('Contact');
  652. $model->setSchema(array('foo' => array(
  653. 'type' => 'float',
  654. 'null' => false,
  655. 'default' => null,
  656. 'length' => 10
  657. )));
  658. $this->Form->create('Contact');
  659. $result = $this->Form->input('foo');
  660. $expected = array(
  661. 'div' => array('class' => 'input number'),
  662. 'label' => array('for' => 'ContactFoo'),
  663. 'Foo',
  664. '/label',
  665. array('input' => array(
  666. 'type' => 'number',
  667. 'name' => 'data[Contact][foo]',
  668. 'id' => 'ContactFoo',
  669. 'step' => 'any'
  670. )),
  671. '/div'
  672. );
  673. $this->assertTags($result, $expected);
  674. $result = $this->Form->input('foo', array('step' => 0.5));
  675. $expected = array(
  676. 'div' => array('class' => 'input number'),
  677. 'label' => array('for' => 'ContactFoo'),
  678. 'Foo',
  679. '/label',
  680. array('input' => array(
  681. 'type' => 'number',
  682. 'name' => 'data[Contact][foo]',
  683. 'id' => 'ContactFoo',
  684. 'step' => '0.5'
  685. )),
  686. '/div'
  687. );
  688. $this->assertTags($result, $expected);
  689. }
  690. /**
  691. * Tests correct generation of number fields for integer fields
  692. *
  693. * @return void
  694. */
  695. public function testTextFieldTypeNumberGenerationForIntegers() {
  696. $model = ClassRegistry::getObject('Contact');
  697. $model->setSchema(array('foo' => array(
  698. 'type' => 'integer',
  699. 'null' => false,
  700. 'default' => null,
  701. 'length' => null
  702. )));
  703. $this->Form->create('Contact');
  704. $result = $this->Form->input('foo');
  705. $expected = array(
  706. 'div' => array('class' => 'input number'),
  707. 'label' => array('for' => 'ContactFoo'),
  708. 'Foo',
  709. '/label',
  710. array('input' => array(
  711. 'type' => 'number', 'name' => 'data[Contact][foo]',
  712. 'id' => 'ContactFoo'
  713. )),
  714. '/div'
  715. );
  716. $this->assertTags($result, $expected);
  717. }
  718. /**
  719. * Tests correct generation of file upload fields for binary fields
  720. *
  721. * @return void
  722. */
  723. public function testFileUploadFieldTypeGenerationForBinaries() {
  724. $model = ClassRegistry::getObject('Contact');
  725. $model->setSchema(array('foo' => array(
  726. 'type' => 'binary',
  727. 'null' => false,
  728. 'default' => null,
  729. 'length' => 1024
  730. )));
  731. $this->Form->create('Contact');
  732. $result = $this->Form->input('foo');
  733. $expected = array(
  734. 'div' => array('class' => 'input file'),
  735. 'label' => array('for' => 'ContactFoo'),
  736. 'Foo',
  737. '/label',
  738. array('input' => array(
  739. 'type' => 'file', 'name' => 'data[Contact][foo]',
  740. 'id' => 'ContactFoo'
  741. )),
  742. '/div'
  743. );
  744. $this->assertTags($result, $expected);
  745. }
  746. /**
  747. * testFormSecurityMultipleFields method
  748. *
  749. * Test secure() with multiple row form. Ensure hash is correct.
  750. *
  751. * @return void
  752. */
  753. public function testFormSecurityMultipleFields() {
  754. $key = 'testKey';
  755. $fields = array(
  756. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  757. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  758. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  759. );
  760. $this->Form->request['_Token'] = array('key' => $key);
  761. $result = $this->Form->secure($fields);
  762. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  763. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  764. $expected = array(
  765. 'div' => array('style' => 'display:none;'),
  766. array('input' => array(
  767. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  768. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  769. )),
  770. array('input' => array(
  771. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  772. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  773. )),
  774. '/div'
  775. );
  776. $this->assertTags($result, $expected);
  777. }
  778. /**
  779. * testFormSecurityMultipleSubmitButtons
  780. *
  781. * test form submit generation and ensure that _Token is only created on end()
  782. *
  783. * @return void
  784. */
  785. public function testFormSecurityMultipleSubmitButtons() {
  786. $key = 'testKey';
  787. $this->Form->request['_Token'] = array('key' => $key);
  788. $this->Form->create('Addresses');
  789. $this->Form->input('Address.title');
  790. $this->Form->input('Address.first_name');
  791. $result = $this->Form->submit('Save', array('name' => 'save'));
  792. $expected = array(
  793. 'div' => array('class' => 'submit'),
  794. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  795. '/div',
  796. );
  797. $this->assertTags($result, $expected);
  798. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  799. $expected = array(
  800. 'div' => array('class' => 'submit'),
  801. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  802. '/div',
  803. );
  804. $this->assertTags($result, $expected);
  805. $result = $this->Form->end(null);
  806. $expected = array(
  807. 'div' => array('style' => 'display:none;'),
  808. array('input' => array(
  809. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  810. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  811. )),
  812. array('input' => array(
  813. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  814. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  815. )),
  816. '/div'
  817. );
  818. $this->assertTags($result, $expected);
  819. }
  820. /**
  821. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  822. *
  823. * @return void
  824. */
  825. public function testSecurityButtonNestedNamed() {
  826. $key = 'testKey';
  827. $this->Form->request['_Token'] = array('key' => $key);
  828. $this->Form->create('Addresses');
  829. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  830. $result = $this->Form->unlockField();
  831. $this->assertEquals(array('Address.button'), $result);
  832. }
  833. /**
  834. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  835. *
  836. * @return void
  837. */
  838. public function testSecuritySubmitNestedNamed() {
  839. $key = 'testKey';
  840. $this->Form->request['_Token'] = array('key' => $key);
  841. $this->Form->create('Addresses');
  842. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  843. $result = $this->Form->unlockField();
  844. $this->assertEquals(array('Address.button'), $result);
  845. }
  846. /**
  847. * Test that the correct fields are unlocked for image submits with no names.
  848. *
  849. * @return void
  850. */
  851. public function testSecuritySubmitImageNoName() {
  852. $key = 'testKey';
  853. $this->Form->request['_Token'] = array('key' => $key);
  854. $this->Form->create('User');
  855. $result = $this->Form->submit('save.png');
  856. $expected = array(
  857. 'div' => array('class' => 'submit'),
  858. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  859. '/div'
  860. );
  861. $this->assertTags($result, $expected);
  862. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  863. }
  864. /**
  865. * Test that the correct fields are unlocked for image submits with names.
  866. *
  867. * @return void
  868. */
  869. public function testSecuritySubmitImageName() {
  870. $key = 'testKey';
  871. $this->Form->request['_Token'] = array('key' => $key);
  872. $this->Form->create('User');
  873. $result = $this->Form->submit('save.png', array('name' => 'test'));
  874. $expected = array(
  875. 'div' => array('class' => 'submit'),
  876. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  877. '/div'
  878. );
  879. $this->assertTags($result, $expected);
  880. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  881. }
  882. /**
  883. * testFormSecurityMultipleInputFields method
  884. *
  885. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  886. *
  887. * @return void
  888. */
  889. public function testFormSecurityMultipleInputFields() {
  890. $key = 'testKey';
  891. $this->Form->request['_Token'] = array('key' => $key);
  892. $this->Form->create('Addresses');
  893. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  894. $this->Form->input('Addresses.0.title');
  895. $this->Form->input('Addresses.0.first_name');
  896. $this->Form->input('Addresses.0.last_name');
  897. $this->Form->input('Addresses.0.address');
  898. $this->Form->input('Addresses.0.city');
  899. $this->Form->input('Addresses.0.phone');
  900. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  901. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  902. $this->Form->input('Addresses.1.title');
  903. $this->Form->input('Addresses.1.first_name');
  904. $this->Form->input('Addresses.1.last_name');
  905. $this->Form->input('Addresses.1.address');
  906. $this->Form->input('Addresses.1.city');
  907. $this->Form->input('Addresses.1.phone');
  908. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  909. $result = $this->Form->secure($this->Form->fields);
  910. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  911. $expected = array(
  912. 'div' => array('style' => 'display:none;'),
  913. array('input' => array(
  914. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  915. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  916. )),
  917. array('input' => array(
  918. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  919. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  920. )),
  921. '/div'
  922. );
  923. $this->assertTags($result, $expected);
  924. }
  925. /**
  926. * Test form security with Model.field.0 style inputs
  927. *
  928. * @return void
  929. */
  930. public function testFormSecurityArrayFields() {
  931. $key = 'testKey';
  932. $this->Form->request->params['_Token']['key'] = $key;
  933. $this->Form->create('Address');
  934. $this->Form->input('Address.primary.1');
  935. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  936. $this->Form->input('Address.secondary.1.0');
  937. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  938. }
  939. /**
  940. * testFormSecurityMultipleInputDisabledFields method
  941. *
  942. * test secure form generation with multiple records and disabled fields.
  943. *
  944. * @return void
  945. */
  946. public function testFormSecurityMultipleInputDisabledFields() {
  947. $key = 'testKey';
  948. $this->Form->request->params['_Token'] = array(
  949. 'key' => $key,
  950. 'unlockedFields' => array('first_name', 'address')
  951. );
  952. $this->Form->create();
  953. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  954. $this->Form->input('Addresses.0.title');
  955. $this->Form->input('Addresses.0.first_name');
  956. $this->Form->input('Addresses.0.last_name');
  957. $this->Form->input('Addresses.0.address');
  958. $this->Form->input('Addresses.0.city');
  959. $this->Form->input('Addresses.0.phone');
  960. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  961. $this->Form->input('Addresses.1.title');
  962. $this->Form->input('Addresses.1.first_name');
  963. $this->Form->input('Addresses.1.last_name');
  964. $this->Form->input('Addresses.1.address');
  965. $this->Form->input('Addresses.1.city');
  966. $this->Form->input('Addresses.1.phone');
  967. $result = $this->Form->secure($this->Form->fields);
  968. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  969. $expected = array(
  970. 'div' => array('style' => 'display:none;'),
  971. array('input' => array(
  972. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  973. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  974. )),
  975. array('input' => array(
  976. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  977. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  978. )),
  979. '/div'
  980. );
  981. $this->assertTags($result, $expected);
  982. }
  983. /**
  984. * testFormSecurityInputDisabledFields method
  985. *
  986. * Test single record form with disabled fields.
  987. *
  988. * @return void
  989. */
  990. public function testFormSecurityInputUnlockedFields() {
  991. $key = 'testKey';
  992. $this->Form->request['_Token'] = array(
  993. 'key' => $key,
  994. 'unlockedFields' => array('first_name', 'address')
  995. );
  996. $this->Form->create();
  997. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  998. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  999. $this->Form->input('Addresses.title');
  1000. $this->Form->input('Addresses.first_name');
  1001. $this->Form->input('Addresses.last_name');
  1002. $this->Form->input('Addresses.address');
  1003. $this->Form->input('Addresses.city');
  1004. $this->Form->input('Addresses.phone');
  1005. $result = $this->Form->fields;
  1006. $expected = array(
  1007. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1008. 'Addresses.city', 'Addresses.phone'
  1009. );
  1010. $this->assertEquals($expected, $result);
  1011. $result = $this->Form->secure($expected);
  1012. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1013. $expected = array(
  1014. 'div' => array('style' => 'display:none;'),
  1015. array('input' => array(
  1016. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1017. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1018. )),
  1019. array('input' => array(
  1020. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1021. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1022. )),
  1023. '/div'
  1024. );
  1025. $this->assertTags($result, $expected);
  1026. }
  1027. /**
  1028. * test securing inputs with custom name attributes.
  1029. *
  1030. * @return void
  1031. */
  1032. public function testFormSecureWithCustomNameAttribute() {
  1033. $this->Form->request->params['_Token']['key'] = 'testKey';
  1034. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1035. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1036. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1037. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1038. }
  1039. /**
  1040. * testFormSecuredInput method
  1041. *
  1042. * Test generation of entire secure form, assertions made on input() output.
  1043. *
  1044. * @return void
  1045. */
  1046. public function testFormSecuredInput() {
  1047. $this->Form->request['_Token'] = array('key' => 'testKey');
  1048. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1049. $encoding = strtolower(Configure::read('App.encoding'));
  1050. $expected = array(
  1051. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1052. 'div' => array('style' => 'display:none;'),
  1053. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1054. array('input' => array(
  1055. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1056. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1057. )),
  1058. '/div'
  1059. );
  1060. $this->assertTags($result, $expected);
  1061. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1062. $expected = array(
  1063. 'div' => array('class' => 'input text'),
  1064. 'label' => array('for' => 'UserFormPublished'),
  1065. 'Published',
  1066. '/label',
  1067. array('input' => array(
  1068. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1069. 'id' => 'UserFormPublished'
  1070. )),
  1071. '/div'
  1072. );
  1073. $this->assertTags($result, $expected);
  1074. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1075. $expected = array(
  1076. 'div' => array('class' => 'input text'),
  1077. 'label' => array('for' => 'UserFormOther'),
  1078. 'Other',
  1079. '/label',
  1080. array('input' => array(
  1081. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1082. 'id' => 'UserFormOther'
  1083. )),
  1084. '/div'
  1085. );
  1086. $this->assertTags($result, $expected);
  1087. $result = $this->Form->hidden('UserForm.stuff');
  1088. $expected = array(
  1089. 'input' => array(
  1090. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1091. 'id' => 'UserFormStuff'
  1092. ));
  1093. $this->assertTags($result, $expected);
  1094. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1095. $expected = array('input' => array(
  1096. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1097. 'value' => '0', 'id' => 'UserFormHidden'
  1098. ));
  1099. $this->assertTags($result, $expected);
  1100. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1101. $expected = array(
  1102. 'div' => array('class' => 'input checkbox'),
  1103. array('input' => array(
  1104. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1105. 'value' => '0', 'id' => 'UserFormSomething_'
  1106. )),
  1107. array('input' => array(
  1108. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1109. 'value' => '1', 'id' => 'UserFormSomething'
  1110. )),
  1111. 'label' => array('for' => 'UserFormSomething'),
  1112. 'Something',
  1113. '/label',
  1114. '/div'
  1115. );
  1116. $this->assertTags($result, $expected);
  1117. $result = $this->Form->fields;
  1118. $expected = array(
  1119. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1120. 'UserForm.hidden' => '0', 'UserForm.something'
  1121. );
  1122. $this->assertEquals($expected, $result);
  1123. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1124. $result = $this->Form->secure($this->Form->fields);
  1125. $expected = array(
  1126. 'div' => array('style' => 'display:none;'),
  1127. array('input' => array(
  1128. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1129. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1130. )),
  1131. array('input' => array(
  1132. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1133. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1134. )),
  1135. '/div'
  1136. );
  1137. $this->assertTags($result, $expected);
  1138. }
  1139. /**
  1140. * Test secured inputs with custom names.
  1141. *
  1142. * @return void
  1143. */
  1144. public function testSecuredInputCustomName() {
  1145. $this->Form->request['_Token'] = array('key' => 'testKey');
  1146. $this->assertEquals(array(), $this->Form->fields);
  1147. $this->Form->input('text_input', array(
  1148. 'name' => 'data[Option][General.default_role]',
  1149. ));
  1150. $expected = array('Option.General.default_role');
  1151. $this->assertEquals($expected, $this->Form->fields);
  1152. $this->Form->input('select_box', array(
  1153. 'name' => 'data[Option][General.select_role]',
  1154. 'type' => 'select',
  1155. 'options' => array(1, 2),
  1156. ));
  1157. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1158. $this->assertEquals($expected, $this->Form->fields);
  1159. }
  1160. /**
  1161. * Tests that the correct keys are added to the field hash index
  1162. *
  1163. * @return void
  1164. */
  1165. public function testFormSecuredFileInput() {
  1166. $this->Form->request['_Token'] = array('key' => 'testKey');
  1167. $this->assertEquals(array(), $this->Form->fields);
  1168. $this->Form->file('Attachment.file');
  1169. $expected = array(
  1170. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1171. 'Attachment.file.error', 'Attachment.file.size'
  1172. );
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * test that multiple selects keys are added to field hash
  1177. *
  1178. * @return void
  1179. */
  1180. public function testFormSecuredMultipleSelect() {
  1181. $this->Form->request['_Token'] = array('key' => 'testKey');
  1182. $this->assertEquals(array(), $this->Form->fields);
  1183. $options = array('1' => 'one', '2' => 'two');
  1184. $this->Form->select('Model.select', $options);
  1185. $expected = array('Model.select');
  1186. $this->assertEquals($expected, $this->Form->fields);
  1187. $this->Form->fields = array();
  1188. $this->Form->select('Model.select', $options, array('multiple' => true));
  1189. $this->assertEquals($expected, $this->Form->fields);
  1190. }
  1191. /**
  1192. * testFormSecuredRadio method
  1193. *
  1194. * @return void
  1195. */
  1196. public function testFormSecuredRadio() {
  1197. $this->Form->request['_Token'] = array('key' => 'testKey');
  1198. $this->assertEquals(array(), $this->Form->fields);
  1199. $options = array('1' => 'option1', '2' => 'option2');
  1200. $this->Form->radio('Test.test', $options);
  1201. $expected = array('Test.test');
  1202. $this->assertEquals($expected, $this->Form->fields);
  1203. }
  1204. /**
  1205. * Test that when disabled is in a list based attribute array it works.
  1206. *
  1207. * @return void
  1208. */
  1209. public function testFormSecuredAndDisabledNotAssoc() {
  1210. $this->Form->request['_Token'] = array('key' => 'testKey');
  1211. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1212. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1213. $this->Form->text('Model.text', array('disabled'));
  1214. $this->Form->textarea('Model.textarea', array('disabled'));
  1215. $this->Form->password('Model.password', array('disabled'));
  1216. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1217. $expected = array(
  1218. 'Model.radio' => ''
  1219. );
  1220. $this->assertEquals($expected, $this->Form->fields);
  1221. }
  1222. /**
  1223. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1224. * hashing.
  1225. *
  1226. * @return void
  1227. */
  1228. public function testFormSecuredAndDisabled() {
  1229. $this->Form->request['_Token'] = array('key' => 'testKey');
  1230. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1231. $this->Form->text('Model.text', array('disabled' => true));
  1232. $this->Form->password('Model.text', array('disabled' => true));
  1233. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1234. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1235. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1236. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1237. $this->Form->month('Model.month', array('disabled' => true));
  1238. $this->Form->day('Model.day', array('disabled' => true));
  1239. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1240. $this->Form->minute('Model.minute', array('disabled' => true));
  1241. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1242. $expected = array(
  1243. 'Model.radio' => ''
  1244. );
  1245. $this->assertEquals($expected, $this->Form->fields);
  1246. }
  1247. /**
  1248. * testDisableSecurityUsingForm method
  1249. *
  1250. * @return void
  1251. */
  1252. public function testDisableSecurityUsingForm() {
  1253. $this->Form->request['_Token'] = array(
  1254. 'key' => 'testKey',
  1255. 'disabledFields' => array()
  1256. );
  1257. $this->Form->create();
  1258. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1259. $this->Form->input('Addresses.title');
  1260. $this->Form->input('Addresses.first_name', array('secure' => false));
  1261. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1262. $this->Form->input('Addresses.zip', array(
  1263. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1264. ));
  1265. $result = $this->Form->fields;
  1266. $expected = array(
  1267. 'Addresses.id' => '123456', 'Addresses.title',
  1268. );
  1269. $this->assertEquals($expected, $result);
  1270. }
  1271. /**
  1272. * test disableField
  1273. *
  1274. * @return void
  1275. */
  1276. public function testUnlockFieldAddsToList() {
  1277. $this->Form->request['_Token'] = array(
  1278. 'key' => 'testKey',
  1279. 'unlockedFields' => array()
  1280. );
  1281. $this->Form->create('Contact');
  1282. $this->Form->unlockField('Contact.name');
  1283. $this->Form->text('Contact.name');
  1284. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1285. $this->assertEquals(array(), $this->Form->fields);
  1286. }
  1287. /**
  1288. * test unlockField removing from fields array.
  1289. *
  1290. * @return void
  1291. */
  1292. public function testUnlockFieldRemovingFromFields() {
  1293. $this->Form->request['_Token'] = array(
  1294. 'key' => 'testKey',
  1295. 'unlockedFields' => array()
  1296. );
  1297. $this->Form->create('Contact');
  1298. $this->Form->hidden('Contact.id', array('value' => 1));
  1299. $this->Form->text('Contact.name');
  1300. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1301. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1302. $this->Form->unlockField('Contact.name');
  1303. $this->Form->unlockField('Contact.id');
  1304. $this->assertEquals(array(), $this->Form->fields);
  1305. }
  1306. /**
  1307. * testTagIsInvalid method
  1308. *
  1309. * @return void
  1310. */
  1311. public function testTagIsInvalid() {
  1312. $Contact = ClassRegistry::getObject('Contact');
  1313. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1314. $this->Form->setEntity('Contact.0.email');
  1315. $result = $this->Form->tagIsInvalid();
  1316. $this->assertEquals($expected, $result);
  1317. $this->Form->setEntity('Contact.1.email');
  1318. $result = $this->Form->tagIsInvalid();
  1319. $this->assertFalse($result);
  1320. $this->Form->setEntity('Contact.0.name');
  1321. $result = $this->Form->tagIsInvalid();
  1322. $this->assertFalse($result);
  1323. }
  1324. /**
  1325. * Test tagIsInvalid with validation errors from a saveMany
  1326. *
  1327. * @return void
  1328. */
  1329. public function testTagIsInvalidSaveMany() {
  1330. $Contact = ClassRegistry::getObject('Contact');
  1331. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1332. $this->Form->create('Contact');
  1333. $this->Form->setEntity('0.email');
  1334. $result = $this->Form->tagIsInvalid();
  1335. $this->assertEquals($expected, $result);
  1336. $this->Form->setEntity('0.Contact.email');
  1337. $result = $this->Form->tagIsInvalid();
  1338. $this->assertEquals($expected, $result);
  1339. }
  1340. /**
  1341. * Test validation errors.
  1342. *
  1343. * @return void
  1344. */
  1345. public function testPasswordValidation() {
  1346. $Contact = ClassRegistry::getObject('Contact');
  1347. $Contact->validationErrors['password'] = array('Please provide a password');
  1348. $result = $this->Form->input('Contact.password');
  1349. $expected = array(
  1350. 'div' => array('class' => 'input password error'),
  1351. 'label' => array('for' => 'ContactPassword'),
  1352. 'Password',
  1353. '/label',
  1354. 'input' => array(
  1355. 'type' => 'password', 'name' => 'data[Contact][password]',
  1356. 'id' => 'ContactPassword', 'class' => 'form-error'
  1357. ),
  1358. array('div' => array('class' => 'error-message')),
  1359. 'Please provide a password',
  1360. '/div',
  1361. '/div'
  1362. );
  1363. $this->assertTags($result, $expected);
  1364. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1365. $expected = array(
  1366. 'div' => array('class' => 'input password error'),
  1367. 'label' => array('for' => 'ContactPassword'),
  1368. 'Password',
  1369. '/label',
  1370. 'input' => array(
  1371. 'type' => 'password', 'name' => 'data[Contact][password]',
  1372. 'id' => 'ContactPassword', 'class' => 'form-error'
  1373. ),
  1374. '/div'
  1375. );
  1376. $this->assertTags($result, $expected);
  1377. }
  1378. /**
  1379. * Test validation errors, when validation message is an empty string.
  1380. *
  1381. * @return void
  1382. */
  1383. public function testEmptyErrorValidation() {
  1384. $this->Form->validationErrors['Contact']['password'] = '';
  1385. $result = $this->Form->input('Contact.password');
  1386. $expected = array(
  1387. 'div' => array('class' => 'input password error'),
  1388. 'label' => array('for' => 'ContactPassword'),
  1389. 'Password',
  1390. '/label',
  1391. 'input' => array(
  1392. 'type' => 'password', 'name' => 'data[Contact][password]',
  1393. 'id' => 'ContactPassword', 'class' => 'form-error'
  1394. ),
  1395. array('div' => array('class' => 'error-message')),
  1396. array(),
  1397. '/div',
  1398. '/div'
  1399. );
  1400. $this->assertTags($result, $expected);
  1401. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1402. $expected = array(
  1403. 'div' => array('class' => 'input password error'),
  1404. 'label' => array('for' => 'ContactPassword'),
  1405. 'Password',
  1406. '/label',
  1407. 'input' => array(
  1408. 'type' => 'password', 'name' => 'data[Contact][password]',
  1409. 'id' => 'ContactPassword', 'class' => 'form-error'
  1410. ),
  1411. '/div'
  1412. );
  1413. $this->assertTags($result, $expected);
  1414. }
  1415. /**
  1416. * Test validation errors, when calling input() overriding validation message by an empty string.
  1417. *
  1418. * @return void
  1419. */
  1420. public function testEmptyInputErrorValidation() {
  1421. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1422. $result = $this->Form->input('Contact.password', array('error' => ''));
  1423. $expected = array(
  1424. 'div' => array('class' => 'input password error'),
  1425. 'label' => array('for' => 'ContactPassword'),
  1426. 'Password',
  1427. '/label',
  1428. 'input' => array(
  1429. 'type' => 'password', 'name' => 'data[Contact][password]',
  1430. 'id' => 'ContactPassword', 'class' => 'form-error'
  1431. ),
  1432. array('div' => array('class' => 'error-message')),
  1433. array(),
  1434. '/div',
  1435. '/div'
  1436. );
  1437. $this->assertTags($result, $expected);
  1438. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1439. $expected = array(
  1440. 'div' => array('class' => 'input password error'),
  1441. 'label' => array('for' => 'ContactPassword'),
  1442. 'Password',
  1443. '/label',
  1444. 'input' => array(
  1445. 'type' => 'password', 'name' => 'data[Contact][password]',
  1446. 'id' => 'ContactPassword', 'class' => 'form-error'
  1447. ),
  1448. '/div'
  1449. );
  1450. $this->assertTags($result, $expected);
  1451. }
  1452. /**
  1453. * testFormValidationAssociated method
  1454. *
  1455. * test display of form errors in conjunction with model::validates.
  1456. *
  1457. * @return void
  1458. */
  1459. public function testFormValidationAssociated() {
  1460. $this->UserForm = ClassRegistry::getObject('UserForm');
  1461. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1462. $data = array(
  1463. 'UserForm' => array('name' => 'user'),
  1464. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1465. );
  1466. $result = $this->UserForm->OpenidUrl->create($data);
  1467. $this->assertFalse(empty($result));
  1468. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1469. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1470. $encoding = strtolower(Configure::read('App.encoding'));
  1471. $expected = array(
  1472. 'form' => array(
  1473. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1474. 'accept-charset' => $encoding
  1475. ),
  1476. 'div' => array('style' => 'display:none;'),
  1477. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1478. '/div'
  1479. );
  1480. $this->assertTags($result, $expected);
  1481. $result = $this->Form->error(
  1482. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1483. );
  1484. $this->assertEquals('Error, not registered', $result);
  1485. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1486. }
  1487. /**
  1488. * testFormValidationAssociatedFirstLevel method
  1489. *
  1490. * test form error display with associated model.
  1491. *
  1492. * @return void
  1493. */
  1494. public function testFormValidationAssociatedFirstLevel() {
  1495. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1496. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1497. $data = array(
  1498. 'ValidateUser' => array('name' => 'mariano'),
  1499. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1500. );
  1501. $result = $this->ValidateUser->create($data);
  1502. $this->assertFalse(empty($result));
  1503. $this->assertFalse($this->ValidateUser->validates());
  1504. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1505. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1506. $encoding = strtolower(Configure::read('App.encoding'));
  1507. $expected = array(
  1508. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1509. 'div' => array('style' => 'display:none;'),
  1510. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1511. '/div'
  1512. );
  1513. $this->assertTags($result, $expected);
  1514. $result = $this->Form->error(
  1515. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1516. );
  1517. $this->assertEquals('Invalid email', $result);
  1518. $result = $this->Form->error(
  1519. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1520. );
  1521. $this->assertEquals('Invalid name', $result);
  1522. $result = $this->Form->error(
  1523. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1524. );
  1525. $this->assertEquals('Invalid city', $result);
  1526. unset($this->ValidateUser->ValidateProfile);
  1527. unset($this->ValidateUser);
  1528. }
  1529. /**
  1530. * testFormValidationAssociatedSecondLevel method
  1531. *
  1532. * test form error display with associated model.
  1533. *
  1534. * @return void
  1535. */
  1536. public function testFormValidationAssociatedSecondLevel() {
  1537. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1538. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1539. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1540. $data = array(
  1541. 'ValidateUser' => array('name' => 'mariano'),
  1542. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1543. 'ValidateItem' => array('name' => 'Item')
  1544. );
  1545. $result = $this->ValidateUser->create($data);
  1546. $this->assertFalse(empty($result));
  1547. $this->assertFalse($this->ValidateUser->validates());
  1548. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1549. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1550. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1551. $encoding = strtolower(Configure::read('App.encoding'));
  1552. $expected = array(
  1553. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1554. 'div' => array('style' => 'display:none;'),
  1555. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1556. '/div'
  1557. );
  1558. $this->assertTags($result, $expected);
  1559. $result = $this->Form->error(
  1560. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1561. );
  1562. $this->assertEquals('Invalid email', $result);
  1563. $result = $this->Form->error(
  1564. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1565. );
  1566. $this->assertEquals('Invalid name', $result);
  1567. $result = $this->Form->error(
  1568. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1569. );
  1570. $result = $this->Form->error(
  1571. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1572. );
  1573. $this->assertEquals('Invalid description', $result);
  1574. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1575. unset($this->ValidateUser->ValidateProfile);
  1576. unset($this->ValidateUser);
  1577. }
  1578. /**
  1579. * testFormValidationMultiRecord method
  1580. *
  1581. * test form error display with multiple records.
  1582. *
  1583. * @return void
  1584. */
  1585. public function testFormValidationMultiRecord() {
  1586. $Contact = ClassRegistry::getObject('Contact');
  1587. $Contact->validationErrors[2] = array(
  1588. 'name' => array('This field cannot be left blank')
  1589. );
  1590. $result = $this->Form->input('Contact.2.name');
  1591. $expected = array(
  1592. 'div' => array('class' => 'input text error'),
  1593. 'label' => array('for' => 'Contact2Name'),
  1594. 'Name',
  1595. '/label',
  1596. 'input' => array(
  1597. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1598. 'class' => 'form-error', 'maxlength' => 255
  1599. ),
  1600. array('div' => array('class' => 'error-message')),
  1601. 'This field cannot be left blank',
  1602. '/div',
  1603. '/div'
  1604. );
  1605. $this->assertTags($result, $expected);
  1606. }
  1607. /**
  1608. * testMultipleInputValidation method
  1609. *
  1610. * test multiple record form validation error display.
  1611. *
  1612. * @return void
  1613. */
  1614. public function testMultipleInputValidation() {
  1615. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1616. $Address->validationErrors[0] = array(
  1617. 'title' => array('This field cannot be empty'),
  1618. 'first_name' => array('This field cannot be empty')
  1619. );
  1620. $Address->validationErrors[1] = array(
  1621. 'last_name' => array('You must have a last name')
  1622. );
  1623. $this->Form->create();
  1624. $result = $this->Form->input('Address.0.title');
  1625. $expected = array(
  1626. 'div' => array('class'),
  1627. 'label' => array('for'),
  1628. 'preg:/[^<]+/',
  1629. '/label',
  1630. 'input' => array(
  1631. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1632. ),
  1633. array('div' => array('class' => 'error-message')),
  1634. 'This field cannot be empty',
  1635. '/div',
  1636. '/div'
  1637. );
  1638. $this->assertTags($result, $expected);
  1639. $result = $this->Form->input('Address.0.first_name');
  1640. $expected = array(
  1641. 'div' => array('class'),
  1642. 'label' => array('for'),
  1643. 'preg:/[^<]+/',
  1644. '/label',
  1645. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1646. array('div' => array('class' => 'error-message')),
  1647. 'This field cannot be empty',
  1648. '/div',
  1649. '/div'
  1650. );
  1651. $this->assertTags($result, $expected);
  1652. $result = $this->Form->input('Address.0.last_name');
  1653. $expected = array(
  1654. 'div' => array('class'),
  1655. 'label' => array('for'),
  1656. 'preg:/[^<]+/',
  1657. '/label',
  1658. 'input' => array('type' => 'text', 'name', 'id'),
  1659. '/div'
  1660. );
  1661. $this->assertTags($result, $expected);
  1662. $result = $this->Form->input('Address.1.last_name');
  1663. $expected = array(
  1664. 'div' => array('class'),
  1665. 'label' => array('for'),
  1666. 'preg:/[^<]+/',
  1667. '/label',
  1668. 'input' => array(
  1669. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1670. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1671. ),
  1672. array('div' => array('class' => 'error-message')),
  1673. 'You must have a last name',
  1674. '/div',
  1675. '/div'
  1676. );
  1677. $this->assertTags($result, $expected);
  1678. }
  1679. /**
  1680. * testInput method
  1681. *
  1682. * Test various incarnations of input().
  1683. *
  1684. * @return void
  1685. */
  1686. public function testInput() {
  1687. $result = $this->Form->input('ValidateUser.balance');
  1688. $expected = array(
  1689. 'div' => array('class'),
  1690. 'label' => array('for'),
  1691. 'Balance',
  1692. '/label',
  1693. 'input' => array('name', 'type' => 'number', 'id'),
  1694. '/div',
  1695. );
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->input('ValidateUser.cost_decimal');
  1698. $expected = array(
  1699. 'div' => array('class'),
  1700. 'label' => array('for'),
  1701. 'Cost Decimal',
  1702. '/label',
  1703. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1704. '/div',
  1705. );
  1706. $this->assertTags($result, $expected);
  1707. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1708. $expected = array(
  1709. 'div' => array('class' => 'input email'),
  1710. 'label' => array('for' => 'custom'),
  1711. 'Email',
  1712. '/label',
  1713. array('input' => array(
  1714. 'type' => 'email', 'name' => 'data[Contact][email]',
  1715. 'id' => 'custom', 'maxlength' => 255
  1716. )),
  1717. '/div'
  1718. );
  1719. $this->assertTags($result, $expected);
  1720. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1721. $expected = array(
  1722. '<div',
  1723. 'label' => array('for' => 'ContactEmail'),
  1724. 'Email',
  1725. '/label',
  1726. array('input' => array(
  1727. 'type' => 'email', 'name' => 'data[Contact][email]',
  1728. 'id' => 'ContactEmail', 'maxlength' => 255
  1729. )),
  1730. '/div'
  1731. );
  1732. $this->assertTags($result, $expected);
  1733. $result = $this->Form->hidden('Contact.idontexist');
  1734. $expected = array('input' => array(
  1735. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1736. 'id' => 'ContactIdontexist'
  1737. ));
  1738. $this->assertTags($result, $expected);
  1739. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1740. $expected = array(
  1741. 'div' => array('class' => 'input text'),
  1742. 'label' => array('for' => 'ContactEmail'),
  1743. 'Email',
  1744. '/label',
  1745. array('input' => array(
  1746. 'type' => 'text', 'name' => 'data[Contact][email]',
  1747. 'id' => 'ContactEmail'
  1748. )),
  1749. '/div'
  1750. );
  1751. $this->assertTags($result, $expected);
  1752. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1753. $expected = array(
  1754. 'div' => array('class' => 'input text'),
  1755. 'label' => array('for' => 'Contact5Email'),
  1756. 'Email',
  1757. '/label',
  1758. array('input' => array(
  1759. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1760. 'id' => 'Contact5Email'
  1761. )),
  1762. '/div'
  1763. );
  1764. $this->assertTags($result, $expected);
  1765. $result = $this->Form->input('Contact.password');
  1766. $expected = array(
  1767. 'div' => array('class' => 'input password'),
  1768. 'label' => array('for' => 'ContactPassword'),
  1769. 'Password',
  1770. '/label',
  1771. array('input' => array(
  1772. 'type' => 'password', 'name' => 'data[Contact][password]',
  1773. 'id' => 'ContactPassword'
  1774. )),
  1775. '/div'
  1776. );
  1777. $this->assertTags($result, $expected);
  1778. $result = $this->Form->input('Contact.email', array(
  1779. 'type' => 'file', 'class' => 'textbox'
  1780. ));
  1781. $expected = array(
  1782. 'div' => array('class' => 'input file'),
  1783. 'label' => array('for' => 'ContactEmail'),
  1784. 'Email',
  1785. '/label',
  1786. array('input' => array(
  1787. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1788. 'id' => 'ContactEmail'
  1789. )),
  1790. '/div'
  1791. );
  1792. $this->assertTags($result, $expected);
  1793. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1794. $result = $this->Form->input('Contact.phone');
  1795. $expected = array(
  1796. 'div' => array('class' => 'input tel'),
  1797. 'label' => array('for' => 'ContactPhone'),
  1798. 'Phone',
  1799. '/label',
  1800. array('input' => array(
  1801. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1802. 'value' => 'Hello &amp; World &gt; weird chars',
  1803. 'id' => 'ContactPhone', 'maxlength' => 255
  1804. )),
  1805. '/div'
  1806. );
  1807. $this->assertTags($result, $expected);
  1808. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1809. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1810. $expected = array(
  1811. 'div' => array('class' => 'input text'),
  1812. 'label' => array('for' => 'myId'),
  1813. 'Field',
  1814. '/label',
  1815. 'input' => array(
  1816. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1817. 'value' => 'My value', 'id' => 'myId'
  1818. ),
  1819. '/div'
  1820. );
  1821. $this->assertTags($result, $expected);
  1822. unset($this->Form->request->data);
  1823. $Contact = ClassRegistry::getObject('Contact');
  1824. $Contact->validationErrors['field'] = array('Badness!');
  1825. $result = $this->Form->input('Contact.field');
  1826. $expected = array(
  1827. 'div' => array('class' => 'input text error'),
  1828. 'label' => array('for' => 'ContactField'),
  1829. 'Field',
  1830. '/label',
  1831. 'input' => array(
  1832. 'type' => 'text', 'name' => 'data[Contact][field]',
  1833. 'id' => 'ContactField', 'class' => 'form-error'
  1834. ),
  1835. array('div' => array('class' => 'error-message')),
  1836. 'Badness!',
  1837. '/div',
  1838. '/div'
  1839. );
  1840. $this->assertTags($result, $expected);
  1841. $result = $this->Form->input('Contact.field', array(
  1842. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1843. ));
  1844. $expected = array(
  1845. 'label' => array('for' => 'ContactField'),
  1846. 'Field',
  1847. '/label',
  1848. 'input' => array(
  1849. 'type' => 'text', 'name' => 'data[Contact][field]',
  1850. 'id' => 'ContactField', 'class' => 'form-error'
  1851. ),
  1852. array('span' => array('class' => 'error-message')),
  1853. 'Badness!',
  1854. '/span'
  1855. );
  1856. $this->assertTags($result, $expected);
  1857. $result = $this->Form->input('Contact.field', array(
  1858. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1859. ));
  1860. $expected = array(
  1861. 'div' => array('class' => 'input text error'),
  1862. 'label' => array('for' => 'ContactField'),
  1863. 'Field',
  1864. '/label',
  1865. 'input' => array(
  1866. 'type' => 'text', 'name' => 'data[Contact][field]',
  1867. 'id' => 'ContactField', 'class' => 'form-error'
  1868. ),
  1869. array('div' => array('class' => 'error')),
  1870. 'Badness!',
  1871. '/div'
  1872. );
  1873. $this->assertTags($result, $expected);
  1874. $result = $this->Form->input('Contact.field', array(
  1875. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1876. ));
  1877. $expected = array(
  1878. 'span' => array('class' => 'input text error'),
  1879. 'label' => array('for' => 'ContactField'),
  1880. 'Field',
  1881. '/label',
  1882. 'input' => array(
  1883. 'type' => 'text', 'name' => 'data[Contact][field]',
  1884. 'id' => 'ContactField', 'class' => 'form-error'
  1885. ),
  1886. 'Badness!',
  1887. '/span'
  1888. );
  1889. $this->assertTags($result, $expected);
  1890. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1891. $expected = array(
  1892. 'div' => array('class' => 'input text error'),
  1893. 'label' => array('for' => 'ContactField'),
  1894. 'Field',
  1895. '/label',
  1896. 'input' => array(
  1897. 'type' => 'text', 'name' => 'data[Contact][field]',
  1898. 'id' => 'ContactField', 'class' => 'form-error'
  1899. ),
  1900. 'A message to you, Rudy',
  1901. array('div' => array('class' => 'error-message')),
  1902. 'Badness!',
  1903. '/div',
  1904. '/div'
  1905. );
  1906. $this->assertTags($result, $expected);
  1907. $this->Form->setEntity(null);
  1908. $this->Form->setEntity('Contact.field');
  1909. $result = $this->Form->input('Contact.field', array(
  1910. 'after' => 'A message to you, Rudy', 'error' => false
  1911. ));
  1912. $expected = array(
  1913. 'div' => array('class' => 'input text'),
  1914. 'label' => array('for' => 'ContactField'),
  1915. 'Field',
  1916. '/label',
  1917. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1918. 'A message to you, Rudy',
  1919. '/div'
  1920. );
  1921. $this->assertTags($result, $expected);
  1922. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1923. $expected = array(
  1924. 'div' => array('class' => 'input text'),
  1925. 'label' => array('for' => 'ObjectField'),
  1926. 'Field',
  1927. '/label',
  1928. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1929. 'A message to you, Rudy',
  1930. '/div'
  1931. );
  1932. $this->assertTags($result, $expected);
  1933. $Contact->validationErrors['field'] = array('minLength');
  1934. $result = $this->Form->input('Contact.field', array(
  1935. 'error' => array(
  1936. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1937. 'maxLength' => 'login too large'
  1938. )
  1939. ));
  1940. $expected = array(
  1941. 'div' => array('class' => 'input text error'),
  1942. 'label' => array('for' => 'ContactField'),
  1943. 'Field',
  1944. '/label',
  1945. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1946. array('div' => array('class' => 'error-message')),
  1947. 'Le login doit contenir au moins 2 caractères',
  1948. '/div',
  1949. '/div'
  1950. );
  1951. $this->assertTags($result, $expected);
  1952. $Contact->validationErrors['field'] = array('maxLength');
  1953. $result = $this->Form->input('Contact.field', array(
  1954. 'error' => array(
  1955. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1956. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1957. 'maxLength' => 'login too large',
  1958. )
  1959. ));
  1960. $expected = array(
  1961. 'div' => array('class' => 'input text error'),
  1962. 'label' => array('for' => 'ContactField'),
  1963. 'Field',
  1964. '/label',
  1965. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1966. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1967. 'login too large',
  1968. '/span',
  1969. '/div'
  1970. );
  1971. $this->assertTags($result, $expected);
  1972. }
  1973. /**
  1974. * Test that inputs with 0 can be created.
  1975. *
  1976. * @return void
  1977. */
  1978. public function testInputZero() {
  1979. $this->Form->create('User');
  1980. $result = $this->Form->input('0');
  1981. $expected = array(
  1982. 'div' => array('class' => 'input text'),
  1983. 'label' => array('for' => 'User0'), '/label',
  1984. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1985. '/div'
  1986. );
  1987. $this->assertTags($result, $expected);
  1988. }
  1989. /**
  1990. * test input() with checkbox creation
  1991. *
  1992. * @return void
  1993. */
  1994. public function testInputCheckbox() {
  1995. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1996. $expected = array(
  1997. 'div' => array('class' => 'input checkbox'),
  1998. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1999. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2000. '/div'
  2001. );
  2002. $this->assertTags($result, $expected);
  2003. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2004. $expected = array(
  2005. 'div' => array('class' => 'input checkbox'),
  2006. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2007. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2008. '/div'
  2009. );
  2010. $this->assertTags($result, $expected);
  2011. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2012. $expected = array(
  2013. 'div' => array('class' => 'input checkbox'),
  2014. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2015. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2016. '/div'
  2017. );
  2018. $this->assertTags($result, $expected);
  2019. $result = $this->Form->input('User.disabled', array(
  2020. 'label' => 'Disabled',
  2021. 'type' => 'checkbox',
  2022. 'data-foo' => 'disabled'
  2023. ));
  2024. $expected = array(
  2025. 'div' => array('class' => 'input checkbox'),
  2026. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2027. array('input' => array(
  2028. 'type' => 'checkbox',
  2029. 'name' => 'data[User][disabled]',
  2030. 'value' => '1',
  2031. 'id' => 'UserDisabled',
  2032. 'data-foo' => 'disabled'
  2033. )),
  2034. 'label' => array('for' => 'UserDisabled'),
  2035. 'Disabled',
  2036. '/label',
  2037. '/div'
  2038. );
  2039. $this->assertTags($result, $expected);
  2040. }
  2041. /**
  2042. * test form->input() with time types.
  2043. *
  2044. */
  2045. public function testInputTime() {
  2046. extract($this->dateRegex);
  2047. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2048. $result = explode(':', $result);
  2049. $this->assertRegExp('/option value="23"/', $result[0]);
  2050. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2051. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2052. $result = explode(':', $result);
  2053. $this->assertRegExp('/option value="23"/', $result[0]);
  2054. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2055. $result = $this->Form->input('Model.field', array(
  2056. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2057. ));
  2058. $result = explode(':', $result);
  2059. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2060. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2061. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2062. $result = $this->Form->input('Model.field', array(
  2063. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2064. ));
  2065. $result = explode(':', $result);
  2066. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2067. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2068. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2069. $result = $this->Form->input('prueba', array(
  2070. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2071. 'maxYear' => date('Y') + 1, 'interval' => 15
  2072. ));
  2073. $result = explode(':', $result);
  2074. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2075. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2076. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2077. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2078. $result = $this->Form->input('Random.start_time', array(
  2079. 'type' => 'time',
  2080. 'selected' => '18:15'
  2081. ));
  2082. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2083. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2084. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2085. $result = $this->Form->input('published', array('type' => 'time'));
  2086. $now = strtotime('now');
  2087. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2088. $now = strtotime('2013-03-09 00:42:21');
  2089. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2090. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2091. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2092. }
  2093. /**
  2094. * Test interval + selected near the hour roll over.
  2095. *
  2096. * @return void
  2097. */
  2098. public function testTimeSelectedWithInterval() {
  2099. $result = $this->Form->input('Model.start_time', array(
  2100. 'type' => 'time',
  2101. 'interval' => 15,
  2102. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2103. ));
  2104. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2105. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2106. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2107. $result = $this->Form->input('Model.start_time', array(
  2108. 'type' => 'time',
  2109. 'interval' => 15,
  2110. 'selected' => '2012-10-23 15:57:00'
  2111. ));
  2112. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2113. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2114. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2115. $result = $this->Form->input('Model.start_time', array(
  2116. 'timeFormat' => 24,
  2117. 'type' => 'time',
  2118. 'interval' => 15,
  2119. 'selected' => '15:57'
  2120. ));
  2121. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2122. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2123. $result = $this->Form->input('Model.start_time', array(
  2124. 'timeFormat' => 24,
  2125. 'type' => 'time',
  2126. 'interval' => 15,
  2127. 'selected' => '23:57'
  2128. ));
  2129. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2130. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2131. $result = $this->Form->input('Model.created', array(
  2132. 'timeFormat' => 24,
  2133. 'type' => 'datetime',
  2134. 'interval' => 15,
  2135. 'selected' => '2012-09-30 23:56'
  2136. ));
  2137. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2138. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2139. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2140. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2141. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2142. }
  2143. /**
  2144. * Test time with selected values around 12:xx:xx
  2145. *
  2146. * @return void
  2147. */
  2148. public function testTimeSelectedWithIntervalTwelve() {
  2149. $result = $this->Form->input('Model.start_time', array(
  2150. 'type' => 'time',
  2151. 'timeFormat' => 12,
  2152. 'interval' => 15,
  2153. 'selected' => '00:00:00'
  2154. ));
  2155. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2156. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2157. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2158. $result = $this->Form->input('Model.start_time', array(
  2159. 'type' => 'time',
  2160. 'timeFormat' => 12,
  2161. 'interval' => 15,
  2162. 'selected' => '12:00:00'
  2163. ));
  2164. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2165. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2166. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2167. $result = $this->Form->input('Model.start_time', array(
  2168. 'type' => 'time',
  2169. 'timeFormat' => 12,
  2170. 'interval' => 15,
  2171. 'selected' => '12:15:00'
  2172. ));
  2173. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2174. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2175. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2176. }
  2177. /**
  2178. * Test interval & timeFormat = 12
  2179. *
  2180. * @return void
  2181. */
  2182. public function testInputTimeWithIntervalAnd12HourFormat() {
  2183. $result = $this->Form->input('Model.start_time', array(
  2184. 'type' => 'time',
  2185. 'timeFormat' => 12,
  2186. 'interval' => 5,
  2187. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2188. ));
  2189. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2190. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2191. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2192. $result = $this->Form->input('Model.start_time', array(
  2193. 'type' => 'time',
  2194. 'timeFormat' => '12',
  2195. 'interval' => 5,
  2196. 'selected' => '2013-04-19 16:30:00'
  2197. ));
  2198. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2199. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2200. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2201. $result = $this->Form->input('Model.start_time', array(
  2202. 'type' => 'time',
  2203. 'timeFormat' => '12',
  2204. 'interval' => 10,
  2205. 'selected' => '2013-05-19 00:33:00'
  2206. ));
  2207. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2208. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2209. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2210. $result = $this->Form->input('Model.start_time', array(
  2211. 'type' => 'time',
  2212. 'timeFormat' => '12',
  2213. 'interval' => 10,
  2214. 'selected' => '2013-05-19 13:33:00'
  2215. ));
  2216. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2217. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2218. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2219. $result = $this->Form->input('Model.start_time', array(
  2220. 'type' => 'time',
  2221. 'timeFormat' => '12',
  2222. 'interval' => 10,
  2223. 'selected' => '2013-05-19 01:33:00'
  2224. ));
  2225. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2226. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2227. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2228. }
  2229. /**
  2230. * test form->input() with datetime, date and time types
  2231. *
  2232. * @return void
  2233. */
  2234. public function testInputDatetime() {
  2235. extract($this->dateRegex);
  2236. $result = $this->Form->input('prueba', array(
  2237. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2238. 'maxYear' => date('Y') + 1, 'interval' => 15
  2239. ));
  2240. $result = explode(':', $result);
  2241. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2242. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2243. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2244. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2245. //related to ticket #5013
  2246. $result = $this->Form->input('Contact.date', array(
  2247. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2248. ));
  2249. $this->assertRegExp('/class="customClass"/', $result);
  2250. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2251. $result = $this->Form->input('Contact.date', array(
  2252. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2253. ));
  2254. $this->assertRegExp('/id="customIdDay"/', $result);
  2255. $this->assertRegExp('/id="customIdMonth"/', $result);
  2256. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2257. $result = $this->Form->input('Model.field', array(
  2258. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2259. ));
  2260. $this->assertRegExp('/id="customIDDay"/', $result);
  2261. $this->assertRegExp('/id="customIDHour"/', $result);
  2262. $result = explode('</select><select', $result);
  2263. $result = explode(':', $result[1]);
  2264. $this->assertRegExp('/option value="23"/', $result[0]);
  2265. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2266. $result = $this->Form->input('Model.field', array(
  2267. 'type' => 'datetime', 'timeFormat' => 12
  2268. ));
  2269. $result = explode('</select><select', $result);
  2270. $result = explode(':', $result[1]);
  2271. $this->assertRegExp('/option value="12"/', $result[0]);
  2272. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2273. $this->Form->request->data = array('Contact' => array('created' => null));
  2274. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2275. $expected = array(
  2276. 'div' => array('class' => 'input date'),
  2277. 'label' => array('for' => 'ContactCreatedMonth'),
  2278. 'Created',
  2279. '/label',
  2280. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2281. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2282. $monthsRegex,
  2283. '/select', '-',
  2284. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2285. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2286. $daysRegex,
  2287. '/select', '-',
  2288. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2289. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2290. $yearsRegex,
  2291. '/select',
  2292. '/div'
  2293. );
  2294. $this->assertTags($result, $expected);
  2295. $this->Form->request->data = array('Contact' => array('created' => null));
  2296. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2297. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2298. $this->Form->request->data = array('Contact' => array('created' => null));
  2299. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2300. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2301. $result = $this->Form->input('Contact.created', array(
  2302. 'type' => 'date',
  2303. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2304. 'timeFormat' => 'NONE'
  2305. ));
  2306. $this->assertRegExp('/for\="created-month"/', $result);
  2307. }
  2308. /**
  2309. * Test generating checkboxes in a loop.
  2310. *
  2311. * @return void
  2312. */
  2313. public function testInputCheckboxesInLoop() {
  2314. for ($i = 1; $i < 5; $i++) {
  2315. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2316. $expected = array(
  2317. 'div' => array('class' => 'input checkbox'),
  2318. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2319. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2320. 'label' => array('for' => "Contact{$i}Email"),
  2321. 'Email',
  2322. '/label',
  2323. '/div'
  2324. );
  2325. $this->assertTags($result, $expected);
  2326. }
  2327. }
  2328. /**
  2329. * Test generating checkboxes with disabled elements.
  2330. *
  2331. * @return void
  2332. */
  2333. public function testInputCheckboxWithDisabledElements() {
  2334. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2335. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2336. $expected = array(
  2337. array('div' => array('class' => 'input select')),
  2338. array('label' => array('for' => "ContactMultiple")),
  2339. 'Multiple',
  2340. '/label',
  2341. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2342. array('div' => array('class' => 'checkbox')),
  2343. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2344. array('label' => array('for' => "ContactMultiple1")),
  2345. 'One',
  2346. '/label',
  2347. '/div',
  2348. array('div' => array('class' => 'checkbox')),
  2349. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2350. array('label' => array('for' => "ContactMultiple2")),
  2351. 'Two',
  2352. '/label',
  2353. '/div',
  2354. array('div' => array('class' => 'checkbox')),
  2355. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2356. array('label' => array('for' => "ContactMultiple3")),
  2357. 'Three',
  2358. '/label',
  2359. '/div',
  2360. '/div'
  2361. );
  2362. $this->assertTags($result, $expected);
  2363. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2364. $this->assertTags($result, $expected);
  2365. $disabled = array('2', 3);
  2366. $expected = array(
  2367. array('div' => array('class' => 'input select')),
  2368. array('label' => array('for' => "ContactMultiple")),
  2369. 'Multiple',
  2370. '/label',
  2371. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2372. array('div' => array('class' => 'checkbox')),
  2373. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2374. array('label' => array('for' => "ContactMultiple1")),
  2375. 'One',
  2376. '/label',
  2377. '/div',
  2378. array('div' => array('class' => 'checkbox')),
  2379. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2380. array('label' => array('for' => "ContactMultiple2")),
  2381. 'Two',
  2382. '/label',
  2383. '/div',
  2384. array('div' => array('class' => 'checkbox')),
  2385. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2386. array('label' => array('for' => "ContactMultiple3")),
  2387. 'Three',
  2388. '/label',
  2389. '/div',
  2390. '/div'
  2391. );
  2392. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2393. $this->assertTags($result, $expected);
  2394. // make sure 50 does only disable 50, and not 50f5c0cf
  2395. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2396. $disabled = array(50);
  2397. $expected = array(
  2398. array('div' => array('class' => 'input select')),
  2399. array('label' => array('for' => "ContactMultiple")),
  2400. 'Multiple',
  2401. '/label',
  2402. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2403. array('div' => array('class' => 'checkbox')),
  2404. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2405. array('label' => array('for' => "ContactMultiple50")),
  2406. 'Fifty',
  2407. '/label',
  2408. '/div',
  2409. array('div' => array('class' => 'checkbox')),
  2410. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2411. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2412. 'Stringy',
  2413. '/label',
  2414. '/div',
  2415. '/div'
  2416. );
  2417. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2418. $this->assertTags($result, $expected);
  2419. }
  2420. /**
  2421. * test input name with leading integer, ensure attributes are generated correctly.
  2422. *
  2423. * @return void
  2424. */
  2425. public function testInputWithLeadingInteger() {
  2426. $result = $this->Form->text('0.Node.title');
  2427. $expected = array(
  2428. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2429. );
  2430. $this->assertTags($result, $expected);
  2431. }
  2432. /**
  2433. * test form->input() with select type inputs.
  2434. *
  2435. * @return void
  2436. */
  2437. public function testInputSelectType() {
  2438. $result = $this->Form->input('email', array(
  2439. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2440. );
  2441. $expected = array(
  2442. 'div' => array('class' => 'input select'),
  2443. 'label' => array('for' => 'email'),
  2444. 'Email',
  2445. '/label',
  2446. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2447. array('option' => array('value' => '')),
  2448. '/option',
  2449. array('option' => array('value' => 'è')),
  2450. 'Firést',
  2451. '/option',
  2452. array('option' => array('value' => 'é')),
  2453. 'Secoènd',
  2454. '/option',
  2455. '/select',
  2456. '/div'
  2457. );
  2458. $this->assertTags($result, $expected);
  2459. $result = $this->Form->input('email', array(
  2460. 'options' => array('First', 'Second'), 'empty' => true)
  2461. );
  2462. $expected = array(
  2463. 'div' => array('class' => 'input select'),
  2464. 'label' => array('for' => 'email'),
  2465. 'Email',
  2466. '/label',
  2467. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2468. array('option' => array('value' => '')),
  2469. '/option',
  2470. array('option' => array('value' => '0')),
  2471. 'First',
  2472. '/option',
  2473. array('option' => array('value' => '1')),
  2474. 'Second',
  2475. '/option',
  2476. '/select',
  2477. '/div'
  2478. );
  2479. $this->assertTags($result, $expected);
  2480. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2481. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2482. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2483. $expected = array(
  2484. 'div' => array('class' => 'input select'),
  2485. 'label' => array('for' => 'ModelUserId'),
  2486. 'User',
  2487. '/label',
  2488. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2489. array('option' => array('value' => '')),
  2490. '/option',
  2491. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2492. 'good',
  2493. '/option',
  2494. array('option' => array('value' => 'other')),
  2495. 'bad',
  2496. '/option',
  2497. '/select',
  2498. '/div'
  2499. );
  2500. $this->assertTags($result, $expected);
  2501. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2502. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2503. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2504. $expected = array(
  2505. 'div' => array('class' => 'input select'),
  2506. 'label' => array('for' => 'ThingUserId'),
  2507. 'User',
  2508. '/label',
  2509. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2510. array('option' => array('value' => '')),
  2511. 'Some Empty',
  2512. '/option',
  2513. array('option' => array('value' => 'value')),
  2514. 'good',
  2515. '/option',
  2516. array('option' => array('value' => 'other')),
  2517. 'bad',
  2518. '/option',
  2519. '/select',
  2520. '/div'
  2521. );
  2522. $this->assertTags($result, $expected);
  2523. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2524. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2525. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2526. $expected = array(
  2527. 'div' => array('class' => 'input select'),
  2528. 'label' => array('for' => 'ThingUserId'),
  2529. 'User',
  2530. '/label',
  2531. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2532. array('option' => array('value' => '')),
  2533. 'Some Empty',
  2534. '/option',
  2535. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2536. 'good',
  2537. '/option',
  2538. array('option' => array('value' => 'other')),
  2539. 'bad',
  2540. '/option',
  2541. '/select',
  2542. '/div'
  2543. );
  2544. $this->assertTags($result, $expected);
  2545. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2546. $this->Form->request->data = array('User' => array('User' => array('value')));
  2547. $result = $this->Form->input('User.User', array('empty' => true));
  2548. $expected = array(
  2549. 'div' => array('class' => 'input select'),
  2550. 'label' => array('for' => 'UserUser'),
  2551. 'User',
  2552. '/label',
  2553. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2554. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2555. array('option' => array('value' => '')),
  2556. '/option',
  2557. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2558. 'good',
  2559. '/option',
  2560. array('option' => array('value' => 'other')),
  2561. 'bad',
  2562. '/option',
  2563. '/select',
  2564. '/div'
  2565. );
  2566. $this->assertTags($result, $expected);
  2567. $this->Form->data = array();
  2568. $result = $this->Form->input('Publisher.id', array(
  2569. 'label' => 'Publisher',
  2570. 'type' => 'select',
  2571. 'multiple' => 'checkbox',
  2572. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2573. ));
  2574. $expected = array(
  2575. array('div' => array('class' => 'input select')),
  2576. array('label' => array('for' => 'PublisherId')),
  2577. 'Publisher',
  2578. '/label',
  2579. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2580. array('div' => array('class' => 'checkbox')),
  2581. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2582. array('label' => array('for' => 'PublisherIdValue1')),
  2583. 'Label 1',
  2584. '/label',
  2585. '/div',
  2586. array('div' => array('class' => 'checkbox')),
  2587. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2588. array('label' => array('for' => 'PublisherIdValue2')),
  2589. 'Label 2',
  2590. '/label',
  2591. '/div',
  2592. '/div'
  2593. );
  2594. $this->assertTags($result, $expected);
  2595. }
  2596. /**
  2597. * test that input() and a non standard primary key makes a hidden input by default.
  2598. *
  2599. * @return void
  2600. */
  2601. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2602. $this->Form->create('User');
  2603. $this->Form->fieldset = array(
  2604. 'User' => array(
  2605. 'fields' => array(
  2606. 'model_id' => array('type' => 'integer')
  2607. ),
  2608. 'validates' => array(),
  2609. 'key' => 'model_id'
  2610. )
  2611. );
  2612. $result = $this->Form->input('model_id');
  2613. $expected = array(
  2614. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2615. );
  2616. $this->assertTags($result, $expected);
  2617. }
  2618. /**
  2619. * test that overriding the magic select type widget is possible
  2620. *
  2621. * @return void
  2622. */
  2623. public function testInputOverridingMagicSelectType() {
  2624. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2625. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2626. $expected = array(
  2627. 'div' => array('class' => 'input text'),
  2628. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2629. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2630. '/div'
  2631. );
  2632. $this->assertTags($result, $expected);
  2633. //Check that magic types still work for plural/singular vars
  2634. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2635. $result = $this->Form->input('Model.type');
  2636. $expected = array(
  2637. 'div' => array('class' => 'input select'),
  2638. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2639. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2640. array('option' => array('value' => 'value')), 'good', '/option',
  2641. array('option' => array('value' => 'other')), 'bad', '/option',
  2642. '/select',
  2643. '/div'
  2644. );
  2645. $this->assertTags($result, $expected);
  2646. }
  2647. /**
  2648. * Test that inferred types do not override developer input
  2649. *
  2650. * @return void
  2651. */
  2652. public function testInputMagicTypeDoesNotOverride() {
  2653. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2654. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2655. $expected = array(
  2656. 'div' => array('class' => 'input checkbox'),
  2657. array('input' => array(
  2658. 'type' => 'hidden',
  2659. 'name' => 'data[Model][user]',
  2660. 'id' => 'ModelUser_',
  2661. 'value' => 0,
  2662. )),
  2663. array('input' => array(
  2664. 'name' => 'data[Model][user]',
  2665. 'type' => 'checkbox',
  2666. 'id' => 'ModelUser',
  2667. 'value' => 1
  2668. )),
  2669. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2670. '/div'
  2671. );
  2672. $this->assertTags($result, $expected);
  2673. }
  2674. /**
  2675. * Test that magic input() selects are created for type=number
  2676. *
  2677. * @return void
  2678. */
  2679. public function testInputMagicSelectForTypeNumber() {
  2680. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2681. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2682. $result = $this->Form->input('ValidateUser.balance');
  2683. $expected = array(
  2684. 'div' => array('class' => 'input select'),
  2685. 'label' => array('for' => 'ValidateUserBalance'),
  2686. 'Balance',
  2687. '/label',
  2688. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2689. array('option' => array('value' => '0')),
  2690. 'nothing',
  2691. '/option',
  2692. array('option' => array('value' => '1', 'selected' => 'selected')),
  2693. 'some',
  2694. '/option',
  2695. array('option' => array('value' => '100')),
  2696. 'a lot',
  2697. '/option',
  2698. '/select',
  2699. '/div'
  2700. );
  2701. $this->assertTags($result, $expected);
  2702. }
  2703. /**
  2704. * Test that magic input() selects can easily be converted into radio types without error.
  2705. *
  2706. * @return void
  2707. */
  2708. public function testInputMagicSelectChangeToRadio() {
  2709. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2710. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2711. $this->assertRegExp('/input type="radio"/', $result);
  2712. }
  2713. /**
  2714. * fields with the same name as the model should work.
  2715. *
  2716. * @return void
  2717. */
  2718. public function testInputWithMatchingFieldAndModelName() {
  2719. $this->Form->create('User');
  2720. $this->Form->fieldset = array(
  2721. 'User' => array(
  2722. 'fields' => array(
  2723. 'User' => array('type' => 'text')
  2724. ),
  2725. 'validates' => array(),
  2726. 'key' => 'id'
  2727. )
  2728. );
  2729. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2730. $result = $this->Form->input('User', array('type' => 'text'));
  2731. $expected = array(
  2732. 'div' => array('class' => 'input text'),
  2733. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2734. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2735. '/div'
  2736. );
  2737. $this->assertTags($result, $expected);
  2738. }
  2739. /**
  2740. * testFormInputs method
  2741. *
  2742. * test correct results from form::inputs().
  2743. *
  2744. * @return void
  2745. */
  2746. public function testFormInputs() {
  2747. $this->Form->create('Contact');
  2748. $result = $this->Form->inputs('The Legend');
  2749. $expected = array(
  2750. '<fieldset',
  2751. '<legend',
  2752. 'The Legend',
  2753. '/legend',
  2754. '*/fieldset',
  2755. );
  2756. $this->assertTags($result, $expected);
  2757. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2758. $expected = array(
  2759. 'fieldset' => array('class' => 'classy-stuff'),
  2760. '<legend',
  2761. 'Field of Dreams',
  2762. '/legend',
  2763. '*/fieldset'
  2764. );
  2765. $this->assertTags($result, $expected);
  2766. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2767. $this->assertTags($result, $expected);
  2768. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2769. $this->assertTags($result, $expected);
  2770. $this->Form->create('Contact');
  2771. $this->Form->request['prefix'] = 'admin';
  2772. $this->Form->request['action'] = 'admin_edit';
  2773. $result = $this->Form->inputs();
  2774. $expected = array(
  2775. '<fieldset',
  2776. '<legend',
  2777. 'Edit Contact',
  2778. '/legend',
  2779. '*/fieldset',
  2780. );
  2781. $this->assertTags($result, $expected);
  2782. $this->Form->create('Contact');
  2783. $result = $this->Form->inputs(false);
  2784. $expected = array(
  2785. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2786. array('div' => array('class' => 'input text')),
  2787. '*/div',
  2788. array('div' => array('class' => 'input email')),
  2789. '*/div',
  2790. array('div' => array('class' => 'input tel')),
  2791. '*/div',
  2792. array('div' => array('class' => 'input password')),
  2793. '*/div',
  2794. array('div' => array('class' => 'input date')),
  2795. '*/div',
  2796. array('div' => array('class' => 'input date')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input datetime')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input number')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input select')),
  2803. '*/div',
  2804. );
  2805. $this->assertTags($result, $expected);
  2806. $this->Form->create('Contact');
  2807. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2808. $expected = array(
  2809. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2810. array('div' => array('class' => 'input text')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input email')),
  2813. '*/div',
  2814. array('div' => array('class' => 'input tel')),
  2815. '*/div',
  2816. array('div' => array('class' => 'input password')),
  2817. '*/div',
  2818. array('div' => array('class' => 'input date')),
  2819. '*/div',
  2820. array('div' => array('class' => 'input date')),
  2821. '*/div',
  2822. array('div' => array('class' => 'input datetime')),
  2823. '*/div',
  2824. array('div' => array('class' => 'input number')),
  2825. '*/div',
  2826. array('div' => array('class' => 'input select')),
  2827. '*/div',
  2828. );
  2829. $this->assertTags($result, $expected);
  2830. $this->Form->create('Contact');
  2831. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2832. $this->assertTags($result, $expected);
  2833. $this->Form->create('Contact');
  2834. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2835. $expected = array(
  2836. 'fieldset' => array(),
  2837. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2838. array('div' => array('class' => 'input text')),
  2839. '*/div',
  2840. array('div' => array('class' => 'input email')),
  2841. '*/div',
  2842. array('div' => array('class' => 'input tel')),
  2843. '*/div',
  2844. array('div' => array('class' => 'input password')),
  2845. '*/div',
  2846. array('div' => array('class' => 'input date')),
  2847. '*/div',
  2848. array('div' => array('class' => 'input date')),
  2849. '*/div',
  2850. array('div' => array('class' => 'input datetime')),
  2851. '*/div',
  2852. array('div' => array('class' => 'input number')),
  2853. '*/div',
  2854. array('div' => array('class' => 'input select')),
  2855. '*/div',
  2856. '/fieldset'
  2857. );
  2858. $this->assertTags($result, $expected);
  2859. $this->Form->create('Contact');
  2860. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2861. $expected = array(
  2862. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2863. array('div' => array('class' => 'input text')),
  2864. '*/div',
  2865. array('div' => array('class' => 'input email')),
  2866. '*/div',
  2867. array('div' => array('class' => 'input tel')),
  2868. '*/div',
  2869. array('div' => array('class' => 'input password')),
  2870. '*/div',
  2871. array('div' => array('class' => 'input date')),
  2872. '*/div',
  2873. array('div' => array('class' => 'input date')),
  2874. '*/div',
  2875. array('div' => array('class' => 'input datetime')),
  2876. '*/div',
  2877. array('div' => array('class' => 'input number')),
  2878. '*/div',
  2879. array('div' => array('class' => 'input select')),
  2880. '*/div',
  2881. );
  2882. $this->assertTags($result, $expected);
  2883. $this->Form->create('Contact');
  2884. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2885. $this->assertTags($result, $expected);
  2886. $this->Form->create('Contact');
  2887. $result = $this->Form->inputs('Hello');
  2888. $expected = array(
  2889. 'fieldset' => array(),
  2890. 'legend' => array(),
  2891. 'Hello',
  2892. '/legend',
  2893. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2894. array('div' => array('class' => 'input text')),
  2895. '*/div',
  2896. array('div' => array('class' => 'input email')),
  2897. '*/div',
  2898. array('div' => array('class' => 'input tel')),
  2899. '*/div',
  2900. array('div' => array('class' => 'input password')),
  2901. '*/div',
  2902. array('div' => array('class' => 'input date')),
  2903. '*/div',
  2904. array('div' => array('class' => 'input date')),
  2905. '*/div',
  2906. array('div' => array('class' => 'input datetime')),
  2907. '*/div',
  2908. array('div' => array('class' => 'input number')),
  2909. '*/div',
  2910. array('div' => array('class' => 'input select')),
  2911. '*/div',
  2912. '/fieldset'
  2913. );
  2914. $this->assertTags($result, $expected);
  2915. $this->Form->create('Contact');
  2916. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2917. $expected = array(
  2918. 'fieldset' => array(),
  2919. 'legend' => array(),
  2920. 'Hello',
  2921. '/legend',
  2922. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2923. array('div' => array('class' => 'input text')),
  2924. '*/div',
  2925. array('div' => array('class' => 'input email')),
  2926. '*/div',
  2927. array('div' => array('class' => 'input tel')),
  2928. '*/div',
  2929. array('div' => array('class' => 'input password')),
  2930. '*/div',
  2931. array('div' => array('class' => 'input date')),
  2932. '*/div',
  2933. array('div' => array('class' => 'input date')),
  2934. '*/div',
  2935. array('div' => array('class' => 'input datetime')),
  2936. '*/div',
  2937. array('div' => array('class' => 'input number')),
  2938. '*/div',
  2939. array('div' => array('class' => 'input select')),
  2940. '*/div',
  2941. '/fieldset'
  2942. );
  2943. $this->assertTags($result, $expected);
  2944. $this->Form->create('Contact');
  2945. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2946. $this->assertTags($result, $expected);
  2947. $this->Form->end();
  2948. $this->Form->create(false);
  2949. $expected = array(
  2950. 'fieldset' => array(),
  2951. array('div' => array('class' => 'input text')),
  2952. 'label' => array('for' => 'foo'),
  2953. 'Foo',
  2954. '/label',
  2955. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2956. '*/div',
  2957. '/fieldset'
  2958. );
  2959. $result = $this->Form->inputs(
  2960. array('foo' => array('type' => 'text')),
  2961. array(),
  2962. array('legend' => false)
  2963. );
  2964. $this->assertTags($result, $expected);
  2965. }
  2966. /**
  2967. * Tests inputs() works with plugin models
  2968. *
  2969. * @return void
  2970. */
  2971. public function testInputsPluginModel() {
  2972. $this->loadFixtures('Post');
  2973. App::build(array(
  2974. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2975. ));
  2976. CakePlugin::load('TestPlugin');
  2977. $this->Form->request['models'] = array(
  2978. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2979. );
  2980. $this->Form->create('TestPlugin.TestPluginPost');
  2981. $result = $this->Form->inputs();
  2982. $this->assertContains('data[TestPluginPost][id]', $result);
  2983. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2984. $this->assertContains('data[TestPluginPost][title]', $result);
  2985. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2986. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2987. $this->assertEquals('TestPluginPost', $this->Form->model());
  2988. }
  2989. /**
  2990. * testSelectAsCheckbox method
  2991. *
  2992. * test multi-select widget with checkbox formatting.
  2993. *
  2994. * @return void
  2995. */
  2996. public function testSelectAsCheckbox() {
  2997. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2998. $expected = array(
  2999. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3000. array('div' => array('class' => 'checkbox')),
  3001. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  3002. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  3003. 'first',
  3004. '/label',
  3005. '/div',
  3006. array('div' => array('class' => 'checkbox')),
  3007. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  3008. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  3009. 'second',
  3010. '/label',
  3011. '/div',
  3012. array('div' => array('class' => 'checkbox')),
  3013. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3014. array('label' => array('for' => 'ModelMultiField2')),
  3015. 'third',
  3016. '/label',
  3017. '/div',
  3018. );
  3019. $this->assertTags($result, $expected);
  3020. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  3021. $expected = array(
  3022. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3023. array('div' => array('class' => 'checkbox')),
  3024. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/2')),
  3025. array('label' => array('for' => 'ModelMultiField1/2')),
  3026. 'half',
  3027. '/label',
  3028. '/div',
  3029. );
  3030. $this->assertTags($result, $expected);
  3031. }
  3032. /**
  3033. * testLabel method
  3034. *
  3035. * test label generation.
  3036. *
  3037. * @return void
  3038. */
  3039. public function testLabel() {
  3040. $this->Form->text('Person.name');
  3041. $result = $this->Form->label();
  3042. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3043. $this->Form->text('Person.name');
  3044. $result = $this->Form->label();
  3045. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3046. $result = $this->Form->label('Person.first_name');
  3047. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3048. $result = $this->Form->label('Person.first_name', 'Your first name');
  3049. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3050. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3051. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3052. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3053. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3054. $result = $this->Form->label('Person.first_name', '');
  3055. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3056. $result = $this->Form->label('Person.2.name', '');
  3057. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3058. }
  3059. /**
  3060. * testTextbox method
  3061. *
  3062. * test textbox element generation
  3063. *
  3064. * @return void
  3065. */
  3066. public function testTextbox() {
  3067. $result = $this->Form->text('Model.field');
  3068. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3069. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3070. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3071. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3072. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3073. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3074. $result = $this->Form->text('Model.text');
  3075. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3076. $Contact = ClassRegistry::getObject('Contact');
  3077. $Contact->validationErrors['text'] = array(true);
  3078. $this->Form->request->data['Contact']['text'] = 'test';
  3079. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3080. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3081. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3082. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3083. $expected = array(
  3084. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3085. );
  3086. $this->assertTags($result, $expected);
  3087. }
  3088. /**
  3089. * testDefaultValue method
  3090. *
  3091. * Test default value setting
  3092. *
  3093. * @return void
  3094. */
  3095. public function testDefaultValue() {
  3096. $this->Form->request->data['Model']['field'] = 'test';
  3097. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3098. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3099. unset($this->Form->request->data['Model']['field']);
  3100. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3101. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3102. }
  3103. /**
  3104. * testCheckboxDefaultValue method
  3105. *
  3106. * Test default value setting on checkbox() method
  3107. *
  3108. * @return void
  3109. */
  3110. public function testCheckboxDefaultValue() {
  3111. $this->Form->request->data['Model']['field'] = false;
  3112. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3113. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3114. unset($this->Form->request->data['Model']['field']);
  3115. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3116. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3117. $this->Form->request->data['Model']['field'] = true;
  3118. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3119. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3120. unset($this->Form->request->data['Model']['field']);
  3121. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3122. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3123. }
  3124. /**
  3125. * testError method
  3126. *
  3127. * Test field error generation
  3128. *
  3129. * @return void
  3130. */
  3131. public function testError() {
  3132. $Contact = ClassRegistry::getObject('Contact');
  3133. $Contact->validationErrors['field'] = array(1);
  3134. $result = $this->Form->error('Contact.field');
  3135. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3136. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3137. $this->assertEquals('Error in field Field', $result);
  3138. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3139. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3140. $this->assertEquals('This field contains invalid input', $result);
  3141. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3142. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3143. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3144. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3145. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3146. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3147. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3148. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3149. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3150. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3151. $this->assertEquals('<strong>Badness!</strong>', $result);
  3152. $Contact->validationErrors['field'] = array("email");
  3153. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3154. $expected = array(
  3155. 'div' => array('class' => 'field-error'),
  3156. 'No good!',
  3157. '/div'
  3158. );
  3159. $this->assertTags($result, $expected);
  3160. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3161. $result = $this->Form->error('Contact.field', array(
  3162. 'notEmpty' => 'Cannot be empty',
  3163. 'email' => 'No good!'
  3164. ));
  3165. $expected = array(
  3166. 'div' => array('class' => 'error-message'),
  3167. 'ul' => array(),
  3168. '<li', 'Cannot be empty', '/li',
  3169. '<li', 'No good!', '/li',
  3170. '<li', 'Something else', '/li',
  3171. '/ul',
  3172. '/div'
  3173. );
  3174. $this->assertTags($result, $expected);
  3175. // Testing error messages list options
  3176. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3177. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3178. $expected = array(
  3179. 'div' => array('class' => 'error-message'),
  3180. 'ol' => array(),
  3181. '<li', 'notEmpty', '/li',
  3182. '<li', 'email', '/li',
  3183. '/ol',
  3184. '/div'
  3185. );
  3186. $this->assertTags($result, $expected);
  3187. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3188. $expected = array(
  3189. 'div' => array('class' => 'error-message'),
  3190. 'ol' => array(),
  3191. '<li', 'notEmpty', '/li',
  3192. '<li', 'email', '/li',
  3193. '/ol',
  3194. '/div'
  3195. );
  3196. $this->assertTags($result, $expected);
  3197. $result = $this->Form->error('Contact.field', null, array(
  3198. 'listOptions' => array(
  3199. 'class' => 'ul-class',
  3200. 'itemOptions' => array(
  3201. 'class' => 'li-class'
  3202. )
  3203. )
  3204. ));
  3205. $expected = array(
  3206. 'div' => array('class' => 'error-message'),
  3207. 'ul' => array('class' => 'ul-class'),
  3208. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3209. array('li' => array('class' => 'li-class')), 'email', '/li',
  3210. '/ul',
  3211. '/div'
  3212. );
  3213. $this->assertTags($result, $expected);
  3214. }
  3215. /**
  3216. * test error options when using form->input();
  3217. *
  3218. * @return void
  3219. */
  3220. public function testInputErrorEscape() {
  3221. $this->Form->create('ValidateProfile');
  3222. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3223. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3224. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3225. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3226. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3227. $this->assertRegExp('/required<br>/', $result);
  3228. }
  3229. /**
  3230. * testPassword method
  3231. *
  3232. * Test password element generation
  3233. *
  3234. * @return void
  3235. */
  3236. public function testPassword() {
  3237. $Contact = ClassRegistry::getObject('Contact');
  3238. $result = $this->Form->password('Contact.field');
  3239. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3240. $Contact->validationErrors['passwd'] = 1;
  3241. $this->Form->request->data['Contact']['passwd'] = 'test';
  3242. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3243. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3244. }
  3245. /**
  3246. * testRadio method
  3247. *
  3248. * Test radio element set generation
  3249. *
  3250. * @return void
  3251. */
  3252. public function testRadio() {
  3253. $result = $this->Form->radio('Model.field', array('option A'));
  3254. $expected = array(
  3255. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3256. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3257. 'label' => array('for' => 'ModelField0'),
  3258. 'option A',
  3259. '/label'
  3260. );
  3261. $this->assertTags($result, $expected);
  3262. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3263. $expected = array(
  3264. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3265. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField1/2')),
  3266. 'label' => array('for' => 'ModelField1/2'),
  3267. 'half',
  3268. '/label'
  3269. );
  3270. $this->assertTags($result, $expected);
  3271. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3272. $expected = array(
  3273. 'fieldset' => array(),
  3274. 'legend' => array(),
  3275. 'Field',
  3276. '/legend',
  3277. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3278. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3279. array('label' => array('for' => 'ModelField0')),
  3280. 'option A',
  3281. '/label',
  3282. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3283. array('label' => array('for' => 'ModelField1')),
  3284. 'option B',
  3285. '/label',
  3286. '/fieldset'
  3287. );
  3288. $this->assertTags($result, $expected);
  3289. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3290. $expected = array(
  3291. 'fieldset' => array(),
  3292. 'legend' => array(),
  3293. 'Field',
  3294. '/legend',
  3295. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3296. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3297. array('label' => array('for' => 'ModelField0')),
  3298. 'option A',
  3299. '/label',
  3300. 'br' => array(),
  3301. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3302. array('label' => array('for' => 'ModelField1')),
  3303. 'option B',
  3304. '/label',
  3305. '/fieldset'
  3306. );
  3307. $this->assertTags($result, $expected);
  3308. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3309. $expected = array(
  3310. 'fieldset' => array(),
  3311. 'legend' => array(),
  3312. 'Gender',
  3313. '/legend',
  3314. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3315. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3316. array('label' => array('for' => 'EmployeeGenderMale')),
  3317. 'Male',
  3318. '/label',
  3319. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3320. array('label' => array('for' => 'EmployeeGenderFemale')),
  3321. 'Female',
  3322. '/label',
  3323. '/fieldset',
  3324. );
  3325. $this->assertTags($result, $expected);
  3326. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3327. $expected = array(
  3328. 'fieldset' => array(),
  3329. 'legend' => array(),
  3330. 'Gender',
  3331. '/legend',
  3332. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3333. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3334. array('label' => array('for' => 'OfficerGenderMale')),
  3335. 'Male',
  3336. '/label',
  3337. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3338. array('label' => array('for' => 'OfficerGenderFemale')),
  3339. 'Female',
  3340. '/label',
  3341. '/fieldset',
  3342. );
  3343. $this->assertTags($result, $expected);
  3344. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3345. $expected = array(
  3346. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3347. array('input' => array(
  3348. 'type' => 'radio',
  3349. 'name' => 'data[Contact][1][imrequired]',
  3350. 'value' => '0',
  3351. 'id' => 'Contact1Imrequired0',
  3352. 'required' => 'required'
  3353. )),
  3354. 'label' => array('for' => 'Contact1Imrequired0'),
  3355. 'option A',
  3356. '/label'
  3357. );
  3358. $this->assertTags($result, $expected);
  3359. $result = $this->Form->radio('Model.1.field', array('option A'));
  3360. $expected = array(
  3361. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3362. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3363. 'label' => array('for' => 'Model1Field0'),
  3364. 'option A',
  3365. '/label'
  3366. );
  3367. $this->assertTags($result, $expected);
  3368. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3369. $expected = array(
  3370. 'fieldset' => array(),
  3371. 'legend' => array(),
  3372. 'Field',
  3373. '/legend',
  3374. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3375. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3376. array('label' => array('for' => 'ModelField0')),
  3377. 'option A',
  3378. '/label',
  3379. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3380. array('label' => array('for' => 'ModelField1')),
  3381. 'option B',
  3382. '/label',
  3383. '/fieldset'
  3384. );
  3385. $this->assertTags($result, $expected);
  3386. $result = $this->Form->radio(
  3387. 'Model.field',
  3388. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3389. );
  3390. $expected = array(
  3391. 'fieldset' => array(),
  3392. 'legend' => array(),
  3393. 'Field',
  3394. '/legend',
  3395. 'input' => array(
  3396. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3397. 'id' => 'ModelField_', 'value' => '',
  3398. ),
  3399. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3400. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3401. array('label' => array('for' => 'ModelFieldAB')),
  3402. 'first',
  3403. '/label',
  3404. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3405. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3406. array('label' => array('for' => 'ModelFieldAB1')),
  3407. 'second',
  3408. '/label',
  3409. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3410. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3411. array('label' => array('for' => 'ModelFieldAB2')),
  3412. 'third',
  3413. '/label',
  3414. '/fieldset'
  3415. );
  3416. $this->assertTags($result, $expected);
  3417. }
  3418. /**
  3419. * Test radio inputs with between as string or array. Also ensure
  3420. * that an array with less between elements works.
  3421. *
  3422. * @return void
  3423. */
  3424. public function testRadioBetween() {
  3425. $result = $this->Form->radio(
  3426. 'Model.field',
  3427. array('option A', 'option B'),
  3428. array('between' => 'I am between')
  3429. );
  3430. $expected = array(
  3431. 'fieldset' => array(),
  3432. 'legend' => array(),
  3433. 'Field',
  3434. '/legend',
  3435. 'I am between',
  3436. 'input' => array(
  3437. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3438. 'value' => '', 'id' => 'ModelField_'
  3439. ),
  3440. array('input' => array(
  3441. 'type' => 'radio', 'name' => 'data[Model][field]',
  3442. 'value' => '0', 'id' => 'ModelField0'
  3443. )),
  3444. array('label' => array('for' => 'ModelField0')),
  3445. 'option A',
  3446. '/label',
  3447. array('input' => array(
  3448. 'type' => 'radio', 'name' => 'data[Model][field]',
  3449. 'value' => '1', 'id' => 'ModelField1'
  3450. )),
  3451. array('label' => array('for' => 'ModelField1')),
  3452. 'option B',
  3453. '/label',
  3454. '/fieldset'
  3455. );
  3456. $this->assertTags($result, $expected);
  3457. $result = $this->Form->radio(
  3458. 'Model.field',
  3459. array('option A', 'option B', 'option C'),
  3460. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3461. );
  3462. $expected = array(
  3463. 'fieldset' => array(),
  3464. 'legend' => array(),
  3465. 'Field',
  3466. '/legend',
  3467. 'input' => array(
  3468. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3469. 'value' => '', 'id' => 'ModelField_'
  3470. ),
  3471. array('input' => array(
  3472. 'type' => 'radio', 'name' => 'data[Model][field]',
  3473. 'value' => '0', 'id' => 'ModelField0'
  3474. )),
  3475. array('label' => array('for' => 'ModelField0')),
  3476. 'option A',
  3477. '/label',
  3478. 'between A',
  3479. '--separator--',
  3480. array('input' => array(
  3481. 'type' => 'radio', 'name' => 'data[Model][field]',
  3482. 'value' => '1', 'id' => 'ModelField1'
  3483. )),
  3484. array('label' => array('for' => 'ModelField1')),
  3485. 'option B',
  3486. '/label',
  3487. 'between B',
  3488. '--separator--',
  3489. array('input' => array(
  3490. 'type' => 'radio', 'name' => 'data[Model][field]',
  3491. 'value' => '2', 'id' => 'ModelField2'
  3492. )),
  3493. array('label' => array('for' => 'ModelField2')),
  3494. 'option C',
  3495. '/label',
  3496. 'between C',
  3497. '/fieldset'
  3498. );
  3499. $this->assertTags($result, $expected);
  3500. $result = $this->Form->input('Model.field', array(
  3501. 'options' => array('1' => 'first', '2' => 'second'),
  3502. 'type' => 'radio',
  3503. 'before' => '--before--',
  3504. 'after' => '--after--',
  3505. 'separator' => '--separator--',
  3506. 'between' => array('--between first--', '--between second--')
  3507. ));
  3508. $expected = array(
  3509. 'div' => array('class' => 'input radio'),
  3510. '--before--',
  3511. 'fieldset' => array(),
  3512. 'legend' => array(),
  3513. 'Field',
  3514. '/legend',
  3515. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3516. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3517. array('label' => array('for' => 'ModelField1')),
  3518. 'first',
  3519. '/label',
  3520. '--between first--',
  3521. '--separator--',
  3522. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3523. array('label' => array('for' => 'ModelField2')),
  3524. 'second',
  3525. '/label',
  3526. '--between second--',
  3527. '/fieldset',
  3528. '--after--',
  3529. '/div'
  3530. );
  3531. $this->assertTags($result, $expected);
  3532. $result = $this->Form->input('Model.field', array(
  3533. 'options' => array('1' => 'first', '2' => 'second'),
  3534. 'type' => 'radio',
  3535. 'before' => '--before--',
  3536. 'after' => '--after--',
  3537. 'separator' => '--separator--',
  3538. 'between' => array('--between first--')
  3539. ));
  3540. $expected = array(
  3541. 'div' => array('class' => 'input radio'),
  3542. '--before--',
  3543. 'fieldset' => array(),
  3544. 'legend' => array(),
  3545. 'Field',
  3546. '/legend',
  3547. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3548. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3549. array('label' => array('for' => 'ModelField1')),
  3550. 'first',
  3551. '/label',
  3552. '--between first--',
  3553. '--separator--',
  3554. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3555. array('label' => array('for' => 'ModelField2')),
  3556. 'second',
  3557. '/label',
  3558. '/fieldset',
  3559. '--after--',
  3560. '/div'
  3561. );
  3562. $this->assertTags($result, $expected);
  3563. }
  3564. /**
  3565. * Test that radios with a 0 value are selected under the correct conditions.
  3566. * Also ensure that values that are booleanish are handled correctly.
  3567. *
  3568. * @return void
  3569. */
  3570. public function testRadioOptionWithBooleanishValues() {
  3571. $expected = array(
  3572. 'fieldset' => array(),
  3573. 'legend' => array(),
  3574. 'Field',
  3575. '/legend',
  3576. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3577. array('label' => array('for' => 'ModelField1')),
  3578. 'Yes',
  3579. '/label',
  3580. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3581. array('label' => array('for' => 'ModelField0')),
  3582. 'No',
  3583. '/label',
  3584. '/fieldset'
  3585. );
  3586. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3587. $this->assertTags($result, $expected);
  3588. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3589. $this->assertTags($result, $expected);
  3590. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3591. $this->assertTags($result, $expected);
  3592. $expected = array(
  3593. 'fieldset' => array(),
  3594. 'legend' => array(),
  3595. 'Field',
  3596. '/legend',
  3597. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3598. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3599. array('label' => array('for' => 'ModelField1')),
  3600. 'Yes',
  3601. '/label',
  3602. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3603. array('label' => array('for' => 'ModelField0')),
  3604. 'No',
  3605. '/label',
  3606. '/fieldset'
  3607. );
  3608. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3609. $this->assertTags($result, $expected);
  3610. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3611. $this->assertTags($result, $expected);
  3612. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3613. $this->assertTags($result, $expected);
  3614. $expected = array(
  3615. 'fieldset' => array(),
  3616. 'legend' => array(),
  3617. 'Field',
  3618. '/legend',
  3619. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3620. array('label' => array('for' => 'ModelField1')),
  3621. 'Yes',
  3622. '/label',
  3623. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3624. array('label' => array('for' => 'ModelField0')),
  3625. 'No',
  3626. '/label',
  3627. '/fieldset'
  3628. );
  3629. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3630. $this->assertTags($result, $expected);
  3631. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3632. $this->assertTags($result, $expected);
  3633. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3634. $this->assertTags($result, $expected);
  3635. }
  3636. /**
  3637. * test disabled radio options
  3638. *
  3639. * @return void
  3640. */
  3641. public function testRadioDisabled() {
  3642. $result = $this->Form->radio(
  3643. 'Model.field',
  3644. array('option A', 'option B'),
  3645. array('disabled' => array(0), 'value' => '0')
  3646. );
  3647. $expected = array(
  3648. 'fieldset' => array(),
  3649. 'legend' => array(),
  3650. 'Field',
  3651. '/legend',
  3652. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3653. array('label' => array('for' => 'ModelField0')),
  3654. 'option A',
  3655. '/label',
  3656. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3657. array('label' => array('for' => 'ModelField1')),
  3658. 'option B',
  3659. '/label',
  3660. '/fieldset'
  3661. );
  3662. $this->assertTags($result, $expected);
  3663. $result = $this->Form->radio(
  3664. 'Model.field',
  3665. array('option A', 'option B'),
  3666. array('disabled' => true, 'value' => '0')
  3667. );
  3668. $expected = array(
  3669. 'fieldset' => array(),
  3670. 'legend' => array(),
  3671. 'Field',
  3672. '/legend',
  3673. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3674. array('label' => array('for' => 'ModelField0')),
  3675. 'option A',
  3676. '/label',
  3677. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3678. array('label' => array('for' => 'ModelField1')),
  3679. 'option B',
  3680. '/label',
  3681. '/fieldset'
  3682. );
  3683. $this->assertTags($result, $expected);
  3684. $result = $this->Form->radio(
  3685. 'Model.field',
  3686. array('option A', 'option B'),
  3687. array('disabled' => 'disabled', 'value' => '0')
  3688. );
  3689. $expected = array(
  3690. 'fieldset' => array(),
  3691. 'legend' => array(),
  3692. 'Field',
  3693. '/legend',
  3694. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3695. array('label' => array('for' => 'ModelField0')),
  3696. 'option A',
  3697. '/label',
  3698. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3699. array('label' => array('for' => 'ModelField1')),
  3700. 'option B',
  3701. '/label',
  3702. '/fieldset'
  3703. );
  3704. $this->assertTags($result, $expected);
  3705. $result = $this->Form->input('Model.field', array(
  3706. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3707. 'type' => 'radio',
  3708. 'disabled' => array(2, '3x'),
  3709. ));
  3710. $expected = array(
  3711. 'div' => array('class' => 'input radio'),
  3712. 'fieldset' => array(),
  3713. 'legend' => array(),
  3714. 'Field',
  3715. '/legend',
  3716. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3717. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3718. array('label' => array('for' => 'ModelField1')),
  3719. 'first',
  3720. '/label',
  3721. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3722. array('label' => array('for' => 'ModelField2')),
  3723. 'second',
  3724. '/label',
  3725. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3726. array('label' => array('for' => 'ModelField2x')),
  3727. '2x',
  3728. '/label',
  3729. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3730. array('label' => array('for' => 'ModelField3')),
  3731. 'third',
  3732. '/label',
  3733. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3734. array('label' => array('for' => 'ModelField3x')),
  3735. '3x',
  3736. '/label',
  3737. '/fieldset',
  3738. '/div'
  3739. );
  3740. $this->assertTags($result, $expected);
  3741. $result = $this->Form->input('Model.field', array(
  3742. 'type' => 'radio',
  3743. 'options' => array(
  3744. 1 => 'A',
  3745. 2 => 'B',
  3746. 3 => 'C'
  3747. ),
  3748. 'disabled' => array(1)
  3749. ));
  3750. $expected = array(
  3751. 'div' => array('class' => 'input radio'),
  3752. 'fieldset' => array(),
  3753. 'legend' => array(),
  3754. 'Field',
  3755. '/legend',
  3756. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3757. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3758. array('label' => array('for' => 'ModelField1')),
  3759. 'A',
  3760. '/label',
  3761. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3762. array('label' => array('for' => 'ModelField2')),
  3763. 'B',
  3764. '/label',
  3765. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3766. array('label' => array('for' => 'ModelField3')),
  3767. 'C',
  3768. '/label',
  3769. '/fieldset',
  3770. '/div'
  3771. );
  3772. $this->assertTags($result, $expected);
  3773. }
  3774. /**
  3775. * test disabling the hidden input for radio buttons
  3776. *
  3777. * @return void
  3778. */
  3779. public function testRadioHiddenInputDisabling() {
  3780. $result = $this->Form->input('Model.1.field', array(
  3781. 'type' => 'radio',
  3782. 'options' => array('option A'),
  3783. 'hiddenField' => false
  3784. )
  3785. );
  3786. $expected = array(
  3787. 'div' => array('class' => 'input radio'),
  3788. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3789. 'label' => array('for' => 'Model1Field0'),
  3790. 'option A',
  3791. '/label',
  3792. '/div'
  3793. );
  3794. $this->assertTags($result, $expected);
  3795. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3796. $expected = array(
  3797. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3798. 'label' => array('for' => 'Model1Field0'),
  3799. 'option A',
  3800. '/label'
  3801. );
  3802. $this->assertTags($result, $expected);
  3803. }
  3804. /**
  3805. * test adding an empty option for radio buttons
  3806. *
  3807. * @return void
  3808. */
  3809. public function testRadioAddEmptyOption() {
  3810. $result = $this->Form->input('Model.1.field', array(
  3811. 'type' => 'radio',
  3812. 'options' => array('option A'),
  3813. 'empty' => true,
  3814. 'hiddenField' => false
  3815. ));
  3816. $expected = array(
  3817. 'div' => array('class' => 'input radio'),
  3818. 'fieldset' => array(),
  3819. 'legend' => array(),
  3820. 'Field',
  3821. '/legend',
  3822. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3823. array('label' => array('for' => 'Model1Field')),
  3824. __('empty'),
  3825. '/label',
  3826. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3827. array('label' => array('for' => 'Model1Field0')),
  3828. 'option A',
  3829. '/label',
  3830. '/fieldset',
  3831. '/div'
  3832. );
  3833. $this->assertTags($result, $expected);
  3834. $result = $this->Form->input('Model.1.field', array(
  3835. 'type' => 'radio',
  3836. 'options' => array('option A'),
  3837. 'empty' => 'CustomEmptyLabel',
  3838. 'hiddenField' => false
  3839. ));
  3840. $expected = array(
  3841. 'div' => array('class' => 'input radio'),
  3842. 'fieldset' => array(),
  3843. 'legend' => array(),
  3844. 'Field',
  3845. '/legend',
  3846. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3847. array('label' => array('for' => 'Model1Field')),
  3848. 'CustomEmptyLabel',
  3849. '/label',
  3850. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3851. array('label' => array('for' => 'Model1Field0')),
  3852. 'option A',
  3853. '/label',
  3854. '/fieldset',
  3855. '/div'
  3856. );
  3857. $this->assertTags($result, $expected);
  3858. $result = $this->Form->input('Model.1.field', array(
  3859. 'type' => 'radio',
  3860. 'options' => array('option A'),
  3861. 'empty' => false,
  3862. 'hiddenField' => false
  3863. ));
  3864. $this->assertTextNotContains('"Model1Field"', $result);
  3865. }
  3866. /**
  3867. * Test that radio() accepts an array for label
  3868. *
  3869. * @return void
  3870. */
  3871. public function testRadioLabelArray() {
  3872. $result = $this->Form->input('Model.field', array(
  3873. 'type' => 'radio',
  3874. 'legend' => false,
  3875. 'label' => array(
  3876. 'class' => 'checkbox float-left',
  3877. ),
  3878. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3879. ));
  3880. $this->assertTextContains(
  3881. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3882. $result
  3883. );
  3884. }
  3885. /**
  3886. * Test that label id's match the input element id's when radio is called after create().
  3887. *
  3888. * @return void
  3889. */
  3890. public function testRadioWithCreate() {
  3891. $this->Form->create('Model');
  3892. $result = $this->Form->radio('recipient',
  3893. array('1' => '1', '2' => '2', '3' => '3'),
  3894. array('legend' => false, 'value' => '1')
  3895. );
  3896. $this->assertTextNotContains(
  3897. '<label for="ModelModelRecipient1">1</label>',
  3898. $result
  3899. );
  3900. $this->assertTextContains(
  3901. '<label for="ModelRecipient1">1</label>',
  3902. $result
  3903. );
  3904. }
  3905. /**
  3906. * testDomIdSuffix method
  3907. *
  3908. * @return void
  3909. */
  3910. public function testDomIdSuffix() {
  3911. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3912. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3913. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3914. $this->assertEquals('AbcX=FooY=Bar', $result);
  3915. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3916. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3917. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3918. $this->assertEquals('AbcXFooYBar', $result);
  3919. }
  3920. /**
  3921. * testDomIdSuffixCollisionResolvement()
  3922. *
  3923. * @return void
  3924. */
  3925. public function testDomIdSuffixCollisionResolvement() {
  3926. $result = $this->Form->domIdSuffix('a>b');
  3927. $this->assertEquals('AB', $result);
  3928. $result = $this->Form->domIdSuffix('a<b');
  3929. $this->assertEquals('AB1', $result);
  3930. $result = $this->Form->domIdSuffix('a\'b');
  3931. $this->assertEquals('AB2', $result);
  3932. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3933. $this->assertEquals('1StringWith1-dollar', $result);
  3934. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3935. $this->assertEquals('1StringWith1-dollar1', $result);
  3936. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3937. $this->assertEquals('1StringWith1-dollar2', $result);
  3938. }
  3939. /**
  3940. * testSelect method
  3941. *
  3942. * Test select element generation.
  3943. *
  3944. * @return void
  3945. */
  3946. public function testSelect() {
  3947. $result = $this->Form->select('Model.field', array());
  3948. $expected = array(
  3949. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3950. array('option' => array('value' => '')),
  3951. '/option',
  3952. '/select'
  3953. );
  3954. $this->assertTags($result, $expected);
  3955. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3956. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3957. $expected = array(
  3958. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3959. array('option' => array('value' => '')),
  3960. '/option',
  3961. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3962. 'good',
  3963. '/option',
  3964. array('option' => array('value' => 'other')),
  3965. 'bad',
  3966. '/option',
  3967. '/select'
  3968. );
  3969. $this->assertTags($result, $expected);
  3970. $this->Form->request->data = array();
  3971. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3972. $expected = array(
  3973. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3974. array('option' => array('value' => '')),
  3975. '/option',
  3976. array('option' => array('value' => 'value')),
  3977. 'good',
  3978. '/option',
  3979. array('option' => array('value' => 'other')),
  3980. 'bad',
  3981. '/option',
  3982. '/select'
  3983. );
  3984. $this->assertTags($result, $expected);
  3985. $result = $this->Form->select(
  3986. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3987. array('empty' => false)
  3988. );
  3989. $expected = array(
  3990. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3991. array('option' => array('value' => 'first')),
  3992. 'first &quot;html&quot; &lt;chars&gt;',
  3993. '/option',
  3994. array('option' => array('value' => 'second')),
  3995. 'value',
  3996. '/option',
  3997. '/select'
  3998. );
  3999. $this->assertTags($result, $expected);
  4000. $result = $this->Form->select(
  4001. 'Model.field',
  4002. array('first' => 'first "html" <chars>', 'second' => 'value'),
  4003. array('escape' => false, 'empty' => false)
  4004. );
  4005. $expected = array(
  4006. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4007. array('option' => array('value' => 'first')),
  4008. 'first "html" <chars>',
  4009. '/option',
  4010. array('option' => array('value' => 'second')),
  4011. 'value',
  4012. '/option',
  4013. '/select'
  4014. );
  4015. $this->assertTags($result, $expected);
  4016. $options = array(
  4017. array('value' => 'first', 'name' => 'First'),
  4018. array('value' => 'first', 'name' => 'Another First'),
  4019. );
  4020. $result = $this->Form->select(
  4021. 'Model.field',
  4022. $options,
  4023. array('escape' => false, 'empty' => false)
  4024. );
  4025. $expected = array(
  4026. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4027. array('option' => array('value' => 'first')),
  4028. 'First',
  4029. '/option',
  4030. array('option' => array('value' => 'first')),
  4031. 'Another First',
  4032. '/option',
  4033. '/select'
  4034. );
  4035. $this->assertTags($result, $expected);
  4036. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  4037. $result = $this->Form->select(
  4038. 'Model.contact_id',
  4039. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4040. array('escape' => false, 'empty' => 'pick something')
  4041. );
  4042. $expected = array(
  4043. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4044. array('option' => array('value' => '')), 'pick something', '/option',
  4045. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4046. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4047. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4048. '/select'
  4049. );
  4050. $this->assertTags($result, $expected);
  4051. $this->Form->request->data['Model']['field'] = 0;
  4052. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4053. $expected = array(
  4054. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4055. array('option' => array('value' => '')), '/option',
  4056. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4057. array('option' => array('value' => '1')), 'Yes', '/option',
  4058. '/select'
  4059. );
  4060. $this->assertTags($result, $expected);
  4061. $this->Form->request->data['Model']['field'] = 50;
  4062. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4063. $expected = array(
  4064. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4065. array('option' => array('value' => '')), '/option',
  4066. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4067. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4068. '/select'
  4069. );
  4070. $this->assertTags($result, $expected);
  4071. $result = $this->Form->select('Contact.required_one', array('option A'));
  4072. $expected = array(
  4073. 'select' => array(
  4074. 'name' => 'data[Contact][required_one]',
  4075. 'id' => 'ContactRequiredOne',
  4076. 'required' => 'required'
  4077. ),
  4078. array('option' => array('value' => '')), '/option',
  4079. array('option' => array('value' => '0')), 'option A', '/option',
  4080. '/select'
  4081. );
  4082. $this->assertTags($result, $expected);
  4083. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4084. $expected = array(
  4085. 'select' => array(
  4086. 'name' => 'data[Contact][required_one]',
  4087. 'id' => 'ContactRequiredOne',
  4088. 'disabled' => 'disabled'
  4089. ),
  4090. array('option' => array('value' => '')), '/option',
  4091. array('option' => array('value' => '0')), 'option A', '/option',
  4092. '/select'
  4093. );
  4094. $this->assertTags($result, $expected);
  4095. }
  4096. /**
  4097. * test that select() with optiongroups listens to the escape param.
  4098. *
  4099. * @return void
  4100. */
  4101. public function testSelectOptionGroupEscaping() {
  4102. $options = array(
  4103. '>< Key' => array(
  4104. 1 => 'One',
  4105. 2 => 'Two'
  4106. )
  4107. );
  4108. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4109. $expected = array(
  4110. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4111. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4112. array('option' => array('value' => '1')), 'One', '/option',
  4113. array('option' => array('value' => '2')), 'Two', '/option',
  4114. '/optgroup',
  4115. '/select'
  4116. );
  4117. $this->assertTags($result, $expected);
  4118. $options = array(
  4119. '>< Key' => array(
  4120. 1 => 'One',
  4121. 2 => 'Two'
  4122. )
  4123. );
  4124. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4125. $expected = array(
  4126. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4127. 'optgroup' => array('label' => '>< Key'),
  4128. array('option' => array('value' => '1')), 'One', '/option',
  4129. array('option' => array('value' => '2')), 'Two', '/option',
  4130. '/optgroup',
  4131. '/select'
  4132. );
  4133. $this->assertTags($result, $expected);
  4134. }
  4135. /**
  4136. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4137. *
  4138. * @return void
  4139. */
  4140. public function testSelectWithNullAttributes() {
  4141. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4142. $expected = array(
  4143. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4144. array('option' => array('value' => '0')),
  4145. 'first',
  4146. '/option',
  4147. array('option' => array('value' => '1')),
  4148. 'second',
  4149. '/option',
  4150. '/select'
  4151. );
  4152. $this->assertTags($result, $expected);
  4153. }
  4154. /**
  4155. * testNestedSelect method
  4156. *
  4157. * test select element generation with optgroups
  4158. *
  4159. * @return void
  4160. */
  4161. public function testNestedSelect() {
  4162. $result = $this->Form->select(
  4163. 'Model.field',
  4164. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4165. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4166. )), array('empty' => false)
  4167. );
  4168. $expected = array(
  4169. 'select' => array('name' => 'data[Model][field]',
  4170. 'id' => 'ModelField'),
  4171. array('option' => array('value' => 1)),
  4172. 'One',
  4173. '/option',
  4174. array('option' => array('value' => 2)),
  4175. 'Two',
  4176. '/option',
  4177. array('optgroup' => array('label' => 'Three')),
  4178. array('option' => array('value' => 4)),
  4179. 'Four',
  4180. '/option',
  4181. array('option' => array('value' => 5)),
  4182. 'Five',
  4183. '/option',
  4184. '/optgroup',
  4185. '/select'
  4186. );
  4187. $this->assertTags($result, $expected);
  4188. $result = $this->Form->select(
  4189. 'Model.field',
  4190. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4191. array('showParents' => true, 'empty' => false)
  4192. );
  4193. $expected = array(
  4194. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4195. array('option' => array('value' => 1)),
  4196. 'One',
  4197. '/option',
  4198. array('option' => array('value' => 2)),
  4199. 'Two',
  4200. '/option',
  4201. array('optgroup' => array('label' => 'Three')),
  4202. array('option' => array('value' => 3)),
  4203. 'Three',
  4204. '/option',
  4205. array('option' => array('value' => 4)),
  4206. 'Four',
  4207. '/option',
  4208. '/optgroup',
  4209. '/select'
  4210. );
  4211. $this->assertTags($result, $expected);
  4212. }
  4213. /**
  4214. * testSelectMultiple method
  4215. *
  4216. * test generation of multiple select elements
  4217. *
  4218. * @return void
  4219. */
  4220. public function testSelectMultiple() {
  4221. $options = array('first', 'second', 'third');
  4222. $result = $this->Form->select(
  4223. 'Model.multi_field', $options, array('multiple' => true)
  4224. );
  4225. $expected = array(
  4226. 'input' => array(
  4227. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4228. ),
  4229. 'select' => array(
  4230. 'name' => 'data[Model][multi_field][]',
  4231. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4232. ),
  4233. array('option' => array('value' => '0')),
  4234. 'first',
  4235. '/option',
  4236. array('option' => array('value' => '1')),
  4237. 'second',
  4238. '/option',
  4239. array('option' => array('value' => '2')),
  4240. 'third',
  4241. '/option',
  4242. '/select'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. $result = $this->Form->select(
  4246. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4247. );
  4248. $expected = array(
  4249. 'input' => array(
  4250. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4251. ),
  4252. 'select' => array(
  4253. 'name' => 'data[Model][multi_field][]',
  4254. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4255. ),
  4256. array('option' => array('value' => '0')),
  4257. 'first',
  4258. '/option',
  4259. array('option' => array('value' => '1')),
  4260. 'second',
  4261. '/option',
  4262. array('option' => array('value' => '2')),
  4263. 'third',
  4264. '/option',
  4265. '/select'
  4266. );
  4267. $this->assertTags($result, $expected);
  4268. $result = $this->Form->select(
  4269. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4270. );
  4271. $expected = array(
  4272. 'input' => array(
  4273. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4274. ),
  4275. 'select' => array(
  4276. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4277. 'multiple' => 'multiple'
  4278. ),
  4279. array('option' => array('value' => '0', 'selected' => 'selected')),
  4280. 'first',
  4281. '/option',
  4282. array('option' => array('value' => '1', 'selected' => 'selected')),
  4283. 'second',
  4284. '/option',
  4285. array('option' => array('value' => '2')),
  4286. 'third',
  4287. '/option',
  4288. '/select'
  4289. );
  4290. $this->assertTags($result, $expected);
  4291. $result = $this->Form->select(
  4292. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4293. );
  4294. $expected = array(
  4295. 'select' => array(
  4296. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4297. ),
  4298. array('option' => array('value' => '0', 'selected' => 'selected')),
  4299. 'first',
  4300. '/option',
  4301. array('option' => array('value' => '1', 'selected' => 'selected')),
  4302. 'second',
  4303. '/option',
  4304. array('option' => array('value' => '2')),
  4305. 'third',
  4306. '/option',
  4307. '/select'
  4308. );
  4309. $this->assertTags($result, $expected);
  4310. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4311. $selected = array('2', '3x');
  4312. $result = $this->Form->select(
  4313. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4314. );
  4315. $expected = array(
  4316. 'input' => array(
  4317. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4318. ),
  4319. 'select' => array(
  4320. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4321. ),
  4322. array('option' => array('value' => '1')),
  4323. 'One',
  4324. '/option',
  4325. array('option' => array('value' => '2', 'selected' => 'selected')),
  4326. 'Two',
  4327. '/option',
  4328. array('option' => array('value' => '3')),
  4329. 'Three',
  4330. '/option',
  4331. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4332. 'Stringy',
  4333. '/option',
  4334. '/select'
  4335. );
  4336. $this->assertTags($result, $expected);
  4337. $result = $this->Form->select('Contact.required_one', array(
  4338. '1' => 'option A',
  4339. '2' => 'option B'
  4340. ), array('multiple' => true));
  4341. $expected = array(
  4342. 'input' => array(
  4343. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4344. ),
  4345. 'select' => array(
  4346. 'name' => 'data[Contact][required_one][]',
  4347. 'id' => 'ContactRequiredOne',
  4348. 'required' => 'required',
  4349. 'multiple' => 'multiple'
  4350. ),
  4351. array('option' => array('value' => '1')), 'option A', '/option',
  4352. array('option' => array('value' => '2')), 'option B', '/option',
  4353. '/select'
  4354. );
  4355. $this->assertTags($result, $expected);
  4356. $result = $this->Form->select(
  4357. 'Model.multi_field',
  4358. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4359. array('multiple' => true)
  4360. );
  4361. $expected = array(
  4362. 'input' => array(
  4363. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4364. 'id' => 'ModelMultiField_'
  4365. ),
  4366. array('select' => array('name' => 'data[Model][multi_field][]',
  4367. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4368. )),
  4369. array('option' => array('value' => 'a&gt;b')),
  4370. 'first',
  4371. '/option',
  4372. array('option' => array('value' => 'a&lt;b')),
  4373. 'second',
  4374. '/option',
  4375. array('option' => array(
  4376. 'value' => 'a&quot;b'
  4377. )),
  4378. 'third',
  4379. '/option',
  4380. '/select'
  4381. );
  4382. $this->assertTags($result, $expected);
  4383. }
  4384. /**
  4385. * Test generating multiple select with disabled elements.
  4386. *
  4387. * @return void
  4388. */
  4389. public function testSelectMultipleWithDisabledElements() {
  4390. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4391. $disabled = array(2, 3);
  4392. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4393. $expected = array(
  4394. 'input' => array(
  4395. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4396. ),
  4397. 'select' => array(
  4398. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4399. ),
  4400. array('option' => array('value' => '1')),
  4401. 'One',
  4402. '/option',
  4403. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4404. 'Two',
  4405. '/option',
  4406. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4407. 'Three',
  4408. '/option',
  4409. array('option' => array('value' => '3x')),
  4410. 'Stringy',
  4411. '/option',
  4412. '/select'
  4413. );
  4414. $this->assertTags($result, $expected);
  4415. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4416. $disabled = array('2', '3x');
  4417. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4418. $expected = array(
  4419. array('div' => array('class' => 'input select')),
  4420. array('label' => array('for' => 'ContactMultiple')),
  4421. 'Multiple',
  4422. '/label',
  4423. 'input' => array(
  4424. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4425. ),
  4426. 'select' => array(
  4427. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4428. ),
  4429. array('option' => array('value' => '1')),
  4430. 'One',
  4431. '/option',
  4432. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4433. 'Two',
  4434. '/option',
  4435. array('option' => array('value' => '3')),
  4436. 'Three',
  4437. '/option',
  4438. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4439. 'Stringy',
  4440. '/option',
  4441. '/select',
  4442. '/div'
  4443. );
  4444. $this->assertTags($result, $expected);
  4445. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4446. $disabled = true;
  4447. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4448. $expected = array(
  4449. array('div' => array('class' => 'input select')),
  4450. array('label' => array('for' => 'ContactMultiple')),
  4451. 'Multiple',
  4452. '/label',
  4453. 'input' => array(
  4454. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4455. ),
  4456. 'select' => array(
  4457. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4458. ),
  4459. array('option' => array('value' => '1')),
  4460. 'One',
  4461. '/option',
  4462. array('option' => array('value' => '2')),
  4463. 'Two',
  4464. '/option',
  4465. array('option' => array('value' => '3')),
  4466. 'Three',
  4467. '/option',
  4468. array('option' => array('value' => '3x')),
  4469. 'Stringy',
  4470. '/option',
  4471. '/select',
  4472. '/div'
  4473. );
  4474. $this->assertTags($result, $expected);
  4475. }
  4476. /**
  4477. * Test generating select with disabled elements.
  4478. *
  4479. * @return void
  4480. */
  4481. public function testSelectWithDisabledElements() {
  4482. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4483. $disabled = array(2, 3);
  4484. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4485. $expected = array(
  4486. 'select' => array(
  4487. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4488. ),
  4489. array('option' => array('value' => '')),
  4490. '/option',
  4491. array('option' => array('value' => '1')),
  4492. 'One',
  4493. '/option',
  4494. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4495. 'Two',
  4496. '/option',
  4497. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4498. 'Three',
  4499. '/option',
  4500. array('option' => array('value' => '3x')),
  4501. 'Stringy',
  4502. '/option',
  4503. '/select'
  4504. );
  4505. $this->assertTags($result, $expected);
  4506. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4507. $disabled = array('2', '3x');
  4508. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4509. $expected = array(
  4510. array('div' => array('class' => 'input select')),
  4511. array('label' => array('for' => 'ModelField')),
  4512. 'Field',
  4513. '/label',
  4514. 'select' => array(
  4515. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4516. ),
  4517. array('option' => array('value' => '1')),
  4518. 'One',
  4519. '/option',
  4520. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4521. 'Two',
  4522. '/option',
  4523. array('option' => array('value' => '3')),
  4524. 'Three',
  4525. '/option',
  4526. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4527. 'Stringy',
  4528. '/option',
  4529. '/select',
  4530. '/div'
  4531. );
  4532. $this->assertTags($result, $expected);
  4533. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4534. $disabled = true;
  4535. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4536. $expected = array(
  4537. array('div' => array('class' => 'input select')),
  4538. array('label' => array('for' => 'ModelField')),
  4539. 'Field',
  4540. '/label',
  4541. 'select' => array(
  4542. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4543. ),
  4544. array('option' => array('value' => '1')),
  4545. 'One',
  4546. '/option',
  4547. array('option' => array('value' => '2')),
  4548. 'Two',
  4549. '/option',
  4550. array('option' => array('value' => '3')),
  4551. 'Three',
  4552. '/option',
  4553. array('option' => array('value' => '3x')),
  4554. 'Stringy',
  4555. '/option',
  4556. '/select',
  4557. '/div'
  4558. );
  4559. $this->assertTags($result, $expected);
  4560. }
  4561. /**
  4562. * test generation of habtm select boxes.
  4563. *
  4564. * @return void
  4565. */
  4566. public function testHabtmSelectBox() {
  4567. $this->View->viewVars['contactTags'] = array(
  4568. 1 => 'blue',
  4569. 2 => 'red',
  4570. 3 => 'green'
  4571. );
  4572. $this->Form->request->data = array(
  4573. 'Contact' => array(),
  4574. 'ContactTag' => array(
  4575. array(
  4576. 'id' => '1',
  4577. 'name' => 'blue'
  4578. ),
  4579. array(
  4580. 'id' => 3,
  4581. 'name' => 'green'
  4582. )
  4583. )
  4584. );
  4585. $this->Form->create('Contact');
  4586. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4587. $expected = array(
  4588. 'input' => array(
  4589. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4590. ),
  4591. 'select' => array(
  4592. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4593. 'multiple' => 'multiple'
  4594. ),
  4595. array('option' => array('value' => '1', 'selected' => 'selected')),
  4596. 'blue',
  4597. '/option',
  4598. array('option' => array('value' => '2')),
  4599. 'red',
  4600. '/option',
  4601. array('option' => array('value' => '3', 'selected' => 'selected')),
  4602. 'green',
  4603. '/option',
  4604. '/select'
  4605. );
  4606. $this->assertTags($result, $expected);
  4607. // make sure only 50 is selected, and not 50f5c0cf
  4608. $this->View->viewVars['contactTags'] = array(
  4609. '1' => 'blue',
  4610. '50f5c0cf' => 'red',
  4611. '50' => 'green'
  4612. );
  4613. $this->Form->request->data = array(
  4614. 'Contact' => array(),
  4615. 'ContactTag' => array(
  4616. array(
  4617. 'id' => 1,
  4618. 'name' => 'blue'
  4619. ),
  4620. array(
  4621. 'id' => 50,
  4622. 'name' => 'green'
  4623. )
  4624. )
  4625. );
  4626. $this->Form->create('Contact');
  4627. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4628. $expected = array(
  4629. 'input' => array(
  4630. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4631. ),
  4632. 'select' => array(
  4633. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4634. 'multiple' => 'multiple'
  4635. ),
  4636. array('option' => array('value' => '1', 'selected' => 'selected')),
  4637. 'blue',
  4638. '/option',
  4639. array('option' => array('value' => '50f5c0cf')),
  4640. 'red',
  4641. '/option',
  4642. array('option' => array('value' => '50', 'selected' => 'selected')),
  4643. 'green',
  4644. '/option',
  4645. '/select'
  4646. );
  4647. $this->assertTags($result, $expected);
  4648. }
  4649. /**
  4650. * test generation of multi select elements in checkbox format
  4651. *
  4652. * @return void
  4653. */
  4654. public function testSelectMultipleCheckboxes() {
  4655. $result = $this->Form->select(
  4656. 'Model.multi_field',
  4657. array('first', 'second', 'third'),
  4658. array('multiple' => 'checkbox')
  4659. );
  4660. $expected = array(
  4661. 'input' => array(
  4662. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4663. ),
  4664. array('div' => array('class' => 'checkbox')),
  4665. array('input' => array(
  4666. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4667. 'value' => '0', 'id' => 'ModelMultiField0'
  4668. )),
  4669. array('label' => array('for' => 'ModelMultiField0')),
  4670. 'first',
  4671. '/label',
  4672. '/div',
  4673. array('div' => array('class' => 'checkbox')),
  4674. array('input' => array(
  4675. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4676. 'value' => '1', 'id' => 'ModelMultiField1'
  4677. )),
  4678. array('label' => array('for' => 'ModelMultiField1')),
  4679. 'second',
  4680. '/label',
  4681. '/div',
  4682. array('div' => array('class' => 'checkbox')),
  4683. array('input' => array(
  4684. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4685. 'value' => '2', 'id' => 'ModelMultiField2'
  4686. )),
  4687. array('label' => array('for' => 'ModelMultiField2')),
  4688. 'third',
  4689. '/label',
  4690. '/div'
  4691. );
  4692. $this->assertTags($result, $expected);
  4693. $result = $this->Form->select(
  4694. 'Model.multi_field',
  4695. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4696. array('multiple' => 'checkbox')
  4697. );
  4698. $expected = array(
  4699. 'input' => array(
  4700. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4701. ),
  4702. array('div' => array('class' => 'checkbox')),
  4703. array('input' => array(
  4704. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4705. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4706. )),
  4707. array('label' => array('for' => 'ModelMultiFieldA')),
  4708. 'first',
  4709. '/label',
  4710. '/div',
  4711. array('div' => array('class' => 'checkbox')),
  4712. array('input' => array(
  4713. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4714. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4715. )),
  4716. array('label' => array('for' => 'ModelMultiFieldB')),
  4717. 'second',
  4718. '/label',
  4719. '/div',
  4720. array('div' => array('class' => 'checkbox')),
  4721. array('input' => array(
  4722. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4723. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4724. )),
  4725. array('label' => array('for' => 'ModelMultiFieldC')),
  4726. 'third',
  4727. '/label',
  4728. '/div'
  4729. );
  4730. $this->assertTags($result, $expected);
  4731. $result = $this->Form->select(
  4732. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4733. );
  4734. $expected = array(
  4735. 'input' => array(
  4736. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4737. ),
  4738. array('div' => array('class' => 'checkbox')),
  4739. array('input' => array(
  4740. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4741. 'value' => '1', 'id' => 'ModelMultiField1'
  4742. )),
  4743. array('label' => array('for' => 'ModelMultiField1')),
  4744. 'first',
  4745. '/label',
  4746. '/div'
  4747. );
  4748. $this->assertTags($result, $expected);
  4749. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4750. $result = $this->Form->select(
  4751. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4752. );
  4753. $expected = array(
  4754. 'input' => array(
  4755. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4756. ),
  4757. array('div' => array('class' => 'checkbox')),
  4758. array('input' => array(
  4759. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4760. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4761. )),
  4762. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4763. 'first',
  4764. '/label',
  4765. '/div',
  4766. array('div' => array('class' => 'checkbox')),
  4767. array('input' => array(
  4768. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4769. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4770. )),
  4771. array('label' => array('for' => 'ModelTagsArray')),
  4772. 'Array',
  4773. '/label',
  4774. '/div'
  4775. );
  4776. $this->assertTags($result, $expected);
  4777. $result = $this->Form->select(
  4778. 'Model.multi_field',
  4779. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4780. array('multiple' => 'checkbox')
  4781. );
  4782. $expected = array(
  4783. 'input' => array(
  4784. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4785. ),
  4786. array('div' => array('class' => 'checkbox')),
  4787. array('input' => array(
  4788. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4789. 'value' => 'a+', 'id' => 'ModelMultiFieldA+'
  4790. )),
  4791. array('label' => array('for' => 'ModelMultiFieldA+')),
  4792. 'first',
  4793. '/label',
  4794. '/div',
  4795. array('div' => array('class' => 'checkbox')),
  4796. array('input' => array(
  4797. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4798. 'value' => 'a++', 'id' => 'ModelMultiFieldA++'
  4799. )),
  4800. array('label' => array('for' => 'ModelMultiFieldA++')),
  4801. 'second',
  4802. '/label',
  4803. '/div',
  4804. array('div' => array('class' => 'checkbox')),
  4805. array('input' => array(
  4806. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4807. 'value' => 'a+++', 'id' => 'ModelMultiFieldA+++'
  4808. )),
  4809. array('label' => array('for' => 'ModelMultiFieldA+++')),
  4810. 'third',
  4811. '/label',
  4812. '/div'
  4813. );
  4814. $this->assertTags($result, $expected);
  4815. $result = $this->Form->select(
  4816. 'Model.multi_field',
  4817. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4818. array('multiple' => 'checkbox')
  4819. );
  4820. $expected = array(
  4821. 'input' => array(
  4822. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4823. ),
  4824. array('div' => array('class' => 'checkbox')),
  4825. array('input' => array(
  4826. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4827. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4828. )),
  4829. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4830. 'first',
  4831. '/label',
  4832. '/div',
  4833. array('div' => array('class' => 'checkbox')),
  4834. array('input' => array(
  4835. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4836. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4837. )),
  4838. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4839. 'second',
  4840. '/label',
  4841. '/div',
  4842. array('div' => array('class' => 'checkbox')),
  4843. array('input' => array(
  4844. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4845. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4846. )),
  4847. array('label' => array('for' => 'ModelMultiFieldAB')),
  4848. 'third',
  4849. '/label',
  4850. '/div'
  4851. );
  4852. $this->assertTags($result, $expected);
  4853. }
  4854. /**
  4855. * test multiple checkboxes with div styles.
  4856. *
  4857. * @return void
  4858. */
  4859. public function testSelectMultipleCheckboxDiv() {
  4860. $result = $this->Form->select(
  4861. 'Model.tags',
  4862. array('first', 'second'),
  4863. array('multiple' => 'checkbox', 'class' => 'my-class')
  4864. );
  4865. $expected = array(
  4866. 'input' => array(
  4867. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4868. ),
  4869. array('div' => array('class' => 'my-class')),
  4870. array('input' => array(
  4871. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4872. 'value' => '0', 'id' => 'ModelTags0'
  4873. )),
  4874. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4875. '/div',
  4876. array('div' => array('class' => 'my-class')),
  4877. array('input' => array(
  4878. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4879. 'value' => '1', 'id' => 'ModelTags1'
  4880. )),
  4881. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4882. '/div'
  4883. );
  4884. $this->assertTags($result, $expected);
  4885. $result = $this->Form->input('Model.tags', array(
  4886. 'options' => array('first', 'second'),
  4887. 'multiple' => 'checkbox',
  4888. 'class' => 'my-class',
  4889. 'div' => false,
  4890. 'label' => false
  4891. ));
  4892. $this->assertTags($result, $expected);
  4893. $Contact = ClassRegistry::getObject('Contact');
  4894. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4895. $result = $this->Form->input('Contact.tags', array(
  4896. 'options' => array('one'),
  4897. 'multiple' => 'checkbox',
  4898. 'label' => false,
  4899. 'div' => false
  4900. ));
  4901. $expected = array(
  4902. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4903. array('div' => array('class' => 'checkbox form-error')),
  4904. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4905. array('label' => array('for' => 'ContactTags0')),
  4906. 'one',
  4907. '/label',
  4908. '/div'
  4909. );
  4910. $this->assertTags($result, $expected);
  4911. $result = $this->Form->input('Contact.tags', array(
  4912. 'options' => array('one'),
  4913. 'multiple' => 'checkbox',
  4914. 'class' => 'mycheckbox',
  4915. 'label' => false,
  4916. 'div' => false
  4917. ));
  4918. $expected = array(
  4919. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4920. array('div' => array('class' => 'mycheckbox form-error')),
  4921. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4922. array('label' => array('for' => 'ContactTags0')),
  4923. 'one',
  4924. '/label',
  4925. '/div'
  4926. );
  4927. $this->assertTags($result, $expected);
  4928. }
  4929. /**
  4930. * Checks the security hash array generated for multiple-input checkbox elements
  4931. *
  4932. * @return void
  4933. */
  4934. public function testSelectMultipleCheckboxSecurity() {
  4935. $this->Form->request['_Token'] = array('key' => 'testKey');
  4936. $this->assertEquals(array(), $this->Form->fields);
  4937. $result = $this->Form->select(
  4938. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4939. array('multiple' => 'checkbox')
  4940. );
  4941. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4942. $result = $this->Form->secure($this->Form->fields);
  4943. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4944. $this->assertRegExp('/"' . $key . '"/', $result);
  4945. }
  4946. /**
  4947. * Multiple select elements should always be secured as they always participate
  4948. * in the POST data.
  4949. *
  4950. * @return void
  4951. */
  4952. public function testSelectMultipleSecureWithNoOptions() {
  4953. $this->Form->request['_Token'] = array('key' => 'testkey');
  4954. $this->assertEquals(array(), $this->Form->fields);
  4955. $this->Form->select(
  4956. 'Model.select',
  4957. array(),
  4958. array('multiple' => true)
  4959. );
  4960. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4961. }
  4962. /**
  4963. * When a select box has no options it should not be added to the fields list
  4964. * as it always fail post validation.
  4965. *
  4966. * @return void
  4967. */
  4968. public function testSelectNoSecureWithNoOptions() {
  4969. $this->Form->request['_Token'] = array('key' => 'testkey');
  4970. $this->assertEquals(array(), $this->Form->fields);
  4971. $this->Form->select(
  4972. 'Model.select',
  4973. array()
  4974. );
  4975. $this->assertEquals(array(), $this->Form->fields);
  4976. $this->Form->select(
  4977. 'Model.select',
  4978. array(),
  4979. array('empty' => true)
  4980. );
  4981. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4982. }
  4983. /**
  4984. * testInputMultipleCheckboxes method
  4985. *
  4986. * test input() resulting in multi select elements being generated.
  4987. *
  4988. * @return void
  4989. */
  4990. public function testInputMultipleCheckboxes() {
  4991. $result = $this->Form->input('Model.multi_field', array(
  4992. 'options' => array('first', 'second', 'third'),
  4993. 'multiple' => 'checkbox'
  4994. ));
  4995. $expected = array(
  4996. array('div' => array('class' => 'input select')),
  4997. array('label' => array('for' => 'ModelMultiField')),
  4998. 'Multi Field',
  4999. '/label',
  5000. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5001. array('div' => array('class' => 'checkbox')),
  5002. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5003. array('label' => array('for' => 'ModelMultiField0')),
  5004. 'first',
  5005. '/label',
  5006. '/div',
  5007. array('div' => array('class' => 'checkbox')),
  5008. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5009. array('label' => array('for' => 'ModelMultiField1')),
  5010. 'second',
  5011. '/label',
  5012. '/div',
  5013. array('div' => array('class' => 'checkbox')),
  5014. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5015. array('label' => array('for' => 'ModelMultiField2')),
  5016. 'third',
  5017. '/label',
  5018. '/div',
  5019. '/div'
  5020. );
  5021. $this->assertTags($result, $expected);
  5022. $result = $this->Form->input('Model.multi_field', array(
  5023. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  5024. 'multiple' => 'checkbox'
  5025. ));
  5026. $expected = array(
  5027. array('div' => array('class' => 'input select')),
  5028. array('label' => array('for' => 'ModelMultiField')),
  5029. 'Multi Field',
  5030. '/label',
  5031. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5032. array('div' => array('class' => 'checkbox')),
  5033. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  5034. array('label' => array('for' => 'ModelMultiFieldA')),
  5035. 'first',
  5036. '/label',
  5037. '/div',
  5038. array('div' => array('class' => 'checkbox')),
  5039. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5040. array('label' => array('for' => 'ModelMultiFieldB')),
  5041. 'second',
  5042. '/label',
  5043. '/div',
  5044. array('div' => array('class' => 'checkbox')),
  5045. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5046. array('label' => array('for' => 'ModelMultiFieldC')),
  5047. 'third',
  5048. '/label',
  5049. '/div',
  5050. '/div'
  5051. );
  5052. $this->assertTags($result, $expected);
  5053. $result = $this->Form->input('Model.multi_field', array(
  5054. 'options' => array('1' => 'first'),
  5055. 'multiple' => 'checkbox',
  5056. 'label' => false,
  5057. 'div' => false
  5058. ));
  5059. $expected = array(
  5060. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5061. array('div' => array('class' => 'checkbox')),
  5062. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5063. array('label' => array('for' => 'ModelMultiField1')),
  5064. 'first',
  5065. '/label',
  5066. '/div'
  5067. );
  5068. $this->assertTags($result, $expected);
  5069. $result = $this->Form->input('Model.multi_field', array(
  5070. 'options' => array('2' => 'second'),
  5071. 'multiple' => 'checkbox',
  5072. 'label' => false,
  5073. 'div' => false
  5074. ));
  5075. $expected = array(
  5076. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5077. array('div' => array('class' => 'checkbox')),
  5078. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5079. array('label' => array('for' => 'ModelMultiField2')),
  5080. 'second',
  5081. '/label',
  5082. '/div'
  5083. );
  5084. $this->assertTags($result, $expected);
  5085. }
  5086. /**
  5087. * testSelectHiddenFieldOmission method
  5088. *
  5089. * test that select() with 'hiddenField' => false omits the hidden field
  5090. *
  5091. * @return void
  5092. */
  5093. public function testSelectHiddenFieldOmission() {
  5094. $result = $this->Form->select('Model.multi_field',
  5095. array('first', 'second'),
  5096. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5097. );
  5098. $expected = array(
  5099. array('div' => array('class' => 'checkbox')),
  5100. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5101. array('label' => array('for' => 'ModelMultiField0')),
  5102. 'first',
  5103. '/label',
  5104. '/div',
  5105. array('div' => array('class' => 'checkbox')),
  5106. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5107. array('label' => array('for' => 'ModelMultiField1')),
  5108. 'second',
  5109. '/label',
  5110. '/div'
  5111. );
  5112. $this->assertTags($result, $expected);
  5113. $result = $this->Form->input('Model.multi_field', array(
  5114. 'options' => array('first', 'second'),
  5115. 'multiple' => 'checkbox',
  5116. 'hiddenField' => false
  5117. ));
  5118. $expected = array(
  5119. array('div' => array('class' => 'input select')),
  5120. array('label' => array('for' => 'ModelMultiField')),
  5121. 'Multi Field',
  5122. '/label',
  5123. array('div' => array('class' => 'checkbox')),
  5124. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5125. array('label' => array('for' => 'ModelMultiField0')),
  5126. 'first',
  5127. '/label',
  5128. '/div',
  5129. array('div' => array('class' => 'checkbox')),
  5130. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5131. array('label' => array('for' => 'ModelMultiField1')),
  5132. 'second',
  5133. '/label',
  5134. '/div',
  5135. '/div'
  5136. );
  5137. $this->assertTags($result, $expected);
  5138. }
  5139. /**
  5140. * test that select() with multiple = checkbox works with overriding name attribute.
  5141. *
  5142. * @return void
  5143. */
  5144. public function testSelectCheckboxMultipleOverrideName() {
  5145. $result = $this->Form->input('category', array(
  5146. 'type' => 'select',
  5147. 'multiple' => 'checkbox',
  5148. 'name' => 'data[fish]',
  5149. 'options' => array('1', '2'),
  5150. 'div' => false,
  5151. 'label' => false,
  5152. ));
  5153. $expected = array(
  5154. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5155. array('div' => array('class' => 'checkbox')),
  5156. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5157. array('label' => array('for' => 'Category0')), '1', '/label',
  5158. '/div',
  5159. array('div' => array('class' => 'checkbox')),
  5160. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5161. array('label' => array('for' => 'Category1')), '2', '/label',
  5162. '/div'
  5163. );
  5164. $this->assertTags($result, $expected);
  5165. }
  5166. /**
  5167. * Test that 'id' overrides all the checkbox id's as well.
  5168. *
  5169. * @return void
  5170. */
  5171. public function testSelectCheckboxMultipleId() {
  5172. $result = $this->Form->select(
  5173. 'Model.multi_field',
  5174. array('first', 'second', 'third'),
  5175. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5176. );
  5177. $expected = array(
  5178. 'input' => array(
  5179. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5180. ),
  5181. array('div' => array('class' => 'checkbox')),
  5182. array('input' => array(
  5183. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5184. 'value' => '0', 'id' => 'CustomId0'
  5185. )),
  5186. array('label' => array('for' => 'CustomId0')),
  5187. 'first',
  5188. '/label',
  5189. '/div',
  5190. array('div' => array('class' => 'checkbox')),
  5191. array('input' => array(
  5192. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5193. 'value' => '1', 'id' => 'CustomId1'
  5194. )),
  5195. array('label' => array('for' => 'CustomId1')),
  5196. 'second',
  5197. '/label',
  5198. '/div',
  5199. array('div' => array('class' => 'checkbox')),
  5200. array('input' => array(
  5201. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5202. 'value' => '2', 'id' => 'CustomId2'
  5203. )),
  5204. array('label' => array('for' => 'CustomId2')),
  5205. 'third',
  5206. '/label',
  5207. '/div'
  5208. );
  5209. $this->assertTags($result, $expected);
  5210. }
  5211. /**
  5212. * testCheckbox method
  5213. *
  5214. * Test generation of checkboxes
  5215. *
  5216. * @return void
  5217. */
  5218. public function testCheckbox() {
  5219. $result = $this->Form->checkbox('Model.field');
  5220. $expected = array(
  5221. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5222. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5223. );
  5224. $this->assertTags($result, $expected);
  5225. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  5226. $expected = array(
  5227. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  5228. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  5229. );
  5230. $this->assertTags($result, $expected);
  5231. $Contact = ClassRegistry::getObject('Contact');
  5232. $Contact->validationErrors['field'] = 1;
  5233. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5234. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5235. $expected = array(
  5236. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5237. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5238. );
  5239. $this->assertTags($result, $expected);
  5240. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5241. $expected = array(
  5242. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5243. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5244. );
  5245. $this->assertTags($result, $expected);
  5246. $this->Form->request->data['Contact']['field'] = '';
  5247. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5248. $expected = array(
  5249. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5250. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5251. );
  5252. $this->assertTags($result, $expected);
  5253. $Contact->validationErrors = array();
  5254. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5255. $expected = array(
  5256. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5257. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5258. );
  5259. $this->assertTags($result, $expected);
  5260. $this->Form->request->data['Contact']['published'] = 1;
  5261. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5262. $expected = array(
  5263. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5264. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. $this->Form->request->data['Contact']['published'] = 0;
  5268. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5269. $expected = array(
  5270. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5271. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5272. );
  5273. $this->assertTags($result, $expected);
  5274. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5275. $expected = array(
  5276. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5277. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5278. );
  5279. $this->assertTags($result, $expected);
  5280. $result = $this->Form->checkbox('CustomField.1.value');
  5281. $expected = array(
  5282. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5283. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5284. );
  5285. $this->assertTags($result, $expected);
  5286. }
  5287. /**
  5288. * test checkbox() with a custom name attribute
  5289. *
  5290. * @return void
  5291. */
  5292. public function testCheckboxCustomNameAttribute() {
  5293. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5294. $expected = array(
  5295. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5296. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5297. );
  5298. $this->assertTags($result, $expected);
  5299. }
  5300. /**
  5301. * test the checked option for checkboxes.
  5302. *
  5303. * @return void
  5304. */
  5305. public function testCheckboxCheckedOption() {
  5306. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5307. $expected = array(
  5308. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5309. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5310. );
  5311. $this->assertTags($result, $expected);
  5312. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5313. $expected = array(
  5314. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5315. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5316. );
  5317. $this->assertTags($result, $expected);
  5318. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5319. $expected = array(
  5320. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5321. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5322. );
  5323. $this->assertTags($result, $expected);
  5324. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5325. $expected = array(
  5326. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5327. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5328. );
  5329. $this->assertTags($result, $expected);
  5330. $this->Form->request->data['Model']['field'] = 1;
  5331. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5332. $expected = array(
  5333. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5334. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5335. );
  5336. $this->assertTags($result, $expected);
  5337. }
  5338. /**
  5339. * Test that disabled attribute works on both the checkbox and hidden input.
  5340. *
  5341. * @return void
  5342. */
  5343. public function testCheckboxDisabling() {
  5344. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5345. $expected = array(
  5346. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5347. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5348. );
  5349. $this->assertTags($result, $expected);
  5350. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5351. $expected = array(
  5352. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5353. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5354. );
  5355. $this->assertTags($result, $expected);
  5356. }
  5357. /**
  5358. * Test that the hidden input for checkboxes can be omitted or set to a
  5359. * specific value.
  5360. *
  5361. * @return void
  5362. */
  5363. public function testCheckboxHiddenField() {
  5364. $result = $this->Form->input('UserForm.something', array(
  5365. 'type' => 'checkbox',
  5366. 'hiddenField' => false
  5367. ));
  5368. $expected = array(
  5369. 'div' => array('class' => 'input checkbox'),
  5370. array('input' => array(
  5371. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5372. 'value' => '1', 'id' => 'UserFormSomething'
  5373. )),
  5374. 'label' => array('for' => 'UserFormSomething'),
  5375. 'Something',
  5376. '/label',
  5377. '/div'
  5378. );
  5379. $this->assertTags($result, $expected);
  5380. $result = $this->Form->input('UserForm.something', array(
  5381. 'type' => 'checkbox',
  5382. 'value' => 'Y',
  5383. 'hiddenField' => 'N',
  5384. ));
  5385. $expected = array(
  5386. 'div' => array('class' => 'input checkbox'),
  5387. array('input' => array(
  5388. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5389. 'value' => 'N', 'id' => 'UserFormSomething_'
  5390. )),
  5391. array('input' => array(
  5392. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5393. 'value' => 'Y', 'id' => 'UserFormSomething'
  5394. )),
  5395. 'label' => array('for' => 'UserFormSomething'),
  5396. 'Something',
  5397. '/label',
  5398. '/div'
  5399. );
  5400. $this->assertTags($result, $expected);
  5401. }
  5402. /**
  5403. * testDateTime method
  5404. *
  5405. * Test generation of date/time select elements
  5406. *
  5407. * @return void
  5408. */
  5409. public function testDateTime() {
  5410. extract($this->dateRegex);
  5411. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5412. $now = strtotime('now');
  5413. $expected = array(
  5414. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5415. $daysRegex,
  5416. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5417. date('j', $now),
  5418. '/option',
  5419. '*/select',
  5420. '-',
  5421. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5422. $monthsRegex,
  5423. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5424. date('F', $now),
  5425. '/option',
  5426. '*/select',
  5427. '-',
  5428. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5429. $yearsRegex,
  5430. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5431. date('Y', $now),
  5432. '/option',
  5433. '*/select',
  5434. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5435. $hoursRegex,
  5436. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5437. date('g', $now),
  5438. '/option',
  5439. '*/select',
  5440. ':',
  5441. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5442. $minutesRegex,
  5443. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5444. date('i', $now),
  5445. '/option',
  5446. '*/select',
  5447. ' ',
  5448. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5449. $meridianRegex,
  5450. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5451. date('a', $now),
  5452. '/option',
  5453. '*/select'
  5454. );
  5455. $this->assertTags($result, $expected);
  5456. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5457. $expected = array(
  5458. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5459. $daysRegex,
  5460. array('option' => array('value' => '')),
  5461. '/option',
  5462. '*/select',
  5463. '-',
  5464. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5465. $monthsRegex,
  5466. array('option' => array('value' => '')),
  5467. '/option',
  5468. '*/select',
  5469. '-',
  5470. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5471. $yearsRegex,
  5472. array('option' => array('value' => '')),
  5473. '/option',
  5474. '*/select',
  5475. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5476. $hoursRegex,
  5477. array('option' => array('value' => '')),
  5478. '/option',
  5479. '*/select',
  5480. ':',
  5481. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5482. $minutesRegex,
  5483. array('option' => array('value' => '')),
  5484. '/option',
  5485. '*/select',
  5486. ' ',
  5487. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5488. $meridianRegex,
  5489. array('option' => array('value' => '')),
  5490. '/option',
  5491. '*/select'
  5492. );
  5493. $this->assertTags($result, $expected);
  5494. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5495. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5496. $this->assertTags($result, $expected);
  5497. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5498. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5499. $this->assertTags($result, $expected);
  5500. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5501. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5502. $expected = array(
  5503. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5504. $daysRegex,
  5505. array('option' => array('value' => '')),
  5506. '/option',
  5507. '*/select',
  5508. '-',
  5509. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5510. $monthsRegex,
  5511. array('option' => array('value' => '')),
  5512. '/option',
  5513. '*/select',
  5514. '-',
  5515. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5516. $yearsRegex,
  5517. array('option' => array('value' => '')),
  5518. '/option',
  5519. '*/select',
  5520. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5521. $hoursRegex,
  5522. array('option' => array('value' => '')),
  5523. '/option',
  5524. '*/select',
  5525. ':',
  5526. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5527. $minutesRegex,
  5528. array('option' => array('value' => '')),
  5529. '/option',
  5530. array('option' => array('value' => '00')),
  5531. '00',
  5532. '/option',
  5533. array('option' => array('value' => '05')),
  5534. '05',
  5535. '/option',
  5536. array('option' => array('value' => '10')),
  5537. '10',
  5538. '/option',
  5539. '*/select',
  5540. ' ',
  5541. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5542. $meridianRegex,
  5543. array('option' => array('value' => '')),
  5544. '/option',
  5545. '*/select'
  5546. );
  5547. $this->assertTags($result, $expected);
  5548. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5549. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5550. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5551. $this->Form->request->data['Contact']['data'] = null;
  5552. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5553. $expected = array(
  5554. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5555. $daysRegex,
  5556. array('option' => array('value' => '')),
  5557. '/option',
  5558. '*/select',
  5559. '-',
  5560. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5561. $monthsRegex,
  5562. array('option' => array('value' => '')),
  5563. '/option',
  5564. '*/select',
  5565. '-',
  5566. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5567. $yearsRegex,
  5568. array('option' => array('value' => '')),
  5569. '/option',
  5570. '*/select',
  5571. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5572. $hoursRegex,
  5573. array('option' => array('value' => '')),
  5574. '/option',
  5575. '*/select',
  5576. ':',
  5577. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5578. $minutesRegex,
  5579. array('option' => array('value' => '')),
  5580. '/option',
  5581. '*/select',
  5582. ' ',
  5583. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5584. $meridianRegex,
  5585. array('option' => array('value' => '')),
  5586. '/option',
  5587. '*/select'
  5588. );
  5589. $this->assertTags($result, $expected);
  5590. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5591. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5592. $now = strtotime($this->Form->data['Model']['field']);
  5593. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5594. $expected = array(
  5595. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5596. $daysRegex,
  5597. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5598. date('j', $now),
  5599. '/option',
  5600. '*/select',
  5601. '-',
  5602. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5603. $monthsRegex,
  5604. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5605. date('F', $now),
  5606. '/option',
  5607. '*/select',
  5608. '-',
  5609. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5610. $yearsRegex,
  5611. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5612. date('Y', $now),
  5613. '/option',
  5614. '*/select',
  5615. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5616. $hoursRegex,
  5617. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5618. date('g', $now),
  5619. '/option',
  5620. '*/select',
  5621. ':',
  5622. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5623. $minutesRegex,
  5624. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5625. date('i', $now),
  5626. '/option',
  5627. '*/select',
  5628. ' ',
  5629. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5630. $meridianRegex,
  5631. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5632. date('a', $now),
  5633. '/option',
  5634. '*/select'
  5635. );
  5636. $this->assertTags($result, $expected);
  5637. $selected = strtotime('2008-10-26 12:33:00');
  5638. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5639. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5640. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5641. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5642. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5643. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5644. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5645. $this->Form->create('Contact');
  5646. $result = $this->Form->input('published');
  5647. $now = strtotime('now');
  5648. $expected = array(
  5649. 'div' => array('class' => 'input date'),
  5650. 'label' => array('for' => 'ContactPublishedMonth'),
  5651. 'Published',
  5652. '/label',
  5653. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5654. $monthsRegex,
  5655. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5656. date('F', $now),
  5657. '/option',
  5658. '*/select',
  5659. '-',
  5660. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5661. $daysRegex,
  5662. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5663. date('j', $now),
  5664. '/option',
  5665. '*/select',
  5666. '-',
  5667. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5668. $yearsRegex,
  5669. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5670. date('Y', $now),
  5671. '/option',
  5672. '*/select',
  5673. '/div'
  5674. );
  5675. $this->assertTags($result, $expected);
  5676. $result = $this->Form->input('published2', array('type' => 'date'));
  5677. $now = strtotime('now');
  5678. $expected = array(
  5679. 'div' => array('class' => 'input date'),
  5680. 'label' => array('for' => 'ContactPublished2Month'),
  5681. 'Published2',
  5682. '/label',
  5683. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5684. $monthsRegex,
  5685. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5686. date('F', $now),
  5687. '/option',
  5688. '*/select',
  5689. '-',
  5690. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5691. $daysRegex,
  5692. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5693. date('j', $now),
  5694. '/option',
  5695. '*/select',
  5696. '-',
  5697. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5698. $yearsRegex,
  5699. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5700. date('Y', $now),
  5701. '/option',
  5702. '*/select',
  5703. '/div'
  5704. );
  5705. $this->assertTags($result, $expected);
  5706. $this->Form->create('Contact');
  5707. $result = $this->Form->input('published', array('monthNames' => false));
  5708. $now = strtotime('now');
  5709. $expected = array(
  5710. 'div' => array('class' => 'input date'),
  5711. 'label' => array('for' => 'ContactPublishedMonth'),
  5712. 'Published',
  5713. '/label',
  5714. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5715. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5716. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5717. date('m', $now),
  5718. '/option',
  5719. '*/select',
  5720. '-',
  5721. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5722. $daysRegex,
  5723. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5724. date('j', $now),
  5725. '/option',
  5726. '*/select',
  5727. '-',
  5728. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5729. $yearsRegex,
  5730. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5731. date('Y', $now),
  5732. '/option',
  5733. '*/select',
  5734. '/div'
  5735. );
  5736. $this->assertTags($result, $expected);
  5737. $result = $this->Form->input('published', array(
  5738. 'timeFormat' => 24,
  5739. 'interval' => 5,
  5740. 'selected' => strtotime('2009-09-03 13:37:00'),
  5741. 'type' => 'datetime'
  5742. ));
  5743. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5744. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5745. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5746. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5747. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5748. }
  5749. /**
  5750. * Test dateTime with rounding
  5751. *
  5752. * @return void
  5753. */
  5754. public function testDateTimeRounding() {
  5755. $this->Form->request->data['Contact'] = array(
  5756. 'date' => array(
  5757. 'day' => '13',
  5758. 'month' => '12',
  5759. 'year' => '2010',
  5760. 'hour' => '04',
  5761. 'min' => '19',
  5762. 'meridian' => 'AM'
  5763. )
  5764. );
  5765. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5766. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5767. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5768. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5769. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5770. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5771. }
  5772. /**
  5773. * Test that empty values don't trigger errors.
  5774. *
  5775. * @return void
  5776. */
  5777. public function testDateTimeNoErrorsOnEmptyData() {
  5778. $this->Form->request->data['Contact'] = array(
  5779. 'date' => array(
  5780. 'day' => '',
  5781. 'month' => '',
  5782. 'year' => '',
  5783. 'hour' => '',
  5784. 'min' => '',
  5785. 'meridian' => ''
  5786. )
  5787. );
  5788. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5789. $this->assertNotEmpty($result);
  5790. }
  5791. /**
  5792. * test that datetime() and default values work.
  5793. *
  5794. * @return void
  5795. */
  5796. public function testDatetimeWithDefault() {
  5797. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5798. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5799. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5800. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5801. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5802. 'default' => '2009-06-01 11:15:30'
  5803. ));
  5804. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5805. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5806. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5807. }
  5808. /**
  5809. * test that bogus non-date time data doesn't cause errors.
  5810. *
  5811. * @return void
  5812. */
  5813. public function testDateTimeWithBogusData() {
  5814. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5815. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5816. }
  5817. /**
  5818. * testDateTime all zeros
  5819. *
  5820. * @return void
  5821. */
  5822. public function testDateTimeAllZeros() {
  5823. $result = $this->Form->dateTime('Contact.date',
  5824. 'DMY',
  5825. false,
  5826. array(
  5827. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5828. 'value' => '0000-00-00'
  5829. )
  5830. );
  5831. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5832. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5833. }
  5834. /**
  5835. * testDateTimeEmptyAsArray
  5836. *
  5837. * @return void
  5838. */
  5839. public function testDateTimeEmptyAsArray() {
  5840. $result = $this->Form->dateTime('Contact.date',
  5841. 'DMY',
  5842. '12',
  5843. array(
  5844. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5845. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5846. )
  5847. )
  5848. );
  5849. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5850. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5851. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5852. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5853. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5854. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5855. $result = $this->Form->dateTime('Contact.date',
  5856. 'DMY',
  5857. '12',
  5858. array(
  5859. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5860. )
  5861. );
  5862. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5863. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5864. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5865. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5866. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5867. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5868. }
  5869. /**
  5870. * testFormDateTimeMulti method
  5871. *
  5872. * test multiple datetime element generation
  5873. *
  5874. * @return void
  5875. */
  5876. public function testFormDateTimeMulti() {
  5877. extract($this->dateRegex);
  5878. $result = $this->Form->dateTime('Contact.1.updated');
  5879. $expected = array(
  5880. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5881. $daysRegex,
  5882. array('option' => array('value' => '')),
  5883. '/option',
  5884. '*/select',
  5885. '-',
  5886. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5887. $monthsRegex,
  5888. array('option' => array('value' => '')),
  5889. '/option',
  5890. '*/select',
  5891. '-',
  5892. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5893. $yearsRegex,
  5894. array('option' => array('value' => '')),
  5895. '/option',
  5896. '*/select',
  5897. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5898. $hoursRegex,
  5899. array('option' => array('value' => '')),
  5900. '/option',
  5901. '*/select',
  5902. ':',
  5903. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5904. $minutesRegex,
  5905. array('option' => array('value' => '')),
  5906. '/option',
  5907. '*/select',
  5908. ' ',
  5909. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5910. $meridianRegex,
  5911. array('option' => array('value' => '')),
  5912. '/option',
  5913. '*/select'
  5914. );
  5915. $this->assertTags($result, $expected);
  5916. $result = $this->Form->dateTime('Contact.2.updated');
  5917. $expected = array(
  5918. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5919. $daysRegex,
  5920. array('option' => array('value' => '')),
  5921. '/option',
  5922. '*/select',
  5923. '-',
  5924. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5925. $monthsRegex,
  5926. array('option' => array('value' => '')),
  5927. '/option',
  5928. '*/select',
  5929. '-',
  5930. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5931. $yearsRegex,
  5932. array('option' => array('value' => '')),
  5933. '/option',
  5934. '*/select',
  5935. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5936. $hoursRegex,
  5937. array('option' => array('value' => '')),
  5938. '/option',
  5939. '*/select',
  5940. ':',
  5941. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5942. $minutesRegex,
  5943. array('option' => array('value' => '')),
  5944. '/option',
  5945. '*/select',
  5946. ' ',
  5947. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5948. $meridianRegex,
  5949. array('option' => array('value' => '')),
  5950. '/option',
  5951. '*/select'
  5952. );
  5953. $this->assertTags($result, $expected);
  5954. }
  5955. /**
  5956. * When changing the date format, the label should always focus the first select box when
  5957. * clicked.
  5958. *
  5959. * @return void
  5960. */
  5961. public function testDateTimeLabelIdMatchesFirstInput() {
  5962. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5963. $this->assertContains('label for="ModelDateMonth"', $result);
  5964. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5965. $this->assertContains('label for="ModelDateDay"', $result);
  5966. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5967. $this->assertContains('label for="ModelDateYear"', $result);
  5968. }
  5969. /**
  5970. * testMonth method
  5971. *
  5972. * @return void
  5973. */
  5974. public function testMonth() {
  5975. $result = $this->Form->month('Model.field');
  5976. $expected = array(
  5977. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5978. array('option' => array('value' => '')),
  5979. '/option',
  5980. array('option' => array('value' => '01')),
  5981. date('F', strtotime('2008-01-01 00:00:00')),
  5982. '/option',
  5983. array('option' => array('value' => '02')),
  5984. date('F', strtotime('2008-02-01 00:00:00')),
  5985. '/option',
  5986. '*/select',
  5987. );
  5988. $this->assertTags($result, $expected);
  5989. $result = $this->Form->month('Model.field', array('empty' => true));
  5990. $expected = array(
  5991. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5992. array('option' => array('value' => '')),
  5993. '/option',
  5994. array('option' => array('value' => '01')),
  5995. date('F', strtotime('2008-01-01 00:00:00')),
  5996. '/option',
  5997. array('option' => array('value' => '02')),
  5998. date('F', strtotime('2008-02-01 00:00:00')),
  5999. '/option',
  6000. '*/select',
  6001. );
  6002. $this->assertTags($result, $expected);
  6003. $result = $this->Form->month('Model.field', array('monthNames' => false));
  6004. $expected = array(
  6005. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6006. array('option' => array('value' => '')),
  6007. '/option',
  6008. array('option' => array('value' => '01')),
  6009. '01',
  6010. '/option',
  6011. array('option' => array('value' => '02')),
  6012. '02',
  6013. '/option',
  6014. '*/select',
  6015. );
  6016. $this->assertTags($result, $expected);
  6017. $monthNames = array(
  6018. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6019. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  6020. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  6021. $expected = array(
  6022. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6023. array('option' => array('value' => '')),
  6024. '/option',
  6025. array('option' => array('value' => '01')),
  6026. 'Jan',
  6027. '/option',
  6028. array('option' => array('value' => '02')),
  6029. 'Feb',
  6030. '/option',
  6031. '*/select',
  6032. );
  6033. $this->assertTags($result, $expected);
  6034. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6035. $result = $this->Form->month('Project.release');
  6036. $expected = array(
  6037. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  6038. array('option' => array('value' => '')),
  6039. '/option',
  6040. array('option' => array('value' => '01')),
  6041. 'January',
  6042. '/option',
  6043. array('option' => array('value' => '02', 'selected' => 'selected')),
  6044. 'February',
  6045. '/option',
  6046. '*/select',
  6047. );
  6048. $this->assertTags($result, $expected);
  6049. }
  6050. /**
  6051. * testDay method
  6052. *
  6053. * @return void
  6054. */
  6055. public function testDay() {
  6056. extract($this->dateRegex);
  6057. $result = $this->Form->day('Model.field', array('value' => false));
  6058. $expected = array(
  6059. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6060. array('option' => array('value' => '')),
  6061. '/option',
  6062. array('option' => array('value' => '01')),
  6063. '1',
  6064. '/option',
  6065. array('option' => array('value' => '02')),
  6066. '2',
  6067. '/option',
  6068. $daysRegex,
  6069. '/select',
  6070. );
  6071. $this->assertTags($result, $expected);
  6072. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6073. $result = $this->Form->day('Model.field');
  6074. $expected = array(
  6075. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6076. array('option' => array('value' => '')),
  6077. '/option',
  6078. array('option' => array('value' => '01')),
  6079. '1',
  6080. '/option',
  6081. array('option' => array('value' => '02')),
  6082. '2',
  6083. '/option',
  6084. $daysRegex,
  6085. array('option' => array('value' => '10', 'selected' => 'selected')),
  6086. '10',
  6087. '/option',
  6088. $daysRegex,
  6089. '/select',
  6090. );
  6091. $this->assertTags($result, $expected);
  6092. $this->Form->request->data['Model']['field'] = '';
  6093. $result = $this->Form->day('Model.field', array('value' => '10'));
  6094. $expected = array(
  6095. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6096. array('option' => array('value' => '')),
  6097. '/option',
  6098. array('option' => array('value' => '01')),
  6099. '1',
  6100. '/option',
  6101. array('option' => array('value' => '02')),
  6102. '2',
  6103. '/option',
  6104. $daysRegex,
  6105. array('option' => array('value' => '10', 'selected' => 'selected')),
  6106. '10',
  6107. '/option',
  6108. $daysRegex,
  6109. '/select',
  6110. );
  6111. $this->assertTags($result, $expected);
  6112. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6113. $result = $this->Form->day('Model.field', array('value' => true));
  6114. $expected = array(
  6115. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6116. array('option' => array('value' => '')),
  6117. '/option',
  6118. array('option' => array('value' => '01')),
  6119. '1',
  6120. '/option',
  6121. array('option' => array('value' => '02')),
  6122. '2',
  6123. '/option',
  6124. $daysRegex,
  6125. array('option' => array('value' => '10', 'selected' => 'selected')),
  6126. '10',
  6127. '/option',
  6128. $daysRegex,
  6129. '/select',
  6130. );
  6131. $this->assertTags($result, $expected);
  6132. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6133. $result = $this->Form->day('Project.release');
  6134. $expected = array(
  6135. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6136. array('option' => array('value' => '')),
  6137. '/option',
  6138. array('option' => array('value' => '01')),
  6139. '1',
  6140. '/option',
  6141. array('option' => array('value' => '02')),
  6142. '2',
  6143. '/option',
  6144. $daysRegex,
  6145. array('option' => array('value' => '10', 'selected' => 'selected')),
  6146. '10',
  6147. '/option',
  6148. $daysRegex,
  6149. '/select',
  6150. );
  6151. $this->assertTags($result, $expected);
  6152. }
  6153. /**
  6154. * testMinute method
  6155. *
  6156. * @return void
  6157. */
  6158. public function testMinute() {
  6159. extract($this->dateRegex);
  6160. $result = $this->Form->minute('Model.field');
  6161. $expected = array(
  6162. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6163. array('option' => array('value' => '')),
  6164. '/option',
  6165. array('option' => array('value' => '00')),
  6166. '00',
  6167. '/option',
  6168. array('option' => array('value' => '01')),
  6169. '01',
  6170. '/option',
  6171. array('option' => array('value' => '02')),
  6172. '02',
  6173. '/option',
  6174. $minutesRegex,
  6175. '/select',
  6176. );
  6177. $this->assertTags($result, $expected);
  6178. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6179. $result = $this->Form->minute('Model.field');
  6180. $expected = array(
  6181. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6182. array('option' => array('value' => '')),
  6183. '/option',
  6184. array('option' => array('value' => '00')),
  6185. '00',
  6186. '/option',
  6187. array('option' => array('value' => '01')),
  6188. '01',
  6189. '/option',
  6190. array('option' => array('value' => '02')),
  6191. '02',
  6192. '/option',
  6193. $minutesRegex,
  6194. array('option' => array('value' => '12', 'selected' => 'selected')),
  6195. '12',
  6196. '/option',
  6197. $minutesRegex,
  6198. '/select',
  6199. );
  6200. $this->assertTags($result, $expected);
  6201. $this->Form->request->data['Model']['field'] = '';
  6202. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6203. $expected = array(
  6204. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6205. array('option' => array('value' => '')),
  6206. '/option',
  6207. array('option' => array('value' => '00')),
  6208. '00',
  6209. '/option',
  6210. array('option' => array('value' => '05')),
  6211. '05',
  6212. '/option',
  6213. array('option' => array('value' => '10')),
  6214. '10',
  6215. '/option',
  6216. $minutesRegex,
  6217. '/select',
  6218. );
  6219. $this->assertTags($result, $expected);
  6220. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6221. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6222. $expected = array(
  6223. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6224. array('option' => array('value' => '')),
  6225. '/option',
  6226. array('option' => array('value' => '00')),
  6227. '00',
  6228. '/option',
  6229. array('option' => array('value' => '05')),
  6230. '05',
  6231. '/option',
  6232. array('option' => array('value' => '10', 'selected' => 'selected')),
  6233. '10',
  6234. '/option',
  6235. $minutesRegex,
  6236. '/select',
  6237. );
  6238. $this->assertTags($result, $expected);
  6239. }
  6240. /**
  6241. * testHour method
  6242. *
  6243. * @return void
  6244. */
  6245. public function testHour() {
  6246. extract($this->dateRegex);
  6247. $result = $this->Form->hour('Model.field', false);
  6248. $expected = array(
  6249. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6250. array('option' => array('value' => '')),
  6251. '/option',
  6252. array('option' => array('value' => '01')),
  6253. '1',
  6254. '/option',
  6255. array('option' => array('value' => '02')),
  6256. '2',
  6257. '/option',
  6258. $hoursRegex,
  6259. '/select',
  6260. );
  6261. $this->assertTags($result, $expected);
  6262. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6263. $result = $this->Form->hour('Model.field', false);
  6264. $expected = array(
  6265. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6266. array('option' => array('value' => '')),
  6267. '/option',
  6268. array('option' => array('value' => '01')),
  6269. '1',
  6270. '/option',
  6271. array('option' => array('value' => '02')),
  6272. '2',
  6273. '/option',
  6274. $hoursRegex,
  6275. array('option' => array('value' => '12', 'selected' => 'selected')),
  6276. '12',
  6277. '/option',
  6278. '/select',
  6279. );
  6280. $this->assertTags($result, $expected);
  6281. $this->Form->request->data['Model']['field'] = '';
  6282. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6283. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6284. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6285. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6286. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6287. $result = $this->Form->hour('Model.field', true);
  6288. $expected = array(
  6289. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6290. array('option' => array('value' => '')),
  6291. '/option',
  6292. array('option' => array('value' => '00', 'selected' => 'selected')),
  6293. '0',
  6294. '/option',
  6295. array('option' => array('value' => '01')),
  6296. '1',
  6297. '/option',
  6298. array('option' => array('value' => '02')),
  6299. '2',
  6300. '/option',
  6301. $hoursRegex,
  6302. '/select',
  6303. );
  6304. $this->assertTags($result, $expected);
  6305. unset($this->Form->request->data['Model']['field']);
  6306. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6307. $thisHour = date('H');
  6308. $optValue = date('G');
  6309. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6310. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6311. $result = $this->Form->hour('Model.field', true);
  6312. $expected = array(
  6313. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6314. array('option' => array('value' => '')),
  6315. '/option',
  6316. array('option' => array('value' => '00')),
  6317. '0',
  6318. '/option',
  6319. array('option' => array('value' => '01', 'selected' => 'selected')),
  6320. '1',
  6321. '/option',
  6322. array('option' => array('value' => '02')),
  6323. '2',
  6324. '/option',
  6325. $hoursRegex,
  6326. '/select',
  6327. );
  6328. $this->assertTags($result, $expected);
  6329. }
  6330. /**
  6331. * testYear method
  6332. *
  6333. * @return void
  6334. */
  6335. public function testYear() {
  6336. $result = $this->Form->year('Model.field', 2006, 2007);
  6337. $expected = array(
  6338. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6339. array('option' => array('value' => '')),
  6340. '/option',
  6341. array('option' => array('value' => '2007')),
  6342. '2007',
  6343. '/option',
  6344. array('option' => array('value' => '2006')),
  6345. '2006',
  6346. '/option',
  6347. '/select',
  6348. );
  6349. $this->assertTags($result, $expected);
  6350. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6351. $expected = array(
  6352. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6353. array('option' => array('value' => '')),
  6354. '/option',
  6355. array('option' => array('value' => '2006')),
  6356. '2006',
  6357. '/option',
  6358. array('option' => array('value' => '2007')),
  6359. '2007',
  6360. '/option',
  6361. '/select',
  6362. );
  6363. $this->assertTags($result, $expected);
  6364. $this->request->data['Contact']['published'] = '';
  6365. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6366. $expected = array(
  6367. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6368. array('option' => array('value' => '')),
  6369. '/option',
  6370. array('option' => array('value' => '2007')),
  6371. '2007',
  6372. '/option',
  6373. array('option' => array('value' => '2006')),
  6374. '2006',
  6375. '/option',
  6376. '/select',
  6377. );
  6378. $this->assertTags($result, $expected);
  6379. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6380. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6381. $expected = array(
  6382. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6383. array('option' => array('value' => '2007')),
  6384. '2007',
  6385. '/option',
  6386. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6387. '2006',
  6388. '/option',
  6389. '/select',
  6390. );
  6391. $this->assertTags($result, $expected);
  6392. $this->Form->request->data['Contact']['published'] = '';
  6393. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6394. $expected = array(
  6395. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6396. array('option' => array('value' => '')),
  6397. '/option',
  6398. array('option' => array('value' => '2007')),
  6399. '2007',
  6400. '/option',
  6401. array('option' => array('value' => '2006')),
  6402. '2006',
  6403. '/option',
  6404. '/select',
  6405. );
  6406. $this->assertTags($result, $expected);
  6407. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6408. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6409. $expected = array(
  6410. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6411. array('option' => array('value' => '2007')),
  6412. '2007',
  6413. '/option',
  6414. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6415. '2006',
  6416. '/option',
  6417. '/select',
  6418. );
  6419. $this->assertTags($result, $expected);
  6420. $this->Form->request->data['Contact']['published'] = '';
  6421. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6422. $expected = array(
  6423. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6424. array('option' => array('value' => '')),
  6425. '/option',
  6426. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6427. '2007',
  6428. '/option',
  6429. array('option' => array('value' => '2006')),
  6430. '2006',
  6431. '/option',
  6432. '/select',
  6433. );
  6434. $this->assertTags($result, $expected);
  6435. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6436. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6437. $expected = array(
  6438. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6439. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6440. '2007',
  6441. '/option',
  6442. array('option' => array('value' => '2006')),
  6443. '2006',
  6444. '/option',
  6445. '/select',
  6446. );
  6447. $this->assertTags($result, $expected);
  6448. $this->Form->request->data['Contact']['published'] = '';
  6449. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6450. $expected = array(
  6451. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6452. array('option' => array('value' => '2008')),
  6453. '2008',
  6454. '/option',
  6455. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6456. '2007',
  6457. '/option',
  6458. array('option' => array('value' => '2006')),
  6459. '2006',
  6460. '/option',
  6461. '/select',
  6462. );
  6463. $this->assertTags($result, $expected);
  6464. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6465. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6466. $expected = array(
  6467. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6468. array('option' => array('value' => '2008')),
  6469. '2008',
  6470. '/option',
  6471. array('option' => array('value' => '2007')),
  6472. '2007',
  6473. '/option',
  6474. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6475. '2006',
  6476. '/option',
  6477. '/select',
  6478. );
  6479. $this->assertTags($result, $expected);
  6480. $this->Form->request->data = array();
  6481. $this->Form->create('Contact');
  6482. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6483. $expected = array(
  6484. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6485. array('option' => array('value' => '2008')),
  6486. '2008',
  6487. '/option',
  6488. array('option' => array('value' => '2007')),
  6489. '2007',
  6490. '/option',
  6491. array('option' => array('value' => '2006')),
  6492. '2006',
  6493. '/option',
  6494. '/select',
  6495. );
  6496. $this->assertTags($result, $expected);
  6497. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6498. $this->assertContains('data[Contact][published][year]', $result);
  6499. }
  6500. /**
  6501. * testYearAutoExpandRange method
  6502. *
  6503. * @return void
  6504. */
  6505. public function testYearAutoExpandRange() {
  6506. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6507. $result = $this->Form->year('User.birthday');
  6508. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6509. $result = $matches[1];
  6510. $expected = range(date('Y') + 20, 1930);
  6511. $this->assertEquals($expected, $result);
  6512. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6513. $result = $this->Form->year('Project.release');
  6514. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6515. $result = $matches[1];
  6516. $expected = range(2050, date('Y') - 20);
  6517. $this->assertEquals($expected, $result);
  6518. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6519. $result = $this->Form->year('Project.release', 1890, 1900);
  6520. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6521. $result = $matches[1];
  6522. $expected = range(1900, 1881);
  6523. $this->assertEquals($expected, $result);
  6524. }
  6525. /**
  6526. * testInputDate method
  6527. *
  6528. * Test various inputs with type date and different dateFormat values.
  6529. * Failing to provide a dateFormat key should not error.
  6530. * It should simply not pre-select any value then.
  6531. *
  6532. * @return void
  6533. */
  6534. public function testInputDate() {
  6535. $this->Form->request->data = array(
  6536. 'User' => array(
  6537. 'month_year' => array('month' => date('m')),
  6538. 'just_year' => array('month' => date('m')),
  6539. 'just_month' => array('year' => date('Y')),
  6540. 'just_day' => array('month' => date('m')),
  6541. )
  6542. );
  6543. $this->Form->create('User');
  6544. $result = $this->Form->input('month_year',
  6545. array(
  6546. 'label' => false,
  6547. 'div' => false,
  6548. 'type' => 'date',
  6549. 'dateFormat' => 'MY',
  6550. 'minYear' => 2006,
  6551. 'maxYear' => 2008
  6552. )
  6553. );
  6554. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6555. $this->assertNotContains('value="2008" selected="selected"', $result);
  6556. $result = $this->Form->input('just_year',
  6557. array(
  6558. 'type' => 'date',
  6559. 'label' => false,
  6560. 'dateFormat' => 'Y',
  6561. 'minYear' => date('Y'),
  6562. 'maxYear' => date('Y', strtotime('+20 years'))
  6563. )
  6564. );
  6565. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6566. $result = $this->Form->input('just_month',
  6567. array(
  6568. 'type' => 'date',
  6569. 'label' => false,
  6570. 'dateFormat' => 'M',
  6571. 'empty' => false,
  6572. )
  6573. );
  6574. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6575. $result = $this->Form->input('just_day',
  6576. array(
  6577. 'type' => 'date',
  6578. 'label' => false,
  6579. 'dateFormat' => 'D',
  6580. 'empty' => false,
  6581. )
  6582. );
  6583. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6584. }
  6585. /**
  6586. * testInputDateMaxYear method
  6587. *
  6588. * Let's say we want to only allow users born from 2006 to 2008 to register
  6589. * This being the first singup page, we still don't have any data
  6590. *
  6591. * @return void
  6592. */
  6593. public function testInputDateMaxYear() {
  6594. $this->Form->request->data = array();
  6595. $this->Form->create('User');
  6596. $result = $this->Form->input('birthday',
  6597. array(
  6598. 'label' => false,
  6599. 'div' => false,
  6600. 'type' => 'date',
  6601. 'dateFormat' => 'DMY',
  6602. 'minYear' => 2006,
  6603. 'maxYear' => 2008
  6604. )
  6605. );
  6606. $this->assertContains('value="2008" selected="selected"', $result);
  6607. }
  6608. /**
  6609. * testTextArea method
  6610. *
  6611. * @return void
  6612. */
  6613. public function testTextArea() {
  6614. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6615. $result = $this->Form->textarea('Model.field');
  6616. $expected = array(
  6617. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6618. 'some test data',
  6619. '/textarea',
  6620. );
  6621. $this->assertTags($result, $expected);
  6622. $result = $this->Form->textarea('Model.tmp');
  6623. $expected = array(
  6624. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6625. '/textarea',
  6626. );
  6627. $this->assertTags($result, $expected);
  6628. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6629. $result = $this->Form->textarea('Model.field');
  6630. $expected = array(
  6631. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6632. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6633. '/textarea',
  6634. );
  6635. $this->assertTags($result, $expected);
  6636. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6637. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6638. $expected = array(
  6639. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6640. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6641. '/textarea',
  6642. );
  6643. $this->assertTags($result, $expected);
  6644. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6645. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6646. $expected = array(
  6647. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6648. '/textarea'
  6649. );
  6650. $this->assertTags($result, $expected);
  6651. }
  6652. /**
  6653. * testTextAreaWithStupidCharacters method
  6654. *
  6655. * test text area with non-ascii characters
  6656. *
  6657. * @return void
  6658. */
  6659. public function testTextAreaWithStupidCharacters() {
  6660. $this->loadFixtures('Post');
  6661. $result = $this->Form->input('Post.content', array(
  6662. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6663. ));
  6664. $expected = array(
  6665. 'div' => array('class' => 'input textarea'),
  6666. 'label' => array('for' => 'PostContent'),
  6667. 'Current Text',
  6668. '/label',
  6669. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6670. 'GREAT®',
  6671. '/textarea',
  6672. '/div'
  6673. );
  6674. $this->assertTags($result, $expected);
  6675. }
  6676. /**
  6677. * testHiddenField method
  6678. *
  6679. * @return void
  6680. */
  6681. public function testHiddenField() {
  6682. $Contact = ClassRegistry::getObject('Contact');
  6683. $Contact->validationErrors['field'] = 1;
  6684. $this->Form->request->data['Contact']['field'] = 'test';
  6685. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6686. $this->assertTags($result, array(
  6687. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6688. );
  6689. }
  6690. /**
  6691. * testFileUploadField method
  6692. *
  6693. * @return void
  6694. */
  6695. public function testFileUploadField() {
  6696. $result = $this->Form->file('Model.upload');
  6697. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6698. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6699. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6700. $expected = array(
  6701. 'div' => array('class' => 'input file'),
  6702. 'label' => array('for' => 'ModelUpload'),
  6703. 'Upload',
  6704. '/label',
  6705. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6706. '/div'
  6707. );
  6708. $this->assertTags($result, $expected);
  6709. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6710. $result = $this->Form->file('Model.upload');
  6711. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6712. }
  6713. /**
  6714. * test File upload input on a model not used in create();
  6715. *
  6716. * @return void
  6717. */
  6718. public function testFileUploadOnOtherModel() {
  6719. $this->Form->create('ValidateUser', array('type' => 'file'));
  6720. $result = $this->Form->file('ValidateProfile.city');
  6721. $expected = array(
  6722. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6723. );
  6724. $this->assertTags($result, $expected);
  6725. }
  6726. /**
  6727. * testButton method
  6728. *
  6729. * @return void
  6730. */
  6731. public function testButton() {
  6732. $result = $this->Form->button('Hi');
  6733. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6734. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6735. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6736. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6737. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6738. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6739. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6740. $result = $this->Form->button('No type', array('type' => false));
  6741. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6742. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6743. $this->assertNotRegExp('/\&039/', $result);
  6744. }
  6745. /**
  6746. * Test that button() makes unlocked fields by default.
  6747. *
  6748. * @return void
  6749. */
  6750. public function testButtonUnlockedByDefault() {
  6751. $this->Form->request->params['_Token']['key'] = 'secured';
  6752. $this->Form->button('Save', array('name' => 'save'));
  6753. $this->Form->button('Clear');
  6754. $result = $this->Form->unlockField();
  6755. $this->assertEquals(array('save'), $result);
  6756. }
  6757. /**
  6758. * testPostButton method
  6759. *
  6760. * @return void
  6761. */
  6762. public function testPostButton() {
  6763. $result = $this->Form->postButton('Hi', '/controller/action');
  6764. $this->assertTags($result, array(
  6765. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6766. 'div' => array('style' => 'display:none;'),
  6767. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6768. '/div',
  6769. 'button' => array('type' => 'submit'),
  6770. 'Hi',
  6771. '/button',
  6772. '/form'
  6773. ));
  6774. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6775. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6776. }
  6777. /**
  6778. * Test that postButton adds _Token fields.
  6779. *
  6780. * @return void
  6781. */
  6782. public function testSecurePostButton() {
  6783. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6784. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6785. $expected = array(
  6786. 'form' => array(
  6787. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6788. ),
  6789. array('div' => array('style' => 'display:none;')),
  6790. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6791. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6792. '/div',
  6793. 'button' => array('type' => 'submit'),
  6794. 'Delete',
  6795. '/button',
  6796. array('div' => array('style' => 'display:none;')),
  6797. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6798. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6799. '/div',
  6800. '/form',
  6801. );
  6802. $this->assertTags($result, $expected);
  6803. }
  6804. /**
  6805. * testPostLink method
  6806. *
  6807. * @return void
  6808. */
  6809. public function testPostLink() {
  6810. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6811. $this->assertTags($result, array(
  6812. 'form' => array(
  6813. 'method' => 'post', 'action' => '/posts/delete/1',
  6814. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6815. ),
  6816. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6817. '/form',
  6818. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6819. 'Delete',
  6820. '/a'
  6821. ));
  6822. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6823. $this->assertTags($result, array(
  6824. 'form' => array(
  6825. 'method' => 'post', 'action' => '/posts/delete/1',
  6826. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6827. ),
  6828. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6829. '/form',
  6830. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6831. 'Delete',
  6832. '/a'
  6833. ));
  6834. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6835. $this->assertTags($result, array(
  6836. 'form' => array(
  6837. 'method' => 'post', 'action' => '/posts/delete/1',
  6838. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6839. ),
  6840. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6841. '/form',
  6842. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6843. 'Delete',
  6844. '/a'
  6845. ));
  6846. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6847. $this->assertTags($result, array(
  6848. 'form' => array(
  6849. 'method' => 'post', 'action' => '/posts/delete/1',
  6850. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6851. ),
  6852. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6853. '/form',
  6854. '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;/'),
  6855. 'Delete',
  6856. '/a'
  6857. ));
  6858. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6859. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  6860. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6861. $this->assertTags($result, array(
  6862. 'form' => array(
  6863. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6864. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6865. ),
  6866. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6867. '/form',
  6868. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6869. 'Delete',
  6870. '/a'
  6871. ));
  6872. $result = $this->Form->postLink(
  6873. '',
  6874. array('controller' => 'items', 'action' => 'delete', 10),
  6875. array('class' => 'btn btn-danger', 'escape' => false),
  6876. 'Confirm thing'
  6877. );
  6878. $this->assertTags($result, array(
  6879. 'form' => array(
  6880. 'method' => 'post', 'action' => '/items/delete/10',
  6881. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6882. ),
  6883. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6884. '/form',
  6885. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6886. '/a'
  6887. ));
  6888. }
  6889. /**
  6890. * test creating postLinks after a GET form.
  6891. *
  6892. * @return void
  6893. */
  6894. public function testPostLinkAfterGetForm() {
  6895. $this->Form->request->params['_Token']['key'] = 'testkey';
  6896. $this->Form->create('User', array('type' => 'get'));
  6897. $this->Form->end();
  6898. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6899. $this->assertTags($result, array(
  6900. 'form' => array(
  6901. 'method' => 'post', 'action' => '/posts/delete/1',
  6902. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6903. ),
  6904. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6905. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6906. 'div' => array('style' => 'display:none;'),
  6907. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6908. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6909. '/div',
  6910. '/form',
  6911. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6912. 'Delete',
  6913. '/a'
  6914. ));
  6915. }
  6916. /**
  6917. * Test that postLink adds _Token fields.
  6918. *
  6919. * @return void
  6920. */
  6921. public function testSecurePostLink() {
  6922. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6923. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6924. $expected = array(
  6925. 'form' => array(
  6926. 'method' => 'post', 'action' => '/posts/delete/1',
  6927. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6928. ),
  6929. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6930. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6931. 'div' => array('style' => 'display:none;'),
  6932. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6933. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6934. '/div',
  6935. '/form',
  6936. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6937. 'Delete',
  6938. '/a'
  6939. );
  6940. $this->assertTags($result, $expected);
  6941. }
  6942. /**
  6943. * Test that postLink adds form tags to view block
  6944. *
  6945. * @return void
  6946. */
  6947. public function testPostLinkFormBuffer() {
  6948. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  6949. $this->assertTags($result, array(
  6950. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6951. 'Delete',
  6952. '/a'
  6953. ));
  6954. $result = $this->View->fetch('postLink');
  6955. $this->assertTags($result, array(
  6956. 'form' => array(
  6957. 'method' => 'post', 'action' => '/posts/delete/1',
  6958. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6959. ),
  6960. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6961. '/form'
  6962. ));
  6963. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  6964. array('inline' => false, 'method' => 'DELETE')
  6965. );
  6966. $this->assertTags($result, array(
  6967. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6968. 'Delete',
  6969. '/a'
  6970. ));
  6971. $result = $this->View->fetch('postLink');
  6972. $this->assertTags($result, array(
  6973. 'form' => array(
  6974. 'method' => 'post', 'action' => '/posts/delete/1',
  6975. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6976. ),
  6977. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6978. '/form',
  6979. array(
  6980. 'form' => array(
  6981. 'method' => 'post', 'action' => '/posts/delete/2',
  6982. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6983. ),
  6984. ),
  6985. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  6986. '/form'
  6987. ));
  6988. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  6989. $this->assertTags($result, array(
  6990. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6991. 'Delete',
  6992. '/a'
  6993. ));
  6994. $result = $this->View->fetch('foobar');
  6995. $this->assertTags($result, array(
  6996. 'form' => array(
  6997. 'method' => 'post', 'action' => '/posts/delete/1',
  6998. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6999. ),
  7000. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7001. '/form'
  7002. ));
  7003. }
  7004. /**
  7005. * testSubmitButton method
  7006. *
  7007. * @return void
  7008. */
  7009. public function testSubmitButton() {
  7010. $result = $this->Form->submit('');
  7011. $expected = array(
  7012. 'div' => array('class' => 'submit'),
  7013. 'input' => array('type' => 'submit', 'value' => ''),
  7014. '/div'
  7015. );
  7016. $this->assertTags($result, $expected);
  7017. $result = $this->Form->submit('Test Submit');
  7018. $expected = array(
  7019. 'div' => array('class' => 'submit'),
  7020. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7021. '/div'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  7025. $expected = array(
  7026. 'span' => array('class' => 'submit'),
  7027. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7028. '/span'
  7029. );
  7030. $this->assertTags($result, $expected);
  7031. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  7032. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  7033. $this->assertTags($result, $expected);
  7034. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  7035. $expected = array(
  7036. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  7037. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7038. '/div'
  7039. );
  7040. $this->assertTags($result, $expected);
  7041. $result = $this->Form->submit('Next >');
  7042. $expected = array(
  7043. 'div' => array('class' => 'submit'),
  7044. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  7045. '/div'
  7046. );
  7047. $this->assertTags($result, $expected);
  7048. $result = $this->Form->submit('Next >', array('escape' => false));
  7049. $expected = array(
  7050. 'div' => array('class' => 'submit'),
  7051. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  7052. '/div'
  7053. );
  7054. $this->assertTags($result, $expected);
  7055. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7056. $expected = array(
  7057. 'div' => array('class' => 'submit'),
  7058. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7059. '/div'
  7060. );
  7061. $this->assertTags($result, $expected);
  7062. $before = '--before--';
  7063. $after = '--after--';
  7064. $result = $this->Form->submit('Test', array('before' => $before));
  7065. $expected = array(
  7066. 'div' => array('class' => 'submit'),
  7067. '--before--',
  7068. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7069. '/div'
  7070. );
  7071. $this->assertTags($result, $expected);
  7072. $result = $this->Form->submit('Test', array('after' => $after));
  7073. $expected = array(
  7074. 'div' => array('class' => 'submit'),
  7075. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7076. '--after--',
  7077. '/div'
  7078. );
  7079. $this->assertTags($result, $expected);
  7080. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7081. $expected = array(
  7082. 'div' => array('class' => 'submit'),
  7083. '--before--',
  7084. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7085. '--after--',
  7086. '/div'
  7087. );
  7088. $this->assertTags($result, $expected);
  7089. }
  7090. /**
  7091. * test image submit types.
  7092. *
  7093. * @return void
  7094. */
  7095. public function testSubmitImage() {
  7096. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7097. $expected = array(
  7098. 'div' => array('class' => 'submit'),
  7099. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7100. '/div'
  7101. );
  7102. $this->assertTags($result, $expected);
  7103. $result = $this->Form->submit('/relative/cake.power.gif');
  7104. $expected = array(
  7105. 'div' => array('class' => 'submit'),
  7106. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7107. '/div'
  7108. );
  7109. $this->assertTags($result, $expected);
  7110. $result = $this->Form->submit('cake.power.gif');
  7111. $expected = array(
  7112. 'div' => array('class' => 'submit'),
  7113. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7114. '/div'
  7115. );
  7116. $this->assertTags($result, $expected);
  7117. $result = $this->Form->submit('Not.an.image');
  7118. $expected = array(
  7119. 'div' => array('class' => 'submit'),
  7120. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7121. '/div'
  7122. );
  7123. $this->assertTags($result, $expected);
  7124. $after = '--after--';
  7125. $before = '--before--';
  7126. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7127. $expected = array(
  7128. 'div' => array('class' => 'submit'),
  7129. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7130. '--after--',
  7131. '/div'
  7132. );
  7133. $this->assertTags($result, $expected);
  7134. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7135. $expected = array(
  7136. 'div' => array('class' => 'submit'),
  7137. '--before--',
  7138. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7139. '/div'
  7140. );
  7141. $this->assertTags($result, $expected);
  7142. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7143. $expected = array(
  7144. 'div' => array('class' => 'submit'),
  7145. '--before--',
  7146. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7147. '--after--',
  7148. '/div'
  7149. );
  7150. $this->assertTags($result, $expected);
  7151. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7152. $expected = array(
  7153. 'div' => array('class' => 'submit'),
  7154. '--before--',
  7155. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7156. '--after--',
  7157. '/div'
  7158. );
  7159. $this->assertTags($result, $expected);
  7160. }
  7161. /**
  7162. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7163. * be submitted at a time.
  7164. *
  7165. * @return void
  7166. */
  7167. public function testSubmitUnlockedByDefault() {
  7168. $this->Form->request->params['_Token']['key'] = 'secured';
  7169. $this->Form->submit('Go go');
  7170. $this->Form->submit('Save', array('name' => 'save'));
  7171. $result = $this->Form->unlockField();
  7172. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7173. }
  7174. /**
  7175. * Test submit image with timestamps.
  7176. *
  7177. * @return void
  7178. */
  7179. public function testSubmitImageTimestamp() {
  7180. Configure::write('Asset.timestamp', 'force');
  7181. $result = $this->Form->submit('cake.power.gif');
  7182. $expected = array(
  7183. 'div' => array('class' => 'submit'),
  7184. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7185. '/div'
  7186. );
  7187. $this->assertTags($result, $expected);
  7188. }
  7189. /**
  7190. * test the create() method
  7191. *
  7192. * @return void
  7193. */
  7194. public function testCreate() {
  7195. $result = $this->Form->create('Contact');
  7196. $encoding = strtolower(Configure::read('App.encoding'));
  7197. $expected = array(
  7198. 'form' => array(
  7199. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7200. 'accept-charset' => $encoding
  7201. ),
  7202. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7203. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7204. '/div'
  7205. );
  7206. $this->assertTags($result, $expected);
  7207. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7208. $expected = array('form' => array(
  7209. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7210. 'accept-charset' => $encoding
  7211. ));
  7212. $this->assertTags($result, $expected);
  7213. $result = $this->Form->create('Contact', array('type' => 'get'));
  7214. $expected = array('form' => array(
  7215. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7216. 'accept-charset' => $encoding
  7217. ));
  7218. $this->assertTags($result, $expected);
  7219. $result = $this->Form->create('Contact', array('type' => 'put'));
  7220. $expected = array(
  7221. 'form' => array(
  7222. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7223. 'accept-charset' => $encoding
  7224. ),
  7225. 'div' => array('style' => 'display:none;'),
  7226. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7227. '/div'
  7228. );
  7229. $this->assertTags($result, $expected);
  7230. $result = $this->Form->create('Contact', array('type' => 'file'));
  7231. $expected = array(
  7232. 'form' => array(
  7233. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7234. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7235. ),
  7236. 'div' => array('style' => 'display:none;'),
  7237. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7238. '/div'
  7239. );
  7240. $this->assertTags($result, $expected);
  7241. $this->Form->request->data['Contact']['id'] = 1;
  7242. $this->Form->request->here = '/contacts/edit/1';
  7243. $this->Form->request['action'] = 'edit';
  7244. $result = $this->Form->create('Contact');
  7245. $expected = array(
  7246. 'form' => array(
  7247. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7248. 'accept-charset' => $encoding
  7249. ),
  7250. 'div' => array('style' => 'display:none;'),
  7251. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7252. '/div'
  7253. );
  7254. $this->assertTags($result, $expected);
  7255. $this->Form->request->data['Contact']['id'] = 1;
  7256. $this->Form->request->here = '/contacts/edit/1';
  7257. $this->Form->request['action'] = 'edit';
  7258. $result = $this->Form->create('Contact', array('type' => 'file'));
  7259. $expected = array(
  7260. 'form' => array(
  7261. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7262. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7263. ),
  7264. 'div' => array('style' => 'display:none;'),
  7265. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7266. '/div'
  7267. );
  7268. $this->assertTags($result, $expected);
  7269. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7270. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7271. $expected = array(
  7272. 'form' => array(
  7273. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7274. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7275. ),
  7276. 'div' => array('style' => 'display:none;'),
  7277. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7278. '/div'
  7279. );
  7280. $this->assertTags($result, $expected);
  7281. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7282. $expected = array(
  7283. 'form' => array(
  7284. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7285. 'accept-charset' => $encoding
  7286. ),
  7287. 'div' => array('style' => 'display:none;'),
  7288. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7289. '/div'
  7290. );
  7291. $this->assertTags($result, $expected);
  7292. $this->Form->request['action'] = 'add';
  7293. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7294. $expected = array(
  7295. 'form' => array(
  7296. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7297. 'accept-charset' => $encoding
  7298. ),
  7299. 'div' => array('style' => 'display:none;'),
  7300. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7301. '/div'
  7302. );
  7303. $this->assertTags($result, $expected);
  7304. $result = $this->Form->create('User', array('action' => 'login'));
  7305. $expected = array(
  7306. 'form' => array(
  7307. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7308. 'accept-charset' => $encoding
  7309. ),
  7310. 'div' => array('style' => 'display:none;'),
  7311. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7312. '/div'
  7313. );
  7314. $this->assertTags($result, $expected);
  7315. $result = $this->Form->create('User', array('url' => '/users/login'));
  7316. $expected = array(
  7317. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7318. 'div' => array('style' => 'display:none;'),
  7319. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7320. '/div'
  7321. );
  7322. $this->assertTags($result, $expected);
  7323. $this->Form->request['controller'] = 'pages';
  7324. $result = $this->Form->create('User', array('action' => 'signup'));
  7325. $expected = array(
  7326. 'form' => array(
  7327. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7328. 'accept-charset' => $encoding
  7329. ),
  7330. 'div' => array('style' => 'display:none;'),
  7331. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7332. '/div'
  7333. );
  7334. $this->assertTags($result, $expected);
  7335. $this->Form->request->data = array();
  7336. $this->Form->request['controller'] = 'contacts';
  7337. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7338. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7339. $expected = array(
  7340. 'form' => array(
  7341. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7342. 'accept-charset' => 'utf-8'
  7343. ),
  7344. 'div' => array('style' => 'display:none;'),
  7345. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7346. '/div'
  7347. );
  7348. $this->assertTags($result, $expected);
  7349. }
  7350. /**
  7351. * Test the onsubmit option for create()
  7352. *
  7353. * @return void
  7354. */
  7355. public function testCreateOnSubmit() {
  7356. $this->Form->request->data = array();
  7357. $this->Form->request['controller'] = 'contacts';
  7358. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7359. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7360. $expected = array(
  7361. 'form' => array(
  7362. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7363. 'accept-charset' => 'utf-8'
  7364. ),
  7365. 'div' => array('style' => 'display:none;'),
  7366. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7367. '/div'
  7368. );
  7369. $this->assertTags($result, $expected);
  7370. $this->Form->request->data = array();
  7371. $this->Form->request['controller'] = 'contacts';
  7372. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7373. $result = $this->Form->create(array(
  7374. 'url' => array('action' => 'index', 'param'),
  7375. 'default' => false,
  7376. 'onsubmit' => 'someFunction();'
  7377. ));
  7378. $expected = array(
  7379. 'form' => array(
  7380. 'id' => 'ContactAddForm', 'method' => 'post',
  7381. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7382. 'action' => '/contacts/index/param',
  7383. 'accept-charset' => 'utf-8'
  7384. ),
  7385. 'div' => array('style' => 'display:none;'),
  7386. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7387. '/div'
  7388. );
  7389. $this->assertTags($result, $expected);
  7390. }
  7391. /**
  7392. * test create() with automatic url generation
  7393. *
  7394. * @return void
  7395. */
  7396. public function testCreateAutoUrl() {
  7397. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7398. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7399. $this->Form->request->base = '/base_url';
  7400. $result = $this->Form->create('Contact');
  7401. $expected = array(
  7402. 'form' => array(
  7403. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7404. 'accept-charset' => 'utf-8'
  7405. ),
  7406. 'div' => array('style' => 'display:none;'),
  7407. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7408. '/div'
  7409. );
  7410. $this->assertTags($result, $expected);
  7411. $this->Form->request['action'] = 'delete';
  7412. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7413. $this->Form->request->base = '/base_url';
  7414. $result = $this->Form->create('Contact');
  7415. $expected = array(
  7416. 'form' => array(
  7417. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7418. 'accept-charset' => 'utf-8'
  7419. ),
  7420. 'div' => array('style' => 'display:none;'),
  7421. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7422. '/div'
  7423. );
  7424. $this->assertTags($result, $expected);
  7425. }
  7426. /**
  7427. * test create() with a custom route
  7428. *
  7429. * @return void
  7430. */
  7431. public function testCreateCustomRoute() {
  7432. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7433. $encoding = strtolower(Configure::read('App.encoding'));
  7434. $result = $this->Form->create('User', array('action' => 'login'));
  7435. $expected = array(
  7436. 'form' => array(
  7437. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7438. 'accept-charset' => $encoding
  7439. ),
  7440. 'div' => array('style' => 'display:none;'),
  7441. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7442. '/div'
  7443. );
  7444. $this->assertTags($result, $expected);
  7445. }
  7446. /**
  7447. * test that inputDefaults are stored and used.
  7448. *
  7449. * @return void
  7450. */
  7451. public function testCreateWithInputDefaults() {
  7452. $this->Form->create('User', array(
  7453. 'inputDefaults' => array(
  7454. 'div' => false,
  7455. 'label' => false,
  7456. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7457. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7458. )
  7459. ));
  7460. $result = $this->Form->input('username');
  7461. $expected = array(
  7462. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7463. );
  7464. $this->assertTags($result, $expected);
  7465. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7466. $expected = array(
  7467. 'div' => array('class' => 'input text'),
  7468. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7469. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7470. '/div'
  7471. );
  7472. $this->assertTags($result, $expected);
  7473. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7474. $expected = array(
  7475. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7476. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7477. );
  7478. $this->assertTags($result, $expected);
  7479. $this->Form->create('User', array(
  7480. 'inputDefaults' => array(
  7481. 'div' => false,
  7482. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7483. )
  7484. ));
  7485. $result = $this->Form->input('username', array('div' => true));
  7486. $expected = array(
  7487. 'div' => array('class' => 'input text'),
  7488. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7489. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7490. '/div'
  7491. );
  7492. $this->assertTags($result, $expected);
  7493. }
  7494. /**
  7495. * test automatic accept-charset overriding
  7496. *
  7497. * @return void
  7498. */
  7499. public function testCreateWithAcceptCharset() {
  7500. $result = $this->Form->create('UserForm', array(
  7501. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7502. )
  7503. );
  7504. $expected = array(
  7505. 'form' => array(
  7506. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7507. 'accept-charset' => 'iso-8859-1'
  7508. ),
  7509. 'div' => array('style' => 'display:none;'),
  7510. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7511. '/div'
  7512. );
  7513. $this->assertTags($result, $expected);
  7514. }
  7515. /**
  7516. * Test base form URL when url param is passed with multiple parameters (&)
  7517. *
  7518. */
  7519. public function testCreateQuerystringrequest() {
  7520. $encoding = strtolower(Configure::read('App.encoding'));
  7521. $result = $this->Form->create('Contact', array(
  7522. 'type' => 'post',
  7523. 'escape' => false,
  7524. 'url' => array(
  7525. 'controller' => 'controller',
  7526. 'action' => 'action',
  7527. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7528. )
  7529. ));
  7530. $expected = array(
  7531. 'form' => array(
  7532. 'id' => 'ContactAddForm',
  7533. 'method' => 'post',
  7534. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7535. 'accept-charset' => $encoding
  7536. ),
  7537. 'div' => array('style' => 'display:none;'),
  7538. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7539. '/div'
  7540. );
  7541. $this->assertTags($result, $expected);
  7542. $result = $this->Form->create('Contact', array(
  7543. 'type' => 'post',
  7544. 'url' => array(
  7545. 'controller' => 'controller',
  7546. 'action' => 'action',
  7547. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7548. )
  7549. ));
  7550. $expected = array(
  7551. 'form' => array(
  7552. 'id' => 'ContactAddForm',
  7553. 'method' => 'post',
  7554. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7555. 'accept-charset' => $encoding
  7556. ),
  7557. 'div' => array('style' => 'display:none;'),
  7558. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7559. '/div'
  7560. );
  7561. $this->assertTags($result, $expected);
  7562. }
  7563. /**
  7564. * test that create() doesn't cause errors by multiple id's being in the primary key
  7565. * as could happen with multiple select or checkboxes.
  7566. *
  7567. * @return void
  7568. */
  7569. public function testCreateWithMultipleIdInData() {
  7570. $encoding = strtolower(Configure::read('App.encoding'));
  7571. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7572. $result = $this->Form->create('Contact');
  7573. $expected = array(
  7574. 'form' => array(
  7575. 'id' => 'ContactAddForm',
  7576. 'method' => 'post',
  7577. 'action' => '/contacts/add',
  7578. 'accept-charset' => $encoding
  7579. ),
  7580. 'div' => array('style' => 'display:none;'),
  7581. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7582. '/div'
  7583. );
  7584. $this->assertTags($result, $expected);
  7585. }
  7586. /**
  7587. * test that create() doesn't add in extra passed params.
  7588. *
  7589. * @return void
  7590. */
  7591. public function testCreatePassedArgs() {
  7592. $encoding = strtolower(Configure::read('App.encoding'));
  7593. $this->Form->request->data['Contact']['id'] = 1;
  7594. $result = $this->Form->create('Contact', array(
  7595. 'type' => 'post',
  7596. 'escape' => false,
  7597. 'url' => array(
  7598. 'action' => 'edit',
  7599. 'myparam'
  7600. )
  7601. ));
  7602. $expected = array(
  7603. 'form' => array(
  7604. 'id' => 'ContactAddForm',
  7605. 'method' => 'post',
  7606. 'action' => '/contacts/edit/myparam',
  7607. 'accept-charset' => $encoding
  7608. ),
  7609. 'div' => array('style' => 'display:none;'),
  7610. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7611. '/div'
  7612. );
  7613. $this->assertTags($result, $expected);
  7614. }
  7615. /**
  7616. * test creating a get form, and get form inputs.
  7617. *
  7618. * @return void
  7619. */
  7620. public function testGetFormCreate() {
  7621. $encoding = strtolower(Configure::read('App.encoding'));
  7622. $result = $this->Form->create('Contact', array('type' => 'get'));
  7623. $this->assertTags($result, array('form' => array(
  7624. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7625. 'accept-charset' => $encoding
  7626. )));
  7627. $result = $this->Form->text('Contact.name');
  7628. $this->assertTags($result, array('input' => array(
  7629. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7630. )));
  7631. $result = $this->Form->password('password');
  7632. $this->assertTags($result, array('input' => array(
  7633. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7634. )));
  7635. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7636. $result = $this->Form->text('user_form');
  7637. $this->assertTags($result, array('input' => array(
  7638. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7639. )));
  7640. }
  7641. /**
  7642. * test get form, and inputs when the model param is false
  7643. *
  7644. * @return void
  7645. */
  7646. public function testGetFormWithFalseModel() {
  7647. $encoding = strtolower(Configure::read('App.encoding'));
  7648. $this->Form->request['controller'] = 'contact_test';
  7649. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7650. $expected = array('form' => array(
  7651. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7652. 'accept-charset' => $encoding
  7653. ));
  7654. $this->assertTags($result, $expected);
  7655. $result = $this->Form->text('reason');
  7656. $expected = array(
  7657. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7658. );
  7659. $this->assertTags($result, $expected);
  7660. }
  7661. /**
  7662. * test that datetime() works with GET style forms.
  7663. *
  7664. * @return void
  7665. */
  7666. public function testDateTimeWithGetForms() {
  7667. extract($this->dateRegex);
  7668. $this->Form->create('Contact', array('type' => 'get'));
  7669. $result = $this->Form->datetime('created');
  7670. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7671. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7672. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7673. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7674. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7675. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7676. }
  7677. /**
  7678. * testEditFormWithData method
  7679. *
  7680. * test auto populating form elements from submitted data.
  7681. *
  7682. * @return void
  7683. */
  7684. public function testEditFormWithData() {
  7685. $this->Form->request->data = array('Person' => array(
  7686. 'id' => 1,
  7687. 'first_name' => 'Nate',
  7688. 'last_name' => 'Abele',
  7689. 'email' => 'nate@example.com'
  7690. ));
  7691. $this->Form->request->addParams(array(
  7692. 'models' => array('Person'),
  7693. 'controller' => 'people',
  7694. 'action' => 'add'
  7695. ));
  7696. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7697. $this->Form->create();
  7698. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7699. $expected = array(
  7700. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7701. 'select' => array(
  7702. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7703. ),
  7704. array('option' => array('value' => 1)), 'Nate', '/option',
  7705. array('option' => array('value' => 2)), 'Garrett', '/option',
  7706. array('option' => array('value' => 3)), 'Larry', '/option',
  7707. '/select'
  7708. );
  7709. $this->assertTags($result, $expected);
  7710. }
  7711. /**
  7712. * Test that required fields are created for various types of validation.
  7713. *
  7714. * @return void
  7715. */
  7716. public function testFormInputRequiredDetection() {
  7717. $this->Form->create('Contact');
  7718. $result = $this->Form->input('Contact.non_existing');
  7719. $expected = array(
  7720. 'div' => array('class' => 'input text'),
  7721. 'label' => array('for' => 'ContactNonExisting'),
  7722. 'Non Existing',
  7723. '/label',
  7724. 'input' => array(
  7725. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7726. 'id' => 'ContactNonExisting'
  7727. ),
  7728. '/div'
  7729. );
  7730. $this->assertTags($result, $expected);
  7731. $result = $this->Form->input('Contact.imrequired');
  7732. $expected = array(
  7733. 'div' => array('class' => 'input text required'),
  7734. 'label' => array('for' => 'ContactImrequired'),
  7735. 'Imrequired',
  7736. '/label',
  7737. 'input' => array(
  7738. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7739. 'id' => 'ContactImrequired',
  7740. 'required' => 'required'
  7741. ),
  7742. '/div'
  7743. );
  7744. $this->assertTags($result, $expected);
  7745. $result = $this->Form->input('Contact.imalsorequired');
  7746. $expected = array(
  7747. 'div' => array('class' => 'input text required'),
  7748. 'label' => array('for' => 'ContactImalsorequired'),
  7749. 'Imalsorequired',
  7750. '/label',
  7751. 'input' => array(
  7752. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7753. 'id' => 'ContactImalsorequired',
  7754. 'required' => 'required'
  7755. ),
  7756. '/div'
  7757. );
  7758. $this->assertTags($result, $expected);
  7759. $result = $this->Form->input('Contact.imrequiredtoo');
  7760. $expected = array(
  7761. 'div' => array('class' => 'input text required'),
  7762. 'label' => array('for' => 'ContactImrequiredtoo'),
  7763. 'Imrequiredtoo',
  7764. '/label',
  7765. 'input' => array(
  7766. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7767. 'id' => 'ContactImrequiredtoo',
  7768. 'required' => 'required'
  7769. ),
  7770. '/div'
  7771. );
  7772. $this->assertTags($result, $expected);
  7773. $result = $this->Form->input('Contact.required_one');
  7774. $expected = array(
  7775. 'div' => array('class' => 'input text required'),
  7776. 'label' => array('for' => 'ContactRequiredOne'),
  7777. 'Required One',
  7778. '/label',
  7779. 'input' => array(
  7780. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7781. 'id' => 'ContactRequiredOne',
  7782. 'required' => 'required'
  7783. ),
  7784. '/div'
  7785. );
  7786. $this->assertTags($result, $expected);
  7787. $result = $this->Form->input('Contact.string_required');
  7788. $expected = array(
  7789. 'div' => array('class' => 'input text required'),
  7790. 'label' => array('for' => 'ContactStringRequired'),
  7791. 'String Required',
  7792. '/label',
  7793. 'input' => array(
  7794. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7795. 'id' => 'ContactStringRequired',
  7796. 'required' => 'required'
  7797. ),
  7798. '/div'
  7799. );
  7800. $this->assertTags($result, $expected);
  7801. $result = $this->Form->input('Contact.imnotrequired');
  7802. $expected = array(
  7803. 'div' => array('class' => 'input text'),
  7804. 'label' => array('for' => 'ContactImnotrequired'),
  7805. 'Imnotrequired',
  7806. '/label',
  7807. 'input' => array(
  7808. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  7809. 'id' => 'ContactImnotrequired'
  7810. ),
  7811. '/div'
  7812. );
  7813. $this->assertTags($result, $expected);
  7814. $result = $this->Form->input('Contact.imalsonotrequired');
  7815. $expected = array(
  7816. 'div' => array('class' => 'input text'),
  7817. 'label' => array('for' => 'ContactImalsonotrequired'),
  7818. 'Imalsonotrequired',
  7819. '/label',
  7820. 'input' => array(
  7821. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  7822. 'id' => 'ContactImalsonotrequired'
  7823. ),
  7824. '/div'
  7825. );
  7826. $this->assertTags($result, $expected);
  7827. $result = $this->Form->input('Contact.imalsonotrequired2');
  7828. $expected = array(
  7829. 'div' => array('class' => 'input text'),
  7830. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7831. 'Imalsonotrequired2',
  7832. '/label',
  7833. 'input' => array(
  7834. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  7835. 'id' => 'ContactImalsonotrequired2'
  7836. ),
  7837. '/div'
  7838. );
  7839. $this->assertTags($result, $expected);
  7840. $result = $this->Form->input('Contact.imnotrequiredeither');
  7841. $expected = array(
  7842. 'div' => array('class' => 'input text'),
  7843. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7844. 'Imnotrequiredeither',
  7845. '/label',
  7846. 'input' => array(
  7847. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  7848. 'id' => 'ContactImnotrequiredeither'
  7849. ),
  7850. '/div'
  7851. );
  7852. $this->assertTags($result, $expected);
  7853. $result = $this->Form->input('Contact.iamrequiredalways');
  7854. $expected = array(
  7855. 'div' => array('class' => 'input text required'),
  7856. 'label' => array('for' => 'ContactIamrequiredalways'),
  7857. 'Iamrequiredalways',
  7858. '/label',
  7859. 'input' => array(
  7860. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  7861. 'id' => 'ContactIamrequiredalways',
  7862. 'required' => 'required'
  7863. ),
  7864. '/div'
  7865. );
  7866. $this->assertTags($result, $expected);
  7867. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7868. $expected = array(
  7869. 'div' => array('class' => 'input checkbox required'),
  7870. array('input' => array(
  7871. 'type' => 'hidden',
  7872. 'name' => 'data[Contact][boolean_field]',
  7873. 'id' => 'ContactBooleanField_',
  7874. 'value' => '0'
  7875. )),
  7876. array('input' => array(
  7877. 'type' => 'checkbox',
  7878. 'name' => 'data[Contact][boolean_field]',
  7879. 'value' => '1',
  7880. 'id' => 'ContactBooleanField'
  7881. )),
  7882. 'label' => array('for' => 'ContactBooleanField'),
  7883. 'Boolean Field',
  7884. '/label',
  7885. '/div'
  7886. );
  7887. $this->assertTags($result, $expected);
  7888. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7889. $expected = array(
  7890. 'div' => array('class' => 'input checkbox required'),
  7891. array('input' => array(
  7892. 'type' => 'hidden',
  7893. 'name' => 'data[Contact][boolean_field]',
  7894. 'id' => 'ContactBooleanField_',
  7895. 'value' => '0'
  7896. )),
  7897. array('input' => array(
  7898. 'type' => 'checkbox',
  7899. 'name' => 'data[Contact][boolean_field]',
  7900. 'value' => '1',
  7901. 'id' => 'ContactBooleanField',
  7902. 'required' => 'required'
  7903. )),
  7904. 'label' => array('for' => 'ContactBooleanField'),
  7905. 'Boolean Field',
  7906. '/label',
  7907. '/div'
  7908. );
  7909. $this->assertTags($result, $expected);
  7910. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7911. $expected = array(
  7912. 'div' => array('class' => 'input file required'),
  7913. 'label' => array('for' => 'ContactIamrequiredalways'),
  7914. 'Iamrequiredalways',
  7915. '/label',
  7916. 'input' => array(
  7917. 'type' => 'file',
  7918. 'name' => 'data[Contact][iamrequiredalways]',
  7919. 'id' => 'ContactIamrequiredalways',
  7920. 'required' => 'required'
  7921. ),
  7922. '/div'
  7923. );
  7924. $this->assertTags($result, $expected);
  7925. }
  7926. /**
  7927. * Test that required fields are created when only using ModelValidator::add().
  7928. *
  7929. * @return void
  7930. */
  7931. public function testFormInputRequiredDetectionModelValidator() {
  7932. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7933. $this->Form->create('ContactTag');
  7934. $result = $this->Form->input('ContactTag.iwillberequired');
  7935. $expected = array(
  7936. 'div' => array('class' => 'input text required'),
  7937. 'label' => array('for' => 'ContactTagIwillberequired'),
  7938. 'Iwillberequired',
  7939. '/label',
  7940. 'input' => array(
  7941. 'name' => 'data[ContactTag][iwillberequired]',
  7942. 'type' => 'text',
  7943. 'id' => 'ContactTagIwillberequired',
  7944. 'required' => 'required'
  7945. ),
  7946. '/div'
  7947. );
  7948. $this->assertTags($result, $expected);
  7949. }
  7950. /**
  7951. * testFormMagicInput method
  7952. *
  7953. * @return void
  7954. */
  7955. public function testFormMagicInput() {
  7956. $encoding = strtolower(Configure::read('App.encoding'));
  7957. $result = $this->Form->create('Contact');
  7958. $expected = array(
  7959. 'form' => array(
  7960. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7961. 'accept-charset' => $encoding
  7962. ),
  7963. 'div' => array('style' => 'display:none;'),
  7964. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7965. '/div'
  7966. );
  7967. $this->assertTags($result, $expected);
  7968. $result = $this->Form->input('name');
  7969. $expected = array(
  7970. 'div' => array('class' => 'input text'),
  7971. 'label' => array('for' => 'ContactName'),
  7972. 'Name',
  7973. '/label',
  7974. 'input' => array(
  7975. 'type' => 'text', 'name' => 'data[Contact][name]',
  7976. 'id' => 'ContactName', 'maxlength' => '255'
  7977. ),
  7978. '/div'
  7979. );
  7980. $this->assertTags($result, $expected);
  7981. $result = $this->Form->input('non_existing_field_in_contact_model');
  7982. $expected = array(
  7983. 'div' => array('class' => 'input text'),
  7984. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7985. 'Non Existing Field In Contact Model',
  7986. '/label',
  7987. 'input' => array(
  7988. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  7989. 'id' => 'ContactNonExistingFieldInContactModel'
  7990. ),
  7991. '/div'
  7992. );
  7993. $this->assertTags($result, $expected);
  7994. $result = $this->Form->input('Address.street');
  7995. $expected = array(
  7996. 'div' => array('class' => 'input text'),
  7997. 'label' => array('for' => 'AddressStreet'),
  7998. 'Street',
  7999. '/label',
  8000. 'input' => array(
  8001. 'type' => 'text', 'name' => 'data[Address][street]',
  8002. 'id' => 'AddressStreet'
  8003. ),
  8004. '/div'
  8005. );
  8006. $this->assertTags($result, $expected);
  8007. $result = $this->Form->input('Address.non_existing_field_in_model');
  8008. $expected = array(
  8009. 'div' => array('class' => 'input text'),
  8010. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  8011. 'Non Existing Field In Model',
  8012. '/label',
  8013. 'input' => array(
  8014. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  8015. 'id' => 'AddressNonExistingFieldInModel'
  8016. ),
  8017. '/div'
  8018. );
  8019. $this->assertTags($result, $expected);
  8020. $result = $this->Form->input('name', array('div' => false));
  8021. $expected = array(
  8022. 'label' => array('for' => 'ContactName'),
  8023. 'Name',
  8024. '/label',
  8025. 'input' => array(
  8026. 'type' => 'text', 'name' => 'data[Contact][name]',
  8027. 'id' => 'ContactName', 'maxlength' => '255'
  8028. )
  8029. );
  8030. $this->assertTags($result, $expected);
  8031. extract($this->dateRegex);
  8032. $now = strtotime('now');
  8033. $result = $this->Form->input('Contact.published', array('div' => false));
  8034. $expected = array(
  8035. 'label' => array('for' => 'ContactPublishedMonth'),
  8036. 'Published',
  8037. '/label',
  8038. array('select' => array(
  8039. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  8040. )),
  8041. $monthsRegex,
  8042. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8043. date('F', $now),
  8044. '/option',
  8045. '*/select',
  8046. '-',
  8047. array('select' => array(
  8048. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  8049. )),
  8050. $daysRegex,
  8051. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8052. date('j', $now),
  8053. '/option',
  8054. '*/select',
  8055. '-',
  8056. array('select' => array(
  8057. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  8058. )),
  8059. $yearsRegex,
  8060. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8061. date('Y', $now),
  8062. '*/select'
  8063. );
  8064. $this->assertTags($result, $expected);
  8065. $result = $this->Form->input('Contact.updated', array('div' => false));
  8066. $expected = array(
  8067. 'label' => array('for' => 'ContactUpdatedMonth'),
  8068. 'Updated',
  8069. '/label',
  8070. array('select' => array(
  8071. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  8072. )),
  8073. $monthsRegex,
  8074. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8075. date('F', $now),
  8076. '/option',
  8077. '*/select',
  8078. '-',
  8079. array('select' => array(
  8080. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  8081. )),
  8082. $daysRegex,
  8083. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8084. date('j', $now),
  8085. '/option',
  8086. '*/select',
  8087. '-',
  8088. array('select' => array(
  8089. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  8090. )),
  8091. $yearsRegex,
  8092. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8093. date('Y', $now),
  8094. '*/select'
  8095. );
  8096. $this->assertTags($result, $expected);
  8097. $result = $this->Form->input('UserForm.stuff');
  8098. $expected = array(
  8099. 'div' => array('class' => 'input text'),
  8100. 'label' => array('for' => 'UserFormStuff'),
  8101. 'Stuff',
  8102. '/label',
  8103. 'input' => array(
  8104. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8105. 'id' => 'UserFormStuff', 'maxlength' => 10
  8106. ),
  8107. '/div'
  8108. );
  8109. $this->assertTags($result, $expected);
  8110. }
  8111. /**
  8112. * testForMagicInputNonExistingNorValidated method
  8113. *
  8114. * @return void
  8115. */
  8116. public function testForMagicInputNonExistingNorValidated() {
  8117. $encoding = strtolower(Configure::read('App.encoding'));
  8118. $result = $this->Form->create('Contact');
  8119. $expected = array(
  8120. 'form' => array(
  8121. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8122. 'accept-charset' => $encoding
  8123. ),
  8124. 'div' => array('style' => 'display:none;'),
  8125. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8126. '/div'
  8127. );
  8128. $this->assertTags($result, $expected);
  8129. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8130. $expected = array(
  8131. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8132. 'Non Existing Nor Validated',
  8133. '/label',
  8134. 'input' => array(
  8135. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8136. 'id' => 'ContactNonExistingNorValidated'
  8137. )
  8138. );
  8139. $this->assertTags($result, $expected);
  8140. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8141. 'div' => false, 'value' => 'my value'
  8142. ));
  8143. $expected = array(
  8144. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8145. 'Non Existing Nor Validated',
  8146. '/label',
  8147. 'input' => array(
  8148. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8149. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8150. )
  8151. );
  8152. $this->assertTags($result, $expected);
  8153. $this->Form->request->data = array(
  8154. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8155. ));
  8156. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8157. $expected = array(
  8158. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8159. 'Non Existing Nor Validated',
  8160. '/label',
  8161. 'input' => array(
  8162. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8163. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8164. )
  8165. );
  8166. $this->assertTags($result, $expected);
  8167. }
  8168. /**
  8169. * testFormMagicInputLabel method
  8170. *
  8171. * @return void
  8172. */
  8173. public function testFormMagicInputLabel() {
  8174. $encoding = strtolower(Configure::read('App.encoding'));
  8175. $result = $this->Form->create('Contact');
  8176. $expected = array(
  8177. 'form' => array(
  8178. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8179. 'accept-charset' => $encoding
  8180. ),
  8181. 'div' => array('style' => 'display:none;'),
  8182. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8183. '/div'
  8184. );
  8185. $this->assertTags($result, $expected);
  8186. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8187. $this->assertTags($result, array('input' => array(
  8188. 'name' => 'data[Contact][name]', 'type' => 'text',
  8189. 'id' => 'ContactName', 'maxlength' => '255')
  8190. ));
  8191. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8192. $expected = array(
  8193. 'label' => array('for' => 'ContactName'),
  8194. 'My label',
  8195. '/label',
  8196. 'input' => array(
  8197. 'type' => 'text', 'name' => 'data[Contact][name]',
  8198. 'id' => 'ContactName', 'maxlength' => '255'
  8199. )
  8200. );
  8201. $this->assertTags($result, $expected);
  8202. $result = $this->Form->input('Contact.name', array(
  8203. 'div' => false, 'label' => array('class' => 'mandatory')
  8204. ));
  8205. $expected = array(
  8206. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8207. 'Name',
  8208. '/label',
  8209. 'input' => array(
  8210. 'type' => 'text', 'name' => 'data[Contact][name]',
  8211. 'id' => 'ContactName', 'maxlength' => '255'
  8212. )
  8213. );
  8214. $this->assertTags($result, $expected);
  8215. $result = $this->Form->input('Contact.name', array(
  8216. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8217. ));
  8218. $expected = array(
  8219. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8220. 'My label',
  8221. '/label',
  8222. 'input' => array(
  8223. 'type' => 'text', 'name' => 'data[Contact][name]',
  8224. 'id' => 'ContactName', 'maxlength' => '255'
  8225. )
  8226. );
  8227. $this->assertTags($result, $expected);
  8228. $result = $this->Form->input('Contact.name', array(
  8229. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8230. ));
  8231. $expected = array(
  8232. 'label' => array('for' => 'my_id'),
  8233. 'Name',
  8234. '/label',
  8235. 'input' => array(
  8236. 'type' => 'text', 'name' => 'data[Contact][name]',
  8237. 'id' => 'my_id', 'maxlength' => '255'
  8238. )
  8239. );
  8240. $this->assertTags($result, $expected);
  8241. $result = $this->Form->input('1.id');
  8242. $this->assertTags($result, array('input' => array(
  8243. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8244. 'id' => 'Contact1Id'
  8245. )));
  8246. $result = $this->Form->input("1.name");
  8247. $expected = array(
  8248. 'div' => array('class' => 'input text'),
  8249. 'label' => array('for' => 'Contact1Name'),
  8250. 'Name',
  8251. '/label',
  8252. 'input' => array(
  8253. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8254. 'id' => 'Contact1Name', 'maxlength' => '255'
  8255. ),
  8256. '/div'
  8257. );
  8258. $this->assertTags($result, $expected);
  8259. $result = $this->Form->input('Contact.1.id');
  8260. $this->assertTags($result, array(
  8261. 'input' => array(
  8262. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8263. 'id' => 'Contact1Id'
  8264. )
  8265. ));
  8266. $result = $this->Form->input("Model.1.name");
  8267. $expected = array(
  8268. 'div' => array('class' => 'input text'),
  8269. 'label' => array('for' => 'Model1Name'),
  8270. 'Name',
  8271. '/label',
  8272. 'input' => array(
  8273. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8274. 'id' => 'Model1Name'
  8275. ),
  8276. '/div'
  8277. );
  8278. $this->assertTags($result, $expected);
  8279. }
  8280. /**
  8281. * testFormEnd method
  8282. *
  8283. * @return void
  8284. */
  8285. public function testFormEnd() {
  8286. $this->assertEquals('</form>', $this->Form->end());
  8287. $result = $this->Form->end('');
  8288. $expected = array(
  8289. 'div' => array('class' => 'submit'),
  8290. 'input' => array('type' => 'submit', 'value' => ''),
  8291. '/div',
  8292. '/form'
  8293. );
  8294. $this->assertTags($result, $expected);
  8295. $result = $this->Form->end(array('label' => ''));
  8296. $expected = array(
  8297. 'div' => array('class' => 'submit'),
  8298. 'input' => array('type' => 'submit', 'value' => ''),
  8299. '/div',
  8300. '/form'
  8301. );
  8302. $this->assertTags($result, $expected);
  8303. $result = $this->Form->end('save');
  8304. $expected = array(
  8305. 'div' => array('class' => 'submit'),
  8306. 'input' => array('type' => 'submit', 'value' => 'save'),
  8307. '/div',
  8308. '/form'
  8309. );
  8310. $this->assertTags($result, $expected);
  8311. $result = $this->Form->end(array('label' => 'save'));
  8312. $expected = array(
  8313. 'div' => array('class' => 'submit'),
  8314. 'input' => array('type' => 'submit', 'value' => 'save'),
  8315. '/div',
  8316. '/form'
  8317. );
  8318. $this->assertTags($result, $expected);
  8319. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8320. $expected = array(
  8321. 'div' => array('class' => 'submit'),
  8322. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8323. '/div',
  8324. '/form'
  8325. );
  8326. $this->assertTags($result, $expected);
  8327. $result = $this->Form->end(array('name' => 'Whatever'));
  8328. $expected = array(
  8329. 'div' => array('class' => 'submit'),
  8330. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8331. '/div',
  8332. '/form'
  8333. );
  8334. $this->assertTags($result, $expected);
  8335. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8336. $expected = array(
  8337. 'div' => array('class' => 'good'),
  8338. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8339. '/div',
  8340. '/form'
  8341. );
  8342. $this->assertTags($result, $expected);
  8343. $result = $this->Form->end(array(
  8344. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8345. ));
  8346. $expected = array(
  8347. 'div' => array('class' => 'good'),
  8348. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8349. '/div',
  8350. '/form'
  8351. );
  8352. $this->assertTags($result, $expected);
  8353. }
  8354. /**
  8355. * testMultipleFormWithIdFields method
  8356. *
  8357. * @return void
  8358. */
  8359. public function testMultipleFormWithIdFields() {
  8360. $this->Form->create('UserForm');
  8361. $result = $this->Form->input('id');
  8362. $this->assertTags($result, array('input' => array(
  8363. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8364. )));
  8365. $result = $this->Form->input('ValidateItem.id');
  8366. $this->assertTags($result, array('input' => array(
  8367. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8368. 'id' => 'ValidateItemId'
  8369. )));
  8370. $result = $this->Form->input('ValidateUser.id');
  8371. $this->assertTags($result, array('input' => array(
  8372. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8373. 'id' => 'ValidateUserId'
  8374. )));
  8375. }
  8376. /**
  8377. * testDbLessModel method
  8378. *
  8379. * @return void
  8380. */
  8381. public function testDbLessModel() {
  8382. $this->Form->create('TestMail');
  8383. $result = $this->Form->input('name');
  8384. $expected = array(
  8385. 'div' => array('class' => 'input text'),
  8386. 'label' => array('for' => 'TestMailName'),
  8387. 'Name',
  8388. '/label',
  8389. 'input' => array(
  8390. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8391. 'id' => 'TestMailName'
  8392. ),
  8393. '/div'
  8394. );
  8395. $this->assertTags($result, $expected);
  8396. ClassRegistry::init('TestMail');
  8397. $this->Form->create('TestMail');
  8398. $result = $this->Form->input('name');
  8399. $expected = array(
  8400. 'div' => array('class' => 'input text'),
  8401. 'label' => array('for' => 'TestMailName'),
  8402. 'Name',
  8403. '/label',
  8404. 'input' => array(
  8405. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8406. 'id' => 'TestMailName'
  8407. ),
  8408. '/div'
  8409. );
  8410. $this->assertTags($result, $expected);
  8411. }
  8412. /**
  8413. * testBrokenness method
  8414. *
  8415. * @return void
  8416. */
  8417. public function testBrokenness() {
  8418. /*
  8419. * #4 This test has two parents and four children. By default (as of r7117) both
  8420. * parents are show but the first parent is missing a child. This is the inconsistency
  8421. * in the default behaviour - one parent has all children, the other does not - dependent
  8422. * on the data values.
  8423. */
  8424. $result = $this->Form->select('Model.field', array(
  8425. 'Fred' => array(
  8426. 'freds_son_1' => 'Fred',
  8427. 'freds_son_2' => 'Freddie'
  8428. ),
  8429. 'Bert' => array(
  8430. 'berts_son_1' => 'Albert',
  8431. 'berts_son_2' => 'Bertie')
  8432. ),
  8433. array('showParents' => true, 'empty' => false)
  8434. );
  8435. $expected = array(
  8436. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8437. array('optgroup' => array('label' => 'Fred')),
  8438. array('option' => array('value' => 'freds_son_1')),
  8439. 'Fred',
  8440. '/option',
  8441. array('option' => array('value' => 'freds_son_2')),
  8442. 'Freddie',
  8443. '/option',
  8444. '/optgroup',
  8445. array('optgroup' => array('label' => 'Bert')),
  8446. array('option' => array('value' => 'berts_son_1')),
  8447. 'Albert',
  8448. '/option',
  8449. array('option' => array('value' => 'berts_son_2')),
  8450. 'Bertie',
  8451. '/option',
  8452. '/optgroup',
  8453. '/select'
  8454. );
  8455. $this->assertTags($result, $expected);
  8456. /*
  8457. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8458. * changed, so we should expect the same select list data, just with a different name
  8459. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8460. * This is where data corruption can occur, because when a select value is missing from
  8461. * a list a form will substitute the first value in the list - without the user knowing.
  8462. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8463. * affect the availability of 3 => 'Three' as a valid option.
  8464. */
  8465. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8466. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8467. ));
  8468. $result = $this->Form->select(
  8469. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8470. );
  8471. $expected = array(
  8472. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8473. array('option' => array('value' => 1)),
  8474. 'One',
  8475. '/option',
  8476. array('option' => array('value' => 2)),
  8477. 'Two',
  8478. '/option',
  8479. array('optgroup' => array('label' => 'Three')),
  8480. array('option' => array('value' => 3)),
  8481. 'Three',
  8482. '/option',
  8483. array('option' => array('value' => 4)),
  8484. 'Four',
  8485. '/option',
  8486. array('option' => array('value' => 5)),
  8487. 'Five',
  8488. '/option',
  8489. '/optgroup',
  8490. '/select'
  8491. );
  8492. $this->assertTags($result, $expected);
  8493. }
  8494. /**
  8495. * Test the generation of fields for a multi record form.
  8496. *
  8497. * @return void
  8498. */
  8499. public function testMultiRecordForm() {
  8500. $this->Form->create('ValidateProfile');
  8501. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8502. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8503. $expected = array(
  8504. 'div' => array('class' => 'input textarea'),
  8505. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8506. 'Name',
  8507. '/label',
  8508. 'textarea' => array(
  8509. 'id' => 'ValidateProfile1ValidateItem2Name',
  8510. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8511. 'cols' => 30,
  8512. 'rows' => 6
  8513. ),
  8514. 'Value',
  8515. '/textarea',
  8516. '/div'
  8517. );
  8518. $this->assertTags($result, $expected);
  8519. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8520. $expected = array(
  8521. 'div' => array('class' => 'input date'),
  8522. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8523. 'Created',
  8524. '/label',
  8525. array('select' => array(
  8526. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8527. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8528. )
  8529. ),
  8530. array('option' => array('value' => '')), '/option',
  8531. $this->dateRegex['monthsRegex'],
  8532. '/select', '-',
  8533. array('select' => array(
  8534. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8535. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8536. )
  8537. ),
  8538. array('option' => array('value' => '')), '/option',
  8539. $this->dateRegex['daysRegex'],
  8540. '/select', '-',
  8541. array('select' => array(
  8542. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8543. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8544. )
  8545. ),
  8546. array('option' => array('value' => '')), '/option',
  8547. $this->dateRegex['yearsRegex'],
  8548. '/select',
  8549. '/div'
  8550. );
  8551. $this->assertTags($result, $expected);
  8552. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8553. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8554. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8555. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8556. $expected = array(
  8557. 'div' => array('class' => 'input select error'),
  8558. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8559. 'Profile',
  8560. '/label',
  8561. 'select' => array(
  8562. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8563. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8564. 'class' => 'form-error'
  8565. ),
  8566. '/select',
  8567. array('div' => array('class' => 'error-message')),
  8568. 'Error',
  8569. '/div',
  8570. '/div'
  8571. );
  8572. $this->assertTags($result, $expected);
  8573. }
  8574. /**
  8575. * test the correct display of multi-record form validation errors.
  8576. *
  8577. * @return void
  8578. */
  8579. public function testMultiRecordFormValidationErrors() {
  8580. $this->Form->create('ValidateProfile');
  8581. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8582. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8583. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8584. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8585. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8586. $result = $this->Form->error('ValidateProfile.2.city');
  8587. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8588. $result = $this->Form->error('2.city');
  8589. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8590. }
  8591. /**
  8592. * test the correct display of multi-record form validation errors.
  8593. *
  8594. * @return void
  8595. */
  8596. public function testSaveManyRecordFormValidationErrors() {
  8597. $this->Form->create('ValidateUser');
  8598. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8599. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8600. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8601. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8602. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8603. $result = $this->Form->error('ValidateUser.0.city');
  8604. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8605. }
  8606. /**
  8607. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8608. *
  8609. * @return void
  8610. */
  8611. public function testInputTemplate() {
  8612. $result = $this->Form->input('Contact.email', array(
  8613. 'type' => 'text', 'format' => array('input')
  8614. ));
  8615. $expected = array(
  8616. 'div' => array('class' => 'input text'),
  8617. 'input' => array(
  8618. 'type' => 'text', 'name' => 'data[Contact][email]',
  8619. 'id' => 'ContactEmail'
  8620. ),
  8621. '/div'
  8622. );
  8623. $this->assertTags($result, $expected);
  8624. $result = $this->Form->input('Contact.email', array(
  8625. 'type' => 'text', 'format' => array('input', 'label'),
  8626. 'label' => '<em>Email (required)</em>'
  8627. ));
  8628. $expected = array(
  8629. 'div' => array('class' => 'input text'),
  8630. array('input' => array(
  8631. 'type' => 'text', 'name' => 'data[Contact][email]',
  8632. 'id' => 'ContactEmail'
  8633. )),
  8634. 'label' => array('for' => 'ContactEmail'),
  8635. 'em' => array(),
  8636. 'Email (required)',
  8637. '/em',
  8638. '/label',
  8639. '/div'
  8640. );
  8641. $this->assertTags($result, $expected);
  8642. $result = $this->Form->input('Contact.email', array(
  8643. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8644. 'between' => '<div>Something in the middle</div>',
  8645. 'after' => '<span>Some text at the end</span>'
  8646. ));
  8647. $expected = array(
  8648. 'div' => array('class' => 'input text'),
  8649. array('input' => array(
  8650. 'type' => 'text', 'name' => 'data[Contact][email]',
  8651. 'id' => 'ContactEmail'
  8652. )),
  8653. array('div' => array()),
  8654. 'Something in the middle',
  8655. '/div',
  8656. 'label' => array('for' => 'ContactEmail'),
  8657. 'Email',
  8658. '/label',
  8659. 'span' => array(),
  8660. 'Some text at the end',
  8661. '/span',
  8662. '/div'
  8663. );
  8664. $this->assertTags($result, $expected);
  8665. $result = $this->Form->input('Contact.method', array(
  8666. 'type' => 'radio',
  8667. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8668. 'between' => 'I am between',
  8669. ));
  8670. $expected = array(
  8671. 'div' => array('class' => 'input radio'),
  8672. 'fieldset' => array(),
  8673. 'legend' => array(),
  8674. 'Method',
  8675. '/legend',
  8676. 'I am between',
  8677. 'input' => array(
  8678. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8679. 'value' => '', 'id' => 'ContactMethod_'
  8680. ),
  8681. array('input' => array(
  8682. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8683. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8684. )),
  8685. array('label' => array('for' => 'ContactMethodEmail')),
  8686. 'Email',
  8687. '/label',
  8688. array('input' => array(
  8689. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8690. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8691. )),
  8692. array('label' => array('for' => 'ContactMethodPigeon')),
  8693. 'Pigeon',
  8694. '/label',
  8695. '/fieldset',
  8696. '/div',
  8697. );
  8698. $this->assertTags($result, $expected);
  8699. }
  8700. /**
  8701. * test that some html5 inputs + FormHelper::__call() work
  8702. *
  8703. * @return void
  8704. */
  8705. public function testHtml5Inputs() {
  8706. $result = $this->Form->email('User.email');
  8707. $expected = array(
  8708. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8709. );
  8710. $this->assertTags($result, $expected);
  8711. $result = $this->Form->search('User.query');
  8712. $expected = array(
  8713. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8714. );
  8715. $this->assertTags($result, $expected);
  8716. $result = $this->Form->search('User.query', array('value' => 'test'));
  8717. $expected = array(
  8718. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8719. );
  8720. $this->assertTags($result, $expected);
  8721. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8722. $expected = array(
  8723. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8724. );
  8725. $this->assertTags($result, $expected);
  8726. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8727. $expected = array(
  8728. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8729. );
  8730. $this->assertTags($result, $expected);
  8731. }
  8732. /**
  8733. *
  8734. * @expectedException CakeException
  8735. * @return void
  8736. */
  8737. public function testHtml5InputException() {
  8738. $this->Form->email();
  8739. }
  8740. /**
  8741. * Tests that a model can be loaded from the model names passed in the request object
  8742. *
  8743. * @return void
  8744. */
  8745. public function testIntrospectModelFromRequest() {
  8746. $this->loadFixtures('Post');
  8747. App::build(array(
  8748. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8749. ));
  8750. CakePlugin::load('TestPlugin');
  8751. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8752. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8753. $this->Form->create('TestPluginPost');
  8754. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8755. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8756. CakePlugin::unload();
  8757. App::build();
  8758. }
  8759. /**
  8760. * Tests that it is possible to set the validation errors directly in the helper for a field
  8761. *
  8762. * @return void
  8763. */
  8764. public function testCustomValidationErrors() {
  8765. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8766. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8767. $this->assertEquals('Badness!', $result);
  8768. }
  8769. /**
  8770. * Tests that the 'on' key validates as expected on create
  8771. *
  8772. * @return void
  8773. */
  8774. public function testRequiredOnCreate() {
  8775. $this->Form->create('Contact');
  8776. $result = $this->Form->input('Contact.imrequiredonupdate');
  8777. $expected = array(
  8778. 'div' => array('class' => 'input text'),
  8779. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8780. 'Imrequiredonupdate',
  8781. '/label',
  8782. 'input' => array(
  8783. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8784. 'id' => 'ContactImrequiredonupdate'
  8785. ),
  8786. '/div'
  8787. );
  8788. $this->assertTags($result, $expected);
  8789. $result = $this->Form->input('Contact.imrequiredoncreate');
  8790. $expected = array(
  8791. 'div' => array('class' => 'input text required'),
  8792. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8793. 'Imrequiredoncreate',
  8794. '/label',
  8795. 'input' => array(
  8796. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8797. 'id' => 'ContactImrequiredoncreate',
  8798. 'required' => 'required'
  8799. ),
  8800. '/div'
  8801. );
  8802. $this->assertTags($result, $expected);
  8803. $result = $this->Form->input('Contact.imrequiredonboth');
  8804. $expected = array(
  8805. 'div' => array('class' => 'input text required'),
  8806. 'label' => array('for' => 'ContactImrequiredonboth'),
  8807. 'Imrequiredonboth',
  8808. '/label',
  8809. 'input' => array(
  8810. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8811. 'id' => 'ContactImrequiredonboth',
  8812. 'required' => 'required'
  8813. ),
  8814. '/div'
  8815. );
  8816. $this->assertTags($result, $expected);
  8817. $this->Form->inputDefaults(array('required' => false));
  8818. $result = $this->Form->input('Contact.imrequired');
  8819. $expected = array(
  8820. 'div' => array('class' => 'input text'),
  8821. 'label' => array('for' => 'ContactImrequired'),
  8822. 'Imrequired',
  8823. '/label',
  8824. 'input' => array(
  8825. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8826. 'id' => 'ContactImrequired'
  8827. ),
  8828. '/div'
  8829. );
  8830. $this->assertTags($result, $expected);
  8831. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8832. $this->assertTags($result, $expected);
  8833. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8834. $expected = array(
  8835. 'div' => array('class' => 'input text required'),
  8836. 'label' => array('for' => 'ContactImrequired'),
  8837. 'Imrequired',
  8838. '/label',
  8839. 'input' => array(
  8840. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8841. 'id' => 'ContactImrequired'
  8842. ),
  8843. '/div'
  8844. );
  8845. $this->assertTags($result, $expected);
  8846. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8847. $this->assertTags($result, $expected);
  8848. }
  8849. /**
  8850. * Tests that the 'on' key validates as expected on update
  8851. *
  8852. * @return void
  8853. */
  8854. public function testRequiredOnUpdate() {
  8855. $this->Form->request->data['Contact']['id'] = 1;
  8856. $this->Form->create('Contact');
  8857. $result = $this->Form->input('Contact.imrequiredonupdate');
  8858. $expected = array(
  8859. 'div' => array('class' => 'input text required'),
  8860. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8861. 'Imrequiredonupdate',
  8862. '/label',
  8863. 'input' => array(
  8864. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8865. 'id' => 'ContactImrequiredonupdate',
  8866. 'required' => 'required'
  8867. ),
  8868. '/div'
  8869. );
  8870. $this->assertTags($result, $expected);
  8871. $result = $this->Form->input('Contact.imrequiredoncreate');
  8872. $expected = array(
  8873. 'div' => array('class' => 'input text'),
  8874. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8875. 'Imrequiredoncreate',
  8876. '/label',
  8877. 'input' => array(
  8878. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  8879. 'id' => 'ContactImrequiredoncreate'
  8880. ),
  8881. '/div'
  8882. );
  8883. $this->assertTags($result, $expected);
  8884. $result = $this->Form->input('Contact.imrequiredonboth');
  8885. $expected = array(
  8886. 'div' => array('class' => 'input text required'),
  8887. 'label' => array('for' => 'ContactImrequiredonboth'),
  8888. 'Imrequiredonboth',
  8889. '/label',
  8890. 'input' => array(
  8891. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  8892. 'id' => 'ContactImrequiredonboth',
  8893. 'required' => 'required'
  8894. ),
  8895. '/div'
  8896. );
  8897. $this->assertTags($result, $expected);
  8898. $result = $this->Form->input('Contact.imrequired');
  8899. $expected = array(
  8900. 'div' => array('class' => 'input text required'),
  8901. 'label' => array('for' => 'ContactImrequired'),
  8902. 'Imrequired',
  8903. '/label',
  8904. 'input' => array(
  8905. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8906. 'id' => 'ContactImrequired',
  8907. 'required' => 'required'
  8908. ),
  8909. '/div'
  8910. );
  8911. $this->assertTags($result, $expected);
  8912. }
  8913. /**
  8914. * Test inputDefaults setter and getter
  8915. *
  8916. * @return void
  8917. */
  8918. public function testInputDefaults() {
  8919. $this->Form->create('Contact');
  8920. $this->Form->inputDefaults(array(
  8921. 'label' => false,
  8922. 'div' => array(
  8923. 'style' => 'color: #000;'
  8924. )
  8925. ));
  8926. $result = $this->Form->input('Contact.field1');
  8927. $expected = array(
  8928. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8929. 'input' => array(
  8930. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8931. 'id' => 'ContactField1'
  8932. ),
  8933. '/div'
  8934. );
  8935. $this->assertTags($result, $expected);
  8936. $this->Form->inputDefaults(array(
  8937. 'div' => false,
  8938. 'label' => 'Label',
  8939. ));
  8940. $result = $this->Form->input('Contact.field1');
  8941. $expected = array(
  8942. 'label' => array('for' => 'ContactField1'),
  8943. 'Label',
  8944. '/label',
  8945. 'input' => array(
  8946. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8947. 'id' => 'ContactField1'
  8948. ),
  8949. );
  8950. $this->assertTags($result, $expected);
  8951. $this->Form->inputDefaults(array(
  8952. 'label' => false,
  8953. ), true);
  8954. $result = $this->Form->input('Contact.field1');
  8955. $expected = array(
  8956. 'input' => array(
  8957. 'type' => 'text', 'name' => 'data[Contact][field1]',
  8958. 'id' => 'ContactField1'
  8959. ),
  8960. );
  8961. $this->assertTags($result, $expected);
  8962. $result = $this->Form->inputDefaults();
  8963. $expected = array(
  8964. 'div' => false,
  8965. 'label' => false,
  8966. );
  8967. $this->assertEquals($expected, $result);
  8968. }
  8969. }