FormHelperTest.php 315 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @package Cake.Test.Case.View.Helper
  15. * @since CakePHP(tm) v 1.2.0.4206
  16. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  17. */
  18. App::uses('ClassRegistry', 'Utility');
  19. App::uses('Controller', 'Controller');
  20. App::uses('View', 'View');
  21. App::uses('Model', 'Model');
  22. App::uses('Security', 'Utility');
  23. App::uses('CakeRequest', 'Network');
  24. App::uses('HtmlHelper', 'View/Helper');
  25. App::uses('FormHelper', 'View/Helper');
  26. App::uses('Router', 'Routing');
  27. /**
  28. * ContactTestController class
  29. *
  30. * @package Cake.Test.Case.View.Helper
  31. */
  32. class ContactTestController extends Controller {
  33. /**
  34. * uses property
  35. *
  36. * @var mixed null
  37. */
  38. public $uses = null;
  39. }
  40. /**
  41. * Contact class
  42. *
  43. * @package Cake.Test.Case.View.Helper
  44. */
  45. class Contact extends CakeTestModel {
  46. /**
  47. * useTable property
  48. *
  49. * @var boolean
  50. */
  51. public $useTable = false;
  52. /**
  53. * Default schema
  54. *
  55. * @var array
  56. */
  57. protected $_schema = array(
  58. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  59. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  60. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  64. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  65. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  66. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  67. );
  68. /**
  69. * validate property
  70. *
  71. * @var array
  72. */
  73. public $validate = array(
  74. 'non_existing' => array(),
  75. 'idontexist' => array(),
  76. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  77. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  78. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  79. 'imrequiredonboth' => array(
  80. 'required' => array('rule' => 'alphaNumeric'),
  81. ),
  82. 'string_required' => 'notEmpty',
  83. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  84. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  85. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  86. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  87. 'imalsonotrequired' => array(
  88. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  89. 'between' => array('rule' => array('between', 5, 30)),
  90. ),
  91. 'imalsonotrequired2' => array(
  92. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  93. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  94. ),
  95. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  96. 'iamrequiredalways' => array(
  97. 'email' => array('rule' => 'email'),
  98. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  99. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  100. ),
  101. 'boolean_field' => array('rule' => 'boolean')
  102. );
  103. /**
  104. * schema method
  105. *
  106. * @return void
  107. */
  108. public function setSchema($schema) {
  109. $this->_schema = $schema;
  110. }
  111. /**
  112. * hasAndBelongsToMany property
  113. *
  114. * @var array
  115. */
  116. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  117. /**
  118. * hasAndBelongsToMany property
  119. *
  120. * @var array
  121. */
  122. public $belongsTo = array('User' => array('className' => 'UserForm'));
  123. }
  124. /**
  125. * ContactTagsContact class
  126. *
  127. * @package Cake.Test.Case.View.Helper
  128. */
  129. class ContactTagsContact extends CakeTestModel {
  130. /**
  131. * useTable property
  132. *
  133. * @var boolean
  134. */
  135. public $useTable = false;
  136. /**
  137. * Default schema
  138. *
  139. * @var array
  140. */
  141. protected $_schema = array(
  142. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  143. 'contact_tag_id' => array(
  144. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  145. )
  146. );
  147. /**
  148. * schema method
  149. *
  150. * @return void
  151. */
  152. public function setSchema($schema) {
  153. $this->_schema = $schema;
  154. }
  155. }
  156. /**
  157. * ContactNonStandardPk class
  158. *
  159. * @package Cake.Test.Case.View.Helper
  160. */
  161. class ContactNonStandardPk extends Contact {
  162. /**
  163. * primaryKey property
  164. *
  165. * @var string
  166. */
  167. public $primaryKey = 'pk';
  168. /**
  169. * schema method
  170. *
  171. * @return void
  172. */
  173. public function schema($field = false) {
  174. $this->_schema = parent::schema();
  175. $this->_schema['pk'] = $this->_schema['id'];
  176. unset($this->_schema['id']);
  177. return $this->_schema;
  178. }
  179. }
  180. /**
  181. * ContactTag class
  182. *
  183. * @package Cake.Test.Case.View.Helper
  184. */
  185. class ContactTag extends Model {
  186. /**
  187. * useTable property
  188. *
  189. * @var boolean
  190. */
  191. public $useTable = false;
  192. /**
  193. * schema definition
  194. *
  195. * @var array
  196. */
  197. protected $_schema = array(
  198. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  199. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  200. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  201. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  202. );
  203. }
  204. /**
  205. * UserForm class
  206. *
  207. * @package Cake.Test.Case.View.Helper
  208. */
  209. class UserForm extends CakeTestModel {
  210. /**
  211. * useTable property
  212. *
  213. * @var boolean
  214. */
  215. public $useTable = false;
  216. /**
  217. * hasMany property
  218. *
  219. * @var array
  220. */
  221. public $hasMany = array(
  222. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  223. ));
  224. /**
  225. * schema definition
  226. *
  227. * @var array
  228. */
  229. protected $_schema = array(
  230. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  231. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  232. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  233. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  234. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  235. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  236. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  237. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  238. );
  239. }
  240. /**
  241. * OpenidUrl class
  242. *
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class OpenidUrl extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var boolean
  250. */
  251. public $useTable = false;
  252. /**
  253. * belongsTo property
  254. *
  255. * @var array
  256. */
  257. public $belongsTo = array('UserForm' => array(
  258. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  259. ));
  260. /**
  261. * validate property
  262. *
  263. * @var array
  264. */
  265. public $validate = array('openid_not_registered' => array());
  266. /**
  267. * schema method
  268. *
  269. * @var array
  270. */
  271. protected $_schema = array(
  272. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  273. 'user_form_id' => array(
  274. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  275. ),
  276. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  277. );
  278. /**
  279. * beforeValidate method
  280. *
  281. * @return void
  282. */
  283. public function beforeValidate($options = array()) {
  284. $this->invalidate('openid_not_registered');
  285. return true;
  286. }
  287. }
  288. /**
  289. * ValidateUser class
  290. *
  291. * @package Cake.Test.Case.View.Helper
  292. */
  293. class ValidateUser extends CakeTestModel {
  294. /**
  295. * useTable property
  296. *
  297. * @var boolean
  298. */
  299. public $useTable = false;
  300. /**
  301. * hasOne property
  302. *
  303. * @var array
  304. */
  305. public $hasOne = array('ValidateProfile' => array(
  306. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  307. ));
  308. /**
  309. * schema method
  310. *
  311. * @var array
  312. */
  313. protected $_schema = array(
  314. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  315. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  316. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  317. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  318. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  319. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  320. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  321. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  322. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  323. );
  324. /**
  325. * beforeValidate method
  326. *
  327. * @return void
  328. */
  329. public function beforeValidate($options = array()) {
  330. $this->invalidate('email');
  331. return false;
  332. }
  333. }
  334. /**
  335. * ValidateProfile class
  336. *
  337. * @package Cake.Test.Case.View.Helper
  338. */
  339. class ValidateProfile extends CakeTestModel {
  340. /**
  341. * useTable property
  342. *
  343. * @var boolean
  344. */
  345. public $useTable = false;
  346. /**
  347. * schema property
  348. *
  349. * @var array
  350. */
  351. protected $_schema = array(
  352. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  353. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  354. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  355. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  356. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  357. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  358. );
  359. /**
  360. * hasOne property
  361. *
  362. * @var array
  363. */
  364. public $hasOne = array('ValidateItem' => array(
  365. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  366. ));
  367. /**
  368. * belongsTo property
  369. *
  370. * @var array
  371. */
  372. public $belongsTo = array('ValidateUser' => array(
  373. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  374. ));
  375. /**
  376. * beforeValidate method
  377. *
  378. * @return void
  379. */
  380. public function beforeValidate($options = array()) {
  381. $this->invalidate('full_name');
  382. $this->invalidate('city');
  383. return false;
  384. }
  385. }
  386. /**
  387. * ValidateItem class
  388. *
  389. * @package Cake.Test.Case.View.Helper
  390. */
  391. class ValidateItem extends CakeTestModel {
  392. /**
  393. * useTable property
  394. *
  395. * @var boolean
  396. */
  397. public $useTable = false;
  398. /**
  399. * schema property
  400. *
  401. * @var array
  402. */
  403. protected $_schema = array(
  404. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  405. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  406. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  407. 'description' => array(
  408. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  409. ),
  410. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  411. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  412. );
  413. /**
  414. * belongsTo property
  415. *
  416. * @var array
  417. */
  418. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  419. /**
  420. * beforeValidate method
  421. *
  422. * @return void
  423. */
  424. public function beforeValidate($options = array()) {
  425. $this->invalidate('description');
  426. return false;
  427. }
  428. }
  429. /**
  430. * TestMail class
  431. *
  432. * @package Cake.Test.Case.View.Helper
  433. */
  434. class TestMail extends CakeTestModel {
  435. /**
  436. * useTable property
  437. *
  438. * @var boolean
  439. */
  440. public $useTable = false;
  441. }
  442. /**
  443. * FormHelperTest class
  444. *
  445. * @package Cake.Test.Case.View.Helper
  446. * @property FormHelper $Form
  447. */
  448. class FormHelperTest extends CakeTestCase {
  449. /**
  450. * Fixtures to be used
  451. *
  452. * @var array
  453. */
  454. public $fixtures = array('core.post');
  455. /**
  456. * Do not load the fixtures by default
  457. *
  458. * @var boolean
  459. */
  460. public $autoFixtures = false;
  461. /**
  462. * setUp method
  463. *
  464. * @return void
  465. */
  466. public function setUp() {
  467. parent::setUp();
  468. Configure::write('Config.language', 'eng');
  469. Configure::write('App.base', '');
  470. Configure::delete('Asset');
  471. $this->Controller = new ContactTestController();
  472. $this->View = new View($this->Controller);
  473. $this->Form = new FormHelper($this->View);
  474. $this->Form->Html = new HtmlHelper($this->View);
  475. $this->Form->request = new CakeRequest('contacts/add', false);
  476. $this->Form->request->here = '/contacts/add';
  477. $this->Form->request['action'] = 'add';
  478. $this->Form->request->webroot = '';
  479. $this->Form->request->base = '';
  480. ClassRegistry::addObject('Contact', new Contact());
  481. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  482. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  483. ClassRegistry::addObject('User', new UserForm());
  484. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  485. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  486. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  487. $this->oldSalt = Configure::read('Security.salt');
  488. $this->dateRegex = array(
  489. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  490. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  491. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  493. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  494. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  495. );
  496. Configure::write('Security.salt', 'foo!');
  497. }
  498. /**
  499. * tearDown method
  500. *
  501. * @return void
  502. */
  503. public function tearDown() {
  504. parent::tearDown();
  505. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  506. Configure::write('Security.salt', $this->oldSalt);
  507. }
  508. /**
  509. * testFormCreateWithSecurity method
  510. *
  511. * Test form->create() with security key.
  512. *
  513. * @return void
  514. */
  515. public function testCreateWithSecurity() {
  516. $this->Form->request['_Token'] = array('key' => 'testKey');
  517. $encoding = strtolower(Configure::read('App.encoding'));
  518. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  519. $expected = array(
  520. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  521. 'div' => array('style' => 'display:none;'),
  522. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  523. array('input' => array(
  524. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  525. )),
  526. '/div'
  527. );
  528. $this->assertTags($result, $expected);
  529. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  530. $expected['form']['id'] = 'MyForm';
  531. $this->assertTags($result, $expected);
  532. }
  533. /**
  534. * testFormCreateGetNoSecurity method
  535. *
  536. * Test form->create() with no security key as its a get form
  537. *
  538. * @return void
  539. */
  540. public function testCreateEndGetNoSecurity() {
  541. $this->Form->request['_Token'] = array('key' => 'testKey');
  542. $encoding = strtolower(Configure::read('App.encoding'));
  543. $result = $this->Form->create('Contact', array('type' => 'get', 'url' => '/contacts/add'));
  544. $this->assertNotContains('Token', $result);
  545. $result = $this->Form->end('Save');
  546. $this->assertNotContains('Token', $result);
  547. }
  548. /**
  549. * test that create() clears the fields property so it starts fresh
  550. *
  551. * @return void
  552. */
  553. public function testCreateClearingFields() {
  554. $this->Form->fields = array('model_id');
  555. $this->Form->create('Contact');
  556. $this->assertEquals(array(), $this->Form->fields);
  557. }
  558. /**
  559. * Tests form hash generation with model-less data
  560. *
  561. * @return void
  562. */
  563. public function testValidateHashNoModel() {
  564. $this->Form->request['_Token'] = array('key' => 'foo');
  565. $result = $this->Form->secure(array('anything'));
  566. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  567. }
  568. /**
  569. * Tests that models with identical field names get resolved properly
  570. *
  571. * @return void
  572. */
  573. public function testDuplicateFieldNameResolution() {
  574. $result = $this->Form->create('ValidateUser');
  575. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  576. $result = $this->Form->input('ValidateItem.name');
  577. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  578. $result = $this->Form->input('ValidateUser.name');
  579. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  580. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  581. $this->assertRegExp('/type="text"/', $result);
  582. $result = $this->Form->input('ValidateItem.name');
  583. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  584. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  585. $this->assertRegExp('/<textarea/', $result);
  586. $result = $this->Form->input('name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  589. $this->assertRegExp('/type="text"/', $result);
  590. }
  591. /**
  592. * Tests that hidden fields generated for checkboxes don't get locked
  593. *
  594. * @return void
  595. */
  596. public function testNoCheckboxLocking() {
  597. $this->Form->request['_Token'] = array('key' => 'foo');
  598. $this->assertSame(array(), $this->Form->fields);
  599. $this->Form->checkbox('check', array('value' => '1'));
  600. $this->assertSame($this->Form->fields, array('check'));
  601. }
  602. /**
  603. * testFormSecurityFields method
  604. *
  605. * Test generation of secure form hash generation.
  606. *
  607. * @return void
  608. */
  609. public function testFormSecurityFields() {
  610. $key = 'testKey';
  611. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  612. $secureAttributes = array('form' => 'MyTestForm');
  613. $this->Form->request['_Token'] = array('key' => $key);
  614. $result = $this->Form->secure($fields, $secureAttributes);
  615. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  616. $hash .= ':' . 'Model.valid';
  617. $hash = urlencode($hash);
  618. $expected = array(
  619. 'div' => array('style' => 'display:none;'),
  620. array('input' => array(
  621. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  622. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/',
  623. 'form' => 'MyTestForm',
  624. )),
  625. array('input' => array(
  626. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  627. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/',
  628. 'form' => 'MyTestForm',
  629. )),
  630. '/div'
  631. );
  632. $this->assertTags($result, $expected);
  633. }
  634. /**
  635. * Tests correct generation of number fields for double and float fields
  636. *
  637. * @return void
  638. */
  639. public function testTextFieldGenerationForFloats() {
  640. $model = ClassRegistry::getObject('Contact');
  641. $model->setSchema(array('foo' => array(
  642. 'type' => 'float',
  643. 'null' => false,
  644. 'default' => null,
  645. 'length' => 10
  646. )));
  647. $this->Form->create('Contact');
  648. $result = $this->Form->input('foo');
  649. $expected = array(
  650. 'div' => array('class' => 'input number'),
  651. 'label' => array('for' => 'ContactFoo'),
  652. 'Foo',
  653. '/label',
  654. array('input' => array(
  655. 'type' => 'number',
  656. 'name' => 'data[Contact][foo]',
  657. 'id' => 'ContactFoo',
  658. 'step' => 'any'
  659. )),
  660. '/div'
  661. );
  662. $this->assertTags($result, $expected);
  663. $result = $this->Form->input('foo', array('step' => 0.5));
  664. $expected = array(
  665. 'div' => array('class' => 'input number'),
  666. 'label' => array('for' => 'ContactFoo'),
  667. 'Foo',
  668. '/label',
  669. array('input' => array(
  670. 'type' => 'number',
  671. 'name' => 'data[Contact][foo]',
  672. 'id' => 'ContactFoo',
  673. 'step' => '0.5'
  674. )),
  675. '/div'
  676. );
  677. $this->assertTags($result, $expected);
  678. }
  679. /**
  680. * Tests correct generation of number fields for integer fields
  681. *
  682. * @return void
  683. */
  684. public function testTextFieldTypeNumberGenerationForIntegers() {
  685. $model = ClassRegistry::getObject('Contact');
  686. $model->setSchema(array('foo' => array(
  687. 'type' => 'integer',
  688. 'null' => false,
  689. 'default' => null,
  690. 'length' => null
  691. )));
  692. $this->Form->create('Contact');
  693. $result = $this->Form->input('foo');
  694. $expected = array(
  695. 'div' => array('class' => 'input number'),
  696. 'label' => array('for' => 'ContactFoo'),
  697. 'Foo',
  698. '/label',
  699. array('input' => array(
  700. 'type' => 'number', 'name' => 'data[Contact][foo]',
  701. 'id' => 'ContactFoo'
  702. )),
  703. '/div'
  704. );
  705. $this->assertTags($result, $expected);
  706. }
  707. /**
  708. * Tests correct generation of file upload fields for binary fields
  709. *
  710. * @return void
  711. */
  712. public function testFileUploadFieldTypeGenerationForBinaries() {
  713. $model = ClassRegistry::getObject('Contact');
  714. $model->setSchema(array('foo' => array(
  715. 'type' => 'binary',
  716. 'null' => false,
  717. 'default' => null,
  718. 'length' => 1024
  719. )));
  720. $this->Form->create('Contact');
  721. $result = $this->Form->input('foo');
  722. $expected = array(
  723. 'div' => array('class' => 'input file'),
  724. 'label' => array('for' => 'ContactFoo'),
  725. 'Foo',
  726. '/label',
  727. array('input' => array(
  728. 'type' => 'file', 'name' => 'data[Contact][foo]',
  729. 'id' => 'ContactFoo'
  730. )),
  731. '/div'
  732. );
  733. $this->assertTags($result, $expected);
  734. }
  735. /**
  736. * testFormSecurityMultipleFields method
  737. *
  738. * Test secure() with multiple row form. Ensure hash is correct.
  739. *
  740. * @return void
  741. */
  742. public function testFormSecurityMultipleFields() {
  743. $key = 'testKey';
  744. $fields = array(
  745. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  746. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  747. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  748. );
  749. $this->Form->request['_Token'] = array('key' => $key);
  750. $result = $this->Form->secure($fields);
  751. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  752. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  753. $expected = array(
  754. 'div' => array('style' => 'display:none;'),
  755. array('input' => array(
  756. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  757. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  758. )),
  759. array('input' => array(
  760. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  761. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  762. )),
  763. '/div'
  764. );
  765. $this->assertTags($result, $expected);
  766. }
  767. /**
  768. * testFormSecurityMultipleSubmitButtons
  769. *
  770. * test form submit generation and ensure that _Token is only created on end()
  771. *
  772. * @return void
  773. */
  774. public function testFormSecurityMultipleSubmitButtons() {
  775. $key = 'testKey';
  776. $this->Form->request['_Token'] = array('key' => $key);
  777. $this->Form->create('Addresses');
  778. $this->Form->input('Address.title');
  779. $this->Form->input('Address.first_name');
  780. $result = $this->Form->submit('Save', array('name' => 'save'));
  781. $expected = array(
  782. 'div' => array('class' => 'submit'),
  783. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  784. '/div',
  785. );
  786. $this->assertTags($result, $expected);
  787. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  788. $expected = array(
  789. 'div' => array('class' => 'submit'),
  790. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  791. '/div',
  792. );
  793. $this->assertTags($result, $expected);
  794. $result = $this->Form->end(null);
  795. $expected = array(
  796. 'div' => array('style' => 'display:none;'),
  797. array('input' => array(
  798. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  799. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  800. )),
  801. array('input' => array(
  802. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  803. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  804. )),
  805. '/div'
  806. );
  807. $this->assertTags($result, $expected);
  808. }
  809. /**
  810. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  811. *
  812. * @return void
  813. */
  814. public function testSecurityButtonNestedNamed() {
  815. $key = 'testKey';
  816. $this->Form->request['_Token'] = array('key' => $key);
  817. $this->Form->create('Addresses');
  818. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  819. $result = $this->Form->unlockField();
  820. $this->assertEquals(array('Address.button'), $result);
  821. }
  822. /**
  823. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  824. *
  825. * @return void
  826. */
  827. public function testSecuritySubmitNestedNamed() {
  828. $key = 'testKey';
  829. $this->Form->request['_Token'] = array('key' => $key);
  830. $this->Form->create('Addresses');
  831. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  832. $result = $this->Form->unlockField();
  833. $this->assertEquals(array('Address.button'), $result);
  834. }
  835. /**
  836. * Test that the correct fields are unlocked for image submits with no names.
  837. *
  838. * @return void
  839. */
  840. public function testSecuritySubmitImageNoName() {
  841. $key = 'testKey';
  842. $this->Form->request['_Token'] = array('key' => $key);
  843. $this->Form->create('User');
  844. $result = $this->Form->submit('save.png');
  845. $expected = array(
  846. 'div' => array('class' => 'submit'),
  847. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  848. '/div'
  849. );
  850. $this->assertTags($result, $expected);
  851. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  852. }
  853. /**
  854. * Test that the correct fields are unlocked for image submits with names.
  855. *
  856. * @return void
  857. */
  858. public function testSecuritySubmitImageName() {
  859. $key = 'testKey';
  860. $this->Form->request['_Token'] = array('key' => $key);
  861. $this->Form->create('User');
  862. $result = $this->Form->submit('save.png', array('name' => 'test'));
  863. $expected = array(
  864. 'div' => array('class' => 'submit'),
  865. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  866. '/div'
  867. );
  868. $this->assertTags($result, $expected);
  869. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  870. }
  871. /**
  872. * testFormSecurityMultipleInputFields method
  873. *
  874. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  875. *
  876. * @return void
  877. */
  878. public function testFormSecurityMultipleInputFields() {
  879. $key = 'testKey';
  880. $this->Form->request['_Token'] = array('key' => $key);
  881. $this->Form->create('Addresses');
  882. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  883. $this->Form->input('Addresses.0.title');
  884. $this->Form->input('Addresses.0.first_name');
  885. $this->Form->input('Addresses.0.last_name');
  886. $this->Form->input('Addresses.0.address');
  887. $this->Form->input('Addresses.0.city');
  888. $this->Form->input('Addresses.0.phone');
  889. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  890. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  891. $this->Form->input('Addresses.1.title');
  892. $this->Form->input('Addresses.1.first_name');
  893. $this->Form->input('Addresses.1.last_name');
  894. $this->Form->input('Addresses.1.address');
  895. $this->Form->input('Addresses.1.city');
  896. $this->Form->input('Addresses.1.phone');
  897. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  898. $result = $this->Form->secure($this->Form->fields);
  899. $hash = 'a3b9b2ba1cb688838f92818a5970e17dd7943a78%3AAddresses.0.id%7CAddresses.1.id';
  900. $expected = array(
  901. 'div' => array('style' => 'display:none;'),
  902. array('input' => array(
  903. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  904. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  905. )),
  906. array('input' => array(
  907. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  908. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  909. )),
  910. '/div'
  911. );
  912. $this->assertTags($result, $expected);
  913. }
  914. /**
  915. * Test form security with Model.field.0 style inputs
  916. *
  917. * @return void
  918. */
  919. public function testFormSecurityArrayFields() {
  920. $key = 'testKey';
  921. $this->Form->request->params['_Token']['key'] = $key;
  922. $this->Form->create('Address');
  923. $this->Form->input('Address.primary.1');
  924. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  925. $this->Form->input('Address.secondary.1.0');
  926. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  927. }
  928. /**
  929. * testFormSecurityMultipleInputDisabledFields method
  930. *
  931. * test secure form generation with multiple records and disabled fields.
  932. *
  933. * @return void
  934. */
  935. public function testFormSecurityMultipleInputDisabledFields() {
  936. $key = 'testKey';
  937. $this->Form->request->params['_Token'] = array(
  938. 'key' => $key,
  939. 'unlockedFields' => array('first_name', 'address')
  940. );
  941. $this->Form->create();
  942. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  943. $this->Form->input('Addresses.0.title');
  944. $this->Form->input('Addresses.0.first_name');
  945. $this->Form->input('Addresses.0.last_name');
  946. $this->Form->input('Addresses.0.address');
  947. $this->Form->input('Addresses.0.city');
  948. $this->Form->input('Addresses.0.phone');
  949. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  950. $this->Form->input('Addresses.1.title');
  951. $this->Form->input('Addresses.1.first_name');
  952. $this->Form->input('Addresses.1.last_name');
  953. $this->Form->input('Addresses.1.address');
  954. $this->Form->input('Addresses.1.city');
  955. $this->Form->input('Addresses.1.phone');
  956. $result = $this->Form->secure($this->Form->fields);
  957. $hash = '5c9cadf9da008cc444d3960b481391a425a5d979%3AAddresses.0.id%7CAddresses.1.id';
  958. $expected = array(
  959. 'div' => array('style' => 'display:none;'),
  960. array('input' => array(
  961. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  962. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  963. )),
  964. array('input' => array(
  965. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  966. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  967. )),
  968. '/div'
  969. );
  970. $this->assertTags($result, $expected);
  971. }
  972. /**
  973. * testFormSecurityInputDisabledFields method
  974. *
  975. * Test single record form with disabled fields.
  976. *
  977. * @return void
  978. */
  979. public function testFormSecurityInputUnlockedFields() {
  980. $key = 'testKey';
  981. $this->Form->request['_Token'] = array(
  982. 'key' => $key,
  983. 'unlockedFields' => array('first_name', 'address')
  984. );
  985. $this->Form->create();
  986. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  987. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  988. $this->Form->input('Addresses.title');
  989. $this->Form->input('Addresses.first_name');
  990. $this->Form->input('Addresses.last_name');
  991. $this->Form->input('Addresses.address');
  992. $this->Form->input('Addresses.city');
  993. $this->Form->input('Addresses.phone');
  994. $result = $this->Form->fields;
  995. $expected = array(
  996. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  997. 'Addresses.city', 'Addresses.phone'
  998. );
  999. $this->assertEquals($expected, $result);
  1000. $result = $this->Form->secure($expected);
  1001. $hash = '40289bd07811587887ff56585a8526ff9da59d7a%3AAddresses.id';
  1002. $expected = array(
  1003. 'div' => array('style' => 'display:none;'),
  1004. array('input' => array(
  1005. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1006. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1007. )),
  1008. array('input' => array(
  1009. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1010. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1011. )),
  1012. '/div'
  1013. );
  1014. $this->assertTags($result, $expected);
  1015. }
  1016. /**
  1017. * test securing inputs with custom name attributes.
  1018. *
  1019. * @return void
  1020. */
  1021. public function testFormSecureWithCustomNameAttribute() {
  1022. $this->Form->request->params['_Token']['key'] = 'testKey';
  1023. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1024. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1025. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1026. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1027. }
  1028. /**
  1029. * testFormSecuredInput method
  1030. *
  1031. * Test generation of entire secure form, assertions made on input() output.
  1032. *
  1033. * @return void
  1034. */
  1035. public function testFormSecuredInput() {
  1036. $this->Form->request['_Token'] = array('key' => 'testKey');
  1037. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1038. $encoding = strtolower(Configure::read('App.encoding'));
  1039. $expected = array(
  1040. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1041. 'div' => array('style' => 'display:none;'),
  1042. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1043. array('input' => array(
  1044. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1045. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1046. )),
  1047. '/div'
  1048. );
  1049. $this->assertTags($result, $expected);
  1050. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1051. $expected = array(
  1052. 'div' => array('class' => 'input text'),
  1053. 'label' => array('for' => 'UserFormPublished'),
  1054. 'Published',
  1055. '/label',
  1056. array('input' => array(
  1057. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1058. 'id' => 'UserFormPublished'
  1059. )),
  1060. '/div'
  1061. );
  1062. $this->assertTags($result, $expected);
  1063. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1064. $expected = array(
  1065. 'div' => array('class' => 'input text'),
  1066. 'label' => array('for' => 'UserFormOther'),
  1067. 'Other',
  1068. '/label',
  1069. array('input' => array(
  1070. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1071. 'id' => 'UserFormOther'
  1072. )),
  1073. '/div'
  1074. );
  1075. $this->assertTags($result, $expected);
  1076. $result = $this->Form->hidden('UserForm.stuff');
  1077. $expected = array(
  1078. 'input' => array(
  1079. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1080. 'id' => 'UserFormStuff'
  1081. ));
  1082. $this->assertTags($result, $expected);
  1083. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1084. $expected = array('input' => array(
  1085. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1086. 'value' => '0', 'id' => 'UserFormHidden'
  1087. ));
  1088. $this->assertTags($result, $expected);
  1089. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1090. $expected = array(
  1091. 'div' => array('class' => 'input checkbox'),
  1092. array('input' => array(
  1093. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1094. 'value' => '0', 'id' => 'UserFormSomething_'
  1095. )),
  1096. array('input' => array(
  1097. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1098. 'value' => '1', 'id' => 'UserFormSomething'
  1099. )),
  1100. 'label' => array('for' => 'UserFormSomething'),
  1101. 'Something',
  1102. '/label',
  1103. '/div'
  1104. );
  1105. $this->assertTags($result, $expected);
  1106. $result = $this->Form->fields;
  1107. $expected = array(
  1108. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1109. 'UserForm.hidden' => '0', 'UserForm.something'
  1110. );
  1111. $this->assertEquals($expected, $result);
  1112. $hash = '6014b4e1c4f39eb62389712111dbe6435bec66cb%3AUserForm.hidden%7CUserForm.stuff';
  1113. $result = $this->Form->secure($this->Form->fields);
  1114. $expected = array(
  1115. 'div' => array('style' => 'display:none;'),
  1116. array('input' => array(
  1117. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1118. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1119. )),
  1120. array('input' => array(
  1121. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1122. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertTags($result, $expected);
  1127. }
  1128. /**
  1129. * Test secured inputs with custom names.
  1130. *
  1131. * @return void
  1132. */
  1133. public function testSecuredInputCustomName() {
  1134. $this->Form->request['_Token'] = array('key' => 'testKey');
  1135. $this->assertEquals(array(), $this->Form->fields);
  1136. $this->Form->input('text_input', array(
  1137. 'name' => 'data[Option][General.default_role]',
  1138. ));
  1139. $expected = array('Option.General.default_role');
  1140. $this->assertEquals($expected, $this->Form->fields);
  1141. $this->Form->input('select_box', array(
  1142. 'name' => 'data[Option][General.select_role]',
  1143. 'type' => 'select',
  1144. 'options' => array(1, 2),
  1145. ));
  1146. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. }
  1149. /**
  1150. * Tests that the correct keys are added to the field hash index
  1151. *
  1152. * @return void
  1153. */
  1154. public function testFormSecuredFileInput() {
  1155. $this->Form->request['_Token'] = array('key' => 'testKey');
  1156. $this->assertEquals(array(), $this->Form->fields);
  1157. $this->Form->file('Attachment.file');
  1158. $expected = array(
  1159. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1160. 'Attachment.file.error', 'Attachment.file.size'
  1161. );
  1162. $this->assertEquals($expected, $this->Form->fields);
  1163. }
  1164. /**
  1165. * test that multiple selects keys are added to field hash
  1166. *
  1167. * @return void
  1168. */
  1169. public function testFormSecuredMultipleSelect() {
  1170. $this->Form->request['_Token'] = array('key' => 'testKey');
  1171. $this->assertEquals(array(), $this->Form->fields);
  1172. $options = array('1' => 'one', '2' => 'two');
  1173. $this->Form->select('Model.select', $options);
  1174. $expected = array('Model.select');
  1175. $this->assertEquals($expected, $this->Form->fields);
  1176. $this->Form->fields = array();
  1177. $this->Form->select('Model.select', $options, array('multiple' => true));
  1178. $this->assertEquals($expected, $this->Form->fields);
  1179. }
  1180. /**
  1181. * testFormSecuredRadio method
  1182. *
  1183. * @return void
  1184. */
  1185. public function testFormSecuredRadio() {
  1186. $this->Form->request['_Token'] = array('key' => 'testKey');
  1187. $this->assertEquals(array(), $this->Form->fields);
  1188. $options = array('1' => 'option1', '2' => 'option2');
  1189. $this->Form->radio('Test.test', $options);
  1190. $expected = array('Test.test');
  1191. $this->assertEquals($expected, $this->Form->fields);
  1192. }
  1193. /**
  1194. * Test that when disabled is in a list based attribute array it works.
  1195. *
  1196. * @return void
  1197. */
  1198. public function testFormSecuredAndDisabledNotAssoc() {
  1199. $this->Form->request['_Token'] = array('key' => 'testKey');
  1200. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1201. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1202. $this->Form->text('Model.text', array('disabled'));
  1203. $this->Form->textarea('Model.textarea', array('disabled'));
  1204. $this->Form->password('Model.password', array('disabled'));
  1205. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1206. $expected = array(
  1207. 'Model.radio' => ''
  1208. );
  1209. $this->assertEquals($expected, $this->Form->fields);
  1210. }
  1211. /**
  1212. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1213. * hashing.
  1214. *
  1215. * @return void
  1216. */
  1217. public function testFormSecuredAndDisabled() {
  1218. $this->Form->request['_Token'] = array('key' => 'testKey');
  1219. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1220. $this->Form->text('Model.text', array('disabled' => true));
  1221. $this->Form->password('Model.text', array('disabled' => true));
  1222. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1223. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1224. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1225. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1226. $this->Form->month('Model.month', array('disabled' => true));
  1227. $this->Form->day('Model.day', array('disabled' => true));
  1228. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1229. $this->Form->minute('Model.minute', array('disabled' => true));
  1230. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1231. $expected = array(
  1232. 'Model.radio' => ''
  1233. );
  1234. $this->assertEquals($expected, $this->Form->fields);
  1235. }
  1236. /**
  1237. * testDisableSecurityUsingForm method
  1238. *
  1239. * @return void
  1240. */
  1241. public function testDisableSecurityUsingForm() {
  1242. $this->Form->request['_Token'] = array(
  1243. 'key' => 'testKey',
  1244. 'disabledFields' => array()
  1245. );
  1246. $this->Form->create();
  1247. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1248. $this->Form->input('Addresses.title');
  1249. $this->Form->input('Addresses.first_name', array('secure' => false));
  1250. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1251. $this->Form->input('Addresses.zip', array(
  1252. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1253. ));
  1254. $result = $this->Form->fields;
  1255. $expected = array(
  1256. 'Addresses.id' => '123456', 'Addresses.title',
  1257. );
  1258. $this->assertEquals($expected, $result);
  1259. }
  1260. /**
  1261. * test disableField
  1262. *
  1263. * @return void
  1264. */
  1265. public function testUnlockFieldAddsToList() {
  1266. $this->Form->request['_Token'] = array(
  1267. 'key' => 'testKey',
  1268. 'unlockedFields' => array()
  1269. );
  1270. $this->Form->create('Contact');
  1271. $this->Form->unlockField('Contact.name');
  1272. $this->Form->text('Contact.name');
  1273. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1274. $this->assertEquals(array(), $this->Form->fields);
  1275. }
  1276. /**
  1277. * test unlockField removing from fields array.
  1278. *
  1279. * @return void
  1280. */
  1281. public function testUnlockFieldRemovingFromFields() {
  1282. $this->Form->request['_Token'] = array(
  1283. 'key' => 'testKey',
  1284. 'unlockedFields' => array()
  1285. );
  1286. $this->Form->create('Contact');
  1287. $this->Form->hidden('Contact.id', array('value' => 1));
  1288. $this->Form->text('Contact.name');
  1289. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1290. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1291. $this->Form->unlockField('Contact.name');
  1292. $this->Form->unlockField('Contact.id');
  1293. $this->assertEquals(array(), $this->Form->fields);
  1294. }
  1295. /**
  1296. * testTagIsInvalid method
  1297. *
  1298. * @return void
  1299. */
  1300. public function testTagIsInvalid() {
  1301. $Contact = ClassRegistry::getObject('Contact');
  1302. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1303. $this->Form->setEntity('Contact.0.email');
  1304. $result = $this->Form->tagIsInvalid();
  1305. $this->assertEquals($expected, $result);
  1306. $this->Form->setEntity('Contact.1.email');
  1307. $result = $this->Form->tagIsInvalid();
  1308. $this->assertFalse($result);
  1309. $this->Form->setEntity('Contact.0.name');
  1310. $result = $this->Form->tagIsInvalid();
  1311. $this->assertFalse($result);
  1312. }
  1313. /**
  1314. * Test tagIsInvalid with validation errors from a saveMany
  1315. *
  1316. * @return void
  1317. */
  1318. public function testTagIsInvalidSaveMany() {
  1319. $Contact = ClassRegistry::getObject('Contact');
  1320. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1321. $this->Form->create('Contact');
  1322. $this->Form->setEntity('0.email');
  1323. $result = $this->Form->tagIsInvalid();
  1324. $this->assertEquals($expected, $result);
  1325. $this->Form->setEntity('0.Contact.email');
  1326. $result = $this->Form->tagIsInvalid();
  1327. $this->assertEquals($expected, $result);
  1328. }
  1329. /**
  1330. * Test validation errors.
  1331. *
  1332. * @return void
  1333. */
  1334. public function testPasswordValidation() {
  1335. $Contact = ClassRegistry::getObject('Contact');
  1336. $Contact->validationErrors['password'] = array('Please provide a password');
  1337. $result = $this->Form->input('Contact.password');
  1338. $expected = array(
  1339. 'div' => array('class' => 'input password error'),
  1340. 'label' => array('for' => 'ContactPassword'),
  1341. 'Password',
  1342. '/label',
  1343. 'input' => array(
  1344. 'type' => 'password', 'name' => 'data[Contact][password]',
  1345. 'id' => 'ContactPassword', 'class' => 'form-error'
  1346. ),
  1347. array('div' => array('class' => 'error-message')),
  1348. 'Please provide a password',
  1349. '/div',
  1350. '/div'
  1351. );
  1352. $this->assertTags($result, $expected);
  1353. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1354. $expected = array(
  1355. 'div' => array('class' => 'input password error'),
  1356. 'label' => array('for' => 'ContactPassword'),
  1357. 'Password',
  1358. '/label',
  1359. 'input' => array(
  1360. 'type' => 'password', 'name' => 'data[Contact][password]',
  1361. 'id' => 'ContactPassword', 'class' => 'form-error'
  1362. ),
  1363. '/div'
  1364. );
  1365. $this->assertTags($result, $expected);
  1366. }
  1367. /**
  1368. * Test validation errors, when validation message is an empty string.
  1369. *
  1370. * @return void
  1371. */
  1372. public function testEmptyErrorValidation() {
  1373. $this->Form->validationErrors['Contact']['password'] = '';
  1374. $result = $this->Form->input('Contact.password');
  1375. $expected = array(
  1376. 'div' => array('class' => 'input password error'),
  1377. 'label' => array('for' => 'ContactPassword'),
  1378. 'Password',
  1379. '/label',
  1380. 'input' => array(
  1381. 'type' => 'password', 'name' => 'data[Contact][password]',
  1382. 'id' => 'ContactPassword', 'class' => 'form-error'
  1383. ),
  1384. array('div' => array('class' => 'error-message')),
  1385. array(),
  1386. '/div',
  1387. '/div'
  1388. );
  1389. $this->assertTags($result, $expected);
  1390. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1391. $expected = array(
  1392. 'div' => array('class' => 'input password error'),
  1393. 'label' => array('for' => 'ContactPassword'),
  1394. 'Password',
  1395. '/label',
  1396. 'input' => array(
  1397. 'type' => 'password', 'name' => 'data[Contact][password]',
  1398. 'id' => 'ContactPassword', 'class' => 'form-error'
  1399. ),
  1400. '/div'
  1401. );
  1402. $this->assertTags($result, $expected);
  1403. }
  1404. /**
  1405. * Test validation errors, when calling input() overriding validation message by an empty string.
  1406. *
  1407. * @return void
  1408. */
  1409. public function testEmptyInputErrorValidation() {
  1410. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1411. $result = $this->Form->input('Contact.password', array('error' => ''));
  1412. $expected = array(
  1413. 'div' => array('class' => 'input password error'),
  1414. 'label' => array('for' => 'ContactPassword'),
  1415. 'Password',
  1416. '/label',
  1417. 'input' => array(
  1418. 'type' => 'password', 'name' => 'data[Contact][password]',
  1419. 'id' => 'ContactPassword', 'class' => 'form-error'
  1420. ),
  1421. array('div' => array('class' => 'error-message')),
  1422. array(),
  1423. '/div',
  1424. '/div'
  1425. );
  1426. $this->assertTags($result, $expected);
  1427. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1428. $expected = array(
  1429. 'div' => array('class' => 'input password error'),
  1430. 'label' => array('for' => 'ContactPassword'),
  1431. 'Password',
  1432. '/label',
  1433. 'input' => array(
  1434. 'type' => 'password', 'name' => 'data[Contact][password]',
  1435. 'id' => 'ContactPassword', 'class' => 'form-error'
  1436. ),
  1437. '/div'
  1438. );
  1439. $this->assertTags($result, $expected);
  1440. }
  1441. /**
  1442. * testFormValidationAssociated method
  1443. *
  1444. * test display of form errors in conjunction with model::validates.
  1445. *
  1446. * @return void
  1447. */
  1448. public function testFormValidationAssociated() {
  1449. $this->UserForm = ClassRegistry::getObject('UserForm');
  1450. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1451. $data = array(
  1452. 'UserForm' => array('name' => 'user'),
  1453. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1454. );
  1455. $result = $this->UserForm->OpenidUrl->create($data);
  1456. $this->assertFalse(empty($result));
  1457. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1458. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1459. $encoding = strtolower(Configure::read('App.encoding'));
  1460. $expected = array(
  1461. 'form' => array(
  1462. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1463. 'accept-charset' => $encoding
  1464. ),
  1465. 'div' => array('style' => 'display:none;'),
  1466. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1467. '/div'
  1468. );
  1469. $this->assertTags($result, $expected);
  1470. $result = $this->Form->error(
  1471. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1472. );
  1473. $this->assertEquals('Error, not registered', $result);
  1474. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1475. }
  1476. /**
  1477. * testFormValidationAssociatedFirstLevel method
  1478. *
  1479. * test form error display with associated model.
  1480. *
  1481. * @return void
  1482. */
  1483. public function testFormValidationAssociatedFirstLevel() {
  1484. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1485. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1486. $data = array(
  1487. 'ValidateUser' => array('name' => 'mariano'),
  1488. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1489. );
  1490. $result = $this->ValidateUser->create($data);
  1491. $this->assertFalse(empty($result));
  1492. $this->assertFalse($this->ValidateUser->validates());
  1493. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1494. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1495. $encoding = strtolower(Configure::read('App.encoding'));
  1496. $expected = array(
  1497. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1498. 'div' => array('style' => 'display:none;'),
  1499. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1500. '/div'
  1501. );
  1502. $this->assertTags($result, $expected);
  1503. $result = $this->Form->error(
  1504. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1505. );
  1506. $this->assertEquals('Invalid email', $result);
  1507. $result = $this->Form->error(
  1508. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1509. );
  1510. $this->assertEquals('Invalid name', $result);
  1511. $result = $this->Form->error(
  1512. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1513. );
  1514. $this->assertEquals('Invalid city', $result);
  1515. unset($this->ValidateUser->ValidateProfile);
  1516. unset($this->ValidateUser);
  1517. }
  1518. /**
  1519. * testFormValidationAssociatedSecondLevel method
  1520. *
  1521. * test form error display with associated model.
  1522. *
  1523. * @return void
  1524. */
  1525. public function testFormValidationAssociatedSecondLevel() {
  1526. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1527. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1528. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1529. $data = array(
  1530. 'ValidateUser' => array('name' => 'mariano'),
  1531. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1532. 'ValidateItem' => array('name' => 'Item')
  1533. );
  1534. $result = $this->ValidateUser->create($data);
  1535. $this->assertFalse(empty($result));
  1536. $this->assertFalse($this->ValidateUser->validates());
  1537. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1538. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1539. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1540. $encoding = strtolower(Configure::read('App.encoding'));
  1541. $expected = array(
  1542. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1543. 'div' => array('style' => 'display:none;'),
  1544. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1545. '/div'
  1546. );
  1547. $this->assertTags($result, $expected);
  1548. $result = $this->Form->error(
  1549. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1550. );
  1551. $this->assertEquals('Invalid email', $result);
  1552. $result = $this->Form->error(
  1553. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1554. );
  1555. $this->assertEquals('Invalid name', $result);
  1556. $result = $this->Form->error(
  1557. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1558. );
  1559. $result = $this->Form->error(
  1560. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1561. );
  1562. $this->assertEquals('Invalid description', $result);
  1563. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1564. unset($this->ValidateUser->ValidateProfile);
  1565. unset($this->ValidateUser);
  1566. }
  1567. /**
  1568. * testFormValidationMultiRecord method
  1569. *
  1570. * test form error display with multiple records.
  1571. *
  1572. * @return void
  1573. */
  1574. public function testFormValidationMultiRecord() {
  1575. $Contact = ClassRegistry::getObject('Contact');
  1576. $Contact->validationErrors[2] = array(
  1577. 'name' => array('This field cannot be left blank')
  1578. );
  1579. $result = $this->Form->input('Contact.2.name');
  1580. $expected = array(
  1581. 'div' => array('class' => 'input text error'),
  1582. 'label' => array('for' => 'Contact2Name'),
  1583. 'Name',
  1584. '/label',
  1585. 'input' => array(
  1586. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1587. 'class' => 'form-error', 'maxlength' => 255
  1588. ),
  1589. array('div' => array('class' => 'error-message')),
  1590. 'This field cannot be left blank',
  1591. '/div',
  1592. '/div'
  1593. );
  1594. $this->assertTags($result, $expected);
  1595. }
  1596. /**
  1597. * testMultipleInputValidation method
  1598. *
  1599. * test multiple record form validation error display.
  1600. *
  1601. * @return void
  1602. */
  1603. public function testMultipleInputValidation() {
  1604. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1605. $Address->validationErrors[0] = array(
  1606. 'title' => array('This field cannot be empty'),
  1607. 'first_name' => array('This field cannot be empty')
  1608. );
  1609. $Address->validationErrors[1] = array(
  1610. 'last_name' => array('You must have a last name')
  1611. );
  1612. $this->Form->create();
  1613. $result = $this->Form->input('Address.0.title');
  1614. $expected = array(
  1615. 'div' => array('class'),
  1616. 'label' => array('for'),
  1617. 'preg:/[^<]+/',
  1618. '/label',
  1619. 'input' => array(
  1620. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1621. ),
  1622. array('div' => array('class' => 'error-message')),
  1623. 'This field cannot be empty',
  1624. '/div',
  1625. '/div'
  1626. );
  1627. $this->assertTags($result, $expected);
  1628. $result = $this->Form->input('Address.0.first_name');
  1629. $expected = array(
  1630. 'div' => array('class'),
  1631. 'label' => array('for'),
  1632. 'preg:/[^<]+/',
  1633. '/label',
  1634. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1635. array('div' => array('class' => 'error-message')),
  1636. 'This field cannot be empty',
  1637. '/div',
  1638. '/div'
  1639. );
  1640. $this->assertTags($result, $expected);
  1641. $result = $this->Form->input('Address.0.last_name');
  1642. $expected = array(
  1643. 'div' => array('class'),
  1644. 'label' => array('for'),
  1645. 'preg:/[^<]+/',
  1646. '/label',
  1647. 'input' => array('type' => 'text', 'name', 'id'),
  1648. '/div'
  1649. );
  1650. $this->assertTags($result, $expected);
  1651. $result = $this->Form->input('Address.1.last_name');
  1652. $expected = array(
  1653. 'div' => array('class'),
  1654. 'label' => array('for'),
  1655. 'preg:/[^<]+/',
  1656. '/label',
  1657. 'input' => array(
  1658. 'type' => 'text', 'name', 'id',
  1659. 'class' => 'form-error'
  1660. ),
  1661. array('div' => array('class' => 'error-message')),
  1662. 'You must have a last name',
  1663. '/div',
  1664. '/div'
  1665. );
  1666. $this->assertTags($result, $expected);
  1667. }
  1668. /**
  1669. * testInput method
  1670. *
  1671. * Test various incarnations of input().
  1672. *
  1673. * @return void
  1674. */
  1675. public function testInput() {
  1676. $result = $this->Form->input('ValidateUser.balance');
  1677. $expected = array(
  1678. 'div' => array('class'),
  1679. 'label' => array('for'),
  1680. 'Balance',
  1681. '/label',
  1682. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1683. '/div',
  1684. );
  1685. $this->assertTags($result, $expected);
  1686. $result = $this->Form->input('ValidateUser.cost_decimal');
  1687. $expected = array(
  1688. 'div' => array('class'),
  1689. 'label' => array('for'),
  1690. 'Cost Decimal',
  1691. '/label',
  1692. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1693. '/div',
  1694. );
  1695. $this->assertTags($result, $expected);
  1696. $result = $this->Form->input('ValidateUser.ratio');
  1697. $expected = array(
  1698. 'div' => array('class'),
  1699. 'label' => array('for'),
  1700. 'Ratio',
  1701. '/label',
  1702. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1703. '/div',
  1704. );
  1705. $this->assertTags($result, $expected);
  1706. $result = $this->Form->input('ValidateUser.population');
  1707. $expected = array(
  1708. 'div' => array('class'),
  1709. 'label' => array('for'),
  1710. 'Population',
  1711. '/label',
  1712. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1713. '/div',
  1714. );
  1715. $this->assertTags($result, $expected);
  1716. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1717. $expected = array(
  1718. 'div' => array('class' => 'input email'),
  1719. 'label' => array('for' => 'custom'),
  1720. 'Email',
  1721. '/label',
  1722. array('input' => array(
  1723. 'type' => 'email', 'name' => 'data[Contact][email]',
  1724. 'id' => 'custom', 'maxlength' => 255
  1725. )),
  1726. '/div'
  1727. );
  1728. $this->assertTags($result, $expected);
  1729. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1730. $expected = array(
  1731. '<div',
  1732. 'label' => array('for' => 'ContactEmail'),
  1733. 'Email',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'email', 'name' => 'data[Contact][email]',
  1737. 'id' => 'ContactEmail', 'maxlength' => 255
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $result = $this->Form->hidden('Contact.idontexist');
  1743. $expected = array('input' => array(
  1744. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1745. 'id' => 'ContactIdontexist'
  1746. ));
  1747. $this->assertTags($result, $expected);
  1748. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1749. $expected = array(
  1750. 'div' => array('class' => 'input text'),
  1751. 'label' => array('for' => 'ContactEmail'),
  1752. 'Email',
  1753. '/label',
  1754. array('input' => array(
  1755. 'type' => 'text', 'name' => 'data[Contact][email]',
  1756. 'id' => 'ContactEmail'
  1757. )),
  1758. '/div'
  1759. );
  1760. $this->assertTags($result, $expected);
  1761. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1762. $expected = array(
  1763. 'div' => array('class' => 'input text'),
  1764. 'label' => array('for' => 'Contact5Email'),
  1765. 'Email',
  1766. '/label',
  1767. array('input' => array(
  1768. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1769. 'id' => 'Contact5Email'
  1770. )),
  1771. '/div'
  1772. );
  1773. $this->assertTags($result, $expected);
  1774. $result = $this->Form->input('Contact.password');
  1775. $expected = array(
  1776. 'div' => array('class' => 'input password'),
  1777. 'label' => array('for' => 'ContactPassword'),
  1778. 'Password',
  1779. '/label',
  1780. array('input' => array(
  1781. 'type' => 'password', 'name' => 'data[Contact][password]',
  1782. 'id' => 'ContactPassword'
  1783. )),
  1784. '/div'
  1785. );
  1786. $this->assertTags($result, $expected);
  1787. $result = $this->Form->input('Contact.email', array(
  1788. 'type' => 'file', 'class' => 'textbox'
  1789. ));
  1790. $expected = array(
  1791. 'div' => array('class' => 'input file'),
  1792. 'label' => array('for' => 'ContactEmail'),
  1793. 'Email',
  1794. '/label',
  1795. array('input' => array(
  1796. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1797. 'id' => 'ContactEmail'
  1798. )),
  1799. '/div'
  1800. );
  1801. $this->assertTags($result, $expected);
  1802. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1803. $result = $this->Form->input('Contact.phone');
  1804. $expected = array(
  1805. 'div' => array('class' => 'input tel'),
  1806. 'label' => array('for' => 'ContactPhone'),
  1807. 'Phone',
  1808. '/label',
  1809. array('input' => array(
  1810. 'type' => 'tel', 'name' => 'data[Contact][phone]',
  1811. 'value' => 'Hello &amp; World &gt; weird chars',
  1812. 'id' => 'ContactPhone', 'maxlength' => 255
  1813. )),
  1814. '/div'
  1815. );
  1816. $this->assertTags($result, $expected);
  1817. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1818. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1819. $expected = array(
  1820. 'div' => array('class' => 'input text'),
  1821. 'label' => array('for' => 'myId'),
  1822. 'Field',
  1823. '/label',
  1824. 'input' => array(
  1825. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1826. 'value' => 'My value', 'id' => 'myId'
  1827. ),
  1828. '/div'
  1829. );
  1830. $this->assertTags($result, $expected);
  1831. unset($this->Form->request->data);
  1832. $Contact = ClassRegistry::getObject('Contact');
  1833. $Contact->validationErrors['field'] = array('Badness!');
  1834. $result = $this->Form->input('Contact.field');
  1835. $expected = array(
  1836. 'div' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. array('div' => array('class' => 'error-message')),
  1845. 'Badness!',
  1846. '/div',
  1847. '/div'
  1848. );
  1849. $this->assertTags($result, $expected);
  1850. $result = $this->Form->input('Contact.field', array(
  1851. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1852. ));
  1853. $expected = array(
  1854. 'label' => array('for' => 'ContactField'),
  1855. 'Field',
  1856. '/label',
  1857. 'input' => array(
  1858. 'type' => 'text', 'name' => 'data[Contact][field]',
  1859. 'id' => 'ContactField', 'class' => 'form-error'
  1860. ),
  1861. array('span' => array('class' => 'error-message')),
  1862. 'Badness!',
  1863. '/span'
  1864. );
  1865. $this->assertTags($result, $expected);
  1866. $result = $this->Form->input('Contact.field', array(
  1867. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1868. ));
  1869. $expected = array(
  1870. 'div' => array('class' => 'input text error'),
  1871. 'label' => array('for' => 'ContactField'),
  1872. 'Field',
  1873. '/label',
  1874. 'input' => array(
  1875. 'type' => 'text', 'name' => 'data[Contact][field]',
  1876. 'id' => 'ContactField', 'class' => 'form-error'
  1877. ),
  1878. array('div' => array('class' => 'error')),
  1879. 'Badness!',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('Contact.field', array(
  1884. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1885. ));
  1886. $expected = array(
  1887. 'span' => array('class' => 'input text error'),
  1888. 'label' => array('for' => 'ContactField'),
  1889. 'Field',
  1890. '/label',
  1891. 'input' => array(
  1892. 'type' => 'text', 'name' => 'data[Contact][field]',
  1893. 'id' => 'ContactField', 'class' => 'form-error'
  1894. ),
  1895. 'Badness!',
  1896. '/span'
  1897. );
  1898. $this->assertTags($result, $expected);
  1899. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1900. $expected = array(
  1901. 'div' => array('class' => 'input text error'),
  1902. 'label' => array('for' => 'ContactField'),
  1903. 'Field',
  1904. '/label',
  1905. 'input' => array(
  1906. 'type' => 'text', 'name' => 'data[Contact][field]',
  1907. 'id' => 'ContactField', 'class' => 'form-error'
  1908. ),
  1909. 'A message to you, Rudy',
  1910. array('div' => array('class' => 'error-message')),
  1911. 'Badness!',
  1912. '/div',
  1913. '/div'
  1914. );
  1915. $this->assertTags($result, $expected);
  1916. $this->Form->setEntity(null);
  1917. $this->Form->setEntity('Contact.field');
  1918. $result = $this->Form->input('Contact.field', array(
  1919. 'after' => 'A message to you, Rudy', 'error' => false
  1920. ));
  1921. $expected = array(
  1922. 'div' => array('class' => 'input text'),
  1923. 'label' => array('for' => 'ContactField'),
  1924. 'Field',
  1925. '/label',
  1926. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1927. 'A message to you, Rudy',
  1928. '/div'
  1929. );
  1930. $this->assertTags($result, $expected);
  1931. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1932. $expected = array(
  1933. 'div' => array('class' => 'input text'),
  1934. 'label' => array('for' => 'ObjectField'),
  1935. 'Field',
  1936. '/label',
  1937. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1938. 'A message to you, Rudy',
  1939. '/div'
  1940. );
  1941. $this->assertTags($result, $expected);
  1942. $Contact->validationErrors['field'] = array('minLength');
  1943. $result = $this->Form->input('Contact.field', array(
  1944. 'error' => array(
  1945. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1946. 'maxLength' => 'login too large'
  1947. )
  1948. ));
  1949. $expected = array(
  1950. 'div' => array('class' => 'input text error'),
  1951. 'label' => array('for' => 'ContactField'),
  1952. 'Field',
  1953. '/label',
  1954. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1955. array('div' => array('class' => 'error-message')),
  1956. 'Le login doit contenir au moins 2 caractères',
  1957. '/div',
  1958. '/div'
  1959. );
  1960. $this->assertTags($result, $expected);
  1961. $Contact->validationErrors['field'] = array('maxLength');
  1962. $result = $this->Form->input('Contact.field', array(
  1963. 'error' => array(
  1964. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1965. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1966. 'maxLength' => 'login too large',
  1967. )
  1968. ));
  1969. $expected = array(
  1970. 'div' => array('class' => 'input text error'),
  1971. 'label' => array('for' => 'ContactField'),
  1972. 'Field',
  1973. '/label',
  1974. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1975. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1976. 'login too large',
  1977. '/span',
  1978. '/div'
  1979. );
  1980. $this->assertTags($result, $expected);
  1981. }
  1982. /**
  1983. * Test that inputs with 0 can be created.
  1984. *
  1985. * @return void
  1986. */
  1987. public function testInputZero() {
  1988. $this->Form->create('User');
  1989. $result = $this->Form->input('0');
  1990. $expected = array(
  1991. 'div' => array('class' => 'input text'),
  1992. 'label' => array('for' => 'User0'), '/label',
  1993. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1994. '/div'
  1995. );
  1996. $this->assertTags($result, $expected);
  1997. }
  1998. /**
  1999. * test input() with checkbox creation
  2000. *
  2001. * @return void
  2002. */
  2003. public function testInputCheckbox() {
  2004. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2005. $expected = array(
  2006. 'div' => array('class' => 'input checkbox'),
  2007. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2008. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2009. '/div'
  2010. );
  2011. $this->assertTags($result, $expected);
  2012. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2013. $expected = array(
  2014. 'div' => array('class' => 'input checkbox'),
  2015. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2016. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2017. '/div'
  2018. );
  2019. $this->assertTags($result, $expected);
  2020. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2021. $expected = array(
  2022. 'div' => array('class' => 'input checkbox'),
  2023. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  2024. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2025. '/div'
  2026. );
  2027. $this->assertTags($result, $expected);
  2028. $result = $this->Form->input('User.disabled', array(
  2029. 'label' => 'Disabled',
  2030. 'type' => 'checkbox',
  2031. 'data-foo' => 'disabled'
  2032. ));
  2033. $expected = array(
  2034. 'div' => array('class' => 'input checkbox'),
  2035. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2036. array('input' => array(
  2037. 'type' => 'checkbox',
  2038. 'name' => 'data[User][disabled]',
  2039. 'value' => '1',
  2040. 'id' => 'UserDisabled',
  2041. 'data-foo' => 'disabled'
  2042. )),
  2043. 'label' => array('for' => 'UserDisabled'),
  2044. 'Disabled',
  2045. '/label',
  2046. '/div'
  2047. );
  2048. $this->assertTags($result, $expected);
  2049. }
  2050. /**
  2051. * test form->input() with time types.
  2052. *
  2053. * @return void
  2054. */
  2055. public function testInputTime() {
  2056. extract($this->dateRegex);
  2057. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2058. $result = explode(':', $result);
  2059. $this->assertRegExp('/option value="23"/', $result[0]);
  2060. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2061. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2062. $result = explode(':', $result);
  2063. $this->assertRegExp('/option value="23"/', $result[0]);
  2064. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2065. $result = $this->Form->input('Model.field', array(
  2066. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2067. ));
  2068. $result = explode(':', $result);
  2069. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2070. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2071. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2072. $result = $this->Form->input('Model.field', array(
  2073. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2074. ));
  2075. $result = explode(':', $result);
  2076. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2077. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2078. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2079. $result = $this->Form->input('prueba', array(
  2080. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2081. 'maxYear' => date('Y') + 1, 'interval' => 15
  2082. ));
  2083. $result = explode(':', $result);
  2084. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2085. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2086. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2087. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2088. $result = $this->Form->input('Random.start_time', array(
  2089. 'type' => 'time',
  2090. 'selected' => '18:15'
  2091. ));
  2092. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2093. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2094. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2095. $result = $this->Form->input('published', array('type' => 'time'));
  2096. $now = strtotime('now');
  2097. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2098. $now = strtotime('2013-03-09 00:42:21');
  2099. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2100. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2101. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2102. }
  2103. /**
  2104. * Test interval + selected near the hour roll over.
  2105. *
  2106. * @return void
  2107. */
  2108. public function testTimeSelectedWithInterval() {
  2109. $result = $this->Form->input('Model.start_time', array(
  2110. 'type' => 'time',
  2111. 'interval' => 15,
  2112. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2113. ));
  2114. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2115. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2116. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2117. $result = $this->Form->input('Model.start_time', array(
  2118. 'type' => 'time',
  2119. 'interval' => 15,
  2120. 'selected' => '2012-10-23 15:57:00'
  2121. ));
  2122. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2123. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2124. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2125. $result = $this->Form->input('Model.start_time', array(
  2126. 'timeFormat' => 24,
  2127. 'type' => 'time',
  2128. 'interval' => 15,
  2129. 'selected' => '15:57'
  2130. ));
  2131. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2132. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2133. $result = $this->Form->input('Model.start_time', array(
  2134. 'timeFormat' => 24,
  2135. 'type' => 'time',
  2136. 'interval' => 15,
  2137. 'selected' => '23:57'
  2138. ));
  2139. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2140. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2141. $result = $this->Form->input('Model.created', array(
  2142. 'timeFormat' => 24,
  2143. 'type' => 'datetime',
  2144. 'interval' => 15,
  2145. 'selected' => '2012-09-30 23:56'
  2146. ));
  2147. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2148. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2149. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2150. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2151. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2152. }
  2153. /**
  2154. * Test time with selected values around 12:xx:xx
  2155. *
  2156. * @return void
  2157. */
  2158. public function testTimeSelectedWithIntervalTwelve() {
  2159. $result = $this->Form->input('Model.start_time', array(
  2160. 'type' => 'time',
  2161. 'timeFormat' => 12,
  2162. 'interval' => 15,
  2163. 'selected' => '00:00:00'
  2164. ));
  2165. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2166. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2167. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2168. $result = $this->Form->input('Model.start_time', array(
  2169. 'type' => 'time',
  2170. 'timeFormat' => 12,
  2171. 'interval' => 15,
  2172. 'selected' => '12:00:00'
  2173. ));
  2174. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2175. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2176. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2177. $result = $this->Form->input('Model.start_time', array(
  2178. 'type' => 'time',
  2179. 'timeFormat' => 12,
  2180. 'interval' => 15,
  2181. 'selected' => '12:15:00'
  2182. ));
  2183. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2184. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2185. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2186. }
  2187. /**
  2188. * Test interval & timeFormat = 12
  2189. *
  2190. * @return void
  2191. */
  2192. public function testInputTimeWithIntervalAnd12HourFormat() {
  2193. $result = $this->Form->input('Model.start_time', array(
  2194. 'type' => 'time',
  2195. 'timeFormat' => 12,
  2196. 'interval' => 5,
  2197. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2198. ));
  2199. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2200. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2201. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2202. $result = $this->Form->input('Model.start_time', array(
  2203. 'type' => 'time',
  2204. 'timeFormat' => '12',
  2205. 'interval' => 5,
  2206. 'selected' => '2013-04-19 16:30:00'
  2207. ));
  2208. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2209. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2210. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2211. $result = $this->Form->input('Model.start_time', array(
  2212. 'type' => 'time',
  2213. 'timeFormat' => '12',
  2214. 'interval' => 10,
  2215. 'selected' => '2013-05-19 00:33:00'
  2216. ));
  2217. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2218. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2219. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2220. $result = $this->Form->input('Model.start_time', array(
  2221. 'type' => 'time',
  2222. 'timeFormat' => '12',
  2223. 'interval' => 10,
  2224. 'selected' => '2013-05-19 13:33:00'
  2225. ));
  2226. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2227. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2228. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2229. $result = $this->Form->input('Model.start_time', array(
  2230. 'type' => 'time',
  2231. 'timeFormat' => '12',
  2232. 'interval' => 10,
  2233. 'selected' => '2013-05-19 01:33:00'
  2234. ));
  2235. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2236. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2237. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2238. }
  2239. /**
  2240. * test form->input() with datetime, date and time types
  2241. *
  2242. * @return void
  2243. */
  2244. public function testInputDatetime() {
  2245. extract($this->dateRegex);
  2246. $result = $this->Form->input('prueba', array(
  2247. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2248. 'maxYear' => date('Y') + 1, 'interval' => 15
  2249. ));
  2250. $result = explode(':', $result);
  2251. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2252. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2253. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2254. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2255. //related to ticket #5013
  2256. $result = $this->Form->input('Contact.date', array(
  2257. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2258. ));
  2259. $this->assertRegExp('/class="customClass"/', $result);
  2260. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2261. $result = $this->Form->input('Contact.date', array(
  2262. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2263. ));
  2264. $this->assertRegExp('/id="customIdDay"/', $result);
  2265. $this->assertRegExp('/id="customIdMonth"/', $result);
  2266. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2267. $result = $this->Form->input('Model.field', array(
  2268. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2269. ));
  2270. $this->assertRegExp('/id="customIDDay"/', $result);
  2271. $this->assertRegExp('/id="customIDHour"/', $result);
  2272. $result = explode('</select><select', $result);
  2273. $result = explode(':', $result[1]);
  2274. $this->assertRegExp('/option value="23"/', $result[0]);
  2275. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2276. $result = $this->Form->input('Model.field', array(
  2277. 'type' => 'datetime', 'timeFormat' => 12
  2278. ));
  2279. $result = explode('</select><select', $result);
  2280. $result = explode(':', $result[1]);
  2281. $this->assertRegExp('/option value="12"/', $result[0]);
  2282. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2283. $this->Form->request->data = array('Contact' => array('created' => null));
  2284. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2285. $expected = array(
  2286. 'div' => array('class' => 'input date'),
  2287. 'label' => array('for' => 'ContactCreatedMonth'),
  2288. 'Created',
  2289. '/label',
  2290. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2291. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2292. $monthsRegex,
  2293. '/select', '-',
  2294. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2295. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2296. $daysRegex,
  2297. '/select', '-',
  2298. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2299. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2300. $yearsRegex,
  2301. '/select',
  2302. '/div'
  2303. );
  2304. $this->assertTags($result, $expected);
  2305. $this->Form->request->data = array('Contact' => array('created' => null));
  2306. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2307. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2308. $this->Form->request->data = array('Contact' => array('created' => null));
  2309. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2310. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2311. $result = $this->Form->input('Contact.created', array(
  2312. 'type' => 'date',
  2313. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2314. 'timeFormat' => 'NONE'
  2315. ));
  2316. $this->assertRegExp('/for\="created-month"/', $result);
  2317. }
  2318. /**
  2319. * Test generating checkboxes in a loop.
  2320. *
  2321. * @return void
  2322. */
  2323. public function testInputCheckboxesInLoop() {
  2324. for ($i = 1; $i < 5; $i++) {
  2325. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2326. $expected = array(
  2327. 'div' => array('class' => 'input checkbox'),
  2328. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2329. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2330. 'label' => array('for' => "Contact{$i}Email"),
  2331. 'Email',
  2332. '/label',
  2333. '/div'
  2334. );
  2335. $this->assertTags($result, $expected);
  2336. }
  2337. }
  2338. /**
  2339. * Test generating checkboxes with disabled elements.
  2340. *
  2341. * @return void
  2342. */
  2343. public function testInputCheckboxWithDisabledElements() {
  2344. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2345. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2346. $expected = array(
  2347. array('div' => array('class' => 'input select')),
  2348. array('label' => array('for' => "ContactMultiple")),
  2349. 'Multiple',
  2350. '/label',
  2351. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2352. array('div' => array('class' => 'checkbox')),
  2353. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2354. array('label' => array('for' => "ContactMultiple1")),
  2355. 'One',
  2356. '/label',
  2357. '/div',
  2358. array('div' => array('class' => 'checkbox')),
  2359. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2360. array('label' => array('for' => "ContactMultiple2")),
  2361. 'Two',
  2362. '/label',
  2363. '/div',
  2364. array('div' => array('class' => 'checkbox')),
  2365. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2366. array('label' => array('for' => "ContactMultiple3")),
  2367. 'Three',
  2368. '/label',
  2369. '/div',
  2370. '/div'
  2371. );
  2372. $this->assertTags($result, $expected);
  2373. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2374. $this->assertTags($result, $expected);
  2375. $disabled = array('2', 3);
  2376. $expected = array(
  2377. array('div' => array('class' => 'input select')),
  2378. array('label' => array('for' => "ContactMultiple")),
  2379. 'Multiple',
  2380. '/label',
  2381. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2382. array('div' => array('class' => 'checkbox')),
  2383. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2384. array('label' => array('for' => "ContactMultiple1")),
  2385. 'One',
  2386. '/label',
  2387. '/div',
  2388. array('div' => array('class' => 'checkbox')),
  2389. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2390. array('label' => array('for' => "ContactMultiple2")),
  2391. 'Two',
  2392. '/label',
  2393. '/div',
  2394. array('div' => array('class' => 'checkbox')),
  2395. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2396. array('label' => array('for' => "ContactMultiple3")),
  2397. 'Three',
  2398. '/label',
  2399. '/div',
  2400. '/div'
  2401. );
  2402. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2403. $this->assertTags($result, $expected);
  2404. // make sure 50 does only disable 50, and not 50f5c0cf
  2405. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2406. $disabled = array(50);
  2407. $expected = array(
  2408. array('div' => array('class' => 'input select')),
  2409. array('label' => array('for' => "ContactMultiple")),
  2410. 'Multiple',
  2411. '/label',
  2412. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2413. array('div' => array('class' => 'checkbox')),
  2414. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2415. array('label' => array('for' => "ContactMultiple50")),
  2416. 'Fifty',
  2417. '/label',
  2418. '/div',
  2419. array('div' => array('class' => 'checkbox')),
  2420. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2421. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2422. 'Stringy',
  2423. '/label',
  2424. '/div',
  2425. '/div'
  2426. );
  2427. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2428. $this->assertTags($result, $expected);
  2429. }
  2430. /**
  2431. * test input name with leading integer, ensure attributes are generated correctly.
  2432. *
  2433. * @return void
  2434. */
  2435. public function testInputWithLeadingInteger() {
  2436. $result = $this->Form->text('0.Node.title');
  2437. $expected = array(
  2438. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2439. );
  2440. $this->assertTags($result, $expected);
  2441. }
  2442. /**
  2443. * test form->input() with select type inputs.
  2444. *
  2445. * @return void
  2446. */
  2447. public function testInputSelectType() {
  2448. $result = $this->Form->input('email', array(
  2449. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2450. );
  2451. $expected = array(
  2452. 'div' => array('class' => 'input select'),
  2453. 'label' => array('for' => 'email'),
  2454. 'Email',
  2455. '/label',
  2456. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2457. array('option' => array('value' => '')),
  2458. '/option',
  2459. array('option' => array('value' => 'è')),
  2460. 'Firést',
  2461. '/option',
  2462. array('option' => array('value' => 'é')),
  2463. 'Secoènd',
  2464. '/option',
  2465. '/select',
  2466. '/div'
  2467. );
  2468. $this->assertTags($result, $expected);
  2469. $result = $this->Form->input('email', array(
  2470. 'options' => array('First', 'Second'), 'empty' => true)
  2471. );
  2472. $expected = array(
  2473. 'div' => array('class' => 'input select'),
  2474. 'label' => array('for' => 'email'),
  2475. 'Email',
  2476. '/label',
  2477. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2478. array('option' => array('value' => '')),
  2479. '/option',
  2480. array('option' => array('value' => '0')),
  2481. 'First',
  2482. '/option',
  2483. array('option' => array('value' => '1')),
  2484. 'Second',
  2485. '/option',
  2486. '/select',
  2487. '/div'
  2488. );
  2489. $this->assertTags($result, $expected);
  2490. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2491. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2492. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2493. $expected = array(
  2494. 'div' => array('class' => 'input select'),
  2495. 'label' => array('for' => 'ModelUserId'),
  2496. 'User',
  2497. '/label',
  2498. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2499. array('option' => array('value' => '')),
  2500. '/option',
  2501. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2502. 'good',
  2503. '/option',
  2504. array('option' => array('value' => 'other')),
  2505. 'bad',
  2506. '/option',
  2507. '/select',
  2508. '/div'
  2509. );
  2510. $this->assertTags($result, $expected);
  2511. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2512. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2513. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2514. $expected = array(
  2515. 'div' => array('class' => 'input select'),
  2516. 'label' => array('for' => 'ThingUserId'),
  2517. 'User',
  2518. '/label',
  2519. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2520. array('option' => array('value' => '')),
  2521. 'Some Empty',
  2522. '/option',
  2523. array('option' => array('value' => 'value')),
  2524. 'good',
  2525. '/option',
  2526. array('option' => array('value' => 'other')),
  2527. 'bad',
  2528. '/option',
  2529. '/select',
  2530. '/div'
  2531. );
  2532. $this->assertTags($result, $expected);
  2533. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2534. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2535. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2536. $expected = array(
  2537. 'div' => array('class' => 'input select'),
  2538. 'label' => array('for' => 'ThingUserId'),
  2539. 'User',
  2540. '/label',
  2541. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2542. array('option' => array('value' => '')),
  2543. 'Some Empty',
  2544. '/option',
  2545. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2546. 'good',
  2547. '/option',
  2548. array('option' => array('value' => 'other')),
  2549. 'bad',
  2550. '/option',
  2551. '/select',
  2552. '/div'
  2553. );
  2554. $this->assertTags($result, $expected);
  2555. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2556. $this->Form->request->data = array('User' => array('User' => array('value')));
  2557. $result = $this->Form->input('User.User', array('empty' => true));
  2558. $expected = array(
  2559. 'div' => array('class' => 'input select'),
  2560. 'label' => array('for' => 'UserUser'),
  2561. 'User',
  2562. '/label',
  2563. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2564. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2565. array('option' => array('value' => '')),
  2566. '/option',
  2567. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2568. 'good',
  2569. '/option',
  2570. array('option' => array('value' => 'other')),
  2571. 'bad',
  2572. '/option',
  2573. '/select',
  2574. '/div'
  2575. );
  2576. $this->assertTags($result, $expected);
  2577. $this->Form->data = array();
  2578. $result = $this->Form->input('Publisher.id', array(
  2579. 'label' => 'Publisher',
  2580. 'type' => 'select',
  2581. 'multiple' => 'checkbox',
  2582. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2583. ));
  2584. $expected = array(
  2585. array('div' => array('class' => 'input select')),
  2586. array('label' => array('for' => 'PublisherId')),
  2587. 'Publisher',
  2588. '/label',
  2589. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2590. array('div' => array('class' => 'checkbox')),
  2591. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2592. array('label' => array('for' => 'PublisherIdValue1')),
  2593. 'Label 1',
  2594. '/label',
  2595. '/div',
  2596. array('div' => array('class' => 'checkbox')),
  2597. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2598. array('label' => array('for' => 'PublisherIdValue2')),
  2599. 'Label 2',
  2600. '/label',
  2601. '/div',
  2602. '/div'
  2603. );
  2604. $this->assertTags($result, $expected);
  2605. }
  2606. /**
  2607. * test that input() and a non standard primary key makes a hidden input by default.
  2608. *
  2609. * @return void
  2610. */
  2611. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2612. $this->Form->create('User');
  2613. $this->Form->fieldset = array(
  2614. 'User' => array(
  2615. 'fields' => array(
  2616. 'model_id' => array('type' => 'integer')
  2617. ),
  2618. 'validates' => array(),
  2619. 'key' => 'model_id'
  2620. )
  2621. );
  2622. $result = $this->Form->input('model_id');
  2623. $expected = array(
  2624. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2625. );
  2626. $this->assertTags($result, $expected);
  2627. }
  2628. /**
  2629. * test that overriding the magic select type widget is possible
  2630. *
  2631. * @return void
  2632. */
  2633. public function testInputOverridingMagicSelectType() {
  2634. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2635. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2636. $expected = array(
  2637. 'div' => array('class' => 'input text'),
  2638. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2639. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2640. '/div'
  2641. );
  2642. $this->assertTags($result, $expected);
  2643. //Check that magic types still work for plural/singular vars
  2644. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2645. $result = $this->Form->input('Model.type');
  2646. $expected = array(
  2647. 'div' => array('class' => 'input select'),
  2648. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2649. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2650. array('option' => array('value' => 'value')), 'good', '/option',
  2651. array('option' => array('value' => 'other')), 'bad', '/option',
  2652. '/select',
  2653. '/div'
  2654. );
  2655. $this->assertTags($result, $expected);
  2656. }
  2657. /**
  2658. * Test that inferred types do not override developer input
  2659. *
  2660. * @return void
  2661. */
  2662. public function testInputMagicTypeDoesNotOverride() {
  2663. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2664. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2665. $expected = array(
  2666. 'div' => array('class' => 'input checkbox'),
  2667. array('input' => array(
  2668. 'type' => 'hidden',
  2669. 'name' => 'data[Model][user]',
  2670. 'id' => 'ModelUser_',
  2671. 'value' => 0,
  2672. )),
  2673. array('input' => array(
  2674. 'name' => 'data[Model][user]',
  2675. 'type' => 'checkbox',
  2676. 'id' => 'ModelUser',
  2677. 'value' => 1
  2678. )),
  2679. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  2680. '/div'
  2681. );
  2682. $this->assertTags($result, $expected);
  2683. }
  2684. /**
  2685. * Test that magic input() selects are created for type=number
  2686. *
  2687. * @return void
  2688. */
  2689. public function testInputMagicSelectForTypeNumber() {
  2690. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2691. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2692. $result = $this->Form->input('ValidateUser.balance');
  2693. $expected = array(
  2694. 'div' => array('class' => 'input select'),
  2695. 'label' => array('for' => 'ValidateUserBalance'),
  2696. 'Balance',
  2697. '/label',
  2698. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2699. array('option' => array('value' => '0')),
  2700. 'nothing',
  2701. '/option',
  2702. array('option' => array('value' => '1', 'selected' => 'selected')),
  2703. 'some',
  2704. '/option',
  2705. array('option' => array('value' => '100')),
  2706. 'a lot',
  2707. '/option',
  2708. '/select',
  2709. '/div'
  2710. );
  2711. $this->assertTags($result, $expected);
  2712. }
  2713. /**
  2714. * Test that magic input() selects can easily be converted into radio types without error.
  2715. *
  2716. * @return void
  2717. */
  2718. public function testInputMagicSelectChangeToRadio() {
  2719. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2720. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2721. $this->assertRegExp('/input type="radio"/', $result);
  2722. }
  2723. /**
  2724. * fields with the same name as the model should work.
  2725. *
  2726. * @return void
  2727. */
  2728. public function testInputWithMatchingFieldAndModelName() {
  2729. $this->Form->create('User');
  2730. $this->Form->fieldset = array(
  2731. 'User' => array(
  2732. 'fields' => array(
  2733. 'User' => array('type' => 'text')
  2734. ),
  2735. 'validates' => array(),
  2736. 'key' => 'id'
  2737. )
  2738. );
  2739. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2740. $result = $this->Form->input('User', array('type' => 'text'));
  2741. $expected = array(
  2742. 'div' => array('class' => 'input text'),
  2743. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2744. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2745. '/div'
  2746. );
  2747. $this->assertTags($result, $expected);
  2748. }
  2749. /**
  2750. * testFormInputs method
  2751. *
  2752. * test correct results from form::inputs().
  2753. *
  2754. * @return void
  2755. */
  2756. public function testFormInputs() {
  2757. $this->Form->create('Contact');
  2758. $result = $this->Form->inputs('The Legend');
  2759. $expected = array(
  2760. '<fieldset',
  2761. '<legend',
  2762. 'The Legend',
  2763. '/legend',
  2764. '*/fieldset',
  2765. );
  2766. $this->assertTags($result, $expected);
  2767. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2768. $expected = array(
  2769. 'fieldset' => array('class' => 'classy-stuff'),
  2770. '<legend',
  2771. 'Field of Dreams',
  2772. '/legend',
  2773. '*/fieldset'
  2774. );
  2775. $this->assertTags($result, $expected);
  2776. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2777. $this->assertTags($result, $expected);
  2778. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2779. $this->assertTags($result, $expected);
  2780. $this->Form->create('Contact');
  2781. $this->Form->request['prefix'] = 'admin';
  2782. $this->Form->request['action'] = 'admin_edit';
  2783. $result = $this->Form->inputs();
  2784. $expected = array(
  2785. '<fieldset',
  2786. '<legend',
  2787. 'Edit Contact',
  2788. '/legend',
  2789. '*/fieldset',
  2790. );
  2791. $this->assertTags($result, $expected);
  2792. $this->Form->create('Contact');
  2793. $result = $this->Form->inputs(false);
  2794. $expected = array(
  2795. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2796. array('div' => array('class' => 'input text')),
  2797. '*/div',
  2798. array('div' => array('class' => 'input email')),
  2799. '*/div',
  2800. array('div' => array('class' => 'input tel')),
  2801. '*/div',
  2802. array('div' => array('class' => 'input password')),
  2803. '*/div',
  2804. array('div' => array('class' => 'input date')),
  2805. '*/div',
  2806. array('div' => array('class' => 'input date')),
  2807. '*/div',
  2808. array('div' => array('class' => 'input datetime')),
  2809. '*/div',
  2810. array('div' => array('class' => 'input number')),
  2811. '*/div',
  2812. array('div' => array('class' => 'input select')),
  2813. '*/div',
  2814. );
  2815. $this->assertTags($result, $expected);
  2816. $this->Form->create('Contact');
  2817. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2818. $expected = array(
  2819. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2820. array('div' => array('class' => 'input text')),
  2821. '*/div',
  2822. array('div' => array('class' => 'input email')),
  2823. '*/div',
  2824. array('div' => array('class' => 'input tel')),
  2825. '*/div',
  2826. array('div' => array('class' => 'input password')),
  2827. '*/div',
  2828. array('div' => array('class' => 'input date')),
  2829. '*/div',
  2830. array('div' => array('class' => 'input date')),
  2831. '*/div',
  2832. array('div' => array('class' => 'input datetime')),
  2833. '*/div',
  2834. array('div' => array('class' => 'input number')),
  2835. '*/div',
  2836. array('div' => array('class' => 'input select')),
  2837. '*/div',
  2838. );
  2839. $this->assertTags($result, $expected);
  2840. $this->Form->create('Contact');
  2841. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2842. $this->assertTags($result, $expected);
  2843. $this->Form->create('Contact');
  2844. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2845. $expected = array(
  2846. 'fieldset' => array(),
  2847. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2848. array('div' => array('class' => 'input text')),
  2849. '*/div',
  2850. array('div' => array('class' => 'input email')),
  2851. '*/div',
  2852. array('div' => array('class' => 'input tel')),
  2853. '*/div',
  2854. array('div' => array('class' => 'input password')),
  2855. '*/div',
  2856. array('div' => array('class' => 'input date')),
  2857. '*/div',
  2858. array('div' => array('class' => 'input date')),
  2859. '*/div',
  2860. array('div' => array('class' => 'input datetime')),
  2861. '*/div',
  2862. array('div' => array('class' => 'input number')),
  2863. '*/div',
  2864. array('div' => array('class' => 'input select')),
  2865. '*/div',
  2866. '/fieldset'
  2867. );
  2868. $this->assertTags($result, $expected);
  2869. $this->Form->create('Contact');
  2870. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2871. $expected = array(
  2872. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2873. array('div' => array('class' => 'input text')),
  2874. '*/div',
  2875. array('div' => array('class' => 'input email')),
  2876. '*/div',
  2877. array('div' => array('class' => 'input tel')),
  2878. '*/div',
  2879. array('div' => array('class' => 'input password')),
  2880. '*/div',
  2881. array('div' => array('class' => 'input date')),
  2882. '*/div',
  2883. array('div' => array('class' => 'input date')),
  2884. '*/div',
  2885. array('div' => array('class' => 'input datetime')),
  2886. '*/div',
  2887. array('div' => array('class' => 'input number')),
  2888. '*/div',
  2889. array('div' => array('class' => 'input select')),
  2890. '*/div',
  2891. );
  2892. $this->assertTags($result, $expected);
  2893. $this->Form->create('Contact');
  2894. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2895. $this->assertTags($result, $expected);
  2896. $this->Form->create('Contact');
  2897. $result = $this->Form->inputs('Hello');
  2898. $expected = array(
  2899. 'fieldset' => array(),
  2900. 'legend' => array(),
  2901. 'Hello',
  2902. '/legend',
  2903. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2904. array('div' => array('class' => 'input text')),
  2905. '*/div',
  2906. array('div' => array('class' => 'input email')),
  2907. '*/div',
  2908. array('div' => array('class' => 'input tel')),
  2909. '*/div',
  2910. array('div' => array('class' => 'input password')),
  2911. '*/div',
  2912. array('div' => array('class' => 'input date')),
  2913. '*/div',
  2914. array('div' => array('class' => 'input date')),
  2915. '*/div',
  2916. array('div' => array('class' => 'input datetime')),
  2917. '*/div',
  2918. array('div' => array('class' => 'input number')),
  2919. '*/div',
  2920. array('div' => array('class' => 'input select')),
  2921. '*/div',
  2922. '/fieldset'
  2923. );
  2924. $this->assertTags($result, $expected);
  2925. $this->Form->create('Contact');
  2926. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2927. $expected = array(
  2928. 'fieldset' => array(),
  2929. 'legend' => array(),
  2930. 'Hello',
  2931. '/legend',
  2932. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2933. array('div' => array('class' => 'input text')),
  2934. '*/div',
  2935. array('div' => array('class' => 'input email')),
  2936. '*/div',
  2937. array('div' => array('class' => 'input tel')),
  2938. '*/div',
  2939. array('div' => array('class' => 'input password')),
  2940. '*/div',
  2941. array('div' => array('class' => 'input date')),
  2942. '*/div',
  2943. array('div' => array('class' => 'input date')),
  2944. '*/div',
  2945. array('div' => array('class' => 'input datetime')),
  2946. '*/div',
  2947. array('div' => array('class' => 'input number')),
  2948. '*/div',
  2949. array('div' => array('class' => 'input select')),
  2950. '*/div',
  2951. '/fieldset'
  2952. );
  2953. $this->assertTags($result, $expected);
  2954. $this->Form->create('Contact');
  2955. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2956. $this->assertTags($result, $expected);
  2957. $this->Form->end();
  2958. $this->Form->create(false);
  2959. $expected = array(
  2960. 'fieldset' => array(),
  2961. array('div' => array('class' => 'input text')),
  2962. 'label' => array('for' => 'foo'),
  2963. 'Foo',
  2964. '/label',
  2965. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2966. '*/div',
  2967. '/fieldset'
  2968. );
  2969. $result = $this->Form->inputs(
  2970. array('foo' => array('type' => 'text')),
  2971. array(),
  2972. array('legend' => false)
  2973. );
  2974. $this->assertTags($result, $expected);
  2975. }
  2976. /**
  2977. * Tests inputs() works with plugin models
  2978. *
  2979. * @return void
  2980. */
  2981. public function testInputsPluginModel() {
  2982. $this->loadFixtures('Post');
  2983. App::build(array(
  2984. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  2985. ));
  2986. CakePlugin::load('TestPlugin');
  2987. $this->Form->request['models'] = array(
  2988. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2989. );
  2990. $this->Form->create('TestPlugin.TestPluginPost');
  2991. $result = $this->Form->inputs();
  2992. $this->assertContains('data[TestPluginPost][id]', $result);
  2993. $this->assertContains('data[TestPluginPost][author_id]', $result);
  2994. $this->assertContains('data[TestPluginPost][title]', $result);
  2995. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2996. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2997. $this->assertEquals('TestPluginPost', $this->Form->model());
  2998. }
  2999. /**
  3000. * testSelectAsCheckbox method
  3001. *
  3002. * test multi-select widget with checkbox formatting.
  3003. *
  3004. * @return void
  3005. */
  3006. public function testSelectAsCheckbox() {
  3007. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  3008. $expected = array(
  3009. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3010. array('div' => array('class' => 'checkbox')),
  3011. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  3012. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  3013. 'first',
  3014. '/label',
  3015. '/div',
  3016. array('div' => array('class' => 'checkbox')),
  3017. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  3018. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  3019. 'second',
  3020. '/label',
  3021. '/div',
  3022. array('div' => array('class' => 'checkbox')),
  3023. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3024. array('label' => array('for' => 'ModelMultiField2')),
  3025. 'third',
  3026. '/label',
  3027. '/div',
  3028. );
  3029. $this->assertTags($result, $expected);
  3030. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  3031. $expected = array(
  3032. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3033. array('div' => array('class' => 'checkbox')),
  3034. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  3035. array('label' => array('for' => 'ModelMultiField12')),
  3036. 'half',
  3037. '/label',
  3038. '/div',
  3039. );
  3040. $this->assertTags($result, $expected);
  3041. }
  3042. /**
  3043. * testLabel method
  3044. *
  3045. * test label generation.
  3046. *
  3047. * @return void
  3048. */
  3049. public function testLabel() {
  3050. $this->Form->text('Person.name');
  3051. $result = $this->Form->label();
  3052. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3053. $this->Form->text('Person.name');
  3054. $result = $this->Form->label();
  3055. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  3056. $result = $this->Form->label('Person.first_name');
  3057. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  3058. $result = $this->Form->label('Person.first_name', 'Your first name');
  3059. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  3060. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3061. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  3062. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3063. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3064. $result = $this->Form->label('Person.first_name', '');
  3065. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  3066. $result = $this->Form->label('Person.2.name', '');
  3067. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  3068. }
  3069. /**
  3070. * testTextbox method
  3071. *
  3072. * test textbox element generation
  3073. *
  3074. * @return void
  3075. */
  3076. public function testTextbox() {
  3077. $result = $this->Form->text('Model.field');
  3078. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3079. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3080. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  3081. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3082. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  3083. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3084. $result = $this->Form->text('Model.text');
  3085. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  3086. $Contact = ClassRegistry::getObject('Contact');
  3087. $Contact->validationErrors['text'] = array(true);
  3088. $this->Form->request->data['Contact']['text'] = 'test';
  3089. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  3090. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3091. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  3092. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  3093. $expected = array(
  3094. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  3095. );
  3096. $this->assertTags($result, $expected);
  3097. }
  3098. /**
  3099. * testDefaultValue method
  3100. *
  3101. * Test default value setting
  3102. *
  3103. * @return void
  3104. */
  3105. public function testDefaultValue() {
  3106. $this->Form->request->data['Model']['field'] = 'test';
  3107. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3108. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  3109. unset($this->Form->request->data['Model']['field']);
  3110. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3111. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  3112. }
  3113. /**
  3114. * testCheckboxDefaultValue method
  3115. *
  3116. * Test default value setting on checkbox() method
  3117. *
  3118. * @return void
  3119. */
  3120. public function testCheckboxDefaultValue() {
  3121. $this->Form->request->data['Model']['field'] = false;
  3122. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3123. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3124. unset($this->Form->request->data['Model']['field']);
  3125. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3126. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3127. $this->Form->request->data['Model']['field'] = true;
  3128. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3129. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3130. unset($this->Form->request->data['Model']['field']);
  3131. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3132. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  3133. }
  3134. /**
  3135. * testError method
  3136. *
  3137. * Test field error generation
  3138. *
  3139. * @return void
  3140. */
  3141. public function testError() {
  3142. $Contact = ClassRegistry::getObject('Contact');
  3143. $Contact->validationErrors['field'] = array(1);
  3144. $result = $this->Form->error('Contact.field');
  3145. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3146. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3147. $this->assertEquals('Error in field Field', $result);
  3148. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3149. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3150. $this->assertEquals('This field contains invalid input', $result);
  3151. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3152. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3153. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3154. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3155. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3156. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3157. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3158. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3159. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3160. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3161. $this->assertEquals('<strong>Badness!</strong>', $result);
  3162. $Contact->validationErrors['field'] = array("email");
  3163. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3164. $expected = array(
  3165. 'div' => array('class' => 'field-error'),
  3166. 'No good!',
  3167. '/div'
  3168. );
  3169. $this->assertTags($result, $expected);
  3170. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3171. $result = $this->Form->error('Contact.field', array(
  3172. 'notEmpty' => 'Cannot be empty',
  3173. 'email' => 'No good!'
  3174. ));
  3175. $expected = array(
  3176. 'div' => array('class' => 'error-message'),
  3177. 'ul' => array(),
  3178. '<li', 'Cannot be empty', '/li',
  3179. '<li', 'No good!', '/li',
  3180. '<li', 'Something else', '/li',
  3181. '/ul',
  3182. '/div'
  3183. );
  3184. $this->assertTags($result, $expected);
  3185. // Testing error messages list options
  3186. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3187. $result = $this->Form->error('Contact.field', null, array('listOptions' => '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('listOptions' => array('tag' => 'ol')));
  3198. $expected = array(
  3199. 'div' => array('class' => 'error-message'),
  3200. 'ol' => array(),
  3201. '<li', 'notEmpty', '/li',
  3202. '<li', 'email', '/li',
  3203. '/ol',
  3204. '/div'
  3205. );
  3206. $this->assertTags($result, $expected);
  3207. $result = $this->Form->error('Contact.field', null, array(
  3208. 'listOptions' => array(
  3209. 'class' => 'ul-class',
  3210. 'itemOptions' => array(
  3211. 'class' => 'li-class'
  3212. )
  3213. )
  3214. ));
  3215. $expected = array(
  3216. 'div' => array('class' => 'error-message'),
  3217. 'ul' => array('class' => 'ul-class'),
  3218. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3219. array('li' => array('class' => 'li-class')), 'email', '/li',
  3220. '/ul',
  3221. '/div'
  3222. );
  3223. $this->assertTags($result, $expected);
  3224. }
  3225. /**
  3226. * test error options when using form->input();
  3227. *
  3228. * @return void
  3229. */
  3230. public function testInputErrorEscape() {
  3231. $this->Form->create('ValidateProfile');
  3232. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3233. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3234. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3235. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3236. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3237. $this->assertRegExp('/required<br>/', $result);
  3238. }
  3239. /**
  3240. * testPassword method
  3241. *
  3242. * Test password element generation
  3243. *
  3244. * @return void
  3245. */
  3246. public function testPassword() {
  3247. $Contact = ClassRegistry::getObject('Contact');
  3248. $result = $this->Form->password('Contact.field');
  3249. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  3250. $Contact->validationErrors['passwd'] = 1;
  3251. $this->Form->request->data['Contact']['passwd'] = 'test';
  3252. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3253. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3254. }
  3255. /**
  3256. * testRadio method
  3257. *
  3258. * Test radio element set generation
  3259. *
  3260. * @return void
  3261. */
  3262. public function testRadio() {
  3263. $result = $this->Form->radio('Model.field', array('option A'));
  3264. $expected = array(
  3265. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3266. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3267. 'label' => array('for' => 'ModelField0'),
  3268. 'option A',
  3269. '/label'
  3270. );
  3271. $this->assertTags($result, $expected);
  3272. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3273. $expected = array(
  3274. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3275. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3276. 'label' => array('for' => 'ModelField12'),
  3277. 'half',
  3278. '/label'
  3279. );
  3280. $this->assertTags($result, $expected);
  3281. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3282. $expected = array(
  3283. 'fieldset' => array(),
  3284. 'legend' => array(),
  3285. 'Field',
  3286. '/legend',
  3287. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3288. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3289. array('label' => array('for' => 'ModelField0')),
  3290. 'option A',
  3291. '/label',
  3292. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3293. array('label' => array('for' => 'ModelField1')),
  3294. 'option B',
  3295. '/label',
  3296. '/fieldset'
  3297. );
  3298. $this->assertTags($result, $expected);
  3299. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3300. $expected = array(
  3301. 'fieldset' => array(),
  3302. 'legend' => array(),
  3303. 'Field',
  3304. '/legend',
  3305. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3306. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3307. array('label' => array('for' => 'ModelField0')),
  3308. 'option A',
  3309. '/label',
  3310. 'br' => array(),
  3311. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3312. array('label' => array('for' => 'ModelField1')),
  3313. 'option B',
  3314. '/label',
  3315. '/fieldset'
  3316. );
  3317. $this->assertTags($result, $expected);
  3318. $result = $this->Form->radio(
  3319. 'Employee.gender',
  3320. array('male' => 'Male', 'female' => 'Female'),
  3321. array('form' => 'my-form')
  3322. );
  3323. $expected = array(
  3324. 'fieldset' => array(),
  3325. 'legend' => array(),
  3326. 'Gender',
  3327. '/legend',
  3328. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_', 'form' => 'my-form'),
  3329. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale', 'form' => 'my-form')),
  3330. array('label' => array('for' => 'EmployeeGenderMale')),
  3331. 'Male',
  3332. '/label',
  3333. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale', 'form' => 'my-form')),
  3334. array('label' => array('for' => 'EmployeeGenderFemale')),
  3335. 'Female',
  3336. '/label',
  3337. '/fieldset',
  3338. );
  3339. $this->assertTags($result, $expected);
  3340. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3341. $expected = array(
  3342. 'fieldset' => array(),
  3343. 'legend' => array(),
  3344. 'Gender',
  3345. '/legend',
  3346. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3347. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3348. array('label' => array('for' => 'OfficerGenderMale')),
  3349. 'Male',
  3350. '/label',
  3351. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3352. array('label' => array('for' => 'OfficerGenderFemale')),
  3353. 'Female',
  3354. '/label',
  3355. '/fieldset',
  3356. );
  3357. $this->assertTags($result, $expected);
  3358. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3359. $expected = array(
  3360. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3361. array('input' => array(
  3362. 'type' => 'radio',
  3363. 'name' => 'data[Contact][1][imrequired]',
  3364. 'value' => '0',
  3365. 'id' => 'Contact1Imrequired0',
  3366. 'required' => 'required'
  3367. )),
  3368. 'label' => array('for' => 'Contact1Imrequired0'),
  3369. 'option A',
  3370. '/label'
  3371. );
  3372. $this->assertTags($result, $expected);
  3373. $result = $this->Form->radio('Model.1.field', array('option A'));
  3374. $expected = array(
  3375. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3376. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3377. 'label' => array('for' => 'Model1Field0'),
  3378. 'option A',
  3379. '/label'
  3380. );
  3381. $this->assertTags($result, $expected);
  3382. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3383. $expected = array(
  3384. 'fieldset' => array(),
  3385. 'legend' => array(),
  3386. 'Field',
  3387. '/legend',
  3388. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3389. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3390. array('label' => array('for' => 'ModelField0')),
  3391. 'option A',
  3392. '/label',
  3393. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3394. array('label' => array('for' => 'ModelField1')),
  3395. 'option B',
  3396. '/label',
  3397. '/fieldset'
  3398. );
  3399. $this->assertTags($result, $expected);
  3400. $result = $this->Form->radio(
  3401. 'Model.field',
  3402. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third')
  3403. );
  3404. $expected = array(
  3405. 'fieldset' => array(),
  3406. 'legend' => array(),
  3407. 'Field',
  3408. '/legend',
  3409. 'input' => array(
  3410. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3411. 'id' => 'ModelField_', 'value' => '',
  3412. ),
  3413. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3414. 'id' => 'ModelFieldAB', 'value' => 'a&gt;b')),
  3415. array('label' => array('for' => 'ModelFieldAB')),
  3416. 'first',
  3417. '/label',
  3418. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3419. 'id' => 'ModelFieldAB1', 'value' => 'a&lt;b')),
  3420. array('label' => array('for' => 'ModelFieldAB1')),
  3421. 'second',
  3422. '/label',
  3423. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]',
  3424. 'id' => 'ModelFieldAB2', 'value' => 'a&quot;b')),
  3425. array('label' => array('for' => 'ModelFieldAB2')),
  3426. 'third',
  3427. '/label',
  3428. '/fieldset'
  3429. );
  3430. $this->assertTags($result, $expected);
  3431. }
  3432. /**
  3433. * testRadioDifferentModel
  3434. * Refs #2911
  3435. *
  3436. * @return void
  3437. */
  3438. public function testRadioDifferentModel() {
  3439. $this->Form->create('User');
  3440. $result = $this->Form->radio(
  3441. 'Model.field',
  3442. array('v1' => 'option A', 'v2' => 'option B'),
  3443. array('label' => true, 'legend' => false, 'value' => false)
  3444. );
  3445. $expected = array(
  3446. array('input' => array(
  3447. 'type' => 'radio', 'name' => 'data[Model][field]',
  3448. 'value' => 'v1', 'id' => 'ModelFieldV1'
  3449. )),
  3450. array('label' => array('for' => 'ModelFieldV1')),
  3451. 'option A',
  3452. '/label',
  3453. array('input' => array(
  3454. 'type' => 'radio', 'name' => 'data[Model][field]',
  3455. 'value' => 'v2', 'id' => 'ModelFieldV2'
  3456. )),
  3457. array('label' => array('for' => 'ModelFieldV2')),
  3458. 'option B',
  3459. '/label'
  3460. );
  3461. $this->assertTags($result, $expected);
  3462. }
  3463. /**
  3464. * Test radio inputs with between as string or array. Also ensure
  3465. * that an array with less between elements works.
  3466. *
  3467. * @return void
  3468. */
  3469. public function testRadioBetween() {
  3470. $result = $this->Form->radio(
  3471. 'Model.field',
  3472. array('option A', 'option B'),
  3473. array('between' => 'I am between')
  3474. );
  3475. $expected = array(
  3476. 'fieldset' => array(),
  3477. 'legend' => array(),
  3478. 'Field',
  3479. '/legend',
  3480. 'I am between',
  3481. 'input' => array(
  3482. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3483. 'value' => '', 'id' => 'ModelField_'
  3484. ),
  3485. array('input' => array(
  3486. 'type' => 'radio', 'name' => 'data[Model][field]',
  3487. 'value' => '0', 'id' => 'ModelField0'
  3488. )),
  3489. array('label' => array('for' => 'ModelField0')),
  3490. 'option A',
  3491. '/label',
  3492. array('input' => array(
  3493. 'type' => 'radio', 'name' => 'data[Model][field]',
  3494. 'value' => '1', 'id' => 'ModelField1'
  3495. )),
  3496. array('label' => array('for' => 'ModelField1')),
  3497. 'option B',
  3498. '/label',
  3499. '/fieldset'
  3500. );
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->radio(
  3503. 'Model.field',
  3504. array('option A', 'option B', 'option C'),
  3505. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3506. );
  3507. $expected = array(
  3508. 'fieldset' => array(),
  3509. 'legend' => array(),
  3510. 'Field',
  3511. '/legend',
  3512. 'input' => array(
  3513. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3514. 'value' => '', 'id' => 'ModelField_'
  3515. ),
  3516. array('input' => array(
  3517. 'type' => 'radio', 'name' => 'data[Model][field]',
  3518. 'value' => '0', 'id' => 'ModelField0'
  3519. )),
  3520. array('label' => array('for' => 'ModelField0')),
  3521. 'option A',
  3522. '/label',
  3523. 'between A',
  3524. '--separator--',
  3525. array('input' => array(
  3526. 'type' => 'radio', 'name' => 'data[Model][field]',
  3527. 'value' => '1', 'id' => 'ModelField1'
  3528. )),
  3529. array('label' => array('for' => 'ModelField1')),
  3530. 'option B',
  3531. '/label',
  3532. 'between B',
  3533. '--separator--',
  3534. array('input' => array(
  3535. 'type' => 'radio', 'name' => 'data[Model][field]',
  3536. 'value' => '2', 'id' => 'ModelField2'
  3537. )),
  3538. array('label' => array('for' => 'ModelField2')),
  3539. 'option C',
  3540. '/label',
  3541. 'between C',
  3542. '/fieldset'
  3543. );
  3544. $this->assertTags($result, $expected);
  3545. $result = $this->Form->input('Model.field', array(
  3546. 'options' => array('1' => 'first', '2' => 'second'),
  3547. 'type' => 'radio',
  3548. 'before' => '--before--',
  3549. 'after' => '--after--',
  3550. 'separator' => '--separator--',
  3551. 'between' => array('--between first--', '--between second--')
  3552. ));
  3553. $expected = array(
  3554. 'div' => array('class' => 'input radio'),
  3555. '--before--',
  3556. 'fieldset' => array(),
  3557. 'legend' => array(),
  3558. 'Field',
  3559. '/legend',
  3560. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3561. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3562. array('label' => array('for' => 'ModelField1')),
  3563. 'first',
  3564. '/label',
  3565. '--between first--',
  3566. '--separator--',
  3567. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3568. array('label' => array('for' => 'ModelField2')),
  3569. 'second',
  3570. '/label',
  3571. '--between second--',
  3572. '/fieldset',
  3573. '--after--',
  3574. '/div'
  3575. );
  3576. $this->assertTags($result, $expected);
  3577. $result = $this->Form->input('Model.field', array(
  3578. 'options' => array('1' => 'first', '2' => 'second'),
  3579. 'type' => 'radio',
  3580. 'before' => '--before--',
  3581. 'after' => '--after--',
  3582. 'separator' => '--separator--',
  3583. 'between' => array('--between first--')
  3584. ));
  3585. $expected = array(
  3586. 'div' => array('class' => 'input radio'),
  3587. '--before--',
  3588. 'fieldset' => array(),
  3589. 'legend' => array(),
  3590. 'Field',
  3591. '/legend',
  3592. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3593. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3594. array('label' => array('for' => 'ModelField1')),
  3595. 'first',
  3596. '/label',
  3597. '--between first--',
  3598. '--separator--',
  3599. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3600. array('label' => array('for' => 'ModelField2')),
  3601. 'second',
  3602. '/label',
  3603. '/fieldset',
  3604. '--after--',
  3605. '/div'
  3606. );
  3607. $this->assertTags($result, $expected);
  3608. }
  3609. /**
  3610. * Test that radios with a 0 value are selected under the correct conditions.
  3611. * Also ensure that values that are booleanish are handled correctly.
  3612. *
  3613. * @return void
  3614. */
  3615. public function testRadioOptionWithBooleanishValues() {
  3616. $expected = array(
  3617. 'fieldset' => array(),
  3618. 'legend' => array(),
  3619. 'Field',
  3620. '/legend',
  3621. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3622. array('label' => array('for' => 'ModelField1')),
  3623. 'Yes',
  3624. '/label',
  3625. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3626. array('label' => array('for' => 'ModelField0')),
  3627. 'No',
  3628. '/label',
  3629. '/fieldset'
  3630. );
  3631. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3632. $this->assertTags($result, $expected);
  3633. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3634. $this->assertTags($result, $expected);
  3635. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3636. $this->assertTags($result, $expected);
  3637. $expected = array(
  3638. 'fieldset' => array(),
  3639. 'legend' => array(),
  3640. 'Field',
  3641. '/legend',
  3642. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3643. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3644. array('label' => array('for' => 'ModelField1')),
  3645. 'Yes',
  3646. '/label',
  3647. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3648. array('label' => array('for' => 'ModelField0')),
  3649. 'No',
  3650. '/label',
  3651. '/fieldset'
  3652. );
  3653. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3654. $this->assertTags($result, $expected);
  3655. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3656. $this->assertTags($result, $expected);
  3657. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3658. $this->assertTags($result, $expected);
  3659. $expected = array(
  3660. 'fieldset' => array(),
  3661. 'legend' => array(),
  3662. 'Field',
  3663. '/legend',
  3664. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3665. array('label' => array('for' => 'ModelField1')),
  3666. 'Yes',
  3667. '/label',
  3668. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3669. array('label' => array('for' => 'ModelField0')),
  3670. 'No',
  3671. '/label',
  3672. '/fieldset'
  3673. );
  3674. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3675. $this->assertTags($result, $expected);
  3676. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3677. $this->assertTags($result, $expected);
  3678. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3679. $this->assertTags($result, $expected);
  3680. }
  3681. /**
  3682. * test disabled radio options
  3683. *
  3684. * @return void
  3685. */
  3686. public function testRadioDisabled() {
  3687. $result = $this->Form->radio(
  3688. 'Model.field',
  3689. array('option A', 'option B'),
  3690. array('disabled' => array(0), 'value' => '0')
  3691. );
  3692. $expected = array(
  3693. 'fieldset' => array(),
  3694. 'legend' => array(),
  3695. 'Field',
  3696. '/legend',
  3697. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3698. array('label' => array('for' => 'ModelField0')),
  3699. 'option A',
  3700. '/label',
  3701. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3702. array('label' => array('for' => 'ModelField1')),
  3703. 'option B',
  3704. '/label',
  3705. '/fieldset'
  3706. );
  3707. $this->assertTags($result, $expected);
  3708. $result = $this->Form->radio(
  3709. 'Model.field',
  3710. array('option A', 'option B'),
  3711. array('disabled' => true, 'value' => '0')
  3712. );
  3713. $expected = array(
  3714. 'fieldset' => array(),
  3715. 'legend' => array(),
  3716. 'Field',
  3717. '/legend',
  3718. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3719. array('label' => array('for' => 'ModelField0')),
  3720. 'option A',
  3721. '/label',
  3722. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3723. array('label' => array('for' => 'ModelField1')),
  3724. 'option B',
  3725. '/label',
  3726. '/fieldset'
  3727. );
  3728. $this->assertTags($result, $expected);
  3729. $result = $this->Form->radio(
  3730. 'Model.field',
  3731. array('option A', 'option B'),
  3732. array('disabled' => 'disabled', 'value' => '0')
  3733. );
  3734. $expected = array(
  3735. 'fieldset' => array(),
  3736. 'legend' => array(),
  3737. 'Field',
  3738. '/legend',
  3739. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3740. array('label' => array('for' => 'ModelField0')),
  3741. 'option A',
  3742. '/label',
  3743. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3744. array('label' => array('for' => 'ModelField1')),
  3745. 'option B',
  3746. '/label',
  3747. '/fieldset'
  3748. );
  3749. $this->assertTags($result, $expected);
  3750. $result = $this->Form->input('Model.field', array(
  3751. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3752. 'type' => 'radio',
  3753. 'disabled' => array(2, '3x'),
  3754. ));
  3755. $expected = array(
  3756. 'div' => array('class' => 'input radio'),
  3757. 'fieldset' => array(),
  3758. 'legend' => array(),
  3759. 'Field',
  3760. '/legend',
  3761. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3762. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3763. array('label' => array('for' => 'ModelField1')),
  3764. 'first',
  3765. '/label',
  3766. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3767. array('label' => array('for' => 'ModelField2')),
  3768. 'second',
  3769. '/label',
  3770. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3771. array('label' => array('for' => 'ModelField2x')),
  3772. '2x',
  3773. '/label',
  3774. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '3', 'id' => 'ModelField3')),
  3775. array('label' => array('for' => 'ModelField3')),
  3776. 'third',
  3777. '/label',
  3778. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3779. array('label' => array('for' => 'ModelField3x')),
  3780. '3x',
  3781. '/label',
  3782. '/fieldset',
  3783. '/div'
  3784. );
  3785. $this->assertTags($result, $expected);
  3786. $result = $this->Form->input('Model.field', array(
  3787. 'type' => 'radio',
  3788. 'options' => array(
  3789. 1 => 'A',
  3790. 2 => 'B',
  3791. 3 => 'C'
  3792. ),
  3793. 'disabled' => array(1)
  3794. ));
  3795. $expected = array(
  3796. 'div' => array('class' => 'input radio'),
  3797. 'fieldset' => array(),
  3798. 'legend' => array(),
  3799. 'Field',
  3800. '/legend',
  3801. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3802. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField1', 'disabled' => 'disabled', 'value' => '1')),
  3803. array('label' => array('for' => 'ModelField1')),
  3804. 'A',
  3805. '/label',
  3806. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField2', 'value' => '2')),
  3807. array('label' => array('for' => 'ModelField2')),
  3808. 'B',
  3809. '/label',
  3810. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'id' => 'ModelField3', 'value' => '3')),
  3811. array('label' => array('for' => 'ModelField3')),
  3812. 'C',
  3813. '/label',
  3814. '/fieldset',
  3815. '/div'
  3816. );
  3817. $this->assertTags($result, $expected);
  3818. }
  3819. /**
  3820. * test disabling the hidden input for radio buttons
  3821. *
  3822. * @return void
  3823. */
  3824. public function testRadioHiddenInputDisabling() {
  3825. $result = $this->Form->input('Model.1.field', array(
  3826. 'type' => 'radio',
  3827. 'options' => array('option A'),
  3828. 'hiddenField' => false
  3829. )
  3830. );
  3831. $expected = array(
  3832. 'div' => array('class' => 'input radio'),
  3833. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3834. 'label' => array('for' => 'Model1Field0'),
  3835. 'option A',
  3836. '/label',
  3837. '/div'
  3838. );
  3839. $this->assertTags($result, $expected);
  3840. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3841. $expected = array(
  3842. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3843. 'label' => array('for' => 'Model1Field0'),
  3844. 'option A',
  3845. '/label'
  3846. );
  3847. $this->assertTags($result, $expected);
  3848. }
  3849. /**
  3850. * test adding an empty option for radio buttons
  3851. *
  3852. * @return void
  3853. */
  3854. public function testRadioAddEmptyOption() {
  3855. $result = $this->Form->input('Model.1.field', array(
  3856. 'type' => 'radio',
  3857. 'options' => array('option A'),
  3858. 'empty' => true,
  3859. 'hiddenField' => false
  3860. ));
  3861. $expected = array(
  3862. 'div' => array('class' => 'input radio'),
  3863. 'fieldset' => array(),
  3864. 'legend' => array(),
  3865. 'Field',
  3866. '/legend',
  3867. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3868. array('label' => array('for' => 'Model1Field')),
  3869. __('empty'),
  3870. '/label',
  3871. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3872. array('label' => array('for' => 'Model1Field0')),
  3873. 'option A',
  3874. '/label',
  3875. '/fieldset',
  3876. '/div'
  3877. );
  3878. $this->assertTags($result, $expected);
  3879. $result = $this->Form->input('Model.1.field', array(
  3880. 'type' => 'radio',
  3881. 'options' => array('option A'),
  3882. 'empty' => 'CustomEmptyLabel',
  3883. 'hiddenField' => false
  3884. ));
  3885. $expected = array(
  3886. 'div' => array('class' => 'input radio'),
  3887. 'fieldset' => array(),
  3888. 'legend' => array(),
  3889. 'Field',
  3890. '/legend',
  3891. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3892. array('label' => array('for' => 'Model1Field')),
  3893. 'CustomEmptyLabel',
  3894. '/label',
  3895. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3896. array('label' => array('for' => 'Model1Field0')),
  3897. 'option A',
  3898. '/label',
  3899. '/fieldset',
  3900. '/div'
  3901. );
  3902. $this->assertTags($result, $expected);
  3903. $result = $this->Form->input('Model.1.field', array(
  3904. 'type' => 'radio',
  3905. 'options' => array('option A'),
  3906. 'empty' => false,
  3907. 'hiddenField' => false
  3908. ));
  3909. $this->assertTextNotContains('"Model1Field"', $result);
  3910. }
  3911. /**
  3912. * Test that radio() accepts an array for label
  3913. *
  3914. * @return void
  3915. */
  3916. public function testRadioLabelArray() {
  3917. $result = $this->Form->input('Model.field', array(
  3918. 'type' => 'radio',
  3919. 'legend' => false,
  3920. 'label' => array(
  3921. 'class' => 'checkbox float-left',
  3922. ),
  3923. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3924. ));
  3925. $this->assertTextContains(
  3926. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3927. $result
  3928. );
  3929. }
  3930. /**
  3931. * Test that label id's match the input element id's when radio is called after create().
  3932. *
  3933. * @return void
  3934. */
  3935. public function testRadioWithCreate() {
  3936. $this->Form->create('Model');
  3937. $result = $this->Form->radio('recipient',
  3938. array('1' => '1', '2' => '2', '3' => '3'),
  3939. array('legend' => false, 'value' => '1')
  3940. );
  3941. $this->assertTextNotContains(
  3942. '<label for="ModelModelRecipient1">1</label>',
  3943. $result
  3944. );
  3945. $this->assertTextContains(
  3946. '<label for="ModelRecipient1">1</label>',
  3947. $result
  3948. );
  3949. }
  3950. /**
  3951. * testDomIdSuffix method
  3952. *
  3953. * @return void
  3954. */
  3955. public function testDomIdSuffix() {
  3956. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3957. $this->assertEquals('1StringWith1DollarSigns', $result);
  3958. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3959. $this->assertEquals('AbcXFooYBar', $result);
  3960. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'html5');
  3961. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3962. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'html5');
  3963. $this->assertEquals('AbcX=FooY=Bar', $result);
  3964. }
  3965. /**
  3966. * testDomIdSuffixCollisionResolvement()
  3967. *
  3968. * @return void
  3969. */
  3970. public function testDomIdSuffixCollisionResolvement() {
  3971. $result = $this->Form->domIdSuffix('a>b');
  3972. $this->assertEquals('AB', $result);
  3973. $result = $this->Form->domIdSuffix('a<b');
  3974. $this->assertEquals('AB1', $result);
  3975. $result = $this->Form->domIdSuffix('a\'b');
  3976. $this->assertEquals('AB2', $result);
  3977. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3978. $this->assertEquals('1StringWith1Dollar', $result);
  3979. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3980. $this->assertEquals('1StringWith1Dollar1', $result);
  3981. $result = $this->Form->domIdSuffix('1 string with 1$-dollar');
  3982. $this->assertEquals('1StringWith1Dollar2', $result);
  3983. }
  3984. /**
  3985. * testSelect method
  3986. *
  3987. * Test select element generation.
  3988. *
  3989. * @return void
  3990. */
  3991. public function testSelect() {
  3992. $result = $this->Form->select('Model.field', array());
  3993. $expected = array(
  3994. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3995. array('option' => array('value' => '')),
  3996. '/option',
  3997. '/select'
  3998. );
  3999. $this->assertTags($result, $expected);
  4000. $this->Form->request->data = array('Model' => array('field' => 'value'));
  4001. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  4002. $expected = array(
  4003. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4004. array('option' => array('value' => '')),
  4005. '/option',
  4006. array('option' => array('value' => 'value', 'selected' => 'selected')),
  4007. 'good',
  4008. '/option',
  4009. array('option' => array('value' => 'other')),
  4010. 'bad',
  4011. '/option',
  4012. '/select'
  4013. );
  4014. $this->assertTags($result, $expected);
  4015. $this->Form->request->data = array();
  4016. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  4017. $expected = array(
  4018. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4019. array('option' => array('value' => '')),
  4020. '/option',
  4021. array('option' => array('value' => 'value')),
  4022. 'good',
  4023. '/option',
  4024. array('option' => array('value' => 'other')),
  4025. 'bad',
  4026. '/option',
  4027. '/select'
  4028. );
  4029. $this->assertTags($result, $expected);
  4030. $result = $this->Form->select(
  4031. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  4032. array('empty' => false)
  4033. );
  4034. $expected = array(
  4035. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4036. array('option' => array('value' => 'first')),
  4037. 'first &quot;html&quot; &lt;chars&gt;',
  4038. '/option',
  4039. array('option' => array('value' => 'second')),
  4040. 'value',
  4041. '/option',
  4042. '/select'
  4043. );
  4044. $this->assertTags($result, $expected);
  4045. $result = $this->Form->select(
  4046. 'Model.field',
  4047. array('first' => 'first "html" <chars>', 'second' => 'value'),
  4048. array('escape' => false, 'empty' => false)
  4049. );
  4050. $expected = array(
  4051. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4052. array('option' => array('value' => 'first')),
  4053. 'first "html" <chars>',
  4054. '/option',
  4055. array('option' => array('value' => 'second')),
  4056. 'value',
  4057. '/option',
  4058. '/select'
  4059. );
  4060. $this->assertTags($result, $expected);
  4061. $options = array(
  4062. array('value' => 'first', 'name' => 'First'),
  4063. array('value' => 'first', 'name' => 'Another First'),
  4064. );
  4065. $result = $this->Form->select(
  4066. 'Model.field',
  4067. $options,
  4068. array('escape' => false, 'empty' => false)
  4069. );
  4070. $expected = array(
  4071. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4072. array('option' => array('value' => 'first')),
  4073. 'First',
  4074. '/option',
  4075. array('option' => array('value' => 'first')),
  4076. 'Another First',
  4077. '/option',
  4078. '/select'
  4079. );
  4080. $this->assertTags($result, $expected);
  4081. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  4082. $result = $this->Form->select(
  4083. 'Model.contact_id',
  4084. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4085. array('escape' => false, 'empty' => 'pick something')
  4086. );
  4087. $expected = array(
  4088. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  4089. array('option' => array('value' => '')), 'pick something', '/option',
  4090. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4091. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4092. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4093. '/select'
  4094. );
  4095. $this->assertTags($result, $expected);
  4096. $this->Form->request->data['Model']['field'] = 0;
  4097. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4098. $expected = array(
  4099. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4100. array('option' => array('value' => '')), '/option',
  4101. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4102. array('option' => array('value' => '1')), 'Yes', '/option',
  4103. '/select'
  4104. );
  4105. $this->assertTags($result, $expected);
  4106. $this->Form->request->data['Model']['field'] = 50;
  4107. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  4108. $expected = array(
  4109. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4110. array('option' => array('value' => '')), '/option',
  4111. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  4112. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  4113. '/select'
  4114. );
  4115. $this->assertTags($result, $expected);
  4116. $result = $this->Form->select('Contact.required_one', array('option A'));
  4117. $expected = array(
  4118. 'select' => array(
  4119. 'name' => 'data[Contact][required_one]',
  4120. 'id' => 'ContactRequiredOne',
  4121. 'required' => 'required'
  4122. ),
  4123. array('option' => array('value' => '')), '/option',
  4124. array('option' => array('value' => '0')), 'option A', '/option',
  4125. '/select'
  4126. );
  4127. $this->assertTags($result, $expected);
  4128. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  4129. $expected = array(
  4130. 'select' => array(
  4131. 'name' => 'data[Contact][required_one]',
  4132. 'id' => 'ContactRequiredOne',
  4133. 'disabled' => 'disabled'
  4134. ),
  4135. array('option' => array('value' => '')), '/option',
  4136. array('option' => array('value' => '0')), 'option A', '/option',
  4137. '/select'
  4138. );
  4139. $this->assertTags($result, $expected);
  4140. }
  4141. /**
  4142. * test that select() with optiongroups listens to the escape param.
  4143. *
  4144. * @return void
  4145. */
  4146. public function testSelectOptionGroupEscaping() {
  4147. $options = array(
  4148. '>< Key' => array(
  4149. 1 => 'One',
  4150. 2 => 'Two'
  4151. )
  4152. );
  4153. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  4154. $expected = array(
  4155. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4156. 'optgroup' => array('label' => '&gt;&lt; Key'),
  4157. array('option' => array('value' => '1')), 'One', '/option',
  4158. array('option' => array('value' => '2')), 'Two', '/option',
  4159. '/optgroup',
  4160. '/select'
  4161. );
  4162. $this->assertTags($result, $expected);
  4163. $options = array(
  4164. '>< Key' => array(
  4165. 1 => 'One',
  4166. 2 => 'Two'
  4167. )
  4168. );
  4169. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  4170. $expected = array(
  4171. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4172. 'optgroup' => array('label' => '>< Key'),
  4173. array('option' => array('value' => '1')), 'One', '/option',
  4174. array('option' => array('value' => '2')), 'Two', '/option',
  4175. '/optgroup',
  4176. '/select'
  4177. );
  4178. $this->assertTags($result, $expected);
  4179. }
  4180. /**
  4181. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  4182. *
  4183. * @return void
  4184. */
  4185. public function testSelectWithNullAttributes() {
  4186. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  4187. $expected = array(
  4188. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4189. array('option' => array('value' => '0')),
  4190. 'first',
  4191. '/option',
  4192. array('option' => array('value' => '1')),
  4193. 'second',
  4194. '/option',
  4195. '/select'
  4196. );
  4197. $this->assertTags($result, $expected);
  4198. }
  4199. /**
  4200. * testNestedSelect method
  4201. *
  4202. * test select element generation with optgroups
  4203. *
  4204. * @return void
  4205. */
  4206. public function testNestedSelect() {
  4207. $result = $this->Form->select(
  4208. 'Model.field',
  4209. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4210. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4211. )), array('empty' => false)
  4212. );
  4213. $expected = array(
  4214. 'select' => array('name' => 'data[Model][field]',
  4215. 'id' => 'ModelField'),
  4216. array('option' => array('value' => 1)),
  4217. 'One',
  4218. '/option',
  4219. array('option' => array('value' => 2)),
  4220. 'Two',
  4221. '/option',
  4222. array('optgroup' => array('label' => 'Three')),
  4223. array('option' => array('value' => 4)),
  4224. 'Four',
  4225. '/option',
  4226. array('option' => array('value' => 5)),
  4227. 'Five',
  4228. '/option',
  4229. '/optgroup',
  4230. '/select'
  4231. );
  4232. $this->assertTags($result, $expected);
  4233. $result = $this->Form->select(
  4234. 'Model.field',
  4235. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  4236. array('showParents' => true, 'empty' => false)
  4237. );
  4238. $expected = array(
  4239. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  4240. array('option' => array('value' => 1)),
  4241. 'One',
  4242. '/option',
  4243. array('option' => array('value' => 2)),
  4244. 'Two',
  4245. '/option',
  4246. array('optgroup' => array('label' => 'Three')),
  4247. array('option' => array('value' => 3)),
  4248. 'Three',
  4249. '/option',
  4250. array('option' => array('value' => 4)),
  4251. 'Four',
  4252. '/option',
  4253. '/optgroup',
  4254. '/select'
  4255. );
  4256. $this->assertTags($result, $expected);
  4257. }
  4258. /**
  4259. * testSelectMultiple method
  4260. *
  4261. * test generation of multiple select elements
  4262. *
  4263. * @return void
  4264. */
  4265. public function testSelectMultiple() {
  4266. $options = array('first', 'second', 'third');
  4267. $result = $this->Form->select(
  4268. 'Model.multi_field', $options, array('multiple' => true)
  4269. );
  4270. $expected = array(
  4271. 'input' => array(
  4272. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4273. ),
  4274. 'select' => array(
  4275. 'name' => 'data[Model][multi_field][]',
  4276. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4277. ),
  4278. array('option' => array('value' => '0')),
  4279. 'first',
  4280. '/option',
  4281. array('option' => array('value' => '1')),
  4282. 'second',
  4283. '/option',
  4284. array('option' => array('value' => '2')),
  4285. 'third',
  4286. '/option',
  4287. '/select'
  4288. );
  4289. $this->assertTags($result, $expected);
  4290. $result = $this->Form->select(
  4291. 'Model.multi_field', $options, array('form' => 'my-form', 'multiple' => 'multiple')
  4292. );
  4293. $expected = array(
  4294. 'input' => array(
  4295. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_',
  4296. 'form' => 'my-form',
  4297. ),
  4298. 'select' => array(
  4299. 'name' => 'data[Model][multi_field][]',
  4300. 'id' => 'ModelMultiField',
  4301. 'multiple' => 'multiple',
  4302. 'form' => 'my-form',
  4303. ),
  4304. array('option' => array('value' => '0')),
  4305. 'first',
  4306. '/option',
  4307. array('option' => array('value' => '1')),
  4308. 'second',
  4309. '/option',
  4310. array('option' => array('value' => '2')),
  4311. 'third',
  4312. '/option',
  4313. '/select'
  4314. );
  4315. $this->assertTags($result, $expected);
  4316. $result = $this->Form->select(
  4317. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4318. );
  4319. $expected = array(
  4320. 'input' => array(
  4321. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4322. ),
  4323. 'select' => array(
  4324. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  4325. 'multiple' => 'multiple'
  4326. ),
  4327. array('option' => array('value' => '0', 'selected' => 'selected')),
  4328. 'first',
  4329. '/option',
  4330. array('option' => array('value' => '1', 'selected' => 'selected')),
  4331. 'second',
  4332. '/option',
  4333. array('option' => array('value' => '2')),
  4334. 'third',
  4335. '/option',
  4336. '/select'
  4337. );
  4338. $this->assertTags($result, $expected);
  4339. $result = $this->Form->select(
  4340. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4341. );
  4342. $expected = array(
  4343. 'select' => array(
  4344. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  4345. ),
  4346. array('option' => array('value' => '0', 'selected' => 'selected')),
  4347. 'first',
  4348. '/option',
  4349. array('option' => array('value' => '1', 'selected' => 'selected')),
  4350. 'second',
  4351. '/option',
  4352. array('option' => array('value' => '2')),
  4353. 'third',
  4354. '/option',
  4355. '/select'
  4356. );
  4357. $this->assertTags($result, $expected);
  4358. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4359. $selected = array('2', '3x');
  4360. $result = $this->Form->select(
  4361. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4362. );
  4363. $expected = array(
  4364. 'input' => array(
  4365. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4366. ),
  4367. 'select' => array(
  4368. 'name' => 'data[Model][multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4369. ),
  4370. array('option' => array('value' => '1')),
  4371. 'One',
  4372. '/option',
  4373. array('option' => array('value' => '2', 'selected' => 'selected')),
  4374. 'Two',
  4375. '/option',
  4376. array('option' => array('value' => '3')),
  4377. 'Three',
  4378. '/option',
  4379. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4380. 'Stringy',
  4381. '/option',
  4382. '/select'
  4383. );
  4384. $this->assertTags($result, $expected);
  4385. $result = $this->Form->select('Contact.required_one', array(
  4386. '1' => 'option A',
  4387. '2' => 'option B'
  4388. ), array('multiple' => true));
  4389. $expected = array(
  4390. 'input' => array(
  4391. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4392. ),
  4393. 'select' => array(
  4394. 'name' => 'data[Contact][required_one][]',
  4395. 'id' => 'ContactRequiredOne',
  4396. 'required' => 'required',
  4397. 'multiple' => 'multiple'
  4398. ),
  4399. array('option' => array('value' => '1')), 'option A', '/option',
  4400. array('option' => array('value' => '2')), 'option B', '/option',
  4401. '/select'
  4402. );
  4403. $this->assertTags($result, $expected);
  4404. $result = $this->Form->select(
  4405. 'Model.multi_field',
  4406. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4407. array('multiple' => true)
  4408. );
  4409. $expected = array(
  4410. 'input' => array(
  4411. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '',
  4412. 'id' => 'ModelMultiField_'
  4413. ),
  4414. array('select' => array('name' => 'data[Model][multi_field][]',
  4415. 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4416. )),
  4417. array('option' => array('value' => 'a&gt;b')),
  4418. 'first',
  4419. '/option',
  4420. array('option' => array('value' => 'a&lt;b')),
  4421. 'second',
  4422. '/option',
  4423. array('option' => array(
  4424. 'value' => 'a&quot;b'
  4425. )),
  4426. 'third',
  4427. '/option',
  4428. '/select'
  4429. );
  4430. $this->assertTags($result, $expected);
  4431. }
  4432. /**
  4433. * Test generating multiple select with disabled elements.
  4434. *
  4435. * @return void
  4436. */
  4437. public function testSelectMultipleWithDisabledElements() {
  4438. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4439. $disabled = array(2, 3);
  4440. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4441. $expected = array(
  4442. 'input' => array(
  4443. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4444. ),
  4445. 'select' => array(
  4446. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4447. ),
  4448. array('option' => array('value' => '1')),
  4449. 'One',
  4450. '/option',
  4451. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4452. 'Two',
  4453. '/option',
  4454. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4455. 'Three',
  4456. '/option',
  4457. array('option' => array('value' => '3x')),
  4458. 'Stringy',
  4459. '/option',
  4460. '/select'
  4461. );
  4462. $this->assertTags($result, $expected);
  4463. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4464. $disabled = array('2', '3x');
  4465. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4466. $expected = array(
  4467. array('div' => array('class' => 'input select')),
  4468. array('label' => array('for' => 'ContactMultiple')),
  4469. 'Multiple',
  4470. '/label',
  4471. 'input' => array(
  4472. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4473. ),
  4474. 'select' => array(
  4475. 'name' => 'data[Contact][multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4476. ),
  4477. array('option' => array('value' => '1')),
  4478. 'One',
  4479. '/option',
  4480. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4481. 'Two',
  4482. '/option',
  4483. array('option' => array('value' => '3')),
  4484. 'Three',
  4485. '/option',
  4486. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4487. 'Stringy',
  4488. '/option',
  4489. '/select',
  4490. '/div'
  4491. );
  4492. $this->assertTags($result, $expected);
  4493. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4494. $disabled = true;
  4495. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4496. $expected = array(
  4497. array('div' => array('class' => 'input select')),
  4498. array('label' => array('for' => 'ContactMultiple')),
  4499. 'Multiple',
  4500. '/label',
  4501. 'input' => array(
  4502. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4503. ),
  4504. 'select' => array(
  4505. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4506. ),
  4507. array('option' => array('value' => '1')),
  4508. 'One',
  4509. '/option',
  4510. array('option' => array('value' => '2')),
  4511. 'Two',
  4512. '/option',
  4513. array('option' => array('value' => '3')),
  4514. 'Three',
  4515. '/option',
  4516. array('option' => array('value' => '3x')),
  4517. 'Stringy',
  4518. '/option',
  4519. '/select',
  4520. '/div'
  4521. );
  4522. $this->assertTags($result, $expected);
  4523. }
  4524. /**
  4525. * Test generating select with disabled elements.
  4526. *
  4527. * @return void
  4528. */
  4529. public function testSelectWithDisabledElements() {
  4530. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4531. $disabled = array(2, 3);
  4532. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4533. $expected = array(
  4534. 'select' => array(
  4535. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4536. ),
  4537. array('option' => array('value' => '')),
  4538. '/option',
  4539. array('option' => array('value' => '1')),
  4540. 'One',
  4541. '/option',
  4542. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4543. 'Two',
  4544. '/option',
  4545. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4546. 'Three',
  4547. '/option',
  4548. array('option' => array('value' => '3x')),
  4549. 'Stringy',
  4550. '/option',
  4551. '/select'
  4552. );
  4553. $this->assertTags($result, $expected);
  4554. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4555. $disabled = array('2', '3x');
  4556. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4557. $expected = array(
  4558. array('div' => array('class' => 'input select')),
  4559. array('label' => array('for' => 'ModelField')),
  4560. 'Field',
  4561. '/label',
  4562. 'select' => array(
  4563. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4564. ),
  4565. array('option' => array('value' => '1')),
  4566. 'One',
  4567. '/option',
  4568. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4569. 'Two',
  4570. '/option',
  4571. array('option' => array('value' => '3')),
  4572. 'Three',
  4573. '/option',
  4574. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4575. 'Stringy',
  4576. '/option',
  4577. '/select',
  4578. '/div'
  4579. );
  4580. $this->assertTags($result, $expected);
  4581. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4582. $disabled = true;
  4583. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4584. $expected = array(
  4585. array('div' => array('class' => 'input select')),
  4586. array('label' => array('for' => 'ModelField')),
  4587. 'Field',
  4588. '/label',
  4589. 'select' => array(
  4590. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4591. ),
  4592. array('option' => array('value' => '1')),
  4593. 'One',
  4594. '/option',
  4595. array('option' => array('value' => '2')),
  4596. 'Two',
  4597. '/option',
  4598. array('option' => array('value' => '3')),
  4599. 'Three',
  4600. '/option',
  4601. array('option' => array('value' => '3x')),
  4602. 'Stringy',
  4603. '/option',
  4604. '/select',
  4605. '/div'
  4606. );
  4607. $this->assertTags($result, $expected);
  4608. }
  4609. /**
  4610. * test generation of habtm select boxes.
  4611. *
  4612. * @return void
  4613. */
  4614. public function testHabtmSelectBox() {
  4615. $this->View->viewVars['contactTags'] = array(
  4616. 1 => 'blue',
  4617. 2 => 'red',
  4618. 3 => 'green'
  4619. );
  4620. $this->Form->request->data = array(
  4621. 'Contact' => array(),
  4622. 'ContactTag' => array(
  4623. array(
  4624. 'id' => '1',
  4625. 'name' => 'blue'
  4626. ),
  4627. array(
  4628. 'id' => 3,
  4629. 'name' => 'green'
  4630. )
  4631. )
  4632. );
  4633. $this->Form->create('Contact');
  4634. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4635. $expected = array(
  4636. 'input' => array(
  4637. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4638. ),
  4639. 'select' => array(
  4640. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4641. 'multiple' => 'multiple'
  4642. ),
  4643. array('option' => array('value' => '1', 'selected' => 'selected')),
  4644. 'blue',
  4645. '/option',
  4646. array('option' => array('value' => '2')),
  4647. 'red',
  4648. '/option',
  4649. array('option' => array('value' => '3', 'selected' => 'selected')),
  4650. 'green',
  4651. '/option',
  4652. '/select'
  4653. );
  4654. $this->assertTags($result, $expected);
  4655. // make sure only 50 is selected, and not 50f5c0cf
  4656. $this->View->viewVars['contactTags'] = array(
  4657. '1' => 'blue',
  4658. '50f5c0cf' => 'red',
  4659. '50' => 'green'
  4660. );
  4661. $this->Form->request->data = array(
  4662. 'Contact' => array(),
  4663. 'ContactTag' => array(
  4664. array(
  4665. 'id' => 1,
  4666. 'name' => 'blue'
  4667. ),
  4668. array(
  4669. 'id' => 50,
  4670. 'name' => 'green'
  4671. )
  4672. )
  4673. );
  4674. $this->Form->create('Contact');
  4675. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4676. $expected = array(
  4677. 'input' => array(
  4678. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4679. ),
  4680. 'select' => array(
  4681. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  4682. 'multiple' => 'multiple'
  4683. ),
  4684. array('option' => array('value' => '1', 'selected' => 'selected')),
  4685. 'blue',
  4686. '/option',
  4687. array('option' => array('value' => '50f5c0cf')),
  4688. 'red',
  4689. '/option',
  4690. array('option' => array('value' => '50', 'selected' => 'selected')),
  4691. 'green',
  4692. '/option',
  4693. '/select'
  4694. );
  4695. $this->assertTags($result, $expected);
  4696. }
  4697. /**
  4698. * test generation of multi select elements in checkbox format
  4699. *
  4700. * @return void
  4701. */
  4702. public function testSelectMultipleCheckboxes() {
  4703. $result = $this->Form->select(
  4704. 'Model.multi_field',
  4705. array('first', 'second', 'third'),
  4706. array('multiple' => 'checkbox')
  4707. );
  4708. $expected = array(
  4709. 'input' => array(
  4710. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4711. ),
  4712. array('div' => array('class' => 'checkbox')),
  4713. array('input' => array(
  4714. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4715. 'value' => '0', 'id' => 'ModelMultiField0'
  4716. )),
  4717. array('label' => array('for' => 'ModelMultiField0')),
  4718. 'first',
  4719. '/label',
  4720. '/div',
  4721. array('div' => array('class' => 'checkbox')),
  4722. array('input' => array(
  4723. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4724. 'value' => '1', 'id' => 'ModelMultiField1'
  4725. )),
  4726. array('label' => array('for' => 'ModelMultiField1')),
  4727. 'second',
  4728. '/label',
  4729. '/div',
  4730. array('div' => array('class' => 'checkbox')),
  4731. array('input' => array(
  4732. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4733. 'value' => '2', 'id' => 'ModelMultiField2'
  4734. )),
  4735. array('label' => array('for' => 'ModelMultiField2')),
  4736. 'third',
  4737. '/label',
  4738. '/div'
  4739. );
  4740. $this->assertTags($result, $expected);
  4741. $result = $this->Form->select(
  4742. 'Model.multi_field',
  4743. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4744. array('multiple' => 'checkbox')
  4745. );
  4746. $expected = array(
  4747. 'input' => array(
  4748. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4749. ),
  4750. array('div' => array('class' => 'checkbox')),
  4751. array('input' => array(
  4752. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4753. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4754. )),
  4755. array('label' => array('for' => 'ModelMultiFieldA')),
  4756. 'first',
  4757. '/label',
  4758. '/div',
  4759. array('div' => array('class' => 'checkbox')),
  4760. array('input' => array(
  4761. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4762. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4763. )),
  4764. array('label' => array('for' => 'ModelMultiFieldB')),
  4765. 'second',
  4766. '/label',
  4767. '/div',
  4768. array('div' => array('class' => 'checkbox')),
  4769. array('input' => array(
  4770. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4771. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4772. )),
  4773. array('label' => array('for' => 'ModelMultiFieldC')),
  4774. 'third',
  4775. '/label',
  4776. '/div'
  4777. );
  4778. $this->assertTags($result, $expected);
  4779. $result = $this->Form->select(
  4780. 'Model.multi_field', array('1' => 'first'), 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' => '1', 'id' => 'ModelMultiField1'
  4790. )),
  4791. array('label' => array('for' => 'ModelMultiField1')),
  4792. 'first',
  4793. '/label',
  4794. '/div'
  4795. );
  4796. $this->assertTags($result, $expected);
  4797. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4798. $result = $this->Form->select(
  4799. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4800. );
  4801. $expected = array(
  4802. 'input' => array(
  4803. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4804. ),
  4805. array('div' => array('class' => 'checkbox')),
  4806. array('input' => array(
  4807. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4808. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4809. )),
  4810. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4811. 'first',
  4812. '/label',
  4813. '/div',
  4814. array('div' => array('class' => 'checkbox')),
  4815. array('input' => array(
  4816. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4817. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4818. )),
  4819. array('label' => array('for' => 'ModelTagsArray')),
  4820. 'Array',
  4821. '/label',
  4822. '/div'
  4823. );
  4824. $this->assertTags($result, $expected);
  4825. $result = $this->Form->select(
  4826. 'Model.multi_field',
  4827. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4828. array('multiple' => 'checkbox')
  4829. );
  4830. $expected = array(
  4831. 'input' => array(
  4832. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4833. ),
  4834. array('div' => array('class' => 'checkbox')),
  4835. array('input' => array(
  4836. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4837. 'value' => 'a+', 'id' => 'ModelMultiFieldA2'
  4838. )),
  4839. array('label' => array('for' => 'ModelMultiFieldA2')),
  4840. 'first',
  4841. '/label',
  4842. '/div',
  4843. array('div' => array('class' => 'checkbox')),
  4844. array('input' => array(
  4845. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4846. 'value' => 'a++', 'id' => 'ModelMultiFieldA1'
  4847. )),
  4848. array('label' => array('for' => 'ModelMultiFieldA1')),
  4849. 'second',
  4850. '/label',
  4851. '/div',
  4852. array('div' => array('class' => 'checkbox')),
  4853. array('input' => array(
  4854. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4855. 'value' => 'a+++', 'id' => 'ModelMultiFieldA'
  4856. )),
  4857. array('label' => array('for' => 'ModelMultiFieldA')),
  4858. 'third',
  4859. '/label',
  4860. '/div'
  4861. );
  4862. $this->assertTags($result, $expected);
  4863. $result = $this->Form->select(
  4864. 'Model.multi_field',
  4865. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4866. array('multiple' => 'checkbox')
  4867. );
  4868. $expected = array(
  4869. 'input' => array(
  4870. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4871. ),
  4872. array('div' => array('class' => 'checkbox')),
  4873. array('input' => array(
  4874. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4875. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4876. )),
  4877. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4878. 'first',
  4879. '/label',
  4880. '/div',
  4881. array('div' => array('class' => 'checkbox')),
  4882. array('input' => array(
  4883. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4884. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4885. )),
  4886. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4887. 'second',
  4888. '/label',
  4889. '/div',
  4890. array('div' => array('class' => 'checkbox')),
  4891. array('input' => array(
  4892. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4893. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4894. )),
  4895. array('label' => array('for' => 'ModelMultiFieldAB')),
  4896. 'third',
  4897. '/label',
  4898. '/div'
  4899. );
  4900. $this->assertTags($result, $expected);
  4901. }
  4902. /**
  4903. * test multiple checkboxes with div styles.
  4904. *
  4905. * @return void
  4906. */
  4907. public function testSelectMultipleCheckboxDiv() {
  4908. $result = $this->Form->select(
  4909. 'Model.tags',
  4910. array('first', 'second'),
  4911. array('multiple' => 'checkbox', 'class' => 'my-class')
  4912. );
  4913. $expected = array(
  4914. 'input' => array(
  4915. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4916. ),
  4917. array('div' => array('class' => 'my-class')),
  4918. array('input' => array(
  4919. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4920. 'value' => '0', 'id' => 'ModelTags0'
  4921. )),
  4922. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4923. '/div',
  4924. array('div' => array('class' => 'my-class')),
  4925. array('input' => array(
  4926. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4927. 'value' => '1', 'id' => 'ModelTags1'
  4928. )),
  4929. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4930. '/div'
  4931. );
  4932. $this->assertTags($result, $expected);
  4933. $result = $this->Form->input('Model.tags', array(
  4934. 'options' => array('first', 'second'),
  4935. 'multiple' => 'checkbox',
  4936. 'class' => 'my-class',
  4937. 'div' => false,
  4938. 'label' => false
  4939. ));
  4940. $this->assertTags($result, $expected);
  4941. $Contact = ClassRegistry::getObject('Contact');
  4942. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4943. $result = $this->Form->input('Contact.tags', array(
  4944. 'options' => array('one'),
  4945. 'multiple' => 'checkbox',
  4946. 'label' => false,
  4947. 'div' => false
  4948. ));
  4949. $expected = array(
  4950. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4951. array('div' => array('class' => 'checkbox form-error')),
  4952. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4953. array('label' => array('for' => 'ContactTags0')),
  4954. 'one',
  4955. '/label',
  4956. '/div'
  4957. );
  4958. $this->assertTags($result, $expected);
  4959. $result = $this->Form->input('Contact.tags', array(
  4960. 'options' => array('one'),
  4961. 'multiple' => 'checkbox',
  4962. 'class' => 'mycheckbox',
  4963. 'label' => false,
  4964. 'div' => false
  4965. ));
  4966. $expected = array(
  4967. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4968. array('div' => array('class' => 'mycheckbox form-error')),
  4969. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4970. array('label' => array('for' => 'ContactTags0')),
  4971. 'one',
  4972. '/label',
  4973. '/div'
  4974. );
  4975. $this->assertTags($result, $expected);
  4976. }
  4977. /**
  4978. * Checks the security hash array generated for multiple-input checkbox elements
  4979. *
  4980. * @return void
  4981. */
  4982. public function testSelectMultipleCheckboxSecurity() {
  4983. $this->Form->request['_Token'] = array('key' => 'testKey');
  4984. $this->assertEquals(array(), $this->Form->fields);
  4985. $result = $this->Form->select(
  4986. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4987. array('multiple' => 'checkbox')
  4988. );
  4989. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4990. $result = $this->Form->secure($this->Form->fields);
  4991. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4992. $this->assertRegExp('/"' . $key . '"/', $result);
  4993. }
  4994. /**
  4995. * Multiple select elements should always be secured as they always participate
  4996. * in the POST data.
  4997. *
  4998. * @return void
  4999. */
  5000. public function testSelectMultipleSecureWithNoOptions() {
  5001. $this->Form->request['_Token'] = array('key' => 'testkey');
  5002. $this->assertEquals(array(), $this->Form->fields);
  5003. $this->Form->select(
  5004. 'Model.select',
  5005. array(),
  5006. array('multiple' => true)
  5007. );
  5008. $this->assertEquals(array('Model.select'), $this->Form->fields);
  5009. }
  5010. /**
  5011. * When a select box has no options it should not be added to the fields list
  5012. * as it always fail post validation.
  5013. *
  5014. * @return void
  5015. */
  5016. public function testSelectNoSecureWithNoOptions() {
  5017. $this->Form->request['_Token'] = array('key' => 'testkey');
  5018. $this->assertEquals(array(), $this->Form->fields);
  5019. $this->Form->select(
  5020. 'Model.select',
  5021. array()
  5022. );
  5023. $this->assertEquals(array(), $this->Form->fields);
  5024. $this->Form->select(
  5025. 'Model.select',
  5026. array(),
  5027. array('empty' => true)
  5028. );
  5029. $this->assertEquals(array('Model.select'), $this->Form->fields);
  5030. }
  5031. /**
  5032. * testInputMultipleCheckboxes method
  5033. *
  5034. * test input() resulting in multi select elements being generated.
  5035. *
  5036. * @return void
  5037. */
  5038. public function testInputMultipleCheckboxes() {
  5039. $result = $this->Form->input('Model.multi_field', array(
  5040. 'options' => array('first', 'second', 'third'),
  5041. 'multiple' => 'checkbox'
  5042. ));
  5043. $expected = array(
  5044. array('div' => array('class' => 'input select')),
  5045. array('label' => array('for' => 'ModelMultiField')),
  5046. 'Multi Field',
  5047. '/label',
  5048. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5049. array('div' => array('class' => 'checkbox')),
  5050. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5051. array('label' => array('for' => 'ModelMultiField0')),
  5052. 'first',
  5053. '/label',
  5054. '/div',
  5055. array('div' => array('class' => 'checkbox')),
  5056. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5057. array('label' => array('for' => 'ModelMultiField1')),
  5058. 'second',
  5059. '/label',
  5060. '/div',
  5061. array('div' => array('class' => 'checkbox')),
  5062. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5063. array('label' => array('for' => 'ModelMultiField2')),
  5064. 'third',
  5065. '/label',
  5066. '/div',
  5067. '/div'
  5068. );
  5069. $this->assertTags($result, $expected);
  5070. $result = $this->Form->input('Model.multi_field', array(
  5071. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  5072. 'multiple' => 'checkbox'
  5073. ));
  5074. $expected = array(
  5075. array('div' => array('class' => 'input select')),
  5076. array('label' => array('for' => 'ModelMultiField')),
  5077. 'Multi Field',
  5078. '/label',
  5079. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5080. array('div' => array('class' => 'checkbox')),
  5081. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  5082. array('label' => array('for' => 'ModelMultiFieldA')),
  5083. 'first',
  5084. '/label',
  5085. '/div',
  5086. array('div' => array('class' => 'checkbox')),
  5087. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  5088. array('label' => array('for' => 'ModelMultiFieldB')),
  5089. 'second',
  5090. '/label',
  5091. '/div',
  5092. array('div' => array('class' => 'checkbox')),
  5093. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  5094. array('label' => array('for' => 'ModelMultiFieldC')),
  5095. 'third',
  5096. '/label',
  5097. '/div',
  5098. '/div'
  5099. );
  5100. $this->assertTags($result, $expected);
  5101. $result = $this->Form->input('Model.multi_field', array(
  5102. 'options' => array('1' => 'first'),
  5103. 'multiple' => 'checkbox',
  5104. 'label' => false,
  5105. 'div' => false
  5106. ));
  5107. $expected = array(
  5108. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5109. array('div' => array('class' => 'checkbox')),
  5110. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5111. array('label' => array('for' => 'ModelMultiField1')),
  5112. 'first',
  5113. '/label',
  5114. '/div'
  5115. );
  5116. $this->assertTags($result, $expected);
  5117. $result = $this->Form->input('Model.multi_field', array(
  5118. 'options' => array('2' => 'second'),
  5119. 'multiple' => 'checkbox',
  5120. 'label' => false,
  5121. 'div' => false
  5122. ));
  5123. $expected = array(
  5124. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  5125. array('div' => array('class' => 'checkbox')),
  5126. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  5127. array('label' => array('for' => 'ModelMultiField2')),
  5128. 'second',
  5129. '/label',
  5130. '/div'
  5131. );
  5132. $this->assertTags($result, $expected);
  5133. }
  5134. /**
  5135. * testSelectHiddenFieldOmission method
  5136. *
  5137. * test that select() with 'hiddenField' => false omits the hidden field
  5138. *
  5139. * @return void
  5140. */
  5141. public function testSelectHiddenFieldOmission() {
  5142. $result = $this->Form->select('Model.multi_field',
  5143. array('first', 'second'),
  5144. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  5145. );
  5146. $expected = array(
  5147. array('div' => array('class' => 'checkbox')),
  5148. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5149. array('label' => array('for' => 'ModelMultiField0')),
  5150. 'first',
  5151. '/label',
  5152. '/div',
  5153. array('div' => array('class' => 'checkbox')),
  5154. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5155. array('label' => array('for' => 'ModelMultiField1')),
  5156. 'second',
  5157. '/label',
  5158. '/div'
  5159. );
  5160. $this->assertTags($result, $expected);
  5161. $result = $this->Form->input('Model.multi_field', array(
  5162. 'options' => array('first', 'second'),
  5163. 'multiple' => 'checkbox',
  5164. 'hiddenField' => false
  5165. ));
  5166. $expected = array(
  5167. array('div' => array('class' => 'input select')),
  5168. array('label' => array('for' => 'ModelMultiField')),
  5169. 'Multi Field',
  5170. '/label',
  5171. array('div' => array('class' => 'checkbox')),
  5172. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  5173. array('label' => array('for' => 'ModelMultiField0')),
  5174. 'first',
  5175. '/label',
  5176. '/div',
  5177. array('div' => array('class' => 'checkbox')),
  5178. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  5179. array('label' => array('for' => 'ModelMultiField1')),
  5180. 'second',
  5181. '/label',
  5182. '/div',
  5183. '/div'
  5184. );
  5185. $this->assertTags($result, $expected);
  5186. }
  5187. /**
  5188. * test that select() with multiple = checkbox works with overriding name attribute.
  5189. *
  5190. * @return void
  5191. */
  5192. public function testSelectCheckboxMultipleOverrideName() {
  5193. $result = $this->Form->input('category', array(
  5194. 'type' => 'select',
  5195. 'multiple' => 'checkbox',
  5196. 'name' => 'data[fish]',
  5197. 'options' => array('1', '2'),
  5198. 'div' => false,
  5199. 'label' => false,
  5200. ));
  5201. $expected = array(
  5202. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  5203. array('div' => array('class' => 'checkbox')),
  5204. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  5205. array('label' => array('for' => 'Category0')), '1', '/label',
  5206. '/div',
  5207. array('div' => array('class' => 'checkbox')),
  5208. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  5209. array('label' => array('for' => 'Category1')), '2', '/label',
  5210. '/div'
  5211. );
  5212. $this->assertTags($result, $expected);
  5213. }
  5214. /**
  5215. * Test that 'id' overrides all the checkbox id's as well.
  5216. *
  5217. * @return void
  5218. */
  5219. public function testSelectCheckboxMultipleId() {
  5220. $result = $this->Form->select(
  5221. 'Model.multi_field',
  5222. array('first', 'second', 'third'),
  5223. array('multiple' => 'checkbox', 'id' => 'CustomId')
  5224. );
  5225. $expected = array(
  5226. 'input' => array(
  5227. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  5228. ),
  5229. array('div' => array('class' => 'checkbox')),
  5230. array('input' => array(
  5231. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5232. 'value' => '0', 'id' => 'CustomId0'
  5233. )),
  5234. array('label' => array('for' => 'CustomId0')),
  5235. 'first',
  5236. '/label',
  5237. '/div',
  5238. array('div' => array('class' => 'checkbox')),
  5239. array('input' => array(
  5240. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5241. 'value' => '1', 'id' => 'CustomId1'
  5242. )),
  5243. array('label' => array('for' => 'CustomId1')),
  5244. 'second',
  5245. '/label',
  5246. '/div',
  5247. array('div' => array('class' => 'checkbox')),
  5248. array('input' => array(
  5249. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  5250. 'value' => '2', 'id' => 'CustomId2'
  5251. )),
  5252. array('label' => array('for' => 'CustomId2')),
  5253. 'third',
  5254. '/label',
  5255. '/div'
  5256. );
  5257. $this->assertTags($result, $expected);
  5258. }
  5259. /**
  5260. * testCheckbox method
  5261. *
  5262. * Test generation of checkboxes
  5263. *
  5264. * @return void
  5265. */
  5266. public function testCheckbox() {
  5267. $result = $this->Form->checkbox('Model.field');
  5268. $expected = array(
  5269. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5270. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5271. );
  5272. $this->assertTags($result, $expected);
  5273. $result = $this->Form->checkbox('Model.field', array(
  5274. 'id' => 'theID',
  5275. 'value' => 'myvalue',
  5276. 'form' => 'my-form',
  5277. ));
  5278. $expected = array(
  5279. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_', 'form' => 'my-form'),
  5280. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID', 'form' => 'my-form'))
  5281. );
  5282. $this->assertTags($result, $expected);
  5283. $Contact = ClassRegistry::getObject('Contact');
  5284. $Contact->validationErrors['field'] = 1;
  5285. $this->Form->request->data['Contact']['field'] = 'myvalue';
  5286. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  5287. $expected = array(
  5288. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5289. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  5290. );
  5291. $this->assertTags($result, $expected);
  5292. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5293. $expected = array(
  5294. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5295. array('input' => array('type', 'name', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  5296. );
  5297. $this->assertTags($result, $expected);
  5298. $this->Form->request->data['Contact']['field'] = '';
  5299. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  5300. $expected = array(
  5301. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  5302. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  5303. );
  5304. $this->assertTags($result, $expected);
  5305. $Contact->validationErrors = array();
  5306. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  5307. $expected = array(
  5308. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  5309. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  5310. );
  5311. $this->assertTags($result, $expected);
  5312. $this->Form->request->data['Contact']['published'] = 1;
  5313. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5314. $expected = array(
  5315. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5316. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  5317. );
  5318. $this->assertTags($result, $expected);
  5319. $this->Form->request->data['Contact']['published'] = 0;
  5320. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  5321. $expected = array(
  5322. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  5323. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  5324. );
  5325. $this->assertTags($result, $expected);
  5326. $result = $this->Form->checkbox('Model.CustomField.1.value');
  5327. $expected = array(
  5328. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  5329. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  5330. );
  5331. $this->assertTags($result, $expected);
  5332. $result = $this->Form->checkbox('CustomField.1.value');
  5333. $expected = array(
  5334. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  5335. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  5336. );
  5337. $this->assertTags($result, $expected);
  5338. }
  5339. /**
  5340. * test checkbox() with a custom name attribute
  5341. *
  5342. * @return void
  5343. */
  5344. public function testCheckboxCustomNameAttribute() {
  5345. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  5346. $expected = array(
  5347. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  5348. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  5349. );
  5350. $this->assertTags($result, $expected);
  5351. }
  5352. /**
  5353. * test the checked option for checkboxes.
  5354. *
  5355. * @return void
  5356. */
  5357. public function testCheckboxCheckedOption() {
  5358. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5359. $expected = array(
  5360. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5361. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5362. );
  5363. $this->assertTags($result, $expected);
  5364. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5365. $expected = array(
  5366. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5367. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5368. );
  5369. $this->assertTags($result, $expected);
  5370. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5371. $expected = array(
  5372. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5373. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5374. );
  5375. $this->assertTags($result, $expected);
  5376. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5377. $expected = array(
  5378. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5379. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5380. );
  5381. $this->assertTags($result, $expected);
  5382. $this->Form->request->data['Model']['field'] = 1;
  5383. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5384. $expected = array(
  5385. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  5386. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  5387. );
  5388. $this->assertTags($result, $expected);
  5389. }
  5390. /**
  5391. * Test that disabled attribute works on both the checkbox and hidden input.
  5392. *
  5393. * @return void
  5394. */
  5395. public function testCheckboxDisabling() {
  5396. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5397. $expected = array(
  5398. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5399. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5400. );
  5401. $this->assertTags($result, $expected);
  5402. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5403. $expected = array(
  5404. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5405. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5406. );
  5407. $this->assertTags($result, $expected);
  5408. }
  5409. /**
  5410. * Test that the hidden input for checkboxes can be omitted or set to a
  5411. * specific value.
  5412. *
  5413. * @return void
  5414. */
  5415. public function testCheckboxHiddenField() {
  5416. $result = $this->Form->input('UserForm.something', array(
  5417. 'type' => 'checkbox',
  5418. 'hiddenField' => false
  5419. ));
  5420. $expected = array(
  5421. 'div' => array('class' => 'input checkbox'),
  5422. array('input' => array(
  5423. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5424. 'value' => '1', 'id' => 'UserFormSomething'
  5425. )),
  5426. 'label' => array('for' => 'UserFormSomething'),
  5427. 'Something',
  5428. '/label',
  5429. '/div'
  5430. );
  5431. $this->assertTags($result, $expected);
  5432. $result = $this->Form->input('UserForm.something', array(
  5433. 'type' => 'checkbox',
  5434. 'value' => 'Y',
  5435. 'hiddenField' => 'N',
  5436. ));
  5437. $expected = array(
  5438. 'div' => array('class' => 'input checkbox'),
  5439. array('input' => array(
  5440. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  5441. 'value' => 'N', 'id' => 'UserFormSomething_'
  5442. )),
  5443. array('input' => array(
  5444. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  5445. 'value' => 'Y', 'id' => 'UserFormSomething'
  5446. )),
  5447. 'label' => array('for' => 'UserFormSomething'),
  5448. 'Something',
  5449. '/label',
  5450. '/div'
  5451. );
  5452. $this->assertTags($result, $expected);
  5453. }
  5454. /**
  5455. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  5456. *
  5457. * @return void
  5458. */
  5459. public function testCheckboxZeroValue() {
  5460. $result = $this->Form->input('User.get_spam', array(
  5461. 'type' => 'checkbox',
  5462. 'value' => '0',
  5463. 'hiddenField' => '1',
  5464. ));
  5465. $expected = array(
  5466. 'div' => array('class' => 'input checkbox'),
  5467. array('input' => array(
  5468. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  5469. 'value' => '1', 'id' => 'UserGetSpam_'
  5470. )),
  5471. array('input' => array(
  5472. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  5473. 'value' => '0', 'id' => 'UserGetSpam'
  5474. )),
  5475. 'label' => array('for' => 'UserGetSpam'),
  5476. 'Get Spam',
  5477. '/label',
  5478. '/div'
  5479. );
  5480. $this->assertTags($result, $expected);
  5481. }
  5482. /**
  5483. * testDateTime method
  5484. *
  5485. * Test generation of date/time select elements
  5486. *
  5487. * @return void
  5488. */
  5489. public function testDateTime() {
  5490. extract($this->dateRegex);
  5491. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5492. $now = strtotime('now');
  5493. $expected = array(
  5494. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5495. $daysRegex,
  5496. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5497. date('j', $now),
  5498. '/option',
  5499. '*/select',
  5500. '-',
  5501. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5502. $monthsRegex,
  5503. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5504. date('F', $now),
  5505. '/option',
  5506. '*/select',
  5507. '-',
  5508. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5509. $yearsRegex,
  5510. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5511. date('Y', $now),
  5512. '/option',
  5513. '*/select',
  5514. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5515. $hoursRegex,
  5516. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5517. date('g', $now),
  5518. '/option',
  5519. '*/select',
  5520. ':',
  5521. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5522. $minutesRegex,
  5523. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5524. date('i', $now),
  5525. '/option',
  5526. '*/select',
  5527. ' ',
  5528. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5529. $meridianRegex,
  5530. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5531. date('a', $now),
  5532. '/option',
  5533. '*/select'
  5534. );
  5535. $this->assertTags($result, $expected);
  5536. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5537. $expected = array(
  5538. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5539. $daysRegex,
  5540. array('option' => array('value' => '')),
  5541. '/option',
  5542. '*/select',
  5543. '-',
  5544. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5545. $monthsRegex,
  5546. array('option' => array('value' => '')),
  5547. '/option',
  5548. '*/select',
  5549. '-',
  5550. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5551. $yearsRegex,
  5552. array('option' => array('value' => '')),
  5553. '/option',
  5554. '*/select',
  5555. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5556. $hoursRegex,
  5557. array('option' => array('value' => '')),
  5558. '/option',
  5559. '*/select',
  5560. ':',
  5561. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5562. $minutesRegex,
  5563. array('option' => array('value' => '')),
  5564. '/option',
  5565. '*/select',
  5566. ' ',
  5567. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5568. $meridianRegex,
  5569. array('option' => array('value' => '')),
  5570. '/option',
  5571. '*/select'
  5572. );
  5573. $this->assertTags($result, $expected);
  5574. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5575. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5576. $this->assertTags($result, $expected);
  5577. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5578. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5579. $this->assertTags($result, $expected);
  5580. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5581. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5582. $expected = array(
  5583. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5584. $daysRegex,
  5585. array('option' => array('value' => '')),
  5586. '/option',
  5587. '*/select',
  5588. '-',
  5589. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5590. $monthsRegex,
  5591. array('option' => array('value' => '')),
  5592. '/option',
  5593. '*/select',
  5594. '-',
  5595. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5596. $yearsRegex,
  5597. array('option' => array('value' => '')),
  5598. '/option',
  5599. '*/select',
  5600. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5601. $hoursRegex,
  5602. array('option' => array('value' => '')),
  5603. '/option',
  5604. '*/select',
  5605. ':',
  5606. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5607. $minutesRegex,
  5608. array('option' => array('value' => '')),
  5609. '/option',
  5610. array('option' => array('value' => '00')),
  5611. '00',
  5612. '/option',
  5613. array('option' => array('value' => '05')),
  5614. '05',
  5615. '/option',
  5616. array('option' => array('value' => '10')),
  5617. '10',
  5618. '/option',
  5619. '*/select',
  5620. ' ',
  5621. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5622. $meridianRegex,
  5623. array('option' => array('value' => '')),
  5624. '/option',
  5625. '*/select'
  5626. );
  5627. $this->assertTags($result, $expected);
  5628. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5629. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5630. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5631. $this->Form->request->data['Contact']['data'] = null;
  5632. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5633. $expected = array(
  5634. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  5635. $daysRegex,
  5636. array('option' => array('value' => '')),
  5637. '/option',
  5638. '*/select',
  5639. '-',
  5640. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  5641. $monthsRegex,
  5642. array('option' => array('value' => '')),
  5643. '/option',
  5644. '*/select',
  5645. '-',
  5646. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  5647. $yearsRegex,
  5648. array('option' => array('value' => '')),
  5649. '/option',
  5650. '*/select',
  5651. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  5652. $hoursRegex,
  5653. array('option' => array('value' => '')),
  5654. '/option',
  5655. '*/select',
  5656. ':',
  5657. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  5658. $minutesRegex,
  5659. array('option' => array('value' => '')),
  5660. '/option',
  5661. '*/select',
  5662. ' ',
  5663. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  5664. $meridianRegex,
  5665. array('option' => array('value' => '')),
  5666. '/option',
  5667. '*/select'
  5668. );
  5669. $this->assertTags($result, $expected);
  5670. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5671. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5672. $now = strtotime($this->Form->data['Model']['field']);
  5673. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5674. $expected = array(
  5675. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5676. $daysRegex,
  5677. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5678. date('j', $now),
  5679. '/option',
  5680. '*/select',
  5681. '-',
  5682. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5683. $monthsRegex,
  5684. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5685. date('F', $now),
  5686. '/option',
  5687. '*/select',
  5688. '-',
  5689. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5690. $yearsRegex,
  5691. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5692. date('Y', $now),
  5693. '/option',
  5694. '*/select',
  5695. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5696. $hoursRegex,
  5697. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5698. date('g', $now),
  5699. '/option',
  5700. '*/select',
  5701. ':',
  5702. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5703. $minutesRegex,
  5704. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5705. date('i', $now),
  5706. '/option',
  5707. '*/select',
  5708. ' ',
  5709. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  5710. $meridianRegex,
  5711. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5712. date('a', $now),
  5713. '/option',
  5714. '*/select'
  5715. );
  5716. $this->assertTags($result, $expected);
  5717. $selected = strtotime('2008-10-26 12:33:00');
  5718. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5719. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5720. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5721. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5722. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5723. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5724. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5725. $this->Form->create('Contact');
  5726. $result = $this->Form->input('published');
  5727. $now = strtotime('now');
  5728. $expected = array(
  5729. 'div' => array('class' => 'input date'),
  5730. 'label' => array('for' => 'ContactPublishedMonth'),
  5731. 'Published',
  5732. '/label',
  5733. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5734. $monthsRegex,
  5735. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5736. date('F', $now),
  5737. '/option',
  5738. '*/select',
  5739. '-',
  5740. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5741. $daysRegex,
  5742. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5743. date('j', $now),
  5744. '/option',
  5745. '*/select',
  5746. '-',
  5747. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5748. $yearsRegex,
  5749. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5750. date('Y', $now),
  5751. '/option',
  5752. '*/select',
  5753. '/div'
  5754. );
  5755. $this->assertTags($result, $expected);
  5756. $result = $this->Form->input('published2', array('type' => 'date'));
  5757. $now = strtotime('now');
  5758. $expected = array(
  5759. 'div' => array('class' => 'input date'),
  5760. 'label' => array('for' => 'ContactPublished2Month'),
  5761. 'Published2',
  5762. '/label',
  5763. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  5764. $monthsRegex,
  5765. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5766. date('F', $now),
  5767. '/option',
  5768. '*/select',
  5769. '-',
  5770. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  5771. $daysRegex,
  5772. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5773. date('j', $now),
  5774. '/option',
  5775. '*/select',
  5776. '-',
  5777. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  5778. $yearsRegex,
  5779. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5780. date('Y', $now),
  5781. '/option',
  5782. '*/select',
  5783. '/div'
  5784. );
  5785. $this->assertTags($result, $expected);
  5786. $this->Form->create('Contact');
  5787. $result = $this->Form->input('published', array('monthNames' => false));
  5788. $now = strtotime('now');
  5789. $expected = array(
  5790. 'div' => array('class' => 'input date'),
  5791. 'label' => array('for' => 'ContactPublishedMonth'),
  5792. 'Published',
  5793. '/label',
  5794. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  5795. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5796. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5797. date('m', $now),
  5798. '/option',
  5799. '*/select',
  5800. '-',
  5801. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  5802. $daysRegex,
  5803. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5804. date('j', $now),
  5805. '/option',
  5806. '*/select',
  5807. '-',
  5808. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5809. $yearsRegex,
  5810. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5811. date('Y', $now),
  5812. '/option',
  5813. '*/select',
  5814. '/div'
  5815. );
  5816. $this->assertTags($result, $expected);
  5817. $result = $this->Form->input('published', array(
  5818. 'timeFormat' => 24,
  5819. 'interval' => 5,
  5820. 'selected' => strtotime('2009-09-03 13:37:00'),
  5821. 'type' => 'datetime'
  5822. ));
  5823. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5824. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5825. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5826. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5827. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5828. }
  5829. /**
  5830. * Test dateTime with rounding
  5831. *
  5832. * @return void
  5833. */
  5834. public function testDateTimeRounding() {
  5835. $this->Form->request->data['Contact'] = array(
  5836. 'date' => array(
  5837. 'day' => '13',
  5838. 'month' => '12',
  5839. 'year' => '2010',
  5840. 'hour' => '04',
  5841. 'min' => '19',
  5842. 'meridian' => 'AM'
  5843. )
  5844. );
  5845. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5846. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5847. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5848. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5849. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5850. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5851. }
  5852. /**
  5853. * Test that empty values don't trigger errors.
  5854. *
  5855. * @return void
  5856. */
  5857. public function testDateTimeNoErrorsOnEmptyData() {
  5858. $this->Form->request->data['Contact'] = array(
  5859. 'date' => array(
  5860. 'day' => '',
  5861. 'month' => '',
  5862. 'year' => '',
  5863. 'hour' => '',
  5864. 'min' => '',
  5865. 'meridian' => ''
  5866. )
  5867. );
  5868. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5869. $this->assertNotEmpty($result);
  5870. }
  5871. /**
  5872. * test that datetime() and default values work.
  5873. *
  5874. * @return void
  5875. */
  5876. public function testDatetimeWithDefault() {
  5877. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5878. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5879. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5880. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5881. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5882. 'default' => '2009-06-01 11:15:30'
  5883. ));
  5884. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5885. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5886. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5887. }
  5888. /**
  5889. * test that bogus non-date time data doesn't cause errors.
  5890. *
  5891. * @return void
  5892. */
  5893. public function testDateTimeWithBogusData() {
  5894. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5895. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5896. }
  5897. /**
  5898. * testDateTime all zeros
  5899. *
  5900. * @return void
  5901. */
  5902. public function testDateTimeAllZeros() {
  5903. $result = $this->Form->dateTime('Contact.date',
  5904. 'DMY',
  5905. false,
  5906. array(
  5907. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5908. 'value' => '0000-00-00'
  5909. )
  5910. );
  5911. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5912. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5913. }
  5914. /**
  5915. * testDateTimeEmptyAsArray
  5916. *
  5917. * @return void
  5918. */
  5919. public function testDateTimeEmptyAsArray() {
  5920. $result = $this->Form->dateTime('Contact.date',
  5921. 'DMY',
  5922. '12',
  5923. array(
  5924. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5925. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5926. )
  5927. )
  5928. );
  5929. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5930. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5931. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5932. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5933. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5934. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5935. $result = $this->Form->dateTime('Contact.date',
  5936. 'DMY',
  5937. '12',
  5938. array(
  5939. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5940. )
  5941. );
  5942. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5943. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5944. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5945. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5946. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5947. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5948. }
  5949. /**
  5950. * testFormDateTimeMulti method
  5951. *
  5952. * test multiple datetime element generation
  5953. *
  5954. * @return void
  5955. */
  5956. public function testFormDateTimeMulti() {
  5957. extract($this->dateRegex);
  5958. $result = $this->Form->dateTime('Contact.1.updated');
  5959. $expected = array(
  5960. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5961. $daysRegex,
  5962. array('option' => array('value' => '')),
  5963. '/option',
  5964. '*/select',
  5965. '-',
  5966. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5967. $monthsRegex,
  5968. array('option' => array('value' => '')),
  5969. '/option',
  5970. '*/select',
  5971. '-',
  5972. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5973. $yearsRegex,
  5974. array('option' => array('value' => '')),
  5975. '/option',
  5976. '*/select',
  5977. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5978. $hoursRegex,
  5979. array('option' => array('value' => '')),
  5980. '/option',
  5981. '*/select',
  5982. ':',
  5983. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5984. $minutesRegex,
  5985. array('option' => array('value' => '')),
  5986. '/option',
  5987. '*/select',
  5988. ' ',
  5989. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5990. $meridianRegex,
  5991. array('option' => array('value' => '')),
  5992. '/option',
  5993. '*/select'
  5994. );
  5995. $this->assertTags($result, $expected);
  5996. $result = $this->Form->dateTime('Contact.2.updated');
  5997. $expected = array(
  5998. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5999. $daysRegex,
  6000. array('option' => array('value' => '')),
  6001. '/option',
  6002. '*/select',
  6003. '-',
  6004. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  6005. $monthsRegex,
  6006. array('option' => array('value' => '')),
  6007. '/option',
  6008. '*/select',
  6009. '-',
  6010. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  6011. $yearsRegex,
  6012. array('option' => array('value' => '')),
  6013. '/option',
  6014. '*/select',
  6015. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  6016. $hoursRegex,
  6017. array('option' => array('value' => '')),
  6018. '/option',
  6019. '*/select',
  6020. ':',
  6021. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  6022. $minutesRegex,
  6023. array('option' => array('value' => '')),
  6024. '/option',
  6025. '*/select',
  6026. ' ',
  6027. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  6028. $meridianRegex,
  6029. array('option' => array('value' => '')),
  6030. '/option',
  6031. '*/select'
  6032. );
  6033. $this->assertTags($result, $expected);
  6034. }
  6035. /**
  6036. * When changing the date format, the label should always focus the first select box when
  6037. * clicked.
  6038. *
  6039. * @return void
  6040. */
  6041. public function testDateTimeLabelIdMatchesFirstInput() {
  6042. $result = $this->Form->input('Model.date', array('type' => 'date'));
  6043. $this->assertContains('label for="ModelDateMonth"', $result);
  6044. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  6045. $this->assertContains('label for="ModelDateDay"', $result);
  6046. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  6047. $this->assertContains('label for="ModelDateYear"', $result);
  6048. }
  6049. /**
  6050. * testMonth method
  6051. *
  6052. * @return void
  6053. */
  6054. public function testMonth() {
  6055. $result = $this->Form->month('Model.field');
  6056. $expected = array(
  6057. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6058. array('option' => array('value' => '')),
  6059. '/option',
  6060. array('option' => array('value' => '01')),
  6061. date('F', strtotime('2008-01-01 00:00:00')),
  6062. '/option',
  6063. array('option' => array('value' => '02')),
  6064. date('F', strtotime('2008-02-01 00:00:00')),
  6065. '/option',
  6066. '*/select',
  6067. );
  6068. $this->assertTags($result, $expected);
  6069. $result = $this->Form->month('Model.field', array('empty' => true));
  6070. $expected = array(
  6071. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6072. array('option' => array('value' => '')),
  6073. '/option',
  6074. array('option' => array('value' => '01')),
  6075. date('F', strtotime('2008-01-01 00:00:00')),
  6076. '/option',
  6077. array('option' => array('value' => '02')),
  6078. date('F', strtotime('2008-02-01 00:00:00')),
  6079. '/option',
  6080. '*/select',
  6081. );
  6082. $this->assertTags($result, $expected);
  6083. $result = $this->Form->month('Model.field', array('monthNames' => false));
  6084. $expected = array(
  6085. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6086. array('option' => array('value' => '')),
  6087. '/option',
  6088. array('option' => array('value' => '01')),
  6089. '01',
  6090. '/option',
  6091. array('option' => array('value' => '02')),
  6092. '02',
  6093. '/option',
  6094. '*/select',
  6095. );
  6096. $this->assertTags($result, $expected);
  6097. $monthNames = array(
  6098. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  6099. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  6100. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  6101. $expected = array(
  6102. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6103. array('option' => array('value' => '')),
  6104. '/option',
  6105. array('option' => array('value' => '01')),
  6106. 'Jan',
  6107. '/option',
  6108. array('option' => array('value' => '02')),
  6109. 'Feb',
  6110. '/option',
  6111. '*/select',
  6112. );
  6113. $this->assertTags($result, $expected);
  6114. $this->Form->request->data['Project']['release'] = '2050-02-10';
  6115. $result = $this->Form->month('Project.release');
  6116. $expected = array(
  6117. array('select' => array('name' => 'data[Project][release][month]', 'id' => 'ProjectReleaseMonth')),
  6118. array('option' => array('value' => '')),
  6119. '/option',
  6120. array('option' => array('value' => '01')),
  6121. 'January',
  6122. '/option',
  6123. array('option' => array('value' => '02', 'selected' => 'selected')),
  6124. 'February',
  6125. '/option',
  6126. '*/select',
  6127. );
  6128. $this->assertTags($result, $expected);
  6129. $this->Form->request->data['Model']['field'] = '12a';
  6130. $result = $this->Form->month('Model.field');
  6131. $expected = array(
  6132. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  6133. array('option' => array('value' => '')),
  6134. '/option',
  6135. array('option' => array('value' => '01')),
  6136. date('F', strtotime('2008-01-01 00:00:00')),
  6137. '/option',
  6138. array('option' => array('value' => '02')),
  6139. date('F', strtotime('2008-02-01 00:00:00')),
  6140. '/option',
  6141. '*/select',
  6142. );
  6143. $this->assertTags($result, $expected);
  6144. }
  6145. /**
  6146. * testDay method
  6147. *
  6148. * @return void
  6149. */
  6150. public function testDay() {
  6151. extract($this->dateRegex);
  6152. $result = $this->Form->day('Model.field', array('value' => false));
  6153. $expected = array(
  6154. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6155. array('option' => array('value' => '')),
  6156. '/option',
  6157. array('option' => array('value' => '01')),
  6158. '1',
  6159. '/option',
  6160. array('option' => array('value' => '02')),
  6161. '2',
  6162. '/option',
  6163. $daysRegex,
  6164. '/select',
  6165. );
  6166. $this->assertTags($result, $expected);
  6167. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6168. $result = $this->Form->day('Model.field');
  6169. $expected = array(
  6170. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6171. array('option' => array('value' => '')),
  6172. '/option',
  6173. array('option' => array('value' => '01')),
  6174. '1',
  6175. '/option',
  6176. array('option' => array('value' => '02')),
  6177. '2',
  6178. '/option',
  6179. $daysRegex,
  6180. array('option' => array('value' => '10', 'selected' => 'selected')),
  6181. '10',
  6182. '/option',
  6183. $daysRegex,
  6184. '/select',
  6185. );
  6186. $this->assertTags($result, $expected);
  6187. $this->Form->request->data['Model']['field'] = '';
  6188. $result = $this->Form->day('Model.field', array('value' => '10'));
  6189. $expected = array(
  6190. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6191. array('option' => array('value' => '')),
  6192. '/option',
  6193. array('option' => array('value' => '01')),
  6194. '1',
  6195. '/option',
  6196. array('option' => array('value' => '02')),
  6197. '2',
  6198. '/option',
  6199. $daysRegex,
  6200. array('option' => array('value' => '10', 'selected' => 'selected')),
  6201. '10',
  6202. '/option',
  6203. $daysRegex,
  6204. '/select',
  6205. );
  6206. $this->assertTags($result, $expected);
  6207. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  6208. $result = $this->Form->day('Model.field', array('value' => true));
  6209. $expected = array(
  6210. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6211. array('option' => array('value' => '')),
  6212. '/option',
  6213. array('option' => array('value' => '01')),
  6214. '1',
  6215. '/option',
  6216. array('option' => array('value' => '02')),
  6217. '2',
  6218. '/option',
  6219. $daysRegex,
  6220. array('option' => array('value' => '10', 'selected' => 'selected')),
  6221. '10',
  6222. '/option',
  6223. $daysRegex,
  6224. '/select',
  6225. );
  6226. $this->assertTags($result, $expected);
  6227. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6228. $result = $this->Form->day('Project.release');
  6229. $expected = array(
  6230. array('select' => array('name' => 'data[Project][release][day]', 'id' => 'ProjectReleaseDay')),
  6231. array('option' => array('value' => '')),
  6232. '/option',
  6233. array('option' => array('value' => '01')),
  6234. '1',
  6235. '/option',
  6236. array('option' => array('value' => '02')),
  6237. '2',
  6238. '/option',
  6239. $daysRegex,
  6240. array('option' => array('value' => '10', 'selected' => 'selected')),
  6241. '10',
  6242. '/option',
  6243. $daysRegex,
  6244. '/select',
  6245. );
  6246. $this->assertTags($result, $expected);
  6247. $this->Form->request->data['Model']['field'] = '12e';
  6248. $result = $this->Form->day('Model.field');
  6249. $expected = array(
  6250. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  6251. array('option' => array('value' => '')),
  6252. '/option',
  6253. array('option' => array('value' => '01')),
  6254. '1',
  6255. '/option',
  6256. array('option' => array('value' => '02')),
  6257. '2',
  6258. '/option',
  6259. $daysRegex,
  6260. '/select',
  6261. );
  6262. $this->assertTags($result, $expected);
  6263. }
  6264. /**
  6265. * testMinute method
  6266. *
  6267. * @return void
  6268. */
  6269. public function testMinute() {
  6270. extract($this->dateRegex);
  6271. $result = $this->Form->minute('Model.field');
  6272. $expected = array(
  6273. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6274. array('option' => array('value' => '')),
  6275. '/option',
  6276. array('option' => array('value' => '00')),
  6277. '00',
  6278. '/option',
  6279. array('option' => array('value' => '01')),
  6280. '01',
  6281. '/option',
  6282. array('option' => array('value' => '02')),
  6283. '02',
  6284. '/option',
  6285. $minutesRegex,
  6286. '/select',
  6287. );
  6288. $this->assertTags($result, $expected);
  6289. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6290. $result = $this->Form->minute('Model.field');
  6291. $expected = array(
  6292. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6293. array('option' => array('value' => '')),
  6294. '/option',
  6295. array('option' => array('value' => '00')),
  6296. '00',
  6297. '/option',
  6298. array('option' => array('value' => '01')),
  6299. '01',
  6300. '/option',
  6301. array('option' => array('value' => '02')),
  6302. '02',
  6303. '/option',
  6304. $minutesRegex,
  6305. array('option' => array('value' => '12', 'selected' => 'selected')),
  6306. '12',
  6307. '/option',
  6308. $minutesRegex,
  6309. '/select',
  6310. );
  6311. $this->assertTags($result, $expected);
  6312. $this->Form->request->data['Model']['field'] = '';
  6313. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6314. $expected = array(
  6315. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6316. array('option' => array('value' => '')),
  6317. '/option',
  6318. array('option' => array('value' => '00')),
  6319. '00',
  6320. '/option',
  6321. array('option' => array('value' => '05')),
  6322. '05',
  6323. '/option',
  6324. array('option' => array('value' => '10')),
  6325. '10',
  6326. '/option',
  6327. $minutesRegex,
  6328. '/select',
  6329. );
  6330. $this->assertTags($result, $expected);
  6331. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  6332. $result = $this->Form->minute('Model.field', array('interval' => 5));
  6333. $expected = array(
  6334. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6335. array('option' => array('value' => '')),
  6336. '/option',
  6337. array('option' => array('value' => '00')),
  6338. '00',
  6339. '/option',
  6340. array('option' => array('value' => '05')),
  6341. '05',
  6342. '/option',
  6343. array('option' => array('value' => '10', 'selected' => 'selected')),
  6344. '10',
  6345. '/option',
  6346. $minutesRegex,
  6347. '/select',
  6348. );
  6349. $this->assertTags($result, $expected);
  6350. $result = $this->Form->minute('Model.field', array('value' => '#invalid#'));
  6351. $expected = array(
  6352. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  6353. array('option' => array('value' => '')),
  6354. '/option',
  6355. array('option' => array('value' => '00')),
  6356. '00',
  6357. '/option',
  6358. array('option' => array('value' => '01')),
  6359. '01',
  6360. '/option',
  6361. array('option' => array('value' => '02')),
  6362. '02',
  6363. '/option',
  6364. $minutesRegex,
  6365. '/select',
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. }
  6369. /**
  6370. * testHour method
  6371. *
  6372. * @return void
  6373. */
  6374. public function testHour() {
  6375. extract($this->dateRegex);
  6376. $result = $this->Form->hour('Model.field', false);
  6377. $expected = array(
  6378. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6379. array('option' => array('value' => '')),
  6380. '/option',
  6381. array('option' => array('value' => '01')),
  6382. '1',
  6383. '/option',
  6384. array('option' => array('value' => '02')),
  6385. '2',
  6386. '/option',
  6387. $hoursRegex,
  6388. '/select',
  6389. );
  6390. $this->assertTags($result, $expected);
  6391. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6392. $result = $this->Form->hour('Model.field', false);
  6393. $expected = array(
  6394. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6395. array('option' => array('value' => '')),
  6396. '/option',
  6397. array('option' => array('value' => '01')),
  6398. '1',
  6399. '/option',
  6400. array('option' => array('value' => '02')),
  6401. '2',
  6402. '/option',
  6403. $hoursRegex,
  6404. array('option' => array('value' => '12', 'selected' => 'selected')),
  6405. '12',
  6406. '/option',
  6407. '/select',
  6408. );
  6409. $this->assertTags($result, $expected);
  6410. $this->Form->request->data['Model']['field'] = '';
  6411. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  6412. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  6413. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  6414. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  6415. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  6416. $result = $this->Form->hour('Model.field', true);
  6417. $expected = array(
  6418. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6419. array('option' => array('value' => '')),
  6420. '/option',
  6421. array('option' => array('value' => '00', 'selected' => 'selected')),
  6422. '0',
  6423. '/option',
  6424. array('option' => array('value' => '01')),
  6425. '1',
  6426. '/option',
  6427. array('option' => array('value' => '02')),
  6428. '2',
  6429. '/option',
  6430. $hoursRegex,
  6431. '/select',
  6432. );
  6433. $this->assertTags($result, $expected);
  6434. unset($this->Form->request->data['Model']['field']);
  6435. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6436. $thisHour = date('H');
  6437. $optValue = date('G');
  6438. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6439. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6440. $result = $this->Form->hour('Model.field', true);
  6441. $expected = array(
  6442. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6443. array('option' => array('value' => '')),
  6444. '/option',
  6445. array('option' => array('value' => '00')),
  6446. '0',
  6447. '/option',
  6448. array('option' => array('value' => '01', 'selected' => 'selected')),
  6449. '1',
  6450. '/option',
  6451. array('option' => array('value' => '02')),
  6452. '2',
  6453. '/option',
  6454. $hoursRegex,
  6455. '/select',
  6456. );
  6457. $this->assertTags($result, $expected);
  6458. $this->Form->request->data['Model']['field'] = '18a';
  6459. $result = $this->Form->hour('Model.field', false);
  6460. $expected = array(
  6461. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  6462. array('option' => array('value' => '')),
  6463. '/option',
  6464. array('option' => array('value' => '01')),
  6465. '1',
  6466. '/option',
  6467. array('option' => array('value' => '02')),
  6468. '2',
  6469. '/option',
  6470. $hoursRegex,
  6471. '/select',
  6472. );
  6473. $this->assertTags($result, $expected);
  6474. }
  6475. /**
  6476. * testYear method
  6477. *
  6478. * @return void
  6479. */
  6480. public function testYear() {
  6481. $result = $this->Form->year('Model.field', 2006, 2007);
  6482. $expected = array(
  6483. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6484. array('option' => array('value' => '')),
  6485. '/option',
  6486. array('option' => array('value' => '2007')),
  6487. '2007',
  6488. '/option',
  6489. array('option' => array('value' => '2006')),
  6490. '2006',
  6491. '/option',
  6492. '/select',
  6493. );
  6494. $this->assertTags($result, $expected);
  6495. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6496. $expected = array(
  6497. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  6498. array('option' => array('value' => '')),
  6499. '/option',
  6500. array('option' => array('value' => '2006')),
  6501. '2006',
  6502. '/option',
  6503. array('option' => array('value' => '2007')),
  6504. '2007',
  6505. '/option',
  6506. '/select',
  6507. );
  6508. $this->assertTags($result, $expected);
  6509. $this->request->data['Contact']['published'] = '';
  6510. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6511. $expected = array(
  6512. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6513. array('option' => array('value' => '')),
  6514. '/option',
  6515. array('option' => array('value' => '2007')),
  6516. '2007',
  6517. '/option',
  6518. array('option' => array('value' => '2006')),
  6519. '2006',
  6520. '/option',
  6521. '/select',
  6522. );
  6523. $this->assertTags($result, $expected);
  6524. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6525. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6526. $expected = array(
  6527. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6528. array('option' => array('value' => '2007')),
  6529. '2007',
  6530. '/option',
  6531. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6532. '2006',
  6533. '/option',
  6534. '/select',
  6535. );
  6536. $this->assertTags($result, $expected);
  6537. $this->Form->request->data['Contact']['published'] = '';
  6538. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6539. $expected = array(
  6540. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6541. array('option' => array('value' => '')),
  6542. '/option',
  6543. array('option' => array('value' => '2007')),
  6544. '2007',
  6545. '/option',
  6546. array('option' => array('value' => '2006')),
  6547. '2006',
  6548. '/option',
  6549. '/select',
  6550. );
  6551. $this->assertTags($result, $expected);
  6552. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6553. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6554. $expected = array(
  6555. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6556. array('option' => array('value' => '2007')),
  6557. '2007',
  6558. '/option',
  6559. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6560. '2006',
  6561. '/option',
  6562. '/select',
  6563. );
  6564. $this->assertTags($result, $expected);
  6565. $this->Form->request->data['Contact']['published'] = '';
  6566. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6567. $expected = array(
  6568. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6569. array('option' => array('value' => '')),
  6570. '/option',
  6571. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6572. '2007',
  6573. '/option',
  6574. array('option' => array('value' => '2006')),
  6575. '2006',
  6576. '/option',
  6577. '/select',
  6578. );
  6579. $this->assertTags($result, $expected);
  6580. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6581. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6582. $expected = array(
  6583. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6584. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6585. '2007',
  6586. '/option',
  6587. array('option' => array('value' => '2006')),
  6588. '2006',
  6589. '/option',
  6590. '/select',
  6591. );
  6592. $this->assertTags($result, $expected);
  6593. $this->Form->request->data['Contact']['published'] = '';
  6594. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6595. $expected = array(
  6596. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6597. array('option' => array('value' => '2008')),
  6598. '2008',
  6599. '/option',
  6600. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6601. '2007',
  6602. '/option',
  6603. array('option' => array('value' => '2006')),
  6604. '2006',
  6605. '/option',
  6606. '/select',
  6607. );
  6608. $this->assertTags($result, $expected);
  6609. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6610. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6611. $expected = array(
  6612. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6613. array('option' => array('value' => '2008')),
  6614. '2008',
  6615. '/option',
  6616. array('option' => array('value' => '2007')),
  6617. '2007',
  6618. '/option',
  6619. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6620. '2006',
  6621. '/option',
  6622. '/select',
  6623. );
  6624. $this->assertTags($result, $expected);
  6625. $this->Form->request->data = array();
  6626. $this->Form->create('Contact');
  6627. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6628. $expected = array(
  6629. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6630. array('option' => array('value' => '2008')),
  6631. '2008',
  6632. '/option',
  6633. array('option' => array('value' => '2007')),
  6634. '2007',
  6635. '/option',
  6636. array('option' => array('value' => '2006')),
  6637. '2006',
  6638. '/option',
  6639. '/select',
  6640. );
  6641. $this->assertTags($result, $expected);
  6642. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6643. $this->assertContains('data[Contact][published][year]', $result);
  6644. $this->Form->request->data['Contact']['published'] = '2014ee';
  6645. $result = $this->Form->year('Contact.published', 2010, 2011);
  6646. $expected = array(
  6647. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  6648. array('option' => array('value' => '')),
  6649. '/option',
  6650. array('option' => array('value' => '2011')),
  6651. '2011',
  6652. '/option',
  6653. array('option' => array('value' => '2010')),
  6654. '2010',
  6655. '/option',
  6656. '/select',
  6657. );
  6658. $this->assertTags($result, $expected);
  6659. }
  6660. /**
  6661. * testYearAutoExpandRange method
  6662. *
  6663. * @return void
  6664. */
  6665. public function testYearAutoExpandRange() {
  6666. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6667. $result = $this->Form->year('User.birthday');
  6668. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6669. $result = $matches[1];
  6670. $expected = range(date('Y') + 20, 1930);
  6671. $this->assertEquals($expected, $result);
  6672. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6673. $result = $this->Form->year('Project.release');
  6674. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6675. $result = $matches[1];
  6676. $expected = range(2050, date('Y') - 20);
  6677. $this->assertEquals($expected, $result);
  6678. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6679. $result = $this->Form->year('Project.release', 1890, 1900);
  6680. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6681. $result = $matches[1];
  6682. $expected = range(1900, 1881);
  6683. $this->assertEquals($expected, $result);
  6684. }
  6685. /**
  6686. * testInputDate method
  6687. *
  6688. * Test various inputs with type date and different dateFormat values.
  6689. * Failing to provide a dateFormat key should not error.
  6690. * It should simply not pre-select any value then.
  6691. *
  6692. * @return void
  6693. */
  6694. public function testInputDate() {
  6695. $this->Form->request->data = array(
  6696. 'User' => array(
  6697. 'month_year' => array('month' => date('m')),
  6698. 'just_year' => array('month' => date('m')),
  6699. 'just_month' => array('year' => date('Y')),
  6700. 'just_day' => array('month' => date('m')),
  6701. )
  6702. );
  6703. $this->Form->create('User');
  6704. $result = $this->Form->input('month_year',
  6705. array(
  6706. 'label' => false,
  6707. 'div' => false,
  6708. 'type' => 'date',
  6709. 'dateFormat' => 'MY',
  6710. 'minYear' => 2006,
  6711. 'maxYear' => 2008
  6712. )
  6713. );
  6714. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6715. $this->assertNotContains('value="2008" selected="selected"', $result);
  6716. $result = $this->Form->input('just_year',
  6717. array(
  6718. 'type' => 'date',
  6719. 'label' => false,
  6720. 'dateFormat' => 'Y',
  6721. 'minYear' => date('Y'),
  6722. 'maxYear' => date('Y', strtotime('+20 years'))
  6723. )
  6724. );
  6725. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6726. $result = $this->Form->input('just_month',
  6727. array(
  6728. 'type' => 'date',
  6729. 'label' => false,
  6730. 'dateFormat' => 'M',
  6731. 'empty' => false,
  6732. )
  6733. );
  6734. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6735. $result = $this->Form->input('just_day',
  6736. array(
  6737. 'type' => 'date',
  6738. 'label' => false,
  6739. 'dateFormat' => 'D',
  6740. 'empty' => false,
  6741. )
  6742. );
  6743. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6744. }
  6745. /**
  6746. * testInputDateMaxYear method
  6747. *
  6748. * Let's say we want to only allow users born from 2006 to 2008 to register
  6749. * This being the first singup page, we still don't have any data
  6750. *
  6751. * @return void
  6752. */
  6753. public function testInputDateMaxYear() {
  6754. $this->Form->request->data = array();
  6755. $this->Form->create('User');
  6756. $result = $this->Form->input('birthday',
  6757. array(
  6758. 'label' => false,
  6759. 'div' => false,
  6760. 'type' => 'date',
  6761. 'dateFormat' => 'DMY',
  6762. 'minYear' => 2006,
  6763. 'maxYear' => 2008
  6764. )
  6765. );
  6766. $this->assertContains('value="2008" selected="selected"', $result);
  6767. }
  6768. /**
  6769. * testTextArea method
  6770. *
  6771. * @return void
  6772. */
  6773. public function testTextArea() {
  6774. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6775. $result = $this->Form->textarea('Model.field');
  6776. $expected = array(
  6777. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6778. 'some test data',
  6779. '/textarea',
  6780. );
  6781. $this->assertTags($result, $expected);
  6782. $result = $this->Form->textarea('Model.tmp');
  6783. $expected = array(
  6784. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  6785. '/textarea',
  6786. );
  6787. $this->assertTags($result, $expected);
  6788. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6789. $result = $this->Form->textarea('Model.field');
  6790. $expected = array(
  6791. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6792. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6793. '/textarea',
  6794. );
  6795. $this->assertTags($result, $expected);
  6796. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6797. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6798. $expected = array(
  6799. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6800. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6801. '/textarea',
  6802. );
  6803. $this->assertTags($result, $expected);
  6804. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6805. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6806. $expected = array(
  6807. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6808. '/textarea'
  6809. );
  6810. $this->assertTags($result, $expected);
  6811. }
  6812. /**
  6813. * testTextAreaWithStupidCharacters method
  6814. *
  6815. * test text area with non-ascii characters
  6816. *
  6817. * @return void
  6818. */
  6819. public function testTextAreaWithStupidCharacters() {
  6820. $this->loadFixtures('Post');
  6821. $result = $this->Form->input('Post.content', array(
  6822. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6823. ));
  6824. $expected = array(
  6825. 'div' => array('class' => 'input textarea'),
  6826. 'label' => array('for' => 'PostContent'),
  6827. 'Current Text',
  6828. '/label',
  6829. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6830. 'GREAT®',
  6831. '/textarea',
  6832. '/div'
  6833. );
  6834. $this->assertTags($result, $expected);
  6835. }
  6836. /**
  6837. * testHiddenField method
  6838. *
  6839. * @return void
  6840. */
  6841. public function testHiddenField() {
  6842. $Contact = ClassRegistry::getObject('Contact');
  6843. $Contact->validationErrors['field'] = 1;
  6844. $this->Form->request->data['Contact']['field'] = 'test';
  6845. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6846. $this->assertTags($result, array(
  6847. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  6848. );
  6849. }
  6850. /**
  6851. * testFileUploadField method
  6852. *
  6853. * @return void
  6854. */
  6855. public function testFileUploadField() {
  6856. $result = $this->Form->file('Model.upload');
  6857. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6858. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6859. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6860. $expected = array(
  6861. 'div' => array('class' => 'input file'),
  6862. 'label' => array('for' => 'ModelUpload'),
  6863. 'Upload',
  6864. '/label',
  6865. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  6866. '/div'
  6867. );
  6868. $this->assertTags($result, $expected);
  6869. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6870. $result = $this->Form->file('Model.upload');
  6871. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  6872. }
  6873. /**
  6874. * test File upload input on a model not used in create();
  6875. *
  6876. * @return void
  6877. */
  6878. public function testFileUploadOnOtherModel() {
  6879. $this->Form->create('ValidateUser', array('type' => 'file'));
  6880. $result = $this->Form->file('ValidateProfile.city');
  6881. $expected = array(
  6882. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  6883. );
  6884. $this->assertTags($result, $expected);
  6885. }
  6886. /**
  6887. * testButton method
  6888. *
  6889. * @return void
  6890. */
  6891. public function testButton() {
  6892. $result = $this->Form->button('Hi');
  6893. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6894. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6895. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6896. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6897. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6898. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6899. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6900. $result = $this->Form->button('No type', array('type' => false));
  6901. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6902. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6903. $this->assertNotRegExp('/\&039/', $result);
  6904. }
  6905. /**
  6906. * Test that button() makes unlocked fields by default.
  6907. *
  6908. * @return void
  6909. */
  6910. public function testButtonUnlockedByDefault() {
  6911. $this->Form->request->params['_Token']['key'] = 'secured';
  6912. $this->Form->button('Save', array('name' => 'save'));
  6913. $this->Form->button('Clear');
  6914. $result = $this->Form->unlockField();
  6915. $this->assertEquals(array('save'), $result);
  6916. }
  6917. /**
  6918. * testPostButton method
  6919. *
  6920. * @return void
  6921. */
  6922. public function testPostButton() {
  6923. $result = $this->Form->postButton('Hi', '/controller/action');
  6924. $this->assertTags($result, array(
  6925. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6926. 'div' => array('style' => 'display:none;'),
  6927. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6928. '/div',
  6929. 'button' => array('type' => 'submit'),
  6930. 'Hi',
  6931. '/button',
  6932. '/form'
  6933. ));
  6934. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6935. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  6936. }
  6937. /**
  6938. * Test using postButton with N dimensional data.
  6939. *
  6940. * @return void
  6941. */
  6942. public function testPostButtonNestedData() {
  6943. $data = array(
  6944. 'one' => array(
  6945. 'two' => array(
  6946. 3, 4, 5
  6947. )
  6948. )
  6949. );
  6950. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  6951. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  6952. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  6953. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  6954. }
  6955. /**
  6956. * Test that postButton adds _Token fields.
  6957. *
  6958. * @return void
  6959. */
  6960. public function testSecurePostButton() {
  6961. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6962. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6963. $expected = array(
  6964. 'form' => array(
  6965. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6966. ),
  6967. array('div' => array('style' => 'display:none;')),
  6968. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6969. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6970. '/div',
  6971. 'button' => array('type' => 'submit'),
  6972. 'Delete',
  6973. '/button',
  6974. array('div' => array('style' => 'display:none;')),
  6975. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6976. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6977. '/div',
  6978. '/form',
  6979. );
  6980. $this->assertTags($result, $expected);
  6981. }
  6982. /**
  6983. * testPostLink method
  6984. *
  6985. * @return void
  6986. */
  6987. public function testPostLink() {
  6988. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6989. $this->assertTags($result, array(
  6990. 'form' => array(
  6991. 'method' => 'post', 'action' => '/posts/delete/1',
  6992. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6993. ),
  6994. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6995. '/form',
  6996. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6997. 'Delete',
  6998. '/a'
  6999. ));
  7000. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  7001. $this->assertTags($result, array(
  7002. 'form' => array(
  7003. 'method' => 'post', 'action' => '/posts/delete/1',
  7004. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7005. ),
  7006. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  7007. '/form',
  7008. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7009. 'Delete',
  7010. '/a'
  7011. ));
  7012. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  7013. $this->assertTags($result, array(
  7014. 'form' => array(
  7015. 'method' => 'post', 'action' => '/posts/delete/1',
  7016. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7017. ),
  7018. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7019. '/form',
  7020. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7021. 'Delete',
  7022. '/a'
  7023. ));
  7024. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  7025. $this->assertTags($result, array(
  7026. 'form' => array(
  7027. 'method' => 'post', 'action' => '/posts/delete/1',
  7028. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7029. ),
  7030. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7031. '/form',
  7032. '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;/'),
  7033. 'Delete',
  7034. '/a'
  7035. ));
  7036. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  7037. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  7038. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  7039. $this->assertTags($result, array(
  7040. 'form' => array(
  7041. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  7042. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7043. ),
  7044. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7045. '/form',
  7046. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7047. 'Delete',
  7048. '/a'
  7049. ));
  7050. $result = $this->Form->postLink(
  7051. '',
  7052. array('controller' => 'items', 'action' => 'delete', 10),
  7053. array('class' => 'btn btn-danger', 'escape' => false),
  7054. 'Confirm thing'
  7055. );
  7056. $this->assertTags($result, array(
  7057. 'form' => array(
  7058. 'method' => 'post', 'action' => '/items/delete/10',
  7059. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7060. ),
  7061. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7062. '/form',
  7063. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  7064. '/a'
  7065. ));
  7066. }
  7067. /**
  7068. * Test using postLink with N dimensional data.
  7069. *
  7070. * @return void
  7071. */
  7072. public function testPostLinkNestedData() {
  7073. $data = array(
  7074. 'one' => array(
  7075. 'two' => array(
  7076. 3, 4, 5
  7077. )
  7078. )
  7079. );
  7080. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  7081. $this->assertContains('<input type="hidden" name="data[one][two][0]" value="3"', $result);
  7082. $this->assertContains('<input type="hidden" name="data[one][two][1]" value="4"', $result);
  7083. $this->assertContains('<input type="hidden" name="data[one][two][2]" value="5"', $result);
  7084. }
  7085. /**
  7086. * test creating postLinks after a GET form.
  7087. *
  7088. * @return void
  7089. */
  7090. public function testPostLinkAfterGetForm() {
  7091. $this->Form->request->params['_Token']['key'] = 'testkey';
  7092. $this->Form->create('User', array('type' => 'get'));
  7093. $this->Form->end();
  7094. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7095. $this->assertTags($result, array(
  7096. 'form' => array(
  7097. 'method' => 'post', 'action' => '/posts/delete/1',
  7098. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7099. ),
  7100. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7101. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7102. 'div' => array('style' => 'display:none;'),
  7103. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7104. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7105. '/div',
  7106. '/form',
  7107. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7108. 'Delete',
  7109. '/a'
  7110. ));
  7111. }
  7112. /**
  7113. * Test that postLink adds _Token fields.
  7114. *
  7115. * @return void
  7116. */
  7117. public function testSecurePostLink() {
  7118. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  7119. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  7120. $expected = array(
  7121. 'form' => array(
  7122. 'method' => 'post', 'action' => '/posts/delete/1',
  7123. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7124. ),
  7125. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  7126. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  7127. 'div' => array('style' => 'display:none;'),
  7128. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  7129. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  7130. '/div',
  7131. '/form',
  7132. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7133. 'Delete',
  7134. '/a'
  7135. );
  7136. $this->assertTags($result, $expected);
  7137. }
  7138. /**
  7139. * Test that postLink adds form tags to view block
  7140. *
  7141. * @return void
  7142. */
  7143. public function testPostLinkFormBuffer() {
  7144. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  7145. $this->assertTags($result, array(
  7146. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7147. 'Delete',
  7148. '/a'
  7149. ));
  7150. $result = $this->View->fetch('postLink');
  7151. $this->assertTags($result, array(
  7152. 'form' => array(
  7153. 'method' => 'post', 'action' => '/posts/delete/1',
  7154. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7155. ),
  7156. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7157. '/form'
  7158. ));
  7159. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  7160. array('inline' => false, 'method' => 'DELETE')
  7161. );
  7162. $this->assertTags($result, array(
  7163. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7164. 'Delete',
  7165. '/a'
  7166. ));
  7167. $result = $this->View->fetch('postLink');
  7168. $this->assertTags($result, array(
  7169. 'form' => array(
  7170. 'method' => 'post', 'action' => '/posts/delete/1',
  7171. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7172. ),
  7173. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7174. '/form',
  7175. array(
  7176. 'form' => array(
  7177. 'method' => 'post', 'action' => '/posts/delete/2',
  7178. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7179. ),
  7180. ),
  7181. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  7182. '/form'
  7183. ));
  7184. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  7185. $this->assertTags($result, array(
  7186. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  7187. 'Delete',
  7188. '/a'
  7189. ));
  7190. $result = $this->View->fetch('foobar');
  7191. $this->assertTags($result, array(
  7192. 'form' => array(
  7193. 'method' => 'post', 'action' => '/posts/delete/1',
  7194. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  7195. ),
  7196. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7197. '/form'
  7198. ));
  7199. }
  7200. /**
  7201. * testSubmitButton method
  7202. *
  7203. * @return void
  7204. */
  7205. public function testSubmitButton() {
  7206. $result = $this->Form->submit('');
  7207. $expected = array(
  7208. 'div' => array('class' => 'submit'),
  7209. 'input' => array('type' => 'submit', 'value' => ''),
  7210. '/div'
  7211. );
  7212. $this->assertTags($result, $expected);
  7213. $result = $this->Form->submit('Test Submit');
  7214. $expected = array(
  7215. 'div' => array('class' => 'submit'),
  7216. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7217. '/div'
  7218. );
  7219. $this->assertTags($result, $expected);
  7220. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  7221. $expected = array(
  7222. 'span' => array('class' => 'submit'),
  7223. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7224. '/span'
  7225. );
  7226. $this->assertTags($result, $expected);
  7227. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  7228. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  7229. $this->assertTags($result, $expected);
  7230. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  7231. $expected = array(
  7232. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  7233. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  7234. '/div'
  7235. );
  7236. $this->assertTags($result, $expected);
  7237. $result = $this->Form->submit('Next >');
  7238. $expected = array(
  7239. 'div' => array('class' => 'submit'),
  7240. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  7241. '/div'
  7242. );
  7243. $this->assertTags($result, $expected);
  7244. $result = $this->Form->submit('Next >', array('escape' => false));
  7245. $expected = array(
  7246. 'div' => array('class' => 'submit'),
  7247. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  7248. '/div'
  7249. );
  7250. $this->assertTags($result, $expected);
  7251. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  7252. $expected = array(
  7253. 'div' => array('class' => 'submit'),
  7254. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  7255. '/div'
  7256. );
  7257. $this->assertTags($result, $expected);
  7258. $before = '--before--';
  7259. $after = '--after--';
  7260. $result = $this->Form->submit('Test', array('before' => $before));
  7261. $expected = array(
  7262. 'div' => array('class' => 'submit'),
  7263. '--before--',
  7264. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7265. '/div'
  7266. );
  7267. $this->assertTags($result, $expected);
  7268. $result = $this->Form->submit('Test', array('after' => $after));
  7269. $expected = array(
  7270. 'div' => array('class' => 'submit'),
  7271. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7272. '--after--',
  7273. '/div'
  7274. );
  7275. $this->assertTags($result, $expected);
  7276. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  7277. $expected = array(
  7278. 'div' => array('class' => 'submit'),
  7279. '--before--',
  7280. 'input' => array('type' => 'submit', 'value' => 'Test'),
  7281. '--after--',
  7282. '/div'
  7283. );
  7284. $this->assertTags($result, $expected);
  7285. }
  7286. /**
  7287. * test image submit types.
  7288. *
  7289. * @return void
  7290. */
  7291. public function testSubmitImage() {
  7292. $result = $this->Form->submit('http://example.com/cake.power.gif');
  7293. $expected = array(
  7294. 'div' => array('class' => 'submit'),
  7295. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  7296. '/div'
  7297. );
  7298. $this->assertTags($result, $expected);
  7299. $result = $this->Form->submit('/relative/cake.power.gif');
  7300. $expected = array(
  7301. 'div' => array('class' => 'submit'),
  7302. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  7303. '/div'
  7304. );
  7305. $this->assertTags($result, $expected);
  7306. $result = $this->Form->submit('cake.power.gif');
  7307. $expected = array(
  7308. 'div' => array('class' => 'submit'),
  7309. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7310. '/div'
  7311. );
  7312. $this->assertTags($result, $expected);
  7313. $result = $this->Form->submit('Not.an.image');
  7314. $expected = array(
  7315. 'div' => array('class' => 'submit'),
  7316. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7317. '/div'
  7318. );
  7319. $this->assertTags($result, $expected);
  7320. $after = '--after--';
  7321. $before = '--before--';
  7322. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  7323. $expected = array(
  7324. 'div' => array('class' => 'submit'),
  7325. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7326. '--after--',
  7327. '/div'
  7328. );
  7329. $this->assertTags($result, $expected);
  7330. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  7331. $expected = array(
  7332. 'div' => array('class' => 'submit'),
  7333. '--before--',
  7334. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7335. '/div'
  7336. );
  7337. $this->assertTags($result, $expected);
  7338. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  7339. $expected = array(
  7340. 'div' => array('class' => 'submit'),
  7341. '--before--',
  7342. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  7343. '--after--',
  7344. '/div'
  7345. );
  7346. $this->assertTags($result, $expected);
  7347. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  7348. $expected = array(
  7349. 'div' => array('class' => 'submit'),
  7350. '--before--',
  7351. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  7352. '--after--',
  7353. '/div'
  7354. );
  7355. $this->assertTags($result, $expected);
  7356. }
  7357. /**
  7358. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  7359. * be submitted at a time.
  7360. *
  7361. * @return void
  7362. */
  7363. public function testSubmitUnlockedByDefault() {
  7364. $this->Form->request->params['_Token']['key'] = 'secured';
  7365. $this->Form->submit('Go go');
  7366. $this->Form->submit('Save', array('name' => 'save'));
  7367. $result = $this->Form->unlockField();
  7368. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  7369. }
  7370. /**
  7371. * Test submit image with timestamps.
  7372. *
  7373. * @return void
  7374. */
  7375. public function testSubmitImageTimestamp() {
  7376. Configure::write('Asset.timestamp', 'force');
  7377. $result = $this->Form->submit('cake.power.gif');
  7378. $expected = array(
  7379. 'div' => array('class' => 'submit'),
  7380. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  7381. '/div'
  7382. );
  7383. $this->assertTags($result, $expected);
  7384. }
  7385. /**
  7386. * test the create() method
  7387. *
  7388. * @return void
  7389. */
  7390. public function testCreate() {
  7391. $result = $this->Form->create('Contact');
  7392. $encoding = strtolower(Configure::read('App.encoding'));
  7393. $expected = array(
  7394. 'form' => array(
  7395. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7396. 'accept-charset' => $encoding
  7397. ),
  7398. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  7399. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7400. '/div'
  7401. );
  7402. $this->assertTags($result, $expected);
  7403. $result = $this->Form->create('Contact', array('type' => 'GET'));
  7404. $expected = array('form' => array(
  7405. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7406. 'accept-charset' => $encoding
  7407. ));
  7408. $this->assertTags($result, $expected);
  7409. $result = $this->Form->create('Contact', array('type' => 'get'));
  7410. $expected = array('form' => array(
  7411. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7412. 'accept-charset' => $encoding
  7413. ));
  7414. $this->assertTags($result, $expected);
  7415. $result = $this->Form->create('Contact', array('type' => 'put'));
  7416. $expected = array(
  7417. 'form' => array(
  7418. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7419. 'accept-charset' => $encoding
  7420. ),
  7421. 'div' => array('style' => 'display:none;'),
  7422. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7423. '/div'
  7424. );
  7425. $this->assertTags($result, $expected);
  7426. $result = $this->Form->create('Contact', array('type' => 'file'));
  7427. $expected = array(
  7428. 'form' => array(
  7429. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7430. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7431. ),
  7432. 'div' => array('style' => 'display:none;'),
  7433. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7434. '/div'
  7435. );
  7436. $this->assertTags($result, $expected);
  7437. $this->Form->request->data['Contact']['id'] = 1;
  7438. $this->Form->request->here = '/contacts/edit/1';
  7439. $this->Form->request['action'] = 'edit';
  7440. $result = $this->Form->create('Contact');
  7441. $expected = array(
  7442. 'form' => array(
  7443. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7444. 'accept-charset' => $encoding
  7445. ),
  7446. 'div' => array('style' => 'display:none;'),
  7447. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7448. '/div'
  7449. );
  7450. $this->assertTags($result, $expected);
  7451. $this->Form->request->data['Contact']['id'] = 1;
  7452. $this->Form->request->here = '/contacts/edit/1';
  7453. $this->Form->request['action'] = 'edit';
  7454. $result = $this->Form->create('Contact', array('type' => 'file'));
  7455. $expected = array(
  7456. 'form' => array(
  7457. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  7458. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  7459. ),
  7460. 'div' => array('style' => 'display:none;'),
  7461. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7462. '/div'
  7463. );
  7464. $this->assertTags($result, $expected);
  7465. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  7466. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  7467. $expected = array(
  7468. 'form' => array(
  7469. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  7470. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  7471. ),
  7472. 'div' => array('style' => 'display:none;'),
  7473. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7474. '/div'
  7475. );
  7476. $this->assertTags($result, $expected);
  7477. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  7478. $expected = array(
  7479. 'form' => array(
  7480. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  7481. 'accept-charset' => $encoding
  7482. ),
  7483. 'div' => array('style' => 'display:none;'),
  7484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  7485. '/div'
  7486. );
  7487. $this->assertTags($result, $expected);
  7488. $this->Form->request['action'] = 'add';
  7489. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  7490. $expected = array(
  7491. 'form' => array(
  7492. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  7493. 'accept-charset' => $encoding
  7494. ),
  7495. 'div' => array('style' => 'display:none;'),
  7496. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7497. '/div'
  7498. );
  7499. $this->assertTags($result, $expected);
  7500. $result = $this->Form->create('User', array('action' => 'login'));
  7501. $expected = array(
  7502. 'form' => array(
  7503. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  7504. 'accept-charset' => $encoding
  7505. ),
  7506. 'div' => array('style' => 'display:none;'),
  7507. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7508. '/div'
  7509. );
  7510. $this->assertTags($result, $expected);
  7511. $result = $this->Form->create('User', array('url' => '/users/login'));
  7512. $expected = array(
  7513. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  7514. 'div' => array('style' => 'display:none;'),
  7515. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7516. '/div'
  7517. );
  7518. $this->assertTags($result, $expected);
  7519. $this->Form->request['controller'] = 'pages';
  7520. $result = $this->Form->create('User', array('action' => 'signup'));
  7521. $expected = array(
  7522. 'form' => array(
  7523. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  7524. 'accept-charset' => $encoding
  7525. ),
  7526. 'div' => array('style' => 'display:none;'),
  7527. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7528. '/div'
  7529. );
  7530. $this->assertTags($result, $expected);
  7531. $this->Form->request->data = array();
  7532. $this->Form->request['controller'] = 'contacts';
  7533. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7534. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  7535. $expected = array(
  7536. 'form' => array(
  7537. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  7538. 'accept-charset' => 'utf-8'
  7539. ),
  7540. 'div' => array('style' => 'display:none;'),
  7541. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7542. '/div'
  7543. );
  7544. $this->assertTags($result, $expected);
  7545. }
  7546. /**
  7547. * Test the onsubmit option for create()
  7548. *
  7549. * @return void
  7550. */
  7551. public function testCreateOnSubmit() {
  7552. $this->Form->request->data = array();
  7553. $this->Form->request['controller'] = 'contacts';
  7554. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7555. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  7556. $expected = array(
  7557. 'form' => array(
  7558. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  7559. 'accept-charset' => 'utf-8'
  7560. ),
  7561. 'div' => array('style' => 'display:none;'),
  7562. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7563. '/div'
  7564. );
  7565. $this->assertTags($result, $expected);
  7566. $this->Form->request->data = array();
  7567. $this->Form->request['controller'] = 'contacts';
  7568. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  7569. $result = $this->Form->create(array(
  7570. 'url' => array('action' => 'index', 'param'),
  7571. 'default' => false,
  7572. 'onsubmit' => 'someFunction();'
  7573. ));
  7574. $expected = array(
  7575. 'form' => array(
  7576. 'id' => 'ContactAddForm', 'method' => 'post',
  7577. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  7578. 'action' => '/contacts/index/param',
  7579. 'accept-charset' => 'utf-8'
  7580. ),
  7581. 'div' => array('style' => 'display:none;'),
  7582. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7583. '/div'
  7584. );
  7585. $this->assertTags($result, $expected);
  7586. }
  7587. /**
  7588. * test create() with automatic url generation
  7589. *
  7590. * @return void
  7591. */
  7592. public function testCreateAutoUrl() {
  7593. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  7594. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  7595. $this->Form->request->base = '/base_url';
  7596. $result = $this->Form->create('Contact');
  7597. $expected = array(
  7598. 'form' => array(
  7599. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  7600. 'accept-charset' => 'utf-8'
  7601. ),
  7602. 'div' => array('style' => 'display:none;'),
  7603. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7604. '/div'
  7605. );
  7606. $this->assertTags($result, $expected);
  7607. $this->Form->request['action'] = 'delete';
  7608. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  7609. $this->Form->request->base = '/base_url';
  7610. $result = $this->Form->create('Contact');
  7611. $expected = array(
  7612. 'form' => array(
  7613. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  7614. 'accept-charset' => 'utf-8'
  7615. ),
  7616. 'div' => array('style' => 'display:none;'),
  7617. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7618. '/div'
  7619. );
  7620. $this->assertTags($result, $expected);
  7621. }
  7622. /**
  7623. * test create() with a custom route
  7624. *
  7625. * @return void
  7626. */
  7627. public function testCreateCustomRoute() {
  7628. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7629. $encoding = strtolower(Configure::read('App.encoding'));
  7630. $result = $this->Form->create('User', array('action' => 'login'));
  7631. $expected = array(
  7632. 'form' => array(
  7633. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7634. 'accept-charset' => $encoding
  7635. ),
  7636. 'div' => array('style' => 'display:none;'),
  7637. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7638. '/div'
  7639. );
  7640. $this->assertTags($result, $expected);
  7641. }
  7642. /**
  7643. * test that inputDefaults are stored and used.
  7644. *
  7645. * @return void
  7646. */
  7647. public function testCreateWithInputDefaults() {
  7648. $this->Form->create('User', array(
  7649. 'inputDefaults' => array(
  7650. 'div' => false,
  7651. 'label' => false,
  7652. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7653. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7654. )
  7655. ));
  7656. $result = $this->Form->input('username');
  7657. $expected = array(
  7658. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  7659. );
  7660. $this->assertTags($result, $expected);
  7661. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7662. $expected = array(
  7663. 'div' => array('class' => 'input text'),
  7664. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7665. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7666. '/div'
  7667. );
  7668. $this->assertTags($result, $expected);
  7669. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7670. $expected = array(
  7671. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7672. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7673. );
  7674. $this->assertTags($result, $expected);
  7675. $this->Form->create('User', array(
  7676. 'inputDefaults' => array(
  7677. 'div' => false,
  7678. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7679. )
  7680. ));
  7681. $result = $this->Form->input('username', array('div' => true));
  7682. $expected = array(
  7683. 'div' => array('class' => 'input text'),
  7684. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7685. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  7686. '/div'
  7687. );
  7688. $this->assertTags($result, $expected);
  7689. }
  7690. /**
  7691. * test automatic accept-charset overriding
  7692. *
  7693. * @return void
  7694. */
  7695. public function testCreateWithAcceptCharset() {
  7696. $result = $this->Form->create('UserForm', array(
  7697. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7698. )
  7699. );
  7700. $expected = array(
  7701. 'form' => array(
  7702. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7703. 'accept-charset' => 'iso-8859-1'
  7704. ),
  7705. 'div' => array('style' => 'display:none;'),
  7706. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7707. '/div'
  7708. );
  7709. $this->assertTags($result, $expected);
  7710. }
  7711. /**
  7712. * Test base form URL when url param is passed with multiple parameters (&)
  7713. *
  7714. * @return void
  7715. */
  7716. public function testCreateQuerystringrequest() {
  7717. $encoding = strtolower(Configure::read('App.encoding'));
  7718. $result = $this->Form->create('Contact', array(
  7719. 'type' => 'post',
  7720. 'escape' => false,
  7721. 'url' => array(
  7722. 'controller' => 'controller',
  7723. 'action' => 'action',
  7724. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7725. )
  7726. ));
  7727. $expected = array(
  7728. 'form' => array(
  7729. 'id' => 'ContactAddForm',
  7730. 'method' => 'post',
  7731. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7732. 'accept-charset' => $encoding
  7733. ),
  7734. 'div' => array('style' => 'display:none;'),
  7735. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7736. '/div'
  7737. );
  7738. $this->assertTags($result, $expected);
  7739. $result = $this->Form->create('Contact', array(
  7740. 'type' => 'post',
  7741. 'url' => array(
  7742. 'controller' => 'controller',
  7743. 'action' => 'action',
  7744. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7745. )
  7746. ));
  7747. $expected = array(
  7748. 'form' => array(
  7749. 'id' => 'ContactAddForm',
  7750. 'method' => 'post',
  7751. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7752. 'accept-charset' => $encoding
  7753. ),
  7754. 'div' => array('style' => 'display:none;'),
  7755. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7756. '/div'
  7757. );
  7758. $this->assertTags($result, $expected);
  7759. }
  7760. /**
  7761. * test that create() doesn't cause errors by multiple id's being in the primary key
  7762. * as could happen with multiple select or checkboxes.
  7763. *
  7764. * @return void
  7765. */
  7766. public function testCreateWithMultipleIdInData() {
  7767. $encoding = strtolower(Configure::read('App.encoding'));
  7768. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7769. $result = $this->Form->create('Contact');
  7770. $expected = array(
  7771. 'form' => array(
  7772. 'id' => 'ContactAddForm',
  7773. 'method' => 'post',
  7774. 'action' => '/contacts/add',
  7775. 'accept-charset' => $encoding
  7776. ),
  7777. 'div' => array('style' => 'display:none;'),
  7778. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7779. '/div'
  7780. );
  7781. $this->assertTags($result, $expected);
  7782. }
  7783. /**
  7784. * test that create() doesn't add in extra passed params.
  7785. *
  7786. * @return void
  7787. */
  7788. public function testCreatePassedArgs() {
  7789. $encoding = strtolower(Configure::read('App.encoding'));
  7790. $this->Form->request->data['Contact']['id'] = 1;
  7791. $result = $this->Form->create('Contact', array(
  7792. 'type' => 'post',
  7793. 'escape' => false,
  7794. 'url' => array(
  7795. 'action' => 'edit',
  7796. 'myparam'
  7797. )
  7798. ));
  7799. $expected = array(
  7800. 'form' => array(
  7801. 'id' => 'ContactAddForm',
  7802. 'method' => 'post',
  7803. 'action' => '/contacts/edit/myparam',
  7804. 'accept-charset' => $encoding
  7805. ),
  7806. 'div' => array('style' => 'display:none;'),
  7807. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7808. '/div'
  7809. );
  7810. $this->assertTags($result, $expected);
  7811. }
  7812. /**
  7813. * test creating a get form, and get form inputs.
  7814. *
  7815. * @return void
  7816. */
  7817. public function testGetFormCreate() {
  7818. $encoding = strtolower(Configure::read('App.encoding'));
  7819. $result = $this->Form->create('Contact', array('type' => 'get'));
  7820. $this->assertTags($result, array('form' => array(
  7821. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7822. 'accept-charset' => $encoding
  7823. )));
  7824. $result = $this->Form->text('Contact.name');
  7825. $this->assertTags($result, array('input' => array(
  7826. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7827. )));
  7828. $result = $this->Form->password('password');
  7829. $this->assertTags($result, array('input' => array(
  7830. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7831. )));
  7832. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7833. $result = $this->Form->text('user_form');
  7834. $this->assertTags($result, array('input' => array(
  7835. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7836. )));
  7837. }
  7838. /**
  7839. * test get form, and inputs when the model param is false
  7840. *
  7841. * @return void
  7842. */
  7843. public function testGetFormWithFalseModel() {
  7844. $encoding = strtolower(Configure::read('App.encoding'));
  7845. $this->Form->request['controller'] = 'contact_test';
  7846. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7847. $expected = array('form' => array(
  7848. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7849. 'accept-charset' => $encoding
  7850. ));
  7851. $this->assertTags($result, $expected);
  7852. $result = $this->Form->text('reason');
  7853. $expected = array(
  7854. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7855. );
  7856. $this->assertTags($result, $expected);
  7857. }
  7858. /**
  7859. * test that datetime() works with GET style forms.
  7860. *
  7861. * @return void
  7862. */
  7863. public function testDateTimeWithGetForms() {
  7864. extract($this->dateRegex);
  7865. $this->Form->create('Contact', array('type' => 'get'));
  7866. $result = $this->Form->datetime('created');
  7867. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7868. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7869. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7870. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7871. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7872. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7873. }
  7874. /**
  7875. * testEditFormWithData method
  7876. *
  7877. * test auto populating form elements from submitted data.
  7878. *
  7879. * @return void
  7880. */
  7881. public function testEditFormWithData() {
  7882. $this->Form->request->data = array('Person' => array(
  7883. 'id' => 1,
  7884. 'first_name' => 'Nate',
  7885. 'last_name' => 'Abele',
  7886. 'email' => 'nate@example.com'
  7887. ));
  7888. $this->Form->request->addParams(array(
  7889. 'models' => array('Person'),
  7890. 'controller' => 'people',
  7891. 'action' => 'add'
  7892. ));
  7893. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7894. $this->Form->create();
  7895. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7896. $expected = array(
  7897. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7898. 'select' => array(
  7899. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7900. ),
  7901. array('option' => array('value' => 1)), 'Nate', '/option',
  7902. array('option' => array('value' => 2)), 'Garrett', '/option',
  7903. array('option' => array('value' => 3)), 'Larry', '/option',
  7904. '/select'
  7905. );
  7906. $this->assertTags($result, $expected);
  7907. }
  7908. /**
  7909. * Test that required fields are created for various types of validation.
  7910. *
  7911. * @return void
  7912. */
  7913. public function testFormInputRequiredDetection() {
  7914. $this->Form->create('Contact');
  7915. $result = $this->Form->input('Contact.non_existing');
  7916. $expected = array(
  7917. 'div' => array('class' => 'input text'),
  7918. 'label' => array('for' => 'ContactNonExisting'),
  7919. 'Non Existing',
  7920. '/label',
  7921. 'input' => array(
  7922. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  7923. 'id' => 'ContactNonExisting'
  7924. ),
  7925. '/div'
  7926. );
  7927. $this->assertTags($result, $expected);
  7928. $result = $this->Form->input('Contact.imrequired');
  7929. $expected = array(
  7930. 'div' => array('class' => 'input text required'),
  7931. 'label' => array('for' => 'ContactImrequired'),
  7932. 'Imrequired',
  7933. '/label',
  7934. 'input' => array(
  7935. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7936. 'id' => 'ContactImrequired',
  7937. 'required' => 'required'
  7938. ),
  7939. '/div'
  7940. );
  7941. $this->assertTags($result, $expected);
  7942. $result = $this->Form->input('Contact.imalsorequired');
  7943. $expected = array(
  7944. 'div' => array('class' => 'input text required'),
  7945. 'label' => array('for' => 'ContactImalsorequired'),
  7946. 'Imalsorequired',
  7947. '/label',
  7948. 'input' => array(
  7949. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  7950. 'id' => 'ContactImalsorequired',
  7951. 'required' => 'required'
  7952. ),
  7953. '/div'
  7954. );
  7955. $this->assertTags($result, $expected);
  7956. $result = $this->Form->input('Contact.imrequiredtoo');
  7957. $expected = array(
  7958. 'div' => array('class' => 'input text required'),
  7959. 'label' => array('for' => 'ContactImrequiredtoo'),
  7960. 'Imrequiredtoo',
  7961. '/label',
  7962. 'input' => array(
  7963. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  7964. 'id' => 'ContactImrequiredtoo',
  7965. 'required' => 'required'
  7966. ),
  7967. '/div'
  7968. );
  7969. $this->assertTags($result, $expected);
  7970. $result = $this->Form->input('Contact.required_one');
  7971. $expected = array(
  7972. 'div' => array('class' => 'input text required'),
  7973. 'label' => array('for' => 'ContactRequiredOne'),
  7974. 'Required One',
  7975. '/label',
  7976. 'input' => array(
  7977. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  7978. 'id' => 'ContactRequiredOne',
  7979. 'required' => 'required'
  7980. ),
  7981. '/div'
  7982. );
  7983. $this->assertTags($result, $expected);
  7984. $result = $this->Form->input('Contact.string_required');
  7985. $expected = array(
  7986. 'div' => array('class' => 'input text required'),
  7987. 'label' => array('for' => 'ContactStringRequired'),
  7988. 'String Required',
  7989. '/label',
  7990. 'input' => array(
  7991. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  7992. 'id' => 'ContactStringRequired',
  7993. 'required' => 'required'
  7994. ),
  7995. '/div'
  7996. );
  7997. $this->assertTags($result, $expected);
  7998. $result = $this->Form->input('Contact.imnotrequired');
  7999. $expected = array(
  8000. 'div' => array('class' => 'input text'),
  8001. 'label' => array('for' => 'ContactImnotrequired'),
  8002. 'Imnotrequired',
  8003. '/label',
  8004. 'input' => array(
  8005. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  8006. 'id' => 'ContactImnotrequired'
  8007. ),
  8008. '/div'
  8009. );
  8010. $this->assertTags($result, $expected);
  8011. $result = $this->Form->input('Contact.imalsonotrequired');
  8012. $expected = array(
  8013. 'div' => array('class' => 'input text'),
  8014. 'label' => array('for' => 'ContactImalsonotrequired'),
  8015. 'Imalsonotrequired',
  8016. '/label',
  8017. 'input' => array(
  8018. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  8019. 'id' => 'ContactImalsonotrequired'
  8020. ),
  8021. '/div'
  8022. );
  8023. $this->assertTags($result, $expected);
  8024. $result = $this->Form->input('Contact.imalsonotrequired2');
  8025. $expected = array(
  8026. 'div' => array('class' => 'input text'),
  8027. 'label' => array('for' => 'ContactImalsonotrequired2'),
  8028. 'Imalsonotrequired2',
  8029. '/label',
  8030. 'input' => array(
  8031. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  8032. 'id' => 'ContactImalsonotrequired2'
  8033. ),
  8034. '/div'
  8035. );
  8036. $this->assertTags($result, $expected);
  8037. $result = $this->Form->input('Contact.imnotrequiredeither');
  8038. $expected = array(
  8039. 'div' => array('class' => 'input text'),
  8040. 'label' => array('for' => 'ContactImnotrequiredeither'),
  8041. 'Imnotrequiredeither',
  8042. '/label',
  8043. 'input' => array(
  8044. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  8045. 'id' => 'ContactImnotrequiredeither'
  8046. ),
  8047. '/div'
  8048. );
  8049. $this->assertTags($result, $expected);
  8050. $result = $this->Form->input('Contact.iamrequiredalways');
  8051. $expected = array(
  8052. 'div' => array('class' => 'input text required'),
  8053. 'label' => array('for' => 'ContactIamrequiredalways'),
  8054. 'Iamrequiredalways',
  8055. '/label',
  8056. 'input' => array(
  8057. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  8058. 'id' => 'ContactIamrequiredalways',
  8059. 'required' => 'required'
  8060. ),
  8061. '/div'
  8062. );
  8063. $this->assertTags($result, $expected);
  8064. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  8065. $expected = array(
  8066. 'div' => array('class' => 'input checkbox required'),
  8067. array('input' => array(
  8068. 'type' => 'hidden',
  8069. 'name' => 'data[Contact][boolean_field]',
  8070. 'id' => 'ContactBooleanField_',
  8071. 'value' => '0'
  8072. )),
  8073. array('input' => array(
  8074. 'type' => 'checkbox',
  8075. 'name' => 'data[Contact][boolean_field]',
  8076. 'value' => '1',
  8077. 'id' => 'ContactBooleanField'
  8078. )),
  8079. 'label' => array('for' => 'ContactBooleanField'),
  8080. 'Boolean Field',
  8081. '/label',
  8082. '/div'
  8083. );
  8084. $this->assertTags($result, $expected);
  8085. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  8086. $expected = array(
  8087. 'div' => array('class' => 'input checkbox required'),
  8088. array('input' => array(
  8089. 'type' => 'hidden',
  8090. 'name' => 'data[Contact][boolean_field]',
  8091. 'id' => 'ContactBooleanField_',
  8092. 'value' => '0'
  8093. )),
  8094. array('input' => array(
  8095. 'type' => 'checkbox',
  8096. 'name' => 'data[Contact][boolean_field]',
  8097. 'value' => '1',
  8098. 'id' => 'ContactBooleanField',
  8099. 'required' => 'required'
  8100. )),
  8101. 'label' => array('for' => 'ContactBooleanField'),
  8102. 'Boolean Field',
  8103. '/label',
  8104. '/div'
  8105. );
  8106. $this->assertTags($result, $expected);
  8107. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  8108. $expected = array(
  8109. 'div' => array('class' => 'input file required'),
  8110. 'label' => array('for' => 'ContactIamrequiredalways'),
  8111. 'Iamrequiredalways',
  8112. '/label',
  8113. 'input' => array(
  8114. 'type' => 'file',
  8115. 'name' => 'data[Contact][iamrequiredalways]',
  8116. 'id' => 'ContactIamrequiredalways',
  8117. 'required' => 'required'
  8118. ),
  8119. '/div'
  8120. );
  8121. $this->assertTags($result, $expected);
  8122. }
  8123. /**
  8124. * Test that required fields are created when only using ModelValidator::add().
  8125. *
  8126. * @return void
  8127. */
  8128. public function testFormInputRequiredDetectionModelValidator() {
  8129. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  8130. $this->Form->create('ContactTag');
  8131. $result = $this->Form->input('ContactTag.iwillberequired');
  8132. $expected = array(
  8133. 'div' => array('class' => 'input text required'),
  8134. 'label' => array('for' => 'ContactTagIwillberequired'),
  8135. 'Iwillberequired',
  8136. '/label',
  8137. 'input' => array(
  8138. 'name' => 'data[ContactTag][iwillberequired]',
  8139. 'type' => 'text',
  8140. 'id' => 'ContactTagIwillberequired',
  8141. 'required' => 'required'
  8142. ),
  8143. '/div'
  8144. );
  8145. $this->assertTags($result, $expected);
  8146. }
  8147. /**
  8148. * testFormMagicInput method
  8149. *
  8150. * @return void
  8151. */
  8152. public function testFormMagicInput() {
  8153. $encoding = strtolower(Configure::read('App.encoding'));
  8154. $result = $this->Form->create('Contact');
  8155. $expected = array(
  8156. 'form' => array(
  8157. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8158. 'accept-charset' => $encoding
  8159. ),
  8160. 'div' => array('style' => 'display:none;'),
  8161. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8162. '/div'
  8163. );
  8164. $this->assertTags($result, $expected);
  8165. $result = $this->Form->input('name');
  8166. $expected = array(
  8167. 'div' => array('class' => 'input text'),
  8168. 'label' => array('for' => 'ContactName'),
  8169. 'Name',
  8170. '/label',
  8171. 'input' => array(
  8172. 'type' => 'text', 'name' => 'data[Contact][name]',
  8173. 'id' => 'ContactName', 'maxlength' => '255'
  8174. ),
  8175. '/div'
  8176. );
  8177. $this->assertTags($result, $expected);
  8178. $result = $this->Form->input('non_existing_field_in_contact_model');
  8179. $expected = array(
  8180. 'div' => array('class' => 'input text'),
  8181. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  8182. 'Non Existing Field In Contact Model',
  8183. '/label',
  8184. 'input' => array(
  8185. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  8186. 'id' => 'ContactNonExistingFieldInContactModel'
  8187. ),
  8188. '/div'
  8189. );
  8190. $this->assertTags($result, $expected);
  8191. $result = $this->Form->input('Address.street');
  8192. $expected = array(
  8193. 'div' => array('class' => 'input text'),
  8194. 'label' => array('for' => 'AddressStreet'),
  8195. 'Street',
  8196. '/label',
  8197. 'input' => array(
  8198. 'type' => 'text', 'name' => 'data[Address][street]',
  8199. 'id' => 'AddressStreet'
  8200. ),
  8201. '/div'
  8202. );
  8203. $this->assertTags($result, $expected);
  8204. $result = $this->Form->input('Address.non_existing_field_in_model');
  8205. $expected = array(
  8206. 'div' => array('class' => 'input text'),
  8207. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  8208. 'Non Existing Field In Model',
  8209. '/label',
  8210. 'input' => array(
  8211. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  8212. 'id' => 'AddressNonExistingFieldInModel'
  8213. ),
  8214. '/div'
  8215. );
  8216. $this->assertTags($result, $expected);
  8217. $result = $this->Form->input('name', array('div' => false));
  8218. $expected = array(
  8219. 'label' => array('for' => 'ContactName'),
  8220. 'Name',
  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. extract($this->dateRegex);
  8229. $now = strtotime('now');
  8230. $result = $this->Form->input('Contact.published', array('div' => false));
  8231. $expected = array(
  8232. 'label' => array('for' => 'ContactPublishedMonth'),
  8233. 'Published',
  8234. '/label',
  8235. array('select' => array(
  8236. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  8237. )),
  8238. $monthsRegex,
  8239. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8240. date('F', $now),
  8241. '/option',
  8242. '*/select',
  8243. '-',
  8244. array('select' => array(
  8245. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  8246. )),
  8247. $daysRegex,
  8248. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8249. date('j', $now),
  8250. '/option',
  8251. '*/select',
  8252. '-',
  8253. array('select' => array(
  8254. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  8255. )),
  8256. $yearsRegex,
  8257. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8258. date('Y', $now),
  8259. '*/select'
  8260. );
  8261. $this->assertTags($result, $expected);
  8262. $result = $this->Form->input('Contact.updated', array('div' => false));
  8263. $expected = array(
  8264. 'label' => array('for' => 'ContactUpdatedMonth'),
  8265. 'Updated',
  8266. '/label',
  8267. array('select' => array(
  8268. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  8269. )),
  8270. $monthsRegex,
  8271. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  8272. date('F', $now),
  8273. '/option',
  8274. '*/select',
  8275. '-',
  8276. array('select' => array(
  8277. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  8278. )),
  8279. $daysRegex,
  8280. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  8281. date('j', $now),
  8282. '/option',
  8283. '*/select',
  8284. '-',
  8285. array('select' => array(
  8286. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  8287. )),
  8288. $yearsRegex,
  8289. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  8290. date('Y', $now),
  8291. '*/select'
  8292. );
  8293. $this->assertTags($result, $expected);
  8294. $result = $this->Form->input('UserForm.stuff');
  8295. $expected = array(
  8296. 'div' => array('class' => 'input text'),
  8297. 'label' => array('for' => 'UserFormStuff'),
  8298. 'Stuff',
  8299. '/label',
  8300. 'input' => array(
  8301. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  8302. 'id' => 'UserFormStuff', 'maxlength' => 10
  8303. ),
  8304. '/div'
  8305. );
  8306. $this->assertTags($result, $expected);
  8307. }
  8308. /**
  8309. * testForMagicInputNonExistingNorValidated method
  8310. *
  8311. * @return void
  8312. */
  8313. public function testForMagicInputNonExistingNorValidated() {
  8314. $encoding = strtolower(Configure::read('App.encoding'));
  8315. $result = $this->Form->create('Contact');
  8316. $expected = array(
  8317. 'form' => array(
  8318. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8319. 'accept-charset' => $encoding
  8320. ),
  8321. 'div' => array('style' => 'display:none;'),
  8322. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8323. '/div'
  8324. );
  8325. $this->assertTags($result, $expected);
  8326. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8327. $expected = array(
  8328. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8329. 'Non Existing Nor Validated',
  8330. '/label',
  8331. 'input' => array(
  8332. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8333. 'id' => 'ContactNonExistingNorValidated'
  8334. )
  8335. );
  8336. $this->assertTags($result, $expected);
  8337. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  8338. 'div' => false, 'value' => 'my value'
  8339. ));
  8340. $expected = array(
  8341. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8342. 'Non Existing Nor Validated',
  8343. '/label',
  8344. 'input' => array(
  8345. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8346. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  8347. )
  8348. );
  8349. $this->assertTags($result, $expected);
  8350. $this->Form->request->data = array(
  8351. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  8352. ));
  8353. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  8354. $expected = array(
  8355. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  8356. 'Non Existing Nor Validated',
  8357. '/label',
  8358. 'input' => array(
  8359. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  8360. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  8361. )
  8362. );
  8363. $this->assertTags($result, $expected);
  8364. }
  8365. /**
  8366. * testFormMagicInputLabel method
  8367. *
  8368. * @return void
  8369. */
  8370. public function testFormMagicInputLabel() {
  8371. $encoding = strtolower(Configure::read('App.encoding'));
  8372. $result = $this->Form->create('Contact');
  8373. $expected = array(
  8374. 'form' => array(
  8375. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  8376. 'accept-charset' => $encoding
  8377. ),
  8378. 'div' => array('style' => 'display:none;'),
  8379. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  8380. '/div'
  8381. );
  8382. $this->assertTags($result, $expected);
  8383. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  8384. $this->assertTags($result, array('input' => array(
  8385. 'name' => 'data[Contact][name]', 'type' => 'text',
  8386. 'id' => 'ContactName', 'maxlength' => '255')
  8387. ));
  8388. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  8389. $expected = array(
  8390. 'label' => array('for' => 'ContactName'),
  8391. 'My label',
  8392. '/label',
  8393. 'input' => array(
  8394. 'type' => 'text', 'name' => 'data[Contact][name]',
  8395. 'id' => 'ContactName', 'maxlength' => '255'
  8396. )
  8397. );
  8398. $this->assertTags($result, $expected);
  8399. $result = $this->Form->input('Contact.name', array(
  8400. 'div' => false, 'label' => array('class' => 'mandatory')
  8401. ));
  8402. $expected = array(
  8403. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8404. 'Name',
  8405. '/label',
  8406. 'input' => array(
  8407. 'type' => 'text', 'name' => 'data[Contact][name]',
  8408. 'id' => 'ContactName', 'maxlength' => '255'
  8409. )
  8410. );
  8411. $this->assertTags($result, $expected);
  8412. $result = $this->Form->input('Contact.name', array(
  8413. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  8414. ));
  8415. $expected = array(
  8416. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  8417. 'My label',
  8418. '/label',
  8419. 'input' => array(
  8420. 'type' => 'text', 'name' => 'data[Contact][name]',
  8421. 'id' => 'ContactName', 'maxlength' => '255'
  8422. )
  8423. );
  8424. $this->assertTags($result, $expected);
  8425. $result = $this->Form->input('Contact.name', array(
  8426. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  8427. ));
  8428. $expected = array(
  8429. 'label' => array('for' => 'my_id'),
  8430. 'Name',
  8431. '/label',
  8432. 'input' => array(
  8433. 'type' => 'text', 'name' => 'data[Contact][name]',
  8434. 'id' => 'my_id', 'maxlength' => '255'
  8435. )
  8436. );
  8437. $this->assertTags($result, $expected);
  8438. $result = $this->Form->input('1.id');
  8439. $this->assertTags($result, array('input' => array(
  8440. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8441. 'id' => 'Contact1Id'
  8442. )));
  8443. $result = $this->Form->input("1.name");
  8444. $expected = array(
  8445. 'div' => array('class' => 'input text'),
  8446. 'label' => array('for' => 'Contact1Name'),
  8447. 'Name',
  8448. '/label',
  8449. 'input' => array(
  8450. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  8451. 'id' => 'Contact1Name', 'maxlength' => '255'
  8452. ),
  8453. '/div'
  8454. );
  8455. $this->assertTags($result, $expected);
  8456. $result = $this->Form->input('Contact.1.id');
  8457. $this->assertTags($result, array(
  8458. 'input' => array(
  8459. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  8460. 'id' => 'Contact1Id'
  8461. )
  8462. ));
  8463. $result = $this->Form->input("Model.1.name");
  8464. $expected = array(
  8465. 'div' => array('class' => 'input text'),
  8466. 'label' => array('for' => 'Model1Name'),
  8467. 'Name',
  8468. '/label',
  8469. 'input' => array(
  8470. 'type' => 'text', 'name' => 'data[Model][1][name]',
  8471. 'id' => 'Model1Name'
  8472. ),
  8473. '/div'
  8474. );
  8475. $this->assertTags($result, $expected);
  8476. }
  8477. /**
  8478. * testFormEnd method
  8479. *
  8480. * @return void
  8481. */
  8482. public function testFormEnd() {
  8483. $this->assertEquals('</form>', $this->Form->end());
  8484. $result = $this->Form->end('', array('form' => 'form-name'));
  8485. $expected = array(
  8486. 'div' => array('class' => 'submit'),
  8487. 'input' => array('type' => 'submit', 'value' => ''),
  8488. '/div',
  8489. '/form'
  8490. );
  8491. $this->assertTags($result, $expected);
  8492. $result = $this->Form->end('');
  8493. $expected = array(
  8494. 'div' => array('class' => 'submit'),
  8495. 'input' => array('type' => 'submit', 'value' => ''),
  8496. '/div',
  8497. '/form'
  8498. );
  8499. $this->assertTags($result, $expected);
  8500. $result = $this->Form->end(array('label' => ''));
  8501. $expected = array(
  8502. 'div' => array('class' => 'submit'),
  8503. 'input' => array('type' => 'submit', 'value' => ''),
  8504. '/div',
  8505. '/form'
  8506. );
  8507. $this->assertTags($result, $expected);
  8508. $result = $this->Form->end('save');
  8509. $expected = array(
  8510. 'div' => array('class' => 'submit'),
  8511. 'input' => array('type' => 'submit', 'value' => 'save'),
  8512. '/div',
  8513. '/form'
  8514. );
  8515. $this->assertTags($result, $expected);
  8516. $result = $this->Form->end(array('label' => 'save'));
  8517. $expected = array(
  8518. 'div' => array('class' => 'submit'),
  8519. 'input' => array('type' => 'submit', 'value' => 'save'),
  8520. '/div',
  8521. '/form'
  8522. );
  8523. $this->assertTags($result, $expected);
  8524. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  8525. $expected = array(
  8526. 'div' => array('class' => 'submit'),
  8527. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8528. '/div',
  8529. '/form'
  8530. );
  8531. $this->assertTags($result, $expected);
  8532. $result = $this->Form->end(array('name' => 'Whatever'));
  8533. $expected = array(
  8534. 'div' => array('class' => 'submit'),
  8535. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  8536. '/div',
  8537. '/form'
  8538. );
  8539. $this->assertTags($result, $expected);
  8540. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  8541. $expected = array(
  8542. 'div' => array('class' => 'good'),
  8543. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8544. '/div',
  8545. '/form'
  8546. );
  8547. $this->assertTags($result, $expected);
  8548. $result = $this->Form->end(array(
  8549. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  8550. ));
  8551. $expected = array(
  8552. 'div' => array('class' => 'good'),
  8553. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  8554. '/div',
  8555. '/form'
  8556. );
  8557. $this->assertTags($result, $expected);
  8558. }
  8559. /**
  8560. * testMultipleFormWithIdFields method
  8561. *
  8562. * @return void
  8563. */
  8564. public function testMultipleFormWithIdFields() {
  8565. $this->Form->create('UserForm');
  8566. $result = $this->Form->input('id');
  8567. $this->assertTags($result, array('input' => array(
  8568. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  8569. )));
  8570. $result = $this->Form->input('ValidateItem.id');
  8571. $this->assertTags($result, array('input' => array(
  8572. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  8573. 'id' => 'ValidateItemId'
  8574. )));
  8575. $result = $this->Form->input('ValidateUser.id');
  8576. $this->assertTags($result, array('input' => array(
  8577. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  8578. 'id' => 'ValidateUserId'
  8579. )));
  8580. }
  8581. /**
  8582. * testDbLessModel method
  8583. *
  8584. * @return void
  8585. */
  8586. public function testDbLessModel() {
  8587. $this->Form->create('TestMail');
  8588. $result = $this->Form->input('name');
  8589. $expected = array(
  8590. 'div' => array('class' => 'input text'),
  8591. 'label' => array('for' => 'TestMailName'),
  8592. 'Name',
  8593. '/label',
  8594. 'input' => array(
  8595. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8596. 'id' => 'TestMailName'
  8597. ),
  8598. '/div'
  8599. );
  8600. $this->assertTags($result, $expected);
  8601. ClassRegistry::init('TestMail');
  8602. $this->Form->create('TestMail');
  8603. $result = $this->Form->input('name');
  8604. $expected = array(
  8605. 'div' => array('class' => 'input text'),
  8606. 'label' => array('for' => 'TestMailName'),
  8607. 'Name',
  8608. '/label',
  8609. 'input' => array(
  8610. 'name' => 'data[TestMail][name]', 'type' => 'text',
  8611. 'id' => 'TestMailName'
  8612. ),
  8613. '/div'
  8614. );
  8615. $this->assertTags($result, $expected);
  8616. }
  8617. /**
  8618. * testBrokenness method
  8619. *
  8620. * @return void
  8621. */
  8622. public function testBrokenness() {
  8623. /*
  8624. * #4 This test has two parents and four children. By default (as of r7117) both
  8625. * parents are show but the first parent is missing a child. This is the inconsistency
  8626. * in the default behaviour - one parent has all children, the other does not - dependent
  8627. * on the data values.
  8628. */
  8629. $result = $this->Form->select('Model.field', array(
  8630. 'Fred' => array(
  8631. 'freds_son_1' => 'Fred',
  8632. 'freds_son_2' => 'Freddie'
  8633. ),
  8634. 'Bert' => array(
  8635. 'berts_son_1' => 'Albert',
  8636. 'berts_son_2' => 'Bertie')
  8637. ),
  8638. array('showParents' => true, 'empty' => false)
  8639. );
  8640. $expected = array(
  8641. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8642. array('optgroup' => array('label' => 'Fred')),
  8643. array('option' => array('value' => 'freds_son_1')),
  8644. 'Fred',
  8645. '/option',
  8646. array('option' => array('value' => 'freds_son_2')),
  8647. 'Freddie',
  8648. '/option',
  8649. '/optgroup',
  8650. array('optgroup' => array('label' => 'Bert')),
  8651. array('option' => array('value' => 'berts_son_1')),
  8652. 'Albert',
  8653. '/option',
  8654. array('option' => array('value' => 'berts_son_2')),
  8655. 'Bertie',
  8656. '/option',
  8657. '/optgroup',
  8658. '/select'
  8659. );
  8660. $this->assertTags($result, $expected);
  8661. /*
  8662. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8663. * changed, so we should expect the same select list data, just with a different name
  8664. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8665. * This is where data corruption can occur, because when a select value is missing from
  8666. * a list a form will substitute the first value in the list - without the user knowing.
  8667. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8668. * affect the availability of 3 => 'Three' as a valid option.
  8669. */
  8670. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8671. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8672. ));
  8673. $result = $this->Form->select(
  8674. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8675. );
  8676. $expected = array(
  8677. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  8678. array('option' => array('value' => 1)),
  8679. 'One',
  8680. '/option',
  8681. array('option' => array('value' => 2)),
  8682. 'Two',
  8683. '/option',
  8684. array('optgroup' => array('label' => 'Three')),
  8685. array('option' => array('value' => 3)),
  8686. 'Three',
  8687. '/option',
  8688. array('option' => array('value' => 4)),
  8689. 'Four',
  8690. '/option',
  8691. array('option' => array('value' => 5)),
  8692. 'Five',
  8693. '/option',
  8694. '/optgroup',
  8695. '/select'
  8696. );
  8697. $this->assertTags($result, $expected);
  8698. }
  8699. /**
  8700. * Test the generation of fields for a multi record form.
  8701. *
  8702. * @return void
  8703. */
  8704. public function testMultiRecordForm() {
  8705. $this->Form->create('ValidateProfile');
  8706. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8707. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8708. $expected = array(
  8709. 'div' => array('class' => 'input textarea'),
  8710. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8711. 'Name',
  8712. '/label',
  8713. 'textarea' => array(
  8714. 'id' => 'ValidateProfile1ValidateItem2Name',
  8715. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  8716. 'cols' => 30,
  8717. 'rows' => 6
  8718. ),
  8719. 'Value',
  8720. '/textarea',
  8721. '/div'
  8722. );
  8723. $this->assertTags($result, $expected);
  8724. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8725. $expected = array(
  8726. 'div' => array('class' => 'input date'),
  8727. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8728. 'Created',
  8729. '/label',
  8730. array('select' => array(
  8731. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  8732. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8733. )
  8734. ),
  8735. array('option' => array('value' => '')), '/option',
  8736. $this->dateRegex['monthsRegex'],
  8737. '/select', '-',
  8738. array('select' => array(
  8739. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  8740. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8741. )
  8742. ),
  8743. array('option' => array('value' => '')), '/option',
  8744. $this->dateRegex['daysRegex'],
  8745. '/select', '-',
  8746. array('select' => array(
  8747. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  8748. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8749. )
  8750. ),
  8751. array('option' => array('value' => '')), '/option',
  8752. $this->dateRegex['yearsRegex'],
  8753. '/select',
  8754. '/div'
  8755. );
  8756. $this->assertTags($result, $expected);
  8757. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8758. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8759. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8760. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8761. $expected = array(
  8762. 'div' => array('class' => 'input select error'),
  8763. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8764. 'Profile',
  8765. '/label',
  8766. 'select' => array(
  8767. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  8768. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8769. 'class' => 'form-error'
  8770. ),
  8771. '/select',
  8772. array('div' => array('class' => 'error-message')),
  8773. 'Error',
  8774. '/div',
  8775. '/div'
  8776. );
  8777. $this->assertTags($result, $expected);
  8778. }
  8779. /**
  8780. * test the correct display of multi-record form validation errors.
  8781. *
  8782. * @return void
  8783. */
  8784. public function testMultiRecordFormValidationErrors() {
  8785. $this->Form->create('ValidateProfile');
  8786. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8787. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8788. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8789. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8790. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8791. $result = $this->Form->error('ValidateProfile.2.city');
  8792. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8793. $result = $this->Form->error('2.city');
  8794. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8795. }
  8796. /**
  8797. * test the correct display of multi-record form validation errors.
  8798. *
  8799. * @return void
  8800. */
  8801. public function testSaveManyRecordFormValidationErrors() {
  8802. $this->Form->create('ValidateUser');
  8803. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8804. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8805. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8806. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8807. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8808. $result = $this->Form->error('ValidateUser.0.city');
  8809. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8810. }
  8811. /**
  8812. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8813. *
  8814. * @return void
  8815. */
  8816. public function testInputTemplate() {
  8817. $result = $this->Form->input('Contact.email', array(
  8818. 'type' => 'text', 'format' => array('input')
  8819. ));
  8820. $expected = array(
  8821. 'div' => array('class' => 'input text'),
  8822. 'input' => array(
  8823. 'type' => 'text', 'name' => 'data[Contact][email]',
  8824. 'id' => 'ContactEmail'
  8825. ),
  8826. '/div'
  8827. );
  8828. $this->assertTags($result, $expected);
  8829. $result = $this->Form->input('Contact.email', array(
  8830. 'type' => 'text', 'format' => array('input', 'label'),
  8831. 'label' => '<em>Email (required)</em>'
  8832. ));
  8833. $expected = array(
  8834. 'div' => array('class' => 'input text'),
  8835. array('input' => array(
  8836. 'type' => 'text', 'name' => 'data[Contact][email]',
  8837. 'id' => 'ContactEmail'
  8838. )),
  8839. 'label' => array('for' => 'ContactEmail'),
  8840. 'em' => array(),
  8841. 'Email (required)',
  8842. '/em',
  8843. '/label',
  8844. '/div'
  8845. );
  8846. $this->assertTags($result, $expected);
  8847. $result = $this->Form->input('Contact.email', array(
  8848. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8849. 'between' => '<div>Something in the middle</div>',
  8850. 'after' => '<span>Some text at the end</span>'
  8851. ));
  8852. $expected = array(
  8853. 'div' => array('class' => 'input text'),
  8854. array('input' => array(
  8855. 'type' => 'text', 'name' => 'data[Contact][email]',
  8856. 'id' => 'ContactEmail'
  8857. )),
  8858. array('div' => array()),
  8859. 'Something in the middle',
  8860. '/div',
  8861. 'label' => array('for' => 'ContactEmail'),
  8862. 'Email',
  8863. '/label',
  8864. 'span' => array(),
  8865. 'Some text at the end',
  8866. '/span',
  8867. '/div'
  8868. );
  8869. $this->assertTags($result, $expected);
  8870. $result = $this->Form->input('Contact.method', array(
  8871. 'type' => 'radio',
  8872. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8873. 'between' => 'I am between',
  8874. ));
  8875. $expected = array(
  8876. 'div' => array('class' => 'input radio'),
  8877. 'fieldset' => array(),
  8878. 'legend' => array(),
  8879. 'Method',
  8880. '/legend',
  8881. 'I am between',
  8882. 'input' => array(
  8883. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  8884. 'value' => '', 'id' => 'ContactMethod_'
  8885. ),
  8886. array('input' => array(
  8887. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8888. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8889. )),
  8890. array('label' => array('for' => 'ContactMethodEmail')),
  8891. 'Email',
  8892. '/label',
  8893. array('input' => array(
  8894. 'type' => 'radio', 'name' => 'data[Contact][method]',
  8895. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8896. )),
  8897. array('label' => array('for' => 'ContactMethodPigeon')),
  8898. 'Pigeon',
  8899. '/label',
  8900. '/fieldset',
  8901. '/div',
  8902. );
  8903. $this->assertTags($result, $expected);
  8904. }
  8905. /**
  8906. * test that some html5 inputs + FormHelper::__call() work
  8907. *
  8908. * @return void
  8909. */
  8910. public function testHtml5Inputs() {
  8911. $result = $this->Form->email('User.email');
  8912. $expected = array(
  8913. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  8914. );
  8915. $this->assertTags($result, $expected);
  8916. $result = $this->Form->search('User.query');
  8917. $expected = array(
  8918. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  8919. );
  8920. $this->assertTags($result, $expected);
  8921. $result = $this->Form->search('User.query', array('value' => 'test'));
  8922. $expected = array(
  8923. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8924. );
  8925. $this->assertTags($result, $expected);
  8926. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8927. $expected = array(
  8928. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  8929. );
  8930. $this->assertTags($result, $expected);
  8931. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8932. $expected = array(
  8933. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8934. );
  8935. $this->assertTags($result, $expected);
  8936. }
  8937. /**
  8938. *
  8939. * @expectedException CakeException
  8940. * @return void
  8941. */
  8942. public function testHtml5InputException() {
  8943. $this->Form->email();
  8944. }
  8945. /**
  8946. * Tests that a model can be loaded from the model names passed in the request object
  8947. *
  8948. * @return void
  8949. */
  8950. public function testIntrospectModelFromRequest() {
  8951. $this->loadFixtures('Post');
  8952. App::build(array(
  8953. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  8954. ));
  8955. CakePlugin::load('TestPlugin');
  8956. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  8957. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8958. $this->Form->create('TestPluginPost');
  8959. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8960. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8961. CakePlugin::unload();
  8962. App::build();
  8963. }
  8964. /**
  8965. * Tests that it is possible to set the validation errors directly in the helper for a field
  8966. *
  8967. * @return void
  8968. */
  8969. public function testCustomValidationErrors() {
  8970. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8971. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8972. $this->assertEquals('Badness!', $result);
  8973. }
  8974. /**
  8975. * Tests that the 'on' key validates as expected on create
  8976. *
  8977. * @return void
  8978. */
  8979. public function testRequiredOnCreate() {
  8980. $this->Form->create('Contact');
  8981. $result = $this->Form->input('Contact.imrequiredonupdate');
  8982. $expected = array(
  8983. 'div' => array('class' => 'input text'),
  8984. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8985. 'Imrequiredonupdate',
  8986. '/label',
  8987. 'input' => array(
  8988. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  8989. 'id' => 'ContactImrequiredonupdate'
  8990. ),
  8991. '/div'
  8992. );
  8993. $this->assertTags($result, $expected);
  8994. $result = $this->Form->input('Contact.imrequiredoncreate');
  8995. $expected = array(
  8996. 'div' => array('class' => 'input text required'),
  8997. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8998. 'Imrequiredoncreate',
  8999. '/label',
  9000. 'input' => array(
  9001. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  9002. 'id' => 'ContactImrequiredoncreate',
  9003. 'required' => 'required'
  9004. ),
  9005. '/div'
  9006. );
  9007. $this->assertTags($result, $expected);
  9008. $result = $this->Form->input('Contact.imrequiredonboth');
  9009. $expected = array(
  9010. 'div' => array('class' => 'input text required'),
  9011. 'label' => array('for' => 'ContactImrequiredonboth'),
  9012. 'Imrequiredonboth',
  9013. '/label',
  9014. 'input' => array(
  9015. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  9016. 'id' => 'ContactImrequiredonboth',
  9017. 'required' => 'required'
  9018. ),
  9019. '/div'
  9020. );
  9021. $this->assertTags($result, $expected);
  9022. $this->Form->inputDefaults(array('required' => false));
  9023. $result = $this->Form->input('Contact.imrequired');
  9024. $expected = array(
  9025. 'div' => array('class' => 'input text'),
  9026. 'label' => array('for' => 'ContactImrequired'),
  9027. 'Imrequired',
  9028. '/label',
  9029. 'input' => array(
  9030. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9031. 'id' => 'ContactImrequired'
  9032. ),
  9033. '/div'
  9034. );
  9035. $this->assertTags($result, $expected);
  9036. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  9037. $this->assertTags($result, $expected);
  9038. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  9039. $expected = array(
  9040. 'div' => array('class' => 'input text required'),
  9041. 'label' => array('for' => 'ContactImrequired'),
  9042. 'Imrequired',
  9043. '/label',
  9044. 'input' => array(
  9045. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9046. 'id' => 'ContactImrequired'
  9047. ),
  9048. '/div'
  9049. );
  9050. $this->assertTags($result, $expected);
  9051. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  9052. $this->assertTags($result, $expected);
  9053. }
  9054. /**
  9055. * Tests that the 'on' key validates as expected on update
  9056. *
  9057. * @return void
  9058. */
  9059. public function testRequiredOnUpdate() {
  9060. $this->Form->request->data['Contact']['id'] = 1;
  9061. $this->Form->create('Contact');
  9062. $result = $this->Form->input('Contact.imrequiredonupdate');
  9063. $expected = array(
  9064. 'div' => array('class' => 'input text required'),
  9065. 'label' => array('for' => 'ContactImrequiredonupdate'),
  9066. 'Imrequiredonupdate',
  9067. '/label',
  9068. 'input' => array(
  9069. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  9070. 'id' => 'ContactImrequiredonupdate',
  9071. 'required' => 'required'
  9072. ),
  9073. '/div'
  9074. );
  9075. $this->assertTags($result, $expected);
  9076. $result = $this->Form->input('Contact.imrequiredoncreate');
  9077. $expected = array(
  9078. 'div' => array('class' => 'input text'),
  9079. 'label' => array('for' => 'ContactImrequiredoncreate'),
  9080. 'Imrequiredoncreate',
  9081. '/label',
  9082. 'input' => array(
  9083. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  9084. 'id' => 'ContactImrequiredoncreate'
  9085. ),
  9086. '/div'
  9087. );
  9088. $this->assertTags($result, $expected);
  9089. $result = $this->Form->input('Contact.imrequiredonboth');
  9090. $expected = array(
  9091. 'div' => array('class' => 'input text required'),
  9092. 'label' => array('for' => 'ContactImrequiredonboth'),
  9093. 'Imrequiredonboth',
  9094. '/label',
  9095. 'input' => array(
  9096. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  9097. 'id' => 'ContactImrequiredonboth',
  9098. 'required' => 'required'
  9099. ),
  9100. '/div'
  9101. );
  9102. $this->assertTags($result, $expected);
  9103. $result = $this->Form->input('Contact.imrequired');
  9104. $expected = array(
  9105. 'div' => array('class' => 'input text required'),
  9106. 'label' => array('for' => 'ContactImrequired'),
  9107. 'Imrequired',
  9108. '/label',
  9109. 'input' => array(
  9110. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  9111. 'id' => 'ContactImrequired',
  9112. 'required' => 'required'
  9113. ),
  9114. '/div'
  9115. );
  9116. $this->assertTags($result, $expected);
  9117. }
  9118. /**
  9119. * Test inputDefaults setter and getter
  9120. *
  9121. * @return void
  9122. */
  9123. public function testInputDefaults() {
  9124. $this->Form->create('Contact');
  9125. $this->Form->inputDefaults(array(
  9126. 'label' => false,
  9127. 'div' => array(
  9128. 'style' => 'color: #000;'
  9129. )
  9130. ));
  9131. $result = $this->Form->input('Contact.field1');
  9132. $expected = array(
  9133. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  9134. 'input' => array(
  9135. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9136. 'id' => 'ContactField1'
  9137. ),
  9138. '/div'
  9139. );
  9140. $this->assertTags($result, $expected);
  9141. $this->Form->inputDefaults(array(
  9142. 'div' => false,
  9143. 'label' => 'Label',
  9144. ));
  9145. $result = $this->Form->input('Contact.field1');
  9146. $expected = array(
  9147. 'label' => array('for' => 'ContactField1'),
  9148. 'Label',
  9149. '/label',
  9150. 'input' => array(
  9151. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9152. 'id' => 'ContactField1'
  9153. ),
  9154. );
  9155. $this->assertTags($result, $expected);
  9156. $this->Form->inputDefaults(array(
  9157. 'label' => false,
  9158. ), true);
  9159. $result = $this->Form->input('Contact.field1');
  9160. $expected = array(
  9161. 'input' => array(
  9162. 'type' => 'text', 'name' => 'data[Contact][field1]',
  9163. 'id' => 'ContactField1'
  9164. ),
  9165. );
  9166. $this->assertTags($result, $expected);
  9167. $result = $this->Form->inputDefaults();
  9168. $expected = array(
  9169. 'div' => false,
  9170. 'label' => false,
  9171. );
  9172. $this->assertEquals($expected, $result);
  9173. }
  9174. }