FormHelperTest.php 291 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since CakePHP(tm) v 1.2.0.4206
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Controller\Controller;
  17. use Cake\Core\App;
  18. use Cake\Core\Configure;
  19. use Cake\Core\Plugin;
  20. use Cake\Model\Model;
  21. use Cake\Network\Request;
  22. use Cake\Routing\Router;
  23. use Cake\TestSuite\Fixture\TestModel;
  24. use Cake\TestSuite\TestCase;
  25. use Cake\Utility\ClassRegistry;
  26. use Cake\Utility\Security;
  27. use Cake\View\Helper\FormHelper;
  28. use Cake\View\Helper\HtmlHelper;
  29. use Cake\View\View;
  30. /**
  31. * ContactTestController class
  32. *
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. */
  46. class Contact extends TestModel {
  47. /**
  48. * useTable property
  49. *
  50. * @var boolean
  51. */
  52. public $useTable = false;
  53. /**
  54. * Default schema
  55. *
  56. * @var array
  57. */
  58. protected $_schema = array(
  59. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  60. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  61. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  65. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  66. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  67. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  68. );
  69. /**
  70. * validate property
  71. *
  72. * @var array
  73. */
  74. public $validate = array(
  75. 'non_existing' => array(),
  76. 'idontexist' => array(),
  77. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  78. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  79. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  80. 'imrequiredonboth' => array(
  81. 'required' => array('rule' => 'alphaNumeric'),
  82. ),
  83. 'string_required' => 'notEmpty',
  84. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  85. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  86. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  87. 'imnotrequired' => array(
  88. '_allowEmpty' => true,
  89. array('rule' => 'alphaNumeric')
  90. ),
  91. 'imalsonotrequired' => array(
  92. '_allowEmpty' => true,
  93. 'alpha' => array('rule' => 'alphaNumeric'),
  94. 'between' => array('rule' => array('between', 5, 30)),
  95. ),
  96. 'imnotrequiredeither' => array(
  97. '_allowEmpty' => true,
  98. array('rule' => array('between', 5, 30))
  99. ),
  100. 'iamrequiredalways' => array(
  101. 'email' => array('rule' => 'email'),
  102. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  103. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  104. ),
  105. 'boolean_field' => array('rule' => 'boolean')
  106. );
  107. /**
  108. * schema method
  109. *
  110. * @return void
  111. */
  112. public function setSchema($schema) {
  113. $this->_schema = $schema;
  114. }
  115. /**
  116. * hasAndBelongsToMany property
  117. *
  118. * @var array
  119. */
  120. public $hasAndBelongsToMany = array(
  121. 'ContactTag' => array(
  122. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  123. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  124. )
  125. );
  126. /**
  127. * hasAndBelongsToMany property
  128. *
  129. * @var array
  130. */
  131. public $belongsTo = array(
  132. 'User' => array('className' => 'UserForm'
  133. ));
  134. }
  135. /**
  136. * ContactTagsContact class
  137. *
  138. */
  139. class ContactTagsContact extends TestModel {
  140. /**
  141. * useTable property
  142. *
  143. * @var boolean
  144. */
  145. public $useTable = false;
  146. /**
  147. * Default schema
  148. *
  149. * @var array
  150. */
  151. protected $_schema = array(
  152. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  153. 'contact_tag_id' => array(
  154. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  155. )
  156. );
  157. /**
  158. * schema method
  159. *
  160. * @return void
  161. */
  162. public function setSchema($schema) {
  163. $this->_schema = $schema;
  164. }
  165. }
  166. /**
  167. * ContactNonStandardPk class
  168. *
  169. */
  170. class ContactNonStandardPk extends Contact {
  171. /**
  172. * primaryKey property
  173. *
  174. * @var string
  175. */
  176. public $primaryKey = 'pk';
  177. /**
  178. * schema method
  179. *
  180. * @return void
  181. */
  182. public function schema($field = false) {
  183. $this->_schema = parent::schema();
  184. $this->_schema['pk'] = $this->_schema['id'];
  185. unset($this->_schema['id']);
  186. return $this->_schema;
  187. }
  188. }
  189. /**
  190. * ContactTag class
  191. *
  192. */
  193. class ContactTag extends Model {
  194. /**
  195. * useTable property
  196. *
  197. * @var boolean
  198. */
  199. public $useTable = false;
  200. /**
  201. * schema definition
  202. *
  203. * @var array
  204. */
  205. protected $_schema = array(
  206. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  207. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  208. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  209. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  210. );
  211. }
  212. /**
  213. * UserForm class
  214. *
  215. */
  216. class UserForm extends TestModel {
  217. /**
  218. * useTable property
  219. *
  220. * @var boolean
  221. */
  222. public $useTable = false;
  223. /**
  224. * hasMany property
  225. *
  226. * @var array
  227. */
  228. public $hasMany = array(
  229. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  230. ));
  231. /**
  232. * schema definition
  233. *
  234. * @var array
  235. */
  236. protected $_schema = array(
  237. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  238. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  239. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  240. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  241. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  242. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  243. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  244. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  245. );
  246. }
  247. /**
  248. * OpenidUrl class
  249. *
  250. */
  251. class OpenidUrl extends TestModel {
  252. /**
  253. * useTable property
  254. *
  255. * @var boolean
  256. */
  257. public $useTable = false;
  258. /**
  259. * belongsTo property
  260. *
  261. * @var array
  262. */
  263. public $belongsTo = array('UserForm' => array(
  264. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  265. ));
  266. /**
  267. * validate property
  268. *
  269. * @var array
  270. */
  271. public $validate = array('openid_not_registered' => array());
  272. /**
  273. * schema method
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'user_form_id' => array(
  280. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  281. ),
  282. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  283. );
  284. /**
  285. * beforeValidate method
  286. *
  287. * @return void
  288. */
  289. public function beforeValidate($options = array()) {
  290. $this->invalidate('openid_not_registered');
  291. return true;
  292. }
  293. }
  294. /**
  295. * ValidateUser class
  296. *
  297. */
  298. class ValidateUser extends TestModel {
  299. /**
  300. * useTable property
  301. *
  302. * @var boolean
  303. */
  304. public $useTable = false;
  305. /**
  306. * hasOne property
  307. *
  308. * @var array
  309. */
  310. public $hasOne = array('ValidateProfile' => array(
  311. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  312. ));
  313. /**
  314. * schema method
  315. *
  316. * @var array
  317. */
  318. protected $_schema = array(
  319. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  320. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  321. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  322. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  323. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  324. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  325. );
  326. /**
  327. * beforeValidate method
  328. *
  329. * @return void
  330. */
  331. public function beforeValidate($options = array()) {
  332. $this->invalidate('email');
  333. return false;
  334. }
  335. }
  336. /**
  337. * ValidateProfile class
  338. *
  339. */
  340. class ValidateProfile extends TestModel {
  341. /**
  342. * useTable property
  343. *
  344. * @var boolean
  345. */
  346. public $useTable = false;
  347. /**
  348. * schema property
  349. *
  350. * @var array
  351. */
  352. protected $_schema = array(
  353. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  354. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  355. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  356. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  357. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  358. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  359. );
  360. /**
  361. * hasOne property
  362. *
  363. * @var array
  364. */
  365. public $hasOne = array('ValidateItem' => array(
  366. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  367. ));
  368. /**
  369. * belongsTo property
  370. *
  371. * @var array
  372. */
  373. public $belongsTo = array('ValidateUser' => array(
  374. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  375. ));
  376. /**
  377. * beforeValidate method
  378. *
  379. * @return void
  380. */
  381. public function beforeValidate($options = array()) {
  382. $this->invalidate('full_name');
  383. $this->invalidate('city');
  384. return false;
  385. }
  386. }
  387. /**
  388. * ValidateItem class
  389. *
  390. */
  391. class ValidateItem extends TestModel {
  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. */
  433. class TestMail extends TestModel {
  434. /**
  435. * useTable property
  436. *
  437. * @var boolean
  438. */
  439. public $useTable = false;
  440. }
  441. /**
  442. * FormHelperTest class
  443. *
  444. * @property FormHelper $Form
  445. */
  446. class FormHelperTest extends TestCase {
  447. /**
  448. * Fixtures to be used
  449. *
  450. * @var array
  451. */
  452. public $fixtures = array('core.post');
  453. /**
  454. * Do not load the fixtures by default
  455. *
  456. * @var boolean
  457. */
  458. public $autoFixtures = false;
  459. /**
  460. * setUp method
  461. *
  462. * @return void
  463. */
  464. public function setUp() {
  465. parent::setUp();
  466. $this->markTestIncomplete('Need to revisit once models work again.');
  467. Configure::write('Config.language', 'eng');
  468. Configure::write('App.base', '');
  469. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  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 Request('contacts/add');
  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->dateRegex = array(
  488. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  489. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  490. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  491. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  492. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  493. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  494. );
  495. Configure::write('Security.salt', 'foo!');
  496. Router::connect('/:controller', array('action' => 'index'));
  497. Router::connect('/:controller/:action/*');
  498. }
  499. /**
  500. * tearDown method
  501. *
  502. * @return void
  503. */
  504. public function tearDown() {
  505. parent::tearDown();
  506. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  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' => '_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->assertContains('name="ValidateUser[name]"', $result);
  581. $this->assertContains('type="text"', $result);
  582. $result = $this->Form->input('ValidateItem.name');
  583. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  584. $this->assertContains('name="ValidateItem[name]"', $result);
  585. $this->assertContains('<textarea', $result);
  586. $result = $this->Form->input('name');
  587. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  588. $this->assertContains('name="ValidateUser[name]"', $result);
  589. $this->assertContains('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. $this->Form->request['_Token'] = array('key' => $key);
  613. $result = $this->Form->secure($fields);
  614. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  615. $hash .= ':' . 'Model.valid';
  616. $hash = urlencode($hash);
  617. $expected = array(
  618. 'div' => array('style' => 'display:none;'),
  619. array('input' => array(
  620. 'type' => 'hidden', 'name' => '_Token[fields]',
  621. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  622. )),
  623. array('input' => array(
  624. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  625. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $path = CAKE . 'Test/TestApp/Config/';
  631. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  632. $result = $this->Form->secure($fields);
  633. $expected = array(
  634. 'div' => array('class' => 'hidden'),
  635. array('input' => array(
  636. 'type' => 'hidden', 'name' => '_Token[fields]',
  637. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  638. )),
  639. array('input' => array(
  640. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  641. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  642. )),
  643. '/div'
  644. );
  645. $this->assertTags($result, $expected);
  646. }
  647. /**
  648. * Tests correct generation of number fields for double and float fields
  649. *
  650. * @return void
  651. */
  652. public function testTextFieldGenerationForFloats() {
  653. $model = ClassRegistry::getObject('Contact');
  654. $model->setSchema(array('foo' => array(
  655. 'type' => 'float',
  656. 'null' => false,
  657. 'default' => null,
  658. 'length' => 10
  659. )));
  660. $this->Form->create('Contact');
  661. $result = $this->Form->input('foo');
  662. $expected = array(
  663. 'div' => array('class' => 'input number'),
  664. 'label' => array('for' => 'ContactFoo'),
  665. 'Foo',
  666. '/label',
  667. array('input' => array(
  668. 'type' => 'number',
  669. 'name' => 'Contact[foo]',
  670. 'id' => 'ContactFoo',
  671. 'step' => 'any'
  672. )),
  673. '/div'
  674. );
  675. $this->assertTags($result, $expected);
  676. $result = $this->Form->input('foo', array('step' => 0.5));
  677. $expected = array(
  678. 'div' => array('class' => 'input number'),
  679. 'label' => array('for' => 'ContactFoo'),
  680. 'Foo',
  681. '/label',
  682. array('input' => array(
  683. 'type' => 'number',
  684. 'name' => 'Contact[foo]',
  685. 'id' => 'ContactFoo',
  686. 'step' => '0.5'
  687. )),
  688. '/div'
  689. );
  690. $this->assertTags($result, $expected);
  691. }
  692. /**
  693. * Tests correct generation of number fields for integer fields
  694. *
  695. * @return void
  696. */
  697. public function testTextFieldTypeNumberGenerationForIntegers() {
  698. $model = ClassRegistry::getObject('Contact');
  699. $model->setSchema(array('foo' => array(
  700. 'type' => 'integer',
  701. 'null' => false,
  702. 'default' => null,
  703. 'length' => null
  704. )));
  705. $this->Form->create('Contact');
  706. $result = $this->Form->input('foo');
  707. $expected = array(
  708. 'div' => array('class' => 'input number'),
  709. 'label' => array('for' => 'ContactFoo'),
  710. 'Foo',
  711. '/label',
  712. array('input' => array(
  713. 'type' => 'number', 'name' => 'Contact[foo]',
  714. 'id' => 'ContactFoo'
  715. )),
  716. '/div'
  717. );
  718. $this->assertTags($result, $expected);
  719. }
  720. /**
  721. * testFormSecurityMultipleFields method
  722. *
  723. * Test secure() with multiple row form. Ensure hash is correct.
  724. *
  725. * @return void
  726. */
  727. public function testFormSecurityMultipleFields() {
  728. $key = 'testKey';
  729. $fields = array(
  730. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  731. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  732. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  733. );
  734. $this->Form->request['_Token'] = array('key' => $key);
  735. $result = $this->Form->secure($fields);
  736. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  737. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  738. $expected = array(
  739. 'div' => array('style' => 'display:none;'),
  740. array('input' => array(
  741. 'type' => 'hidden', 'name' => '_Token[fields]',
  742. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  743. )),
  744. array('input' => array(
  745. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  746. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  747. )),
  748. '/div'
  749. );
  750. $this->assertTags($result, $expected);
  751. }
  752. /**
  753. * testFormSecurityMultipleSubmitButtons
  754. *
  755. * test form submit generation and ensure that _Token is only created on end()
  756. *
  757. * @return void
  758. */
  759. public function testFormSecurityMultipleSubmitButtons() {
  760. $key = 'testKey';
  761. $this->Form->request['_Token'] = array('key' => $key);
  762. $this->Form->create('Addresses');
  763. $this->Form->input('Address.title');
  764. $this->Form->input('Address.first_name');
  765. $result = $this->Form->submit('Save', array('name' => 'save'));
  766. $expected = array(
  767. 'div' => array('class' => 'submit'),
  768. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  769. '/div',
  770. );
  771. $this->assertTags($result, $expected);
  772. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  773. $expected = array(
  774. 'div' => array('class' => 'submit'),
  775. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  776. '/div',
  777. );
  778. $this->assertTags($result, $expected);
  779. $result = $this->Form->end(null);
  780. $expected = array(
  781. 'div' => array('style' => 'display:none;'),
  782. array('input' => array(
  783. 'type' => 'hidden', 'name' => '_Token[fields]',
  784. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  785. )),
  786. array('input' => array(
  787. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  788. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  789. )),
  790. '/div'
  791. );
  792. $this->assertTags($result, $expected);
  793. }
  794. /**
  795. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  796. *
  797. * @return void
  798. */
  799. public function testSecurityButtonNestedNamed() {
  800. $key = 'testKey';
  801. $this->Form->request['_Token'] = array('key' => $key);
  802. $this->Form->create('Addresses');
  803. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  804. $result = $this->Form->unlockField();
  805. $this->assertEquals(array('Address.button'), $result);
  806. }
  807. /**
  808. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  809. *
  810. * @return void
  811. */
  812. public function testSecuritySubmitNestedNamed() {
  813. $key = 'testKey';
  814. $this->Form->request['_Token'] = array('key' => $key);
  815. $this->Form->create('Addresses');
  816. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  817. $result = $this->Form->unlockField();
  818. $this->assertEquals(array('Address.button'), $result);
  819. }
  820. /**
  821. * Test that the correct fields are unlocked for image submits with no names.
  822. *
  823. * @return void
  824. */
  825. public function testSecuritySubmitImageNoName() {
  826. $key = 'testKey';
  827. $this->Form->request['_Token'] = array('key' => $key);
  828. $this->Form->create('User');
  829. $result = $this->Form->submit('save.png');
  830. $expected = array(
  831. 'div' => array('class' => 'submit'),
  832. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  833. '/div'
  834. );
  835. $this->assertTags($result, $expected);
  836. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  837. }
  838. /**
  839. * Test that the correct fields are unlocked for image submits with names.
  840. *
  841. * @return void
  842. */
  843. public function testSecuritySubmitImageName() {
  844. $key = 'testKey';
  845. $this->Form->request['_Token'] = array('key' => $key);
  846. $this->Form->create('User');
  847. $result = $this->Form->submit('save.png', array('name' => 'test'));
  848. $expected = array(
  849. 'div' => array('class' => 'submit'),
  850. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  851. '/div'
  852. );
  853. $this->assertTags($result, $expected);
  854. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  855. }
  856. /**
  857. * testFormSecurityMultipleInputFields method
  858. *
  859. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  860. *
  861. * @return void
  862. */
  863. public function testFormSecurityMultipleInputFields() {
  864. $key = 'testKey';
  865. $this->Form->request['_Token'] = array('key' => $key);
  866. $this->Form->create('Addresses');
  867. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  868. $this->Form->input('Addresses.0.title');
  869. $this->Form->input('Addresses.0.first_name');
  870. $this->Form->input('Addresses.0.last_name');
  871. $this->Form->input('Addresses.0.address');
  872. $this->Form->input('Addresses.0.city');
  873. $this->Form->input('Addresses.0.phone');
  874. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  875. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  876. $this->Form->input('Addresses.1.title');
  877. $this->Form->input('Addresses.1.first_name');
  878. $this->Form->input('Addresses.1.last_name');
  879. $this->Form->input('Addresses.1.address');
  880. $this->Form->input('Addresses.1.city');
  881. $this->Form->input('Addresses.1.phone');
  882. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  883. $result = $this->Form->secure($this->Form->fields);
  884. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  885. $expected = array(
  886. 'div' => array('style' => 'display:none;'),
  887. array('input' => array(
  888. 'type' => 'hidden', 'name' => '_Token[fields]',
  889. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  890. )),
  891. array('input' => array(
  892. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  893. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  894. )),
  895. '/div'
  896. );
  897. $this->assertTags($result, $expected);
  898. }
  899. /**
  900. * Test form security with Model.field.0 style inputs
  901. *
  902. * @return void
  903. */
  904. public function testFormSecurityArrayFields() {
  905. $key = 'testKey';
  906. $this->Form->request->params['_Token']['key'] = $key;
  907. $this->Form->create('Address');
  908. $this->Form->input('Address.primary.1');
  909. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  910. $this->Form->input('Address.secondary.1.0');
  911. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  912. }
  913. /**
  914. * testFormSecurityMultipleInputDisabledFields method
  915. *
  916. * test secure form generation with multiple records and disabled fields.
  917. *
  918. * @return void
  919. */
  920. public function testFormSecurityMultipleInputDisabledFields() {
  921. $key = 'testKey';
  922. $this->Form->request->params['_Token'] = array(
  923. 'key' => $key,
  924. 'unlockedFields' => array('first_name', 'address')
  925. );
  926. $this->Form->create();
  927. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  928. $this->Form->input('Addresses.0.title');
  929. $this->Form->input('Addresses.0.first_name');
  930. $this->Form->input('Addresses.0.last_name');
  931. $this->Form->input('Addresses.0.address');
  932. $this->Form->input('Addresses.0.city');
  933. $this->Form->input('Addresses.0.phone');
  934. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  935. $this->Form->input('Addresses.1.title');
  936. $this->Form->input('Addresses.1.first_name');
  937. $this->Form->input('Addresses.1.last_name');
  938. $this->Form->input('Addresses.1.address');
  939. $this->Form->input('Addresses.1.city');
  940. $this->Form->input('Addresses.1.phone');
  941. $result = $this->Form->secure($this->Form->fields);
  942. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  943. $expected = array(
  944. 'div' => array('style' => 'display:none;'),
  945. array('input' => array(
  946. 'type' => 'hidden', 'name' => '_Token[fields]',
  947. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  948. )),
  949. array('input' => array(
  950. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  951. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  952. )),
  953. '/div'
  954. );
  955. $this->assertTags($result, $expected);
  956. }
  957. /**
  958. * testFormSecurityInputDisabledFields method
  959. *
  960. * Test single record form with disabled fields.
  961. *
  962. * @return void
  963. */
  964. public function testFormSecurityInputUnlockedFields() {
  965. $key = 'testKey';
  966. $this->Form->request['_Token'] = array(
  967. 'key' => $key,
  968. 'unlockedFields' => array('first_name', 'address')
  969. );
  970. $this->Form->create();
  971. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  972. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  973. $this->Form->input('Addresses.title');
  974. $this->Form->input('Addresses.first_name');
  975. $this->Form->input('Addresses.last_name');
  976. $this->Form->input('Addresses.address');
  977. $this->Form->input('Addresses.city');
  978. $this->Form->input('Addresses.phone');
  979. $result = $this->Form->fields;
  980. $expected = array(
  981. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  982. 'Addresses.city', 'Addresses.phone'
  983. );
  984. $this->assertEquals($expected, $result);
  985. $result = $this->Form->secure($expected);
  986. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  987. $expected = array(
  988. 'div' => array('style' => 'display:none;'),
  989. array('input' => array(
  990. 'type' => 'hidden', 'name' => '_Token[fields]',
  991. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  992. )),
  993. array('input' => array(
  994. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  995. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  996. )),
  997. '/div'
  998. );
  999. $this->assertTags($result, $expected);
  1000. }
  1001. /**
  1002. * test securing inputs with custom name attributes.
  1003. *
  1004. * @return void
  1005. */
  1006. public function testFormSecureWithCustomNameAttribute() {
  1007. $this->Form->request->params['_Token']['key'] = 'testKey';
  1008. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1009. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1010. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1011. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1012. }
  1013. /**
  1014. * testFormSecuredInput method
  1015. *
  1016. * Test generation of entire secure form, assertions made on input() output.
  1017. *
  1018. * @return void
  1019. */
  1020. public function testFormSecuredInput() {
  1021. $this->Form->request['_Token'] = array('key' => 'testKey');
  1022. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1023. $encoding = strtolower(Configure::read('App.encoding'));
  1024. $expected = array(
  1025. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1026. 'div' => array('style' => 'display:none;'),
  1027. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1028. array('input' => array(
  1029. 'type' => 'hidden', 'name' => '_Token[key]',
  1030. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1031. )),
  1032. '/div'
  1033. );
  1034. $this->assertTags($result, $expected);
  1035. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1036. $expected = array(
  1037. 'div' => array('class' => 'input text'),
  1038. 'label' => array('for' => 'UserFormPublished'),
  1039. 'Published',
  1040. '/label',
  1041. array('input' => array(
  1042. 'type' => 'text', 'name' => 'UserForm[published]',
  1043. 'id' => 'UserFormPublished'
  1044. )),
  1045. '/div'
  1046. );
  1047. $this->assertTags($result, $expected);
  1048. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1049. $expected = array(
  1050. 'div' => array('class' => 'input text'),
  1051. 'label' => array('for' => 'UserFormOther'),
  1052. 'Other',
  1053. '/label',
  1054. array('input' => array(
  1055. 'type' => 'text', 'name' => 'UserForm[other]',
  1056. 'id' => 'UserFormOther'
  1057. )),
  1058. '/div'
  1059. );
  1060. $this->assertTags($result, $expected);
  1061. $result = $this->Form->hidden('UserForm.stuff');
  1062. $expected = array(
  1063. 'input' => array(
  1064. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1065. 'id' => 'UserFormStuff'
  1066. ));
  1067. $this->assertTags($result, $expected);
  1068. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1069. $expected = array('input' => array(
  1070. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1071. 'value' => '0', 'id' => 'UserFormHidden'
  1072. ));
  1073. $this->assertTags($result, $expected);
  1074. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1075. $expected = array(
  1076. 'div' => array('class' => 'input checkbox'),
  1077. array('input' => array(
  1078. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1079. 'value' => '0', 'id' => 'UserFormSomething_'
  1080. )),
  1081. array('input' => array(
  1082. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1083. 'value' => '1', 'id' => 'UserFormSomething'
  1084. )),
  1085. 'label' => array('for' => 'UserFormSomething'),
  1086. 'Something',
  1087. '/label',
  1088. '/div'
  1089. );
  1090. $this->assertTags($result, $expected);
  1091. $result = $this->Form->fields;
  1092. $expected = array(
  1093. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1094. 'UserForm.hidden' => '0', 'UserForm.something'
  1095. );
  1096. $this->assertEquals($expected, $result);
  1097. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1098. $result = $this->Form->secure($this->Form->fields);
  1099. $expected = array(
  1100. 'div' => array('style' => 'display:none;'),
  1101. array('input' => array(
  1102. 'type' => 'hidden', 'name' => '_Token[fields]',
  1103. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1104. )),
  1105. array('input' => array(
  1106. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1107. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1108. )),
  1109. '/div'
  1110. );
  1111. $this->assertTags($result, $expected);
  1112. }
  1113. /**
  1114. * Test secured inputs with custom names.
  1115. *
  1116. * @return void
  1117. */
  1118. public function testSecuredInputCustomName() {
  1119. $this->Form->request['_Token'] = array('key' => 'testKey');
  1120. $this->assertEquals(array(), $this->Form->fields);
  1121. $this->Form->input('text_input', array(
  1122. 'name' => 'data[Option][General.default_role]',
  1123. ));
  1124. $expected = array('Option.General.default_role');
  1125. $this->assertEquals($expected, $this->Form->fields);
  1126. $this->Form->input('select_box', array(
  1127. 'name' => 'data[Option][General.select_role]',
  1128. 'type' => 'select',
  1129. 'options' => array(1, 2),
  1130. ));
  1131. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1132. $this->assertEquals($expected, $this->Form->fields);
  1133. }
  1134. /**
  1135. * Tests that the correct keys are added to the field hash index
  1136. *
  1137. * @return void
  1138. */
  1139. public function testFormSecuredFileInput() {
  1140. $this->Form->request['_Token'] = array('key' => 'testKey');
  1141. $this->assertEquals(array(), $this->Form->fields);
  1142. $this->Form->file('Attachment.file');
  1143. $expected = array(
  1144. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1145. 'Attachment.file.error', 'Attachment.file.size'
  1146. );
  1147. $this->assertEquals($expected, $this->Form->fields);
  1148. }
  1149. /**
  1150. * test that multiple selects keys are added to field hash
  1151. *
  1152. * @return void
  1153. */
  1154. public function testFormSecuredMultipleSelect() {
  1155. $this->Form->request['_Token'] = array('key' => 'testKey');
  1156. $this->assertEquals(array(), $this->Form->fields);
  1157. $options = array('1' => 'one', '2' => 'two');
  1158. $this->Form->select('Model.select', $options);
  1159. $expected = array('Model.select');
  1160. $this->assertEquals($expected, $this->Form->fields);
  1161. $this->Form->fields = array();
  1162. $this->Form->select('Model.select', $options, array('multiple' => true));
  1163. $this->assertEquals($expected, $this->Form->fields);
  1164. }
  1165. /**
  1166. * testFormSecuredRadio method
  1167. *
  1168. * @return void
  1169. */
  1170. public function testFormSecuredRadio() {
  1171. $this->Form->request['_Token'] = array('key' => 'testKey');
  1172. $this->assertEquals(array(), $this->Form->fields);
  1173. $options = array('1' => 'option1', '2' => 'option2');
  1174. $this->Form->radio('Test.test', $options);
  1175. $expected = array('Test.test');
  1176. $this->assertEquals($expected, $this->Form->fields);
  1177. }
  1178. /**
  1179. * Test that when disabled is in a list based attribute array it works.
  1180. *
  1181. * @return void
  1182. */
  1183. public function testFormSecuredAndDisabledNotAssoc() {
  1184. $this->Form->request['_Token'] = array('key' => 'testKey');
  1185. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1186. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1187. $this->Form->text('Model.text', array('disabled'));
  1188. $this->Form->textarea('Model.textarea', array('disabled'));
  1189. $this->Form->password('Model.password', array('disabled'));
  1190. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1191. $expected = array(
  1192. 'Model.radio' => ''
  1193. );
  1194. $this->assertEquals($expected, $this->Form->fields);
  1195. }
  1196. /**
  1197. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1198. * hashing.
  1199. *
  1200. * @return void
  1201. */
  1202. public function testFormSecuredAndDisabled() {
  1203. $this->Form->request['_Token'] = array('key' => 'testKey');
  1204. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1205. $this->Form->text('Model.text', array('disabled' => true));
  1206. $this->Form->password('Model.text', array('disabled' => true));
  1207. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1208. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1209. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1210. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1211. $this->Form->month('Model.month', array('disabled' => true));
  1212. $this->Form->day('Model.day', array('disabled' => true));
  1213. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1214. $this->Form->minute('Model.minute', array('disabled' => true));
  1215. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1216. $expected = array(
  1217. 'Model.radio' => ''
  1218. );
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testDisableSecurityUsingForm method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testDisableSecurityUsingForm() {
  1227. $this->Form->request['_Token'] = array(
  1228. 'key' => 'testKey',
  1229. 'disabledFields' => array()
  1230. );
  1231. $this->Form->create();
  1232. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1233. $this->Form->input('Addresses.title');
  1234. $this->Form->input('Addresses.first_name', array('secure' => false));
  1235. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1236. $this->Form->input('Addresses.zip', array(
  1237. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1238. ));
  1239. $result = $this->Form->fields;
  1240. $expected = array(
  1241. 'Addresses.id' => '123456', 'Addresses.title',
  1242. );
  1243. $this->assertEquals($expected, $result);
  1244. }
  1245. /**
  1246. * test disableField
  1247. *
  1248. * @return void
  1249. */
  1250. public function testUnlockFieldAddsToList() {
  1251. $this->Form->request['_Token'] = array(
  1252. 'key' => 'testKey',
  1253. 'unlockedFields' => array()
  1254. );
  1255. $this->Form->create('Contact');
  1256. $this->Form->unlockField('Contact.name');
  1257. $this->Form->text('Contact.name');
  1258. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1259. $this->assertEquals(array(), $this->Form->fields);
  1260. }
  1261. /**
  1262. * test unlockField removing from fields array.
  1263. *
  1264. * @return void
  1265. */
  1266. public function testUnlockFieldRemovingFromFields() {
  1267. $this->Form->request['_Token'] = array(
  1268. 'key' => 'testKey',
  1269. 'unlockedFields' => array()
  1270. );
  1271. $this->Form->create('Contact');
  1272. $this->Form->hidden('Contact.id', array('value' => 1));
  1273. $this->Form->text('Contact.name');
  1274. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1275. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1276. $this->Form->unlockField('Contact.name');
  1277. $this->Form->unlockField('Contact.id');
  1278. $this->assertEquals(array(), $this->Form->fields);
  1279. }
  1280. /**
  1281. * testTagIsInvalid method
  1282. *
  1283. * @return void
  1284. */
  1285. public function testTagIsInvalid() {
  1286. $Contact = ClassRegistry::getObject('Contact');
  1287. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1288. $this->Form->setEntity('Contact.0.email');
  1289. $result = $this->Form->tagIsInvalid();
  1290. $this->assertEquals($expected, $result);
  1291. $this->Form->setEntity('Contact.1.email');
  1292. $result = $this->Form->tagIsInvalid();
  1293. $this->assertFalse($result);
  1294. $this->Form->setEntity('Contact.0.name');
  1295. $result = $this->Form->tagIsInvalid();
  1296. $this->assertFalse($result);
  1297. }
  1298. /**
  1299. * Test tagIsInvalid with validation errors from a saveMany
  1300. *
  1301. * @return void
  1302. */
  1303. public function testTagIsInvalidSaveMany() {
  1304. $Contact = ClassRegistry::getObject('Contact');
  1305. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1306. $this->Form->create('Contact');
  1307. $this->Form->setEntity('0.email');
  1308. $result = $this->Form->tagIsInvalid();
  1309. $this->assertEquals($expected, $result);
  1310. $this->Form->setEntity('0.Contact.email');
  1311. $result = $this->Form->tagIsInvalid();
  1312. $this->assertEquals($expected, $result);
  1313. }
  1314. /**
  1315. * Test validation errors.
  1316. *
  1317. * @return void
  1318. */
  1319. public function testPasswordValidation() {
  1320. $Contact = ClassRegistry::getObject('Contact');
  1321. $Contact->validationErrors['password'] = array('Please provide a password');
  1322. $result = $this->Form->input('Contact.password');
  1323. $expected = array(
  1324. 'div' => array('class' => 'input password error'),
  1325. 'label' => array('for' => 'ContactPassword'),
  1326. 'Password',
  1327. '/label',
  1328. 'input' => array(
  1329. 'type' => 'password', 'name' => 'Contact[password]',
  1330. 'id' => 'ContactPassword', 'class' => 'form-error'
  1331. ),
  1332. array('div' => array('class' => 'error-message')),
  1333. 'Please provide a password',
  1334. '/div',
  1335. '/div'
  1336. );
  1337. $this->assertTags($result, $expected);
  1338. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1339. $expected = array(
  1340. 'div' => array('class' => 'input password error'),
  1341. 'label' => array('for' => 'ContactPassword'),
  1342. 'Password',
  1343. '/label',
  1344. 'input' => array(
  1345. 'type' => 'password', 'name' => 'Contact[password]',
  1346. 'id' => 'ContactPassword', 'class' => 'form-error'
  1347. ),
  1348. '/div'
  1349. );
  1350. $this->assertTags($result, $expected);
  1351. }
  1352. /**
  1353. * Test validation errors, when validation message is an empty string.
  1354. *
  1355. * @return void
  1356. */
  1357. public function testEmptyErrorValidation() {
  1358. $this->Form->validationErrors['Contact']['password'] = '';
  1359. $result = $this->Form->input('Contact.password');
  1360. $expected = array(
  1361. 'div' => array('class' => 'input password error'),
  1362. 'label' => array('for' => 'ContactPassword'),
  1363. 'Password',
  1364. '/label',
  1365. 'input' => array(
  1366. 'type' => 'password', 'name' => 'Contact[password]',
  1367. 'id' => 'ContactPassword', 'class' => 'form-error'
  1368. ),
  1369. array('div' => array('class' => 'error-message')),
  1370. array(),
  1371. '/div',
  1372. '/div'
  1373. );
  1374. $this->assertTags($result, $expected);
  1375. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1376. $expected = array(
  1377. 'div' => array('class' => 'input password error'),
  1378. 'label' => array('for' => 'ContactPassword'),
  1379. 'Password',
  1380. '/label',
  1381. 'input' => array(
  1382. 'type' => 'password', 'name' => 'Contact[password]',
  1383. 'id' => 'ContactPassword', 'class' => 'form-error'
  1384. ),
  1385. '/div'
  1386. );
  1387. $this->assertTags($result, $expected);
  1388. }
  1389. /**
  1390. * Test validation errors, when calling input() overriding validation message by an empty string.
  1391. *
  1392. * @return void
  1393. */
  1394. public function testEmptyInputErrorValidation() {
  1395. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1396. $result = $this->Form->input('Contact.password', array('error' => ''));
  1397. $expected = array(
  1398. 'div' => array('class' => 'input password error'),
  1399. 'label' => array('for' => 'ContactPassword'),
  1400. 'Password',
  1401. '/label',
  1402. 'input' => array(
  1403. 'type' => 'password', 'name' => 'Contact[password]',
  1404. 'id' => 'ContactPassword', 'class' => 'form-error'
  1405. ),
  1406. array('div' => array('class' => 'error-message')),
  1407. array(),
  1408. '/div',
  1409. '/div'
  1410. );
  1411. $this->assertTags($result, $expected);
  1412. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1413. $expected = array(
  1414. 'div' => array('class' => 'input password error'),
  1415. 'label' => array('for' => 'ContactPassword'),
  1416. 'Password',
  1417. '/label',
  1418. 'input' => array(
  1419. 'type' => 'password', 'name' => 'Contact[password]',
  1420. 'id' => 'ContactPassword', 'class' => 'form-error'
  1421. ),
  1422. '/div'
  1423. );
  1424. $this->assertTags($result, $expected);
  1425. }
  1426. /**
  1427. * testFormValidationAssociated method
  1428. *
  1429. * test display of form errors in conjunction with model::validates.
  1430. *
  1431. * @return void
  1432. */
  1433. public function testFormValidationAssociated() {
  1434. $this->UserForm = ClassRegistry::getObject('UserForm');
  1435. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1436. $data = array(
  1437. 'UserForm' => array('name' => 'user'),
  1438. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1439. );
  1440. $result = $this->UserForm->OpenidUrl->create($data);
  1441. $this->assertFalse(empty($result));
  1442. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1443. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1444. $encoding = strtolower(Configure::read('App.encoding'));
  1445. $expected = array(
  1446. 'form' => array(
  1447. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1448. 'accept-charset' => $encoding
  1449. ),
  1450. 'div' => array('style' => 'display:none;'),
  1451. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1452. '/div'
  1453. );
  1454. $this->assertTags($result, $expected);
  1455. $result = $this->Form->error(
  1456. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1457. );
  1458. $this->assertEquals('Error, not registered', $result);
  1459. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1460. }
  1461. /**
  1462. * testFormValidationAssociatedFirstLevel method
  1463. *
  1464. * test form error display with associated model.
  1465. *
  1466. * @return void
  1467. */
  1468. public function testFormValidationAssociatedFirstLevel() {
  1469. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1470. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1471. $data = array(
  1472. 'ValidateUser' => array('name' => 'mariano'),
  1473. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1474. );
  1475. $result = $this->ValidateUser->create($data);
  1476. $this->assertFalse(empty($result));
  1477. $this->assertFalse($this->ValidateUser->validates());
  1478. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1479. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1480. $encoding = strtolower(Configure::read('App.encoding'));
  1481. $expected = array(
  1482. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1483. 'div' => array('style' => 'display:none;'),
  1484. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1485. '/div'
  1486. );
  1487. $this->assertTags($result, $expected);
  1488. $result = $this->Form->error(
  1489. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1490. );
  1491. $this->assertEquals('Invalid email', $result);
  1492. $result = $this->Form->error(
  1493. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1494. );
  1495. $this->assertEquals('Invalid name', $result);
  1496. $result = $this->Form->error(
  1497. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1498. );
  1499. $this->assertEquals('Invalid city', $result);
  1500. unset($this->ValidateUser->ValidateProfile);
  1501. unset($this->ValidateUser);
  1502. }
  1503. /**
  1504. * testFormValidationAssociatedSecondLevel method
  1505. *
  1506. * test form error display with associated model.
  1507. *
  1508. * @return void
  1509. */
  1510. public function testFormValidationAssociatedSecondLevel() {
  1511. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1512. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1513. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1514. $data = array(
  1515. 'ValidateUser' => array('name' => 'mariano'),
  1516. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1517. 'ValidateItem' => array('name' => 'Item')
  1518. );
  1519. $result = $this->ValidateUser->create($data);
  1520. $this->assertFalse(empty($result));
  1521. $this->assertFalse($this->ValidateUser->validates());
  1522. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1523. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1524. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1525. $encoding = strtolower(Configure::read('App.encoding'));
  1526. $expected = array(
  1527. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1528. 'div' => array('style' => 'display:none;'),
  1529. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1530. '/div'
  1531. );
  1532. $this->assertTags($result, $expected);
  1533. $result = $this->Form->error(
  1534. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1535. );
  1536. $this->assertEquals('Invalid email', $result);
  1537. $result = $this->Form->error(
  1538. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1539. );
  1540. $this->assertEquals('Invalid name', $result);
  1541. $result = $this->Form->error(
  1542. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1543. );
  1544. $result = $this->Form->error(
  1545. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1546. );
  1547. $this->assertEquals('Invalid description', $result);
  1548. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1549. unset($this->ValidateUser->ValidateProfile);
  1550. unset($this->ValidateUser);
  1551. }
  1552. /**
  1553. * testFormValidationMultiRecord method
  1554. *
  1555. * test form error display with multiple records.
  1556. *
  1557. * @return void
  1558. */
  1559. public function testFormValidationMultiRecord() {
  1560. $Contact = ClassRegistry::getObject('Contact');
  1561. $Contact->validationErrors[2] = array(
  1562. 'name' => array('The provided value is invalid')
  1563. );
  1564. $result = $this->Form->input('Contact.2.name');
  1565. $expected = array(
  1566. 'div' => array('class' => 'input text error'),
  1567. 'label' => array('for' => 'Contact2Name'),
  1568. 'Name',
  1569. '/label',
  1570. 'input' => array(
  1571. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  1572. 'class' => 'form-error', 'maxlength' => 255
  1573. ),
  1574. array('div' => array('class' => 'error-message')),
  1575. 'The provided value is invalid',
  1576. '/div',
  1577. '/div'
  1578. );
  1579. $this->assertTags($result, $expected);
  1580. }
  1581. /**
  1582. * testMultipleInputValidation method
  1583. *
  1584. * test multiple record form validation error display.
  1585. *
  1586. * @return void
  1587. */
  1588. public function testMultipleInputValidation() {
  1589. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1590. $Address->validationErrors[0] = array(
  1591. 'title' => array('This field cannot be empty'),
  1592. 'first_name' => array('This field cannot be empty')
  1593. );
  1594. $Address->validationErrors[1] = array(
  1595. 'last_name' => array('You must have a last name')
  1596. );
  1597. $this->Form->create();
  1598. $result = $this->Form->input('Address.0.title');
  1599. $expected = array(
  1600. 'div' => array('class'),
  1601. 'label' => array('for'),
  1602. 'preg:/[^<]+/',
  1603. '/label',
  1604. 'input' => array(
  1605. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1606. ),
  1607. array('div' => array('class' => 'error-message')),
  1608. 'This field cannot be empty',
  1609. '/div',
  1610. '/div'
  1611. );
  1612. $this->assertTags($result, $expected);
  1613. $result = $this->Form->input('Address.0.first_name');
  1614. $expected = array(
  1615. 'div' => array('class'),
  1616. 'label' => array('for'),
  1617. 'preg:/[^<]+/',
  1618. '/label',
  1619. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1620. array('div' => array('class' => 'error-message')),
  1621. 'This field cannot be empty',
  1622. '/div',
  1623. '/div'
  1624. );
  1625. $this->assertTags($result, $expected);
  1626. $result = $this->Form->input('Address.0.last_name');
  1627. $expected = array(
  1628. 'div' => array('class'),
  1629. 'label' => array('for'),
  1630. 'preg:/[^<]+/',
  1631. '/label',
  1632. 'input' => array('type' => 'text', 'name', 'id'),
  1633. '/div'
  1634. );
  1635. $this->assertTags($result, $expected);
  1636. $result = $this->Form->input('Address.1.last_name');
  1637. $expected = array(
  1638. 'div' => array('class'),
  1639. 'label' => array('for'),
  1640. 'preg:/[^<]+/',
  1641. '/label',
  1642. 'input' => array(
  1643. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1644. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1645. ),
  1646. array('div' => array('class' => 'error-message')),
  1647. 'You must have a last name',
  1648. '/div',
  1649. '/div'
  1650. );
  1651. $this->assertTags($result, $expected);
  1652. }
  1653. /**
  1654. * testInput method
  1655. *
  1656. * Test various incarnations of input().
  1657. *
  1658. * @return void
  1659. */
  1660. public function testInput() {
  1661. $result = $this->Form->input('ValidateUser.balance');
  1662. $expected = array(
  1663. 'div' => array('class'),
  1664. 'label' => array('for'),
  1665. 'Balance',
  1666. '/label',
  1667. 'input' => array('name', 'type' => 'number', 'id'),
  1668. '/div',
  1669. );
  1670. $this->assertTags($result, $expected);
  1671. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1672. $expected = array(
  1673. 'div' => array('class' => 'input email'),
  1674. 'label' => array('for' => 'custom'),
  1675. 'Email',
  1676. '/label',
  1677. array('input' => array(
  1678. 'type' => 'email', 'name' => 'Contact[email]',
  1679. 'id' => 'custom', 'maxlength' => 255
  1680. )),
  1681. '/div'
  1682. );
  1683. $this->assertTags($result, $expected);
  1684. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1685. $expected = array(
  1686. '<div',
  1687. 'label' => array('for' => 'ContactEmail'),
  1688. 'Email',
  1689. '/label',
  1690. array('input' => array(
  1691. 'type' => 'email', 'name' => 'Contact[email]',
  1692. 'id' => 'ContactEmail', 'maxlength' => 255
  1693. )),
  1694. '/div'
  1695. );
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->hidden('Contact.idontexist');
  1698. $expected = array('input' => array(
  1699. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  1700. 'id' => 'ContactIdontexist'
  1701. ));
  1702. $this->assertTags($result, $expected);
  1703. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1704. $expected = array(
  1705. 'div' => array('class' => 'input text'),
  1706. 'label' => array('for' => 'ContactEmail'),
  1707. 'Email',
  1708. '/label',
  1709. array('input' => array(
  1710. 'type' => 'text', 'name' => 'Contact[email]',
  1711. 'id' => 'ContactEmail'
  1712. )),
  1713. '/div'
  1714. );
  1715. $this->assertTags($result, $expected);
  1716. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1717. $expected = array(
  1718. 'div' => array('class' => 'input text'),
  1719. 'label' => array('for' => 'Contact5Email'),
  1720. 'Email',
  1721. '/label',
  1722. array('input' => array(
  1723. 'type' => 'text', 'name' => 'Contact[5][email]',
  1724. 'id' => 'Contact5Email'
  1725. )),
  1726. '/div'
  1727. );
  1728. $this->assertTags($result, $expected);
  1729. $result = $this->Form->input('Contact.password');
  1730. $expected = array(
  1731. 'div' => array('class' => 'input password'),
  1732. 'label' => array('for' => 'ContactPassword'),
  1733. 'Password',
  1734. '/label',
  1735. array('input' => array(
  1736. 'type' => 'password', 'name' => 'Contact[password]',
  1737. 'id' => 'ContactPassword'
  1738. )),
  1739. '/div'
  1740. );
  1741. $this->assertTags($result, $expected);
  1742. $result = $this->Form->input('Contact.email', array(
  1743. 'type' => 'file', 'class' => 'textbox'
  1744. ));
  1745. $expected = array(
  1746. 'div' => array('class' => 'input file'),
  1747. 'label' => array('for' => 'ContactEmail'),
  1748. 'Email',
  1749. '/label',
  1750. array('input' => array(
  1751. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1752. 'id' => 'ContactEmail'
  1753. )),
  1754. '/div'
  1755. );
  1756. $this->assertTags($result, $expected);
  1757. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1758. $result = $this->Form->input('Contact.phone');
  1759. $expected = array(
  1760. 'div' => array('class' => 'input tel'),
  1761. 'label' => array('for' => 'ContactPhone'),
  1762. 'Phone',
  1763. '/label',
  1764. array('input' => array(
  1765. 'type' => 'tel', 'name' => 'Contact[phone]',
  1766. 'value' => 'Hello &amp; World &gt; weird chars',
  1767. 'id' => 'ContactPhone', 'maxlength' => 255
  1768. )),
  1769. '/div'
  1770. );
  1771. $this->assertTags($result, $expected);
  1772. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1773. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1774. $expected = array(
  1775. 'div' => array('class' => 'input text'),
  1776. 'label' => array('for' => 'myId'),
  1777. 'Field',
  1778. '/label',
  1779. 'input' => array(
  1780. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1781. 'value' => 'My value', 'id' => 'myId'
  1782. ),
  1783. '/div'
  1784. );
  1785. $this->assertTags($result, $expected);
  1786. unset($this->Form->request->data);
  1787. $Contact = ClassRegistry::getObject('Contact');
  1788. $Contact->validationErrors['field'] = array('Badness!');
  1789. $result = $this->Form->input('Contact.field');
  1790. $expected = array(
  1791. 'div' => array('class' => 'input text error'),
  1792. 'label' => array('for' => 'ContactField'),
  1793. 'Field',
  1794. '/label',
  1795. 'input' => array(
  1796. 'type' => 'text', 'name' => 'Contact[field]',
  1797. 'id' => 'ContactField', 'class' => 'form-error'
  1798. ),
  1799. array('div' => array('class' => 'error-message')),
  1800. 'Badness!',
  1801. '/div',
  1802. '/div'
  1803. );
  1804. $this->assertTags($result, $expected);
  1805. $result = $this->Form->input('Contact.field', array(
  1806. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1807. ));
  1808. $expected = array(
  1809. 'label' => array('for' => 'ContactField'),
  1810. 'Field',
  1811. '/label',
  1812. 'input' => array(
  1813. 'type' => 'text', 'name' => 'Contact[field]',
  1814. 'id' => 'ContactField', 'class' => 'form-error'
  1815. ),
  1816. array('span' => array('class' => 'error-message')),
  1817. 'Badness!',
  1818. '/span'
  1819. );
  1820. $this->assertTags($result, $expected);
  1821. $result = $this->Form->input('Contact.field', array(
  1822. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1823. ));
  1824. $expected = array(
  1825. 'div' => array('class' => 'input text error'),
  1826. 'label' => array('for' => 'ContactField'),
  1827. 'Field',
  1828. '/label',
  1829. 'input' => array(
  1830. 'type' => 'text', 'name' => 'Contact[field]',
  1831. 'id' => 'ContactField', 'class' => 'form-error'
  1832. ),
  1833. array('div' => array('class' => 'error')),
  1834. 'Badness!',
  1835. '/div'
  1836. );
  1837. $this->assertTags($result, $expected);
  1838. $result = $this->Form->input('Contact.field', array(
  1839. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1840. ));
  1841. $expected = array(
  1842. 'span' => array('class' => 'input text error'),
  1843. 'label' => array('for' => 'ContactField'),
  1844. 'Field',
  1845. '/label',
  1846. 'input' => array(
  1847. 'type' => 'text', 'name' => 'Contact[field]',
  1848. 'id' => 'ContactField', 'class' => 'form-error'
  1849. ),
  1850. 'Badness!',
  1851. '/span'
  1852. );
  1853. $this->assertTags($result, $expected);
  1854. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1855. $expected = array(
  1856. 'div' => array('class' => 'input text error'),
  1857. 'label' => array('for' => 'ContactField'),
  1858. 'Field',
  1859. '/label',
  1860. 'input' => array(
  1861. 'type' => 'text', 'name' => 'Contact[field]',
  1862. 'id' => 'ContactField', 'class' => 'form-error'
  1863. ),
  1864. 'A message to you, Rudy',
  1865. array('div' => array('class' => 'error-message')),
  1866. 'Badness!',
  1867. '/div',
  1868. '/div'
  1869. );
  1870. $this->assertTags($result, $expected);
  1871. $this->Form->setEntity(null);
  1872. $this->Form->setEntity('Contact.field');
  1873. $result = $this->Form->input('Contact.field', array(
  1874. 'after' => 'A message to you, Rudy', 'error' => false
  1875. ));
  1876. $expected = array(
  1877. 'div' => array('class' => 'input text'),
  1878. 'label' => array('for' => 'ContactField'),
  1879. 'Field',
  1880. '/label',
  1881. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1882. 'A message to you, Rudy',
  1883. '/div'
  1884. );
  1885. $this->assertTags($result, $expected);
  1886. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1887. $expected = array(
  1888. 'div' => array('class' => 'input text'),
  1889. 'label' => array('for' => 'ObjectField'),
  1890. 'Field',
  1891. '/label',
  1892. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  1893. 'A message to you, Rudy',
  1894. '/div'
  1895. );
  1896. $this->assertTags($result, $expected);
  1897. $Contact->validationErrors['field'] = array('minLength');
  1898. $result = $this->Form->input('Contact.field', array(
  1899. 'error' => array(
  1900. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1901. 'maxLength' => 'login too large'
  1902. )
  1903. ));
  1904. $expected = array(
  1905. 'div' => array('class' => 'input text error'),
  1906. 'label' => array('for' => 'ContactField'),
  1907. 'Field',
  1908. '/label',
  1909. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1910. array('div' => array('class' => 'error-message')),
  1911. 'Le login doit contenir au moins 2 caractères',
  1912. '/div',
  1913. '/div'
  1914. );
  1915. $this->assertTags($result, $expected);
  1916. $Contact->validationErrors['field'] = array('maxLength');
  1917. $result = $this->Form->input('Contact.field', array(
  1918. 'error' => array(
  1919. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1920. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1921. 'maxLength' => 'login too large',
  1922. )
  1923. ));
  1924. $expected = array(
  1925. 'div' => array('class' => 'input text error'),
  1926. 'label' => array('for' => 'ContactField'),
  1927. 'Field',
  1928. '/label',
  1929. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1930. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1931. 'login too large',
  1932. '/span',
  1933. '/div'
  1934. );
  1935. $this->assertTags($result, $expected);
  1936. }
  1937. /**
  1938. * Test that inputs with 0 can be created.
  1939. *
  1940. * @return void
  1941. */
  1942. public function testInputZero() {
  1943. $this->Form->create('User');
  1944. $result = $this->Form->input('0');
  1945. $expected = array(
  1946. 'div' => array('class' => 'input text'),
  1947. 'label' => array('for' => 'User0'), '/label',
  1948. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  1949. '/div'
  1950. );
  1951. $this->assertTags($result, $expected);
  1952. }
  1953. /**
  1954. * test input() with checkbox creation
  1955. *
  1956. * @return void
  1957. */
  1958. public function testInputCheckbox() {
  1959. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1960. $expected = array(
  1961. 'div' => array('class' => 'input checkbox'),
  1962. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1963. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1964. '/div'
  1965. );
  1966. $this->assertTags($result, $expected);
  1967. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1968. $expected = array(
  1969. 'div' => array('class' => 'input checkbox'),
  1970. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1971. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1972. '/div'
  1973. );
  1974. $this->assertTags($result, $expected);
  1975. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1976. $expected = array(
  1977. 'div' => array('class' => 'input checkbox'),
  1978. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  1979. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1980. '/div'
  1981. );
  1982. $this->assertTags($result, $expected);
  1983. $result = $this->Form->input('User.disabled', array(
  1984. 'label' => 'Disabled',
  1985. 'type' => 'checkbox',
  1986. 'data-foo' => 'disabled'
  1987. ));
  1988. $expected = array(
  1989. 'div' => array('class' => 'input checkbox'),
  1990. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  1991. array('input' => array(
  1992. 'type' => 'checkbox',
  1993. 'name' => 'data[User][disabled]',
  1994. 'value' => '1',
  1995. 'id' => 'UserDisabled',
  1996. 'data-foo' => 'disabled'
  1997. )),
  1998. 'label' => array('for' => 'UserDisabled'),
  1999. 'Disabled',
  2000. '/label',
  2001. '/div'
  2002. );
  2003. $this->assertTags($result, $expected);
  2004. }
  2005. /**
  2006. * test form->input() with time types.
  2007. *
  2008. */
  2009. public function testInputTime() {
  2010. extract($this->dateRegex);
  2011. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2012. $result = explode(':', $result);
  2013. $this->assertRegExp('/option value="23"/', $result[0]);
  2014. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2015. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2016. $result = explode(':', $result);
  2017. $this->assertRegExp('/option value="23"/', $result[0]);
  2018. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2019. $result = $this->Form->input('Model.field', array(
  2020. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2021. ));
  2022. $result = explode(':', $result);
  2023. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2024. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2025. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2026. $result = $this->Form->input('Model.field', array(
  2027. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2028. ));
  2029. $result = explode(':', $result);
  2030. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2031. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2032. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2033. $result = $this->Form->input('prueba', array(
  2034. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2035. 'maxYear' => date('Y') + 1, 'interval' => 15
  2036. ));
  2037. $result = explode(':', $result);
  2038. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2039. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2040. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2041. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2042. $result = $this->Form->input('Random.start_time', array(
  2043. 'type' => 'time',
  2044. 'selected' => '18:15'
  2045. ));
  2046. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2047. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2048. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2049. $result = $this->Form->input('published', array('type' => 'time'));
  2050. $now = strtotime('now');
  2051. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2052. $now = strtotime('2013-03-09 00:42:21');
  2053. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2054. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2055. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2056. }
  2057. /**
  2058. * Test interval + selected near the hour roll over.
  2059. *
  2060. * @return void
  2061. */
  2062. public function testTimeSelectedWithInterval() {
  2063. $result = $this->Form->input('Model.start_time', array(
  2064. 'type' => 'time',
  2065. 'interval' => 15,
  2066. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2067. ));
  2068. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2069. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2070. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2071. $result = $this->Form->input('Model.start_time', array(
  2072. 'type' => 'time',
  2073. 'interval' => 15,
  2074. 'selected' => '2012-10-23 15:57:00'
  2075. ));
  2076. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2077. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2078. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2079. $result = $this->Form->input('Model.start_time', array(
  2080. 'timeFormat' => 24,
  2081. 'type' => 'time',
  2082. 'interval' => 15,
  2083. 'selected' => '15:57'
  2084. ));
  2085. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2086. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2087. $result = $this->Form->input('Model.start_time', array(
  2088. 'timeFormat' => 24,
  2089. 'type' => 'time',
  2090. 'interval' => 15,
  2091. 'selected' => '23:57'
  2092. ));
  2093. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2094. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2095. $result = $this->Form->input('Model.created', array(
  2096. 'timeFormat' => 24,
  2097. 'type' => 'datetime',
  2098. 'interval' => 15,
  2099. 'selected' => '2012-09-30 23:56'
  2100. ));
  2101. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2102. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2103. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2104. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2105. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2106. }
  2107. /**
  2108. * Test interval & timeFormat = 12
  2109. *
  2110. * @return void
  2111. */
  2112. public function testInputTimeWithIntervalAnd12HourFormat() {
  2113. $result = $this->Form->input('Model.start_time', array(
  2114. 'type' => 'time',
  2115. 'timeFormat' => 12,
  2116. 'interval' => 5,
  2117. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2118. ));
  2119. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2120. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2121. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2122. $result = $this->Form->input('Model.start_time', array(
  2123. 'type' => 'time',
  2124. 'timeFormat' => '12',
  2125. 'interval' => 5,
  2126. 'selected' => '2013-04-19 16:30:00'
  2127. ));
  2128. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2129. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2130. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2131. $result = $this->Form->input('Model.start_time', array(
  2132. 'type' => 'time',
  2133. 'timeFormat' => '12',
  2134. 'interval' => 10,
  2135. 'selected' => '2013-05-19 00:33:00'
  2136. ));
  2137. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2138. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2139. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2140. $result = $this->Form->input('Model.start_time', array(
  2141. 'type' => 'time',
  2142. 'timeFormat' => '12',
  2143. 'interval' => 10,
  2144. 'selected' => '2013-05-19 13:33:00'
  2145. ));
  2146. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2147. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2148. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2149. $result = $this->Form->input('Model.start_time', array(
  2150. 'type' => 'time',
  2151. 'timeFormat' => '12',
  2152. 'interval' => 10,
  2153. 'selected' => '2013-05-19 01:33:00'
  2154. ));
  2155. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2156. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2157. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2158. }
  2159. /**
  2160. * test form->input() with datetime, date and time types
  2161. *
  2162. * @return void
  2163. */
  2164. public function testInputDatetime() {
  2165. extract($this->dateRegex);
  2166. $result = $this->Form->input('prueba', array(
  2167. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2168. 'maxYear' => date('Y') + 1, 'interval' => 15
  2169. ));
  2170. $result = explode(':', $result);
  2171. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2172. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2173. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2174. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2175. //related to ticket #5013
  2176. $result = $this->Form->input('Contact.date', array(
  2177. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2178. ));
  2179. $this->assertRegExp('/class="customClass"/', $result);
  2180. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2181. $result = $this->Form->input('Contact.date', array(
  2182. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2183. ));
  2184. $this->assertRegExp('/id="customIdDay"/', $result);
  2185. $this->assertRegExp('/id="customIdMonth"/', $result);
  2186. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2187. $result = $this->Form->input('Model.field', array(
  2188. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2189. ));
  2190. $this->assertRegExp('/id="customIDDay"/', $result);
  2191. $this->assertRegExp('/id="customIDHour"/', $result);
  2192. $result = explode('</select><select', $result);
  2193. $result = explode(':', $result[1]);
  2194. $this->assertRegExp('/option value="23"/', $result[0]);
  2195. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2196. $result = $this->Form->input('Model.field', array(
  2197. 'type' => 'datetime', 'timeFormat' => 12
  2198. ));
  2199. $result = explode('</select><select', $result);
  2200. $result = explode(':', $result[1]);
  2201. $this->assertRegExp('/option value="12"/', $result[0]);
  2202. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2203. $this->Form->request->data = array('Contact' => array('created' => null));
  2204. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2205. $expected = array(
  2206. 'div' => array('class' => 'input date'),
  2207. 'label' => array('for' => 'ContactCreatedMonth'),
  2208. 'Created',
  2209. '/label',
  2210. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2211. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2212. $monthsRegex,
  2213. '/select', '-',
  2214. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2215. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2216. $daysRegex,
  2217. '/select', '-',
  2218. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2219. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2220. $yearsRegex,
  2221. '/select',
  2222. '/div'
  2223. );
  2224. $this->assertTags($result, $expected);
  2225. $this->Form->request->data = array('Contact' => array('created' => null));
  2226. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2227. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2228. $this->Form->request->data = array('Contact' => array('created' => null));
  2229. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2230. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2231. $result = $this->Form->input('Contact.created', array(
  2232. 'type' => 'date',
  2233. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2234. 'timeFormat' => 'NONE'
  2235. ));
  2236. $this->assertRegExp('/for\="created-month"/', $result);
  2237. }
  2238. /**
  2239. * Test generating checkboxes in a loop.
  2240. *
  2241. * @return void
  2242. */
  2243. public function testInputCheckboxesInLoop() {
  2244. for ($i = 1; $i < 5; $i++) {
  2245. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2246. $expected = array(
  2247. 'div' => array('class' => 'input checkbox'),
  2248. 'input' => array('type' => 'hidden', 'name' => "Contact[{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2249. array('input' => array('type' => 'checkbox', 'name' => "Contact[{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2250. 'label' => array('for' => "Contact{$i}Email"),
  2251. 'Email',
  2252. '/label',
  2253. '/div'
  2254. );
  2255. $this->assertTags($result, $expected);
  2256. }
  2257. }
  2258. /**
  2259. * Test generating checkboxes with disabled elements.
  2260. *
  2261. * @return void
  2262. */
  2263. public function testInputCheckboxWithDisabledElements() {
  2264. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2265. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2266. $expected = array(
  2267. array('div' => array('class' => 'input select')),
  2268. array('label' => array('for' => "ContactMultiple")),
  2269. 'Multiple',
  2270. '/label',
  2271. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2272. array('div' => array('class' => 'checkbox')),
  2273. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2274. array('label' => array('for' => "ContactMultiple1")),
  2275. 'One',
  2276. '/label',
  2277. '/div',
  2278. array('div' => array('class' => 'checkbox')),
  2279. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2280. array('label' => array('for' => "ContactMultiple2")),
  2281. 'Two',
  2282. '/label',
  2283. '/div',
  2284. array('div' => array('class' => 'checkbox')),
  2285. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2286. array('label' => array('for' => "ContactMultiple3")),
  2287. 'Three',
  2288. '/label',
  2289. '/div',
  2290. '/div'
  2291. );
  2292. $this->assertTags($result, $expected);
  2293. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2294. $this->assertTags($result, $expected);
  2295. $disabled = array('2', 3);
  2296. $expected = array(
  2297. array('div' => array('class' => 'input select')),
  2298. array('label' => array('for' => "ContactMultiple")),
  2299. 'Multiple',
  2300. '/label',
  2301. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2302. array('div' => array('class' => 'checkbox')),
  2303. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2304. array('label' => array('for' => "ContactMultiple1")),
  2305. 'One',
  2306. '/label',
  2307. '/div',
  2308. array('div' => array('class' => 'checkbox')),
  2309. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2310. array('label' => array('for' => "ContactMultiple2")),
  2311. 'Two',
  2312. '/label',
  2313. '/div',
  2314. array('div' => array('class' => 'checkbox')),
  2315. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2316. array('label' => array('for' => "ContactMultiple3")),
  2317. 'Three',
  2318. '/label',
  2319. '/div',
  2320. '/div'
  2321. );
  2322. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2323. $this->assertTags($result, $expected);
  2324. // make sure 50 does only disable 50, and not 50f5c0cf
  2325. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2326. $disabled = array(50);
  2327. $expected = array(
  2328. array('div' => array('class' => 'input select')),
  2329. array('label' => array('for' => "ContactMultiple")),
  2330. 'Multiple',
  2331. '/label',
  2332. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2333. array('div' => array('class' => 'checkbox')),
  2334. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2335. array('label' => array('for' => "ContactMultiple50")),
  2336. 'Fifty',
  2337. '/label',
  2338. '/div',
  2339. array('div' => array('class' => 'checkbox')),
  2340. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2341. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2342. 'Stringy',
  2343. '/label',
  2344. '/div',
  2345. '/div'
  2346. );
  2347. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2348. $this->assertTags($result, $expected);
  2349. }
  2350. /**
  2351. * test input name with leading integer, ensure attributes are generated correctly.
  2352. *
  2353. * @return void
  2354. */
  2355. public function testInputWithLeadingInteger() {
  2356. $result = $this->Form->text('0.Node.title');
  2357. $expected = array(
  2358. 'input' => array('name' => '0[Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2359. );
  2360. $this->assertTags($result, $expected);
  2361. }
  2362. /**
  2363. * test form->input() with select type inputs.
  2364. *
  2365. * @return void
  2366. */
  2367. public function testInputSelectType() {
  2368. $result = $this->Form->input('email', array(
  2369. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2370. );
  2371. $expected = array(
  2372. 'div' => array('class' => 'input select'),
  2373. 'label' => array('for' => 'email'),
  2374. 'Email',
  2375. '/label',
  2376. array('select' => array('name' => 'email', 'id' => 'email')),
  2377. array('option' => array('value' => '')),
  2378. '/option',
  2379. array('option' => array('value' => 'è')),
  2380. 'Firést',
  2381. '/option',
  2382. array('option' => array('value' => 'é')),
  2383. 'Secoènd',
  2384. '/option',
  2385. '/select',
  2386. '/div'
  2387. );
  2388. $this->assertTags($result, $expected);
  2389. $result = $this->Form->input('email', array(
  2390. 'options' => array('First', 'Second'), 'empty' => true)
  2391. );
  2392. $expected = array(
  2393. 'div' => array('class' => 'input select'),
  2394. 'label' => array('for' => 'email'),
  2395. 'Email',
  2396. '/label',
  2397. array('select' => array('name' => 'email', 'id' => 'email')),
  2398. array('option' => array('value' => '')),
  2399. '/option',
  2400. array('option' => array('value' => '0')),
  2401. 'First',
  2402. '/option',
  2403. array('option' => array('value' => '1')),
  2404. 'Second',
  2405. '/option',
  2406. '/select',
  2407. '/div'
  2408. );
  2409. $this->assertTags($result, $expected);
  2410. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2411. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2412. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2413. $expected = array(
  2414. 'div' => array('class' => 'input select'),
  2415. 'label' => array('for' => 'ModelUserId'),
  2416. 'User',
  2417. '/label',
  2418. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2419. array('option' => array('value' => '')),
  2420. '/option',
  2421. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2422. 'good',
  2423. '/option',
  2424. array('option' => array('value' => 'other')),
  2425. 'bad',
  2426. '/option',
  2427. '/select',
  2428. '/div'
  2429. );
  2430. $this->assertTags($result, $expected);
  2431. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2432. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2433. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2434. $expected = array(
  2435. 'div' => array('class' => 'input select'),
  2436. 'label' => array('for' => 'ThingUserId'),
  2437. 'User',
  2438. '/label',
  2439. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2440. array('option' => array('value' => '')),
  2441. 'Some Empty',
  2442. '/option',
  2443. array('option' => array('value' => 'value')),
  2444. 'good',
  2445. '/option',
  2446. array('option' => array('value' => 'other')),
  2447. 'bad',
  2448. '/option',
  2449. '/select',
  2450. '/div'
  2451. );
  2452. $this->assertTags($result, $expected);
  2453. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2454. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2455. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2456. $expected = array(
  2457. 'div' => array('class' => 'input select'),
  2458. 'label' => array('for' => 'ThingUserId'),
  2459. 'User',
  2460. '/label',
  2461. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2462. array('option' => array('value' => '')),
  2463. 'Some Empty',
  2464. '/option',
  2465. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2466. 'good',
  2467. '/option',
  2468. array('option' => array('value' => 'other')),
  2469. 'bad',
  2470. '/option',
  2471. '/select',
  2472. '/div'
  2473. );
  2474. $this->assertTags($result, $expected);
  2475. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2476. $this->Form->request->data = array('User' => array('User' => array('value')));
  2477. $result = $this->Form->input('User.User', array('empty' => true));
  2478. $expected = array(
  2479. 'div' => array('class' => 'input select'),
  2480. 'label' => array('for' => 'UserUser'),
  2481. 'User',
  2482. '/label',
  2483. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  2484. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2485. array('option' => array('value' => '')),
  2486. '/option',
  2487. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2488. 'good',
  2489. '/option',
  2490. array('option' => array('value' => 'other')),
  2491. 'bad',
  2492. '/option',
  2493. '/select',
  2494. '/div'
  2495. );
  2496. $this->assertTags($result, $expected);
  2497. $this->Form->data = array();
  2498. $result = $this->Form->input('Publisher.id', array(
  2499. 'label' => 'Publisher',
  2500. 'type' => 'select',
  2501. 'multiple' => 'checkbox',
  2502. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2503. ));
  2504. $expected = array(
  2505. array('div' => array('class' => 'input select')),
  2506. array('label' => array('for' => 'PublisherId')),
  2507. 'Publisher',
  2508. '/label',
  2509. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  2510. array('div' => array('class' => 'checkbox')),
  2511. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2512. array('label' => array('for' => 'PublisherIdValue1')),
  2513. 'Label 1',
  2514. '/label',
  2515. '/div',
  2516. array('div' => array('class' => 'checkbox')),
  2517. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2518. array('label' => array('for' => 'PublisherIdValue2')),
  2519. 'Label 2',
  2520. '/label',
  2521. '/div',
  2522. '/div'
  2523. );
  2524. $this->assertTags($result, $expected);
  2525. }
  2526. /**
  2527. * test that input() and a non standard primary key makes a hidden input by default.
  2528. *
  2529. * @return void
  2530. */
  2531. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2532. $this->Form->create('User');
  2533. $this->Form->fieldset = array(
  2534. 'User' => array(
  2535. 'fields' => array(
  2536. 'model_id' => array('type' => 'integer')
  2537. ),
  2538. 'validates' => array(),
  2539. 'key' => 'model_id'
  2540. )
  2541. );
  2542. $result = $this->Form->input('model_id');
  2543. $expected = array(
  2544. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  2545. );
  2546. $this->assertTags($result, $expected);
  2547. }
  2548. /**
  2549. * test that overriding the magic select type widget is possible
  2550. *
  2551. * @return void
  2552. */
  2553. public function testInputOverridingMagicSelectType() {
  2554. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2555. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2556. $expected = array(
  2557. 'div' => array('class' => 'input text'),
  2558. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2559. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2560. '/div'
  2561. );
  2562. $this->assertTags($result, $expected);
  2563. //Check that magic types still work for plural/singular vars
  2564. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2565. $result = $this->Form->input('Model.type');
  2566. $expected = array(
  2567. 'div' => array('class' => 'input select'),
  2568. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2569. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  2570. array('option' => array('value' => 'value')), 'good', '/option',
  2571. array('option' => array('value' => 'other')), 'bad', '/option',
  2572. '/select',
  2573. '/div'
  2574. );
  2575. $this->assertTags($result, $expected);
  2576. }
  2577. /**
  2578. * Test that magic input() selects are created for type=number
  2579. *
  2580. * @return void
  2581. */
  2582. public function testInputMagicSelectForTypeNumber() {
  2583. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2584. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2585. $result = $this->Form->input('ValidateUser.balance');
  2586. $expected = array(
  2587. 'div' => array('class' => 'input select'),
  2588. 'label' => array('for' => 'ValidateUserBalance'),
  2589. 'Balance',
  2590. '/label',
  2591. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  2592. array('option' => array('value' => '0')),
  2593. 'nothing',
  2594. '/option',
  2595. array('option' => array('value' => '1', 'selected' => 'selected')),
  2596. 'some',
  2597. '/option',
  2598. array('option' => array('value' => '100')),
  2599. 'a lot',
  2600. '/option',
  2601. '/select',
  2602. '/div'
  2603. );
  2604. $this->assertTags($result, $expected);
  2605. }
  2606. /**
  2607. * Test that magic input() selects can easily be converted into radio types without error.
  2608. *
  2609. * @return void
  2610. */
  2611. public function testInputMagicSelectChangeToRadio() {
  2612. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2613. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2614. $this->assertRegExp('/input type="radio"/', $result);
  2615. }
  2616. /**
  2617. * fields with the same name as the model should work.
  2618. *
  2619. * @return void
  2620. */
  2621. public function testInputWithMatchingFieldAndModelName() {
  2622. $this->Form->create('User');
  2623. $this->Form->fieldset = array(
  2624. 'User' => array(
  2625. 'fields' => array(
  2626. 'User' => array('type' => 'text')
  2627. ),
  2628. 'validates' => array(),
  2629. 'key' => 'id'
  2630. )
  2631. );
  2632. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2633. $result = $this->Form->input('User', array('type' => 'text'));
  2634. $expected = array(
  2635. 'div' => array('class' => 'input text'),
  2636. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2637. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2638. '/div'
  2639. );
  2640. $this->assertTags($result, $expected);
  2641. }
  2642. /**
  2643. * testFormInputs method
  2644. *
  2645. * test correct results from form::inputs().
  2646. *
  2647. * @return void
  2648. */
  2649. public function testFormInputs() {
  2650. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  2651. $result = $this->Form->inputs('The Legend');
  2652. $expected = array(
  2653. '<fieldset',
  2654. '<legend',
  2655. 'The Legend',
  2656. '/legend',
  2657. '*/fieldset',
  2658. );
  2659. $this->assertTags($result, $expected);
  2660. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2661. $expected = array(
  2662. 'fieldset' => array('class' => 'classy-stuff'),
  2663. '<legend',
  2664. 'Field of Dreams',
  2665. '/legend',
  2666. '*/fieldset'
  2667. );
  2668. $this->assertTags($result, $expected);
  2669. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2670. $this->assertTags($result, $expected);
  2671. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2672. $this->assertTags($result, $expected);
  2673. $this->Form->create('Contact');
  2674. $this->Form->request['prefix'] = 'admin';
  2675. $this->Form->request['action'] = 'admin_edit';
  2676. $result = $this->Form->inputs();
  2677. $expected = array(
  2678. '<fieldset',
  2679. '<legend',
  2680. 'Edit Contact',
  2681. '/legend',
  2682. '*/fieldset',
  2683. );
  2684. $this->assertTags($result, $expected);
  2685. $this->Form->create('Contact');
  2686. $result = $this->Form->inputs(false);
  2687. $expected = array(
  2688. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2689. array('div' => array('class' => 'input text')),
  2690. '*/div',
  2691. array('div' => array('class' => 'input email')),
  2692. '*/div',
  2693. array('div' => array('class' => 'input tel')),
  2694. '*/div',
  2695. array('div' => array('class' => 'input password')),
  2696. '*/div',
  2697. array('div' => array('class' => 'input date')),
  2698. '*/div',
  2699. array('div' => array('class' => 'input date')),
  2700. '*/div',
  2701. array('div' => array('class' => 'input datetime')),
  2702. '*/div',
  2703. array('div' => array('class' => 'input number')),
  2704. '*/div',
  2705. array('div' => array('class' => 'input select')),
  2706. '*/div',
  2707. );
  2708. $this->assertTags($result, $expected);
  2709. $this->Form->create('Contact');
  2710. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2711. $expected = array(
  2712. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2713. array('div' => array('class' => 'input text')),
  2714. '*/div',
  2715. array('div' => array('class' => 'input email')),
  2716. '*/div',
  2717. array('div' => array('class' => 'input tel')),
  2718. '*/div',
  2719. array('div' => array('class' => 'input password')),
  2720. '*/div',
  2721. array('div' => array('class' => 'input date')),
  2722. '*/div',
  2723. array('div' => array('class' => 'input date')),
  2724. '*/div',
  2725. array('div' => array('class' => 'input datetime')),
  2726. '*/div',
  2727. array('div' => array('class' => 'input number')),
  2728. '*/div',
  2729. array('div' => array('class' => 'input select')),
  2730. '*/div',
  2731. );
  2732. $this->assertTags($result, $expected);
  2733. $this->Form->create('Contact');
  2734. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2735. $this->assertTags($result, $expected);
  2736. $this->Form->create('Contact');
  2737. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2738. $expected = array(
  2739. 'fieldset' => array(),
  2740. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2741. array('div' => array('class' => 'input text')),
  2742. '*/div',
  2743. array('div' => array('class' => 'input email')),
  2744. '*/div',
  2745. array('div' => array('class' => 'input tel')),
  2746. '*/div',
  2747. array('div' => array('class' => 'input password')),
  2748. '*/div',
  2749. array('div' => array('class' => 'input date')),
  2750. '*/div',
  2751. array('div' => array('class' => 'input date')),
  2752. '*/div',
  2753. array('div' => array('class' => 'input datetime')),
  2754. '*/div',
  2755. array('div' => array('class' => 'input number')),
  2756. '*/div',
  2757. array('div' => array('class' => 'input select')),
  2758. '*/div',
  2759. '/fieldset'
  2760. );
  2761. $this->assertTags($result, $expected);
  2762. $this->Form->create('Contact');
  2763. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2764. $expected = array(
  2765. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2766. array('div' => array('class' => 'input text')),
  2767. '*/div',
  2768. array('div' => array('class' => 'input email')),
  2769. '*/div',
  2770. array('div' => array('class' => 'input tel')),
  2771. '*/div',
  2772. array('div' => array('class' => 'input password')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input date')),
  2775. '*/div',
  2776. array('div' => array('class' => 'input date')),
  2777. '*/div',
  2778. array('div' => array('class' => 'input datetime')),
  2779. '*/div',
  2780. array('div' => array('class' => 'input number')),
  2781. '*/div',
  2782. array('div' => array('class' => 'input select')),
  2783. '*/div',
  2784. );
  2785. $this->assertTags($result, $expected);
  2786. $this->Form->create('Contact');
  2787. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2788. $this->assertTags($result, $expected);
  2789. $this->Form->create('Contact');
  2790. $result = $this->Form->inputs('Hello');
  2791. $expected = array(
  2792. 'fieldset' => array(),
  2793. 'legend' => array(),
  2794. 'Hello',
  2795. '/legend',
  2796. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2797. array('div' => array('class' => 'input text')),
  2798. '*/div',
  2799. array('div' => array('class' => 'input email')),
  2800. '*/div',
  2801. array('div' => array('class' => 'input tel')),
  2802. '*/div',
  2803. array('div' => array('class' => 'input password')),
  2804. '*/div',
  2805. array('div' => array('class' => 'input date')),
  2806. '*/div',
  2807. array('div' => array('class' => 'input date')),
  2808. '*/div',
  2809. array('div' => array('class' => 'input datetime')),
  2810. '*/div',
  2811. array('div' => array('class' => 'input number')),
  2812. '*/div',
  2813. array('div' => array('class' => 'input select')),
  2814. '*/div',
  2815. '/fieldset'
  2816. );
  2817. $this->assertTags($result, $expected);
  2818. $this->Form->create('Contact');
  2819. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2820. $expected = array(
  2821. 'fieldset' => array(),
  2822. 'legend' => array(),
  2823. 'Hello',
  2824. '/legend',
  2825. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  2826. array('div' => array('class' => 'input text')),
  2827. '*/div',
  2828. array('div' => array('class' => 'input email')),
  2829. '*/div',
  2830. array('div' => array('class' => 'input tel')),
  2831. '*/div',
  2832. array('div' => array('class' => 'input password')),
  2833. '*/div',
  2834. array('div' => array('class' => 'input date')),
  2835. '*/div',
  2836. array('div' => array('class' => 'input date')),
  2837. '*/div',
  2838. array('div' => array('class' => 'input datetime')),
  2839. '*/div',
  2840. array('div' => array('class' => 'input number')),
  2841. '*/div',
  2842. array('div' => array('class' => 'input select')),
  2843. '*/div',
  2844. '/fieldset'
  2845. );
  2846. $this->assertTags($result, $expected);
  2847. $this->Form->create('Contact');
  2848. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2849. $this->assertTags($result, $expected);
  2850. $this->Form->end();
  2851. $this->Form->create(false);
  2852. $expected = array(
  2853. 'fieldset' => array(),
  2854. array('div' => array('class' => 'input text')),
  2855. 'label' => array('for' => 'foo'),
  2856. 'Foo',
  2857. '/label',
  2858. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2859. '*/div',
  2860. '/fieldset'
  2861. );
  2862. $result = $this->Form->inputs(
  2863. array('foo' => array('type' => 'text')),
  2864. array(),
  2865. array('legend' => false)
  2866. );
  2867. $this->assertTags($result, $expected);
  2868. }
  2869. /**
  2870. * Tests inputs() works with plugin models
  2871. *
  2872. * @return void
  2873. */
  2874. public function testInputsPluginModel() {
  2875. $this->loadFixtures('Post');
  2876. Plugin::load('TestPlugin');
  2877. $this->Form->request['models'] = array(
  2878. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  2879. );
  2880. $this->Form->create('TestPlugin.TestPluginPost');
  2881. $result = $this->Form->inputs();
  2882. $this->assertContains('TestPluginPost[id]', $result);
  2883. $this->assertContains('TestPluginPost[author_id]', $result);
  2884. $this->assertContains('TestPluginPost[title]', $result);
  2885. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  2886. $this->assertFalse(ClassRegistry::isKeySet('TestPlugin'));
  2887. $this->assertEquals('TestPluginPost', $this->Form->model());
  2888. }
  2889. /**
  2890. * testSelectAsCheckbox method
  2891. *
  2892. * test multi-select widget with checkbox formatting.
  2893. *
  2894. * @return void
  2895. */
  2896. public function testSelectAsCheckbox() {
  2897. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2898. $expected = array(
  2899. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2900. array('div' => array('class' => 'checkbox')),
  2901. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2902. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2903. 'first',
  2904. '/label',
  2905. '/div',
  2906. array('div' => array('class' => 'checkbox')),
  2907. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2908. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2909. 'second',
  2910. '/label',
  2911. '/div',
  2912. array('div' => array('class' => 'checkbox')),
  2913. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2914. array('label' => array('for' => 'ModelMultiField2')),
  2915. 'third',
  2916. '/label',
  2917. '/div',
  2918. );
  2919. $this->assertTags($result, $expected);
  2920. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2921. $expected = array(
  2922. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2923. array('div' => array('class' => 'checkbox')),
  2924. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2925. array('label' => array('for' => 'ModelMultiField12')),
  2926. 'half',
  2927. '/label',
  2928. '/div',
  2929. );
  2930. $this->assertTags($result, $expected);
  2931. }
  2932. /**
  2933. * testLabel method
  2934. *
  2935. * test label generation.
  2936. *
  2937. * @return void
  2938. */
  2939. public function testLabel() {
  2940. $this->Form->text('Person.name');
  2941. $result = $this->Form->label();
  2942. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2943. $this->Form->text('Person.name');
  2944. $result = $this->Form->label();
  2945. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2946. $result = $this->Form->label('Person.first_name');
  2947. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2948. $result = $this->Form->label('Person.first_name', 'Your first name');
  2949. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2950. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2951. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2952. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2953. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2954. $result = $this->Form->label('Person.first_name', '');
  2955. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2956. $result = $this->Form->label('Person.2.name', '');
  2957. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2958. }
  2959. /**
  2960. * testTextbox method
  2961. *
  2962. * test textbox element generation
  2963. *
  2964. * @return void
  2965. */
  2966. public function testTextbox() {
  2967. $result = $this->Form->text('Model.field');
  2968. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'ModelField')));
  2969. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2970. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]', 'id' => 'ModelField')));
  2971. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2972. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  2973. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2974. $result = $this->Form->text('Model.text');
  2975. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2976. $Contact = ClassRegistry::getObject('Contact');
  2977. $Contact->validationErrors['text'] = array(true);
  2978. $this->Form->request->data['Contact']['text'] = 'test';
  2979. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2980. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Contact[text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2981. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2982. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2983. $expected = array(
  2984. 'input' => array('type' => 'text', 'name' => 'Model[0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2985. );
  2986. $this->assertTags($result, $expected);
  2987. }
  2988. /**
  2989. * testDefaultValue method
  2990. *
  2991. * Test default value setting
  2992. *
  2993. * @return void
  2994. */
  2995. public function testDefaultValue() {
  2996. $this->Form->request->data['Model']['field'] = 'test';
  2997. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2998. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'test', 'id' => 'ModelField')));
  2999. unset($this->Form->request->data['Model']['field']);
  3000. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3001. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'value' => 'default value', 'id' => 'ModelField')));
  3002. }
  3003. /**
  3004. * testCheckboxDefaultValue method
  3005. *
  3006. * Test default value setting on checkbox() method
  3007. *
  3008. * @return void
  3009. */
  3010. public function testCheckboxDefaultValue() {
  3011. $this->Form->request->data['Model']['field'] = false;
  3012. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3013. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3014. unset($this->Form->request->data['Model']['field']);
  3015. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3016. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3017. $this->Form->request->data['Model']['field'] = true;
  3018. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3019. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3020. unset($this->Form->request->data['Model']['field']);
  3021. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3022. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3023. }
  3024. /**
  3025. * testError method
  3026. *
  3027. * Test field error generation
  3028. *
  3029. * @return void
  3030. */
  3031. public function testError() {
  3032. $Contact = ClassRegistry::getObject('Contact');
  3033. $Contact->validationErrors['field'] = array(1);
  3034. $result = $this->Form->error('Contact.field');
  3035. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3036. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3037. $this->assertEquals('Error in field Field', $result);
  3038. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3039. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3040. $this->assertEquals('This field contains invalid input', $result);
  3041. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3042. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3043. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3044. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3045. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3046. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3047. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3048. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3049. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3050. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3051. $this->assertEquals('<strong>Badness!</strong>', $result);
  3052. $Contact->validationErrors['field'] = array("email");
  3053. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3054. $expected = array(
  3055. 'div' => array('class' => 'field-error'),
  3056. 'No good!',
  3057. '/div'
  3058. );
  3059. $this->assertTags($result, $expected);
  3060. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3061. $result = $this->Form->error('Contact.field', array(
  3062. 'notEmpty' => 'Cannot be empty',
  3063. 'email' => 'No good!'
  3064. ));
  3065. $expected = array(
  3066. 'div' => array('class' => 'error-message'),
  3067. 'ul' => array(),
  3068. '<li', 'Cannot be empty', '/li',
  3069. '<li', 'No good!', '/li',
  3070. '<li', 'Something else', '/li',
  3071. '/ul',
  3072. '/div'
  3073. );
  3074. $this->assertTags($result, $expected);
  3075. // Testing error messages list options
  3076. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3077. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3078. $expected = array(
  3079. 'div' => array('class' => 'error-message'),
  3080. 'ol' => array(),
  3081. '<li', 'notEmpty', '/li',
  3082. '<li', 'email', '/li',
  3083. '/ol',
  3084. '/div'
  3085. );
  3086. $this->assertTags($result, $expected);
  3087. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3088. $expected = array(
  3089. 'div' => array('class' => 'error-message'),
  3090. 'ol' => array(),
  3091. '<li', 'notEmpty', '/li',
  3092. '<li', 'email', '/li',
  3093. '/ol',
  3094. '/div'
  3095. );
  3096. $this->assertTags($result, $expected);
  3097. $result = $this->Form->error('Contact.field', null, array(
  3098. 'listOptions' => array(
  3099. 'class' => 'ul-class',
  3100. 'itemOptions' => array(
  3101. 'class' => 'li-class'
  3102. )
  3103. )
  3104. ));
  3105. $expected = array(
  3106. 'div' => array('class' => 'error-message'),
  3107. 'ul' => array('class' => 'ul-class'),
  3108. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3109. array('li' => array('class' => 'li-class')), 'email', '/li',
  3110. '/ul',
  3111. '/div'
  3112. );
  3113. $this->assertTags($result, $expected);
  3114. }
  3115. /**
  3116. * test error options when using form->input();
  3117. *
  3118. * @return void
  3119. */
  3120. public function testInputErrorEscape() {
  3121. $this->Form->create('ValidateProfile');
  3122. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  3123. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3124. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3125. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3126. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3127. $this->assertRegExp('/required<br>/', $result);
  3128. }
  3129. /**
  3130. * testPassword method
  3131. *
  3132. * Test password element generation
  3133. *
  3134. * @return void
  3135. */
  3136. public function testPassword() {
  3137. $Contact = ClassRegistry::getObject('Contact');
  3138. $result = $this->Form->password('Contact.field');
  3139. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]', 'id' => 'ContactField')));
  3140. $Contact->validationErrors['passwd'] = 1;
  3141. $this->Form->request->data['Contact']['passwd'] = 'test';
  3142. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3143. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3144. }
  3145. /**
  3146. * testRadio method
  3147. *
  3148. * Test radio element set generation
  3149. *
  3150. * @return void
  3151. */
  3152. public function testRadio() {
  3153. $result = $this->Form->radio('Model.field', array('option A'));
  3154. $expected = array(
  3155. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3156. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3157. 'label' => array('for' => 'ModelField0'),
  3158. 'option A',
  3159. '/label'
  3160. );
  3161. $this->assertTags($result, $expected);
  3162. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  3163. $expected = array(
  3164. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3165. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  3166. 'label' => array('for' => 'ModelField12'),
  3167. 'half',
  3168. '/label'
  3169. );
  3170. $this->assertTags($result, $expected);
  3171. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3172. $expected = array(
  3173. 'fieldset' => array(),
  3174. 'legend' => array(),
  3175. 'Field',
  3176. '/legend',
  3177. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3178. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3179. array('label' => array('for' => 'ModelField0')),
  3180. 'option A',
  3181. '/label',
  3182. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3183. array('label' => array('for' => 'ModelField1')),
  3184. 'option B',
  3185. '/label',
  3186. '/fieldset'
  3187. );
  3188. $this->assertTags($result, $expected);
  3189. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3190. $expected = array(
  3191. 'fieldset' => array(),
  3192. 'legend' => array(),
  3193. 'Field',
  3194. '/legend',
  3195. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3196. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3197. array('label' => array('for' => 'ModelField0')),
  3198. 'option A',
  3199. '/label',
  3200. 'br' => array(),
  3201. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3202. array('label' => array('for' => 'ModelField1')),
  3203. 'option B',
  3204. '/label',
  3205. '/fieldset'
  3206. );
  3207. $this->assertTags($result, $expected);
  3208. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3209. $expected = array(
  3210. 'fieldset' => array(),
  3211. 'legend' => array(),
  3212. 'Gender',
  3213. '/legend',
  3214. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3215. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3216. array('label' => array('for' => 'EmployeeGenderMale')),
  3217. 'Male',
  3218. '/label',
  3219. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3220. array('label' => array('for' => 'EmployeeGenderFemale')),
  3221. 'Female',
  3222. '/label',
  3223. '/fieldset',
  3224. );
  3225. $this->assertTags($result, $expected);
  3226. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3227. $expected = array(
  3228. 'fieldset' => array(),
  3229. 'legend' => array(),
  3230. 'Gender',
  3231. '/legend',
  3232. 'input' => array('type' => 'hidden', 'name' => 'Officer[gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3233. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3234. array('label' => array('for' => 'OfficerGenderMale')),
  3235. 'Male',
  3236. '/label',
  3237. array('input' => array('type' => 'radio', 'name' => 'Officer[gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3238. array('label' => array('for' => 'OfficerGenderFemale')),
  3239. 'Female',
  3240. '/label',
  3241. '/fieldset',
  3242. );
  3243. $this->assertTags($result, $expected);
  3244. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3245. $expected = array(
  3246. 'input' => array('type' => 'hidden', 'name' => 'Contact[1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3247. array('input' => array('type' => 'radio', 'name' => 'Contact[1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0', 'required' => 'required')),
  3248. 'label' => array('for' => 'Contact1Imrequired0'),
  3249. 'option A',
  3250. '/label'
  3251. );
  3252. $this->assertTags($result, $expected);
  3253. $result = $this->Form->radio('Model.1.field', array('option A'));
  3254. $expected = array(
  3255. 'input' => array('type' => 'hidden', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3256. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3257. 'label' => array('for' => 'Model1Field0'),
  3258. 'option A',
  3259. '/label'
  3260. );
  3261. $this->assertTags($result, $expected);
  3262. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3263. $expected = array(
  3264. 'fieldset' => array(),
  3265. 'legend' => array(),
  3266. 'Field',
  3267. '/legend',
  3268. 'input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '', 'id' => 'ModelField_'),
  3269. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'ModelField0')),
  3270. array('label' => array('for' => 'ModelField0')),
  3271. 'option A',
  3272. '/label',
  3273. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'ModelField1')),
  3274. array('label' => array('for' => 'ModelField1')),
  3275. 'option B',
  3276. '/label',
  3277. '/fieldset'
  3278. );
  3279. $this->assertTags($result, $expected);
  3280. }
  3281. /**
  3282. * Test radio inputs with between as string or array. Also ensure
  3283. * that an array with less between elements works.
  3284. *
  3285. * @return void
  3286. */
  3287. public function testRadioBetween() {
  3288. $result = $this->Form->radio(
  3289. 'Model.field',
  3290. array('option A', 'option B'),
  3291. array('between' => 'I am between')
  3292. );
  3293. $expected = array(
  3294. 'fieldset' => array(),
  3295. 'legend' => array(),
  3296. 'Field',
  3297. '/legend',
  3298. 'I am between',
  3299. 'input' => array(
  3300. 'type' => 'hidden', 'name' => 'Model[field]',
  3301. 'value' => '', 'id' => 'ModelField_'
  3302. ),
  3303. array('input' => array(
  3304. 'type' => 'radio', 'name' => 'Model[field]',
  3305. 'value' => '0', 'id' => 'ModelField0'
  3306. )),
  3307. array('label' => array('for' => 'ModelField0')),
  3308. 'option A',
  3309. '/label',
  3310. array('input' => array(
  3311. 'type' => 'radio', 'name' => 'Model[field]',
  3312. 'value' => '1', 'id' => 'ModelField1'
  3313. )),
  3314. array('label' => array('for' => 'ModelField1')),
  3315. 'option B',
  3316. '/label',
  3317. '/fieldset'
  3318. );
  3319. $this->assertTags($result, $expected);
  3320. $result = $this->Form->radio(
  3321. 'Model.field',
  3322. array('option A', 'option B', 'option C'),
  3323. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3324. );
  3325. $expected = array(
  3326. 'fieldset' => array(),
  3327. 'legend' => array(),
  3328. 'Field',
  3329. '/legend',
  3330. 'input' => array(
  3331. 'type' => 'hidden', 'name' => 'Model[field]',
  3332. 'value' => '', 'id' => 'ModelField_'
  3333. ),
  3334. array('input' => array(
  3335. 'type' => 'radio', 'name' => 'Model[field]',
  3336. 'value' => '0', 'id' => 'ModelField0'
  3337. )),
  3338. array('label' => array('for' => 'ModelField0')),
  3339. 'option A',
  3340. '/label',
  3341. 'between A',
  3342. '--separator--',
  3343. array('input' => array(
  3344. 'type' => 'radio', 'name' => 'Model[field]',
  3345. 'value' => '1', 'id' => 'ModelField1'
  3346. )),
  3347. array('label' => array('for' => 'ModelField1')),
  3348. 'option B',
  3349. '/label',
  3350. 'between B',
  3351. '--separator--',
  3352. array('input' => array(
  3353. 'type' => 'radio', 'name' => 'Model[field]',
  3354. 'value' => '2', 'id' => 'ModelField2'
  3355. )),
  3356. array('label' => array('for' => 'ModelField2')),
  3357. 'option C',
  3358. '/label',
  3359. 'between C',
  3360. '/fieldset'
  3361. );
  3362. $this->assertTags($result, $expected);
  3363. $result = $this->Form->input('Model.field', array(
  3364. 'options' => array('1' => 'first', '2' => 'second'),
  3365. 'type' => 'radio',
  3366. 'before' => '--before--',
  3367. 'after' => '--after--',
  3368. 'separator' => '--separator--',
  3369. 'between' => array('--between first--', '--between second--')
  3370. ));
  3371. $expected = array(
  3372. 'div' => array('class' => 'input radio'),
  3373. '--before--',
  3374. 'fieldset' => array(),
  3375. 'legend' => array(),
  3376. 'Field',
  3377. '/legend',
  3378. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3379. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3380. array('label' => array('for' => 'ModelField1')),
  3381. 'first',
  3382. '/label',
  3383. '--between first--',
  3384. '--separator--',
  3385. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3386. array('label' => array('for' => 'ModelField2')),
  3387. 'second',
  3388. '/label',
  3389. '--between second--',
  3390. '/fieldset',
  3391. '--after--',
  3392. '/div'
  3393. );
  3394. $this->assertTags($result, $expected);
  3395. $result = $this->Form->input('Model.field', array(
  3396. 'options' => array('1' => 'first', '2' => 'second'),
  3397. 'type' => 'radio',
  3398. 'before' => '--before--',
  3399. 'after' => '--after--',
  3400. 'separator' => '--separator--',
  3401. 'between' => array('--between first--')
  3402. ));
  3403. $expected = array(
  3404. 'div' => array('class' => 'input radio'),
  3405. '--before--',
  3406. 'fieldset' => array(),
  3407. 'legend' => array(),
  3408. 'Field',
  3409. '/legend',
  3410. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3411. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3412. array('label' => array('for' => 'ModelField1')),
  3413. 'first',
  3414. '/label',
  3415. '--between first--',
  3416. '--separator--',
  3417. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2', 'id' => 'ModelField2')),
  3418. array('label' => array('for' => 'ModelField2')),
  3419. 'second',
  3420. '/label',
  3421. '/fieldset',
  3422. '--after--',
  3423. '/div'
  3424. );
  3425. $this->assertTags($result, $expected);
  3426. }
  3427. /**
  3428. * Test that radios with a 0 value are selected under the correct conditions.
  3429. * Also ensure that values that are booleanish are handled correctly.
  3430. *
  3431. * @return void
  3432. */
  3433. public function testRadioOptionWithBooleanishValues() {
  3434. $expected = array(
  3435. 'fieldset' => array(),
  3436. 'legend' => array(),
  3437. 'Field',
  3438. '/legend',
  3439. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3440. array('label' => array('for' => 'ModelField1')),
  3441. 'Yes',
  3442. '/label',
  3443. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3444. array('label' => array('for' => 'ModelField0')),
  3445. 'No',
  3446. '/label',
  3447. '/fieldset'
  3448. );
  3449. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3450. $this->assertTags($result, $expected);
  3451. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3452. $this->assertTags($result, $expected);
  3453. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3454. $this->assertTags($result, $expected);
  3455. $expected = array(
  3456. 'fieldset' => array(),
  3457. 'legend' => array(),
  3458. 'Field',
  3459. '/legend',
  3460. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  3461. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3462. array('label' => array('for' => 'ModelField1')),
  3463. 'Yes',
  3464. '/label',
  3465. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3466. array('label' => array('for' => 'ModelField0')),
  3467. 'No',
  3468. '/label',
  3469. '/fieldset'
  3470. );
  3471. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3472. $this->assertTags($result, $expected);
  3473. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3474. $this->assertTags($result, $expected);
  3475. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3476. $this->assertTags($result, $expected);
  3477. $expected = array(
  3478. 'fieldset' => array(),
  3479. 'legend' => array(),
  3480. 'Field',
  3481. '/legend',
  3482. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3483. array('label' => array('for' => 'ModelField1')),
  3484. 'Yes',
  3485. '/label',
  3486. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0')),
  3487. array('label' => array('for' => 'ModelField0')),
  3488. 'No',
  3489. '/label',
  3490. '/fieldset'
  3491. );
  3492. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3493. $this->assertTags($result, $expected);
  3494. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3495. $this->assertTags($result, $expected);
  3496. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3497. $this->assertTags($result, $expected);
  3498. }
  3499. /**
  3500. * test disabled radio options
  3501. *
  3502. * @return void
  3503. */
  3504. public function testRadioDisabled() {
  3505. $result = $this->Form->radio(
  3506. 'Model.field',
  3507. array('option A', 'option B'),
  3508. array('disabled' => array(0), 'value' => '0')
  3509. );
  3510. $expected = array(
  3511. 'fieldset' => array(),
  3512. 'legend' => array(),
  3513. 'Field',
  3514. '/legend',
  3515. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3516. array('label' => array('for' => 'ModelField0')),
  3517. 'option A',
  3518. '/label',
  3519. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3520. array('label' => array('for' => 'ModelField1')),
  3521. 'option B',
  3522. '/label',
  3523. '/fieldset'
  3524. );
  3525. $this->assertTags($result, $expected);
  3526. $result = $this->Form->radio(
  3527. 'Model.field',
  3528. array('option A', 'option B'),
  3529. array('disabled' => true, 'value' => '0')
  3530. );
  3531. $expected = array(
  3532. 'fieldset' => array(),
  3533. 'legend' => array(),
  3534. 'Field',
  3535. '/legend',
  3536. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3537. array('label' => array('for' => 'ModelField0')),
  3538. 'option A',
  3539. '/label',
  3540. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3541. array('label' => array('for' => 'ModelField1')),
  3542. 'option B',
  3543. '/label',
  3544. '/fieldset'
  3545. );
  3546. $this->assertTags($result, $expected);
  3547. $result = $this->Form->radio(
  3548. 'Model.field',
  3549. array('option A', 'option B'),
  3550. array('disabled' => 'disabled', 'value' => '0')
  3551. );
  3552. $expected = array(
  3553. 'fieldset' => array(),
  3554. 'legend' => array(),
  3555. 'Field',
  3556. '/legend',
  3557. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3558. array('label' => array('for' => 'ModelField0')),
  3559. 'option A',
  3560. '/label',
  3561. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3562. array('label' => array('for' => 'ModelField1')),
  3563. 'option B',
  3564. '/label',
  3565. '/fieldset'
  3566. );
  3567. $this->assertTags($result, $expected);
  3568. $result = $this->Form->input('Model.field', array(
  3569. 'options' => array(1 => 'first', 2 => 'second', '2x' => '2x', '3' => 'third', '3x' => '3x'),
  3570. 'type' => 'radio',
  3571. 'disabled' => array(2, '3x'),
  3572. ));
  3573. $expected = array(
  3574. 'div' => array('class' => 'input radio'),
  3575. 'fieldset' => array(),
  3576. 'legend' => array(),
  3577. 'Field',
  3578. '/legend',
  3579. array('input' => array('type' => 'hidden', 'name' => 'Model[field]', 'id' => 'ModelField_', 'value' => '')),
  3580. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField1')),
  3581. array('label' => array('for' => 'ModelField1')),
  3582. 'first',
  3583. '/label',
  3584. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '2', 'id' => 'ModelField2')),
  3585. array('label' => array('for' => 'ModelField2')),
  3586. 'second',
  3587. '/label',
  3588. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '2x', 'id' => 'ModelField2x')),
  3589. array('label' => array('for' => 'ModelField2x')),
  3590. '2x',
  3591. '/label',
  3592. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '3', 'id' => 'ModelField3')),
  3593. array('label' => array('for' => 'ModelField3')),
  3594. 'third',
  3595. '/label',
  3596. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'disabled' => 'disabled', 'value' => '3x', 'id' => 'ModelField3x')),
  3597. array('label' => array('for' => 'ModelField3x')),
  3598. '3x',
  3599. '/label',
  3600. '/fieldset',
  3601. '/div'
  3602. );
  3603. $this->assertTags($result, $expected);
  3604. }
  3605. /**
  3606. * test disabling the hidden input for radio buttons
  3607. *
  3608. * @return void
  3609. */
  3610. public function testRadioHiddenInputDisabling() {
  3611. $result = $this->Form->input('Model.1.field', array(
  3612. 'type' => 'radio',
  3613. 'options' => array('option A'),
  3614. 'hiddenField' => false
  3615. )
  3616. );
  3617. $expected = array(
  3618. 'div' => array('class' => 'input radio'),
  3619. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3620. 'label' => array('for' => 'Model1Field0'),
  3621. 'option A',
  3622. '/label',
  3623. '/div'
  3624. );
  3625. $this->assertTags($result, $expected);
  3626. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3627. $expected = array(
  3628. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3629. 'label' => array('for' => 'Model1Field0'),
  3630. 'option A',
  3631. '/label'
  3632. );
  3633. $this->assertTags($result, $expected);
  3634. }
  3635. /**
  3636. * test adding an empty option for radio buttons
  3637. *
  3638. * @return void
  3639. */
  3640. public function testRadioAddEmptyOption() {
  3641. $result = $this->Form->input('Model.1.field', array(
  3642. 'type' => 'radio',
  3643. 'options' => array('option A'),
  3644. 'empty' => true,
  3645. 'hiddenField' => false
  3646. ));
  3647. $expected = array(
  3648. 'div' => array('class' => 'input radio'),
  3649. 'fieldset' => array(),
  3650. 'legend' => array(),
  3651. 'Field',
  3652. '/legend',
  3653. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3654. array('label' => array('for' => 'Model1Field')),
  3655. __('empty'),
  3656. '/label',
  3657. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3658. array('label' => array('for' => 'Model1Field0')),
  3659. 'option A',
  3660. '/label',
  3661. '/fieldset',
  3662. '/div'
  3663. );
  3664. $this->assertTags($result, $expected);
  3665. $result = $this->Form->input('Model.1.field', array(
  3666. 'type' => 'radio',
  3667. 'options' => array('option A'),
  3668. 'empty' => 'CustomEmptyLabel',
  3669. 'hiddenField' => false
  3670. ));
  3671. $expected = array(
  3672. 'div' => array('class' => 'input radio'),
  3673. 'fieldset' => array(),
  3674. 'legend' => array(),
  3675. 'Field',
  3676. '/legend',
  3677. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '', 'id' => 'Model1Field')),
  3678. array('label' => array('for' => 'Model1Field')),
  3679. 'CustomEmptyLabel',
  3680. '/label',
  3681. array('input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3682. array('label' => array('for' => 'Model1Field0')),
  3683. 'option A',
  3684. '/label',
  3685. '/fieldset',
  3686. '/div'
  3687. );
  3688. $this->assertTags($result, $expected);
  3689. $result = $this->Form->input('Model.1.field', array(
  3690. 'type' => 'radio',
  3691. 'options' => array('option A'),
  3692. 'empty' => false,
  3693. 'hiddenField' => false
  3694. ));
  3695. $this->assertTextNotContains('"Model1Field"', $result);
  3696. }
  3697. /**
  3698. * Test that radio() accepts an array for label
  3699. *
  3700. * @return void
  3701. */
  3702. public function testRadioLabelArray() {
  3703. $result = $this->Form->input('Model.field', array(
  3704. 'type' => 'radio',
  3705. 'legend' => false,
  3706. 'label' => array(
  3707. 'class' => 'checkbox float-left',
  3708. ),
  3709. 'options' => array('1' => 'Option A', '2' => 'Option B.')
  3710. ));
  3711. $this->assertTextContains(
  3712. '<label for="ModelField1" class="checkbox float-left">Option A</label>',
  3713. $result
  3714. );
  3715. }
  3716. /**
  3717. * Test that label id's match the input element id's when radio is called after create().
  3718. *
  3719. * @return void
  3720. */
  3721. public function testRadioWithCreate() {
  3722. $this->Form->create('Model');
  3723. $result = $this->Form->radio('recipient',
  3724. array('1' => '1', '2' => '2', '3' => '3'),
  3725. array('legend' => false, 'value' => '1')
  3726. );
  3727. $this->assertTextNotContains(
  3728. '<label for="ModelModelRecipient1">1</label>',
  3729. $result
  3730. );
  3731. $this->assertTextContains(
  3732. '<label for="ModelRecipient1">1</label>',
  3733. $result
  3734. );
  3735. }
  3736. /**
  3737. * testSelect method
  3738. *
  3739. * Test select element generation.
  3740. *
  3741. * @return void
  3742. */
  3743. public function testSelect() {
  3744. $result = $this->Form->select('Model.field', array());
  3745. $expected = array(
  3746. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3747. array('option' => array('value' => '')),
  3748. '/option',
  3749. '/select'
  3750. );
  3751. $this->assertTags($result, $expected);
  3752. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3753. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3754. $expected = array(
  3755. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3756. array('option' => array('value' => '')),
  3757. '/option',
  3758. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3759. 'good',
  3760. '/option',
  3761. array('option' => array('value' => 'other')),
  3762. 'bad',
  3763. '/option',
  3764. '/select'
  3765. );
  3766. $this->assertTags($result, $expected);
  3767. $this->Form->request->data = array();
  3768. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3769. $expected = array(
  3770. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3771. array('option' => array('value' => '')),
  3772. '/option',
  3773. array('option' => array('value' => 'value')),
  3774. 'good',
  3775. '/option',
  3776. array('option' => array('value' => 'other')),
  3777. 'bad',
  3778. '/option',
  3779. '/select'
  3780. );
  3781. $this->assertTags($result, $expected);
  3782. $result = $this->Form->select(
  3783. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3784. array('empty' => false)
  3785. );
  3786. $expected = array(
  3787. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3788. array('option' => array('value' => 'first')),
  3789. 'first &quot;html&quot; &lt;chars&gt;',
  3790. '/option',
  3791. array('option' => array('value' => 'second')),
  3792. 'value',
  3793. '/option',
  3794. '/select'
  3795. );
  3796. $this->assertTags($result, $expected);
  3797. $result = $this->Form->select(
  3798. 'Model.field',
  3799. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3800. array('escape' => false, 'empty' => false)
  3801. );
  3802. $expected = array(
  3803. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3804. array('option' => array('value' => 'first')),
  3805. 'first "html" <chars>',
  3806. '/option',
  3807. array('option' => array('value' => 'second')),
  3808. 'value',
  3809. '/option',
  3810. '/select'
  3811. );
  3812. $this->assertTags($result, $expected);
  3813. $options = array(
  3814. array('value' => 'first', 'name' => 'First'),
  3815. array('value' => 'first', 'name' => 'Another First'),
  3816. );
  3817. $result = $this->Form->select(
  3818. 'Model.field',
  3819. $options,
  3820. array('escape' => false, 'empty' => false)
  3821. );
  3822. $expected = array(
  3823. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3824. array('option' => array('value' => 'first')),
  3825. 'First',
  3826. '/option',
  3827. array('option' => array('value' => 'first')),
  3828. 'Another First',
  3829. '/option',
  3830. '/select'
  3831. );
  3832. $this->assertTags($result, $expected);
  3833. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3834. $result = $this->Form->select(
  3835. 'Model.contact_id',
  3836. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3837. array('escape' => false, 'empty' => 'pick something')
  3838. );
  3839. $expected = array(
  3840. 'select' => array('name' => 'Model[contact_id]', 'id' => 'ModelContactId'),
  3841. array('option' => array('value' => '')), 'pick something', '/option',
  3842. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3843. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3844. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3845. '/select'
  3846. );
  3847. $this->assertTags($result, $expected);
  3848. $this->Form->request->data['Model']['field'] = 0;
  3849. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3850. $expected = array(
  3851. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3852. array('option' => array('value' => '')), '/option',
  3853. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3854. array('option' => array('value' => '1')), 'Yes', '/option',
  3855. '/select'
  3856. );
  3857. $this->assertTags($result, $expected);
  3858. $this->Form->request->data['Model']['field'] = 50;
  3859. $result = $this->Form->select('Model.field', array('50f5c0cf' => 'Stringy', '50' => 'fifty'));
  3860. $expected = array(
  3861. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3862. array('option' => array('value' => '')), '/option',
  3863. array('option' => array('value' => '50f5c0cf')), 'Stringy', '/option',
  3864. array('option' => array('value' => '50', 'selected' => 'selected')), 'fifty', '/option',
  3865. '/select'
  3866. );
  3867. $this->assertTags($result, $expected);
  3868. $result = $this->Form->select('Contact.required_one', array('option A'));
  3869. $expected = array(
  3870. 'select' => array(
  3871. 'name' => 'data[Contact][required_one]',
  3872. 'id' => 'ContactRequiredOne',
  3873. 'required' => 'required'
  3874. ),
  3875. array('option' => array('value' => '')), '/option',
  3876. array('option' => array('value' => '0')), 'option A', '/option',
  3877. '/select'
  3878. );
  3879. $this->assertTags($result, $expected);
  3880. $result = $this->Form->select('Contact.required_one', array('option A'), array('disabled' => true));
  3881. $expected = array(
  3882. 'select' => array(
  3883. 'name' => 'data[Contact][required_one]',
  3884. 'id' => 'ContactRequiredOne',
  3885. 'disabled' => 'disabled'
  3886. ),
  3887. array('option' => array('value' => '')), '/option',
  3888. array('option' => array('value' => '0')), 'option A', '/option',
  3889. '/select'
  3890. );
  3891. $this->assertTags($result, $expected);
  3892. }
  3893. /**
  3894. * test that select() with optiongroups listens to the escape param.
  3895. *
  3896. * @return void
  3897. */
  3898. public function testSelectOptionGroupEscaping() {
  3899. $options = array(
  3900. '>< Key' => array(
  3901. 1 => 'One',
  3902. 2 => 'Two'
  3903. )
  3904. );
  3905. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3906. $expected = array(
  3907. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3908. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3909. array('option' => array('value' => '1')), 'One', '/option',
  3910. array('option' => array('value' => '2')), 'Two', '/option',
  3911. '/optgroup',
  3912. '/select'
  3913. );
  3914. $this->assertTags($result, $expected);
  3915. $options = array(
  3916. '>< Key' => array(
  3917. 1 => 'One',
  3918. 2 => 'Two'
  3919. )
  3920. );
  3921. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3922. $expected = array(
  3923. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3924. 'optgroup' => array('label' => '>< Key'),
  3925. array('option' => array('value' => '1')), 'One', '/option',
  3926. array('option' => array('value' => '2')), 'Two', '/option',
  3927. '/optgroup',
  3928. '/select'
  3929. );
  3930. $this->assertTags($result, $expected);
  3931. }
  3932. /**
  3933. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3934. *
  3935. * @return void
  3936. */
  3937. public function testSelectWithNullAttributes() {
  3938. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3939. $expected = array(
  3940. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3941. array('option' => array('value' => '0')),
  3942. 'first',
  3943. '/option',
  3944. array('option' => array('value' => '1')),
  3945. 'second',
  3946. '/option',
  3947. '/select'
  3948. );
  3949. $this->assertTags($result, $expected);
  3950. }
  3951. /**
  3952. * testNestedSelect method
  3953. *
  3954. * test select element generation with optgroups
  3955. *
  3956. * @return void
  3957. */
  3958. public function testNestedSelect() {
  3959. $result = $this->Form->select(
  3960. 'Model.field',
  3961. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3962. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3963. )), array('empty' => false)
  3964. );
  3965. $expected = array(
  3966. 'select' => array('name' => 'Model[field]',
  3967. 'id' => 'ModelField'),
  3968. array('option' => array('value' => 1)),
  3969. 'One',
  3970. '/option',
  3971. array('option' => array('value' => 2)),
  3972. 'Two',
  3973. '/option',
  3974. array('optgroup' => array('label' => 'Three')),
  3975. array('option' => array('value' => 4)),
  3976. 'Four',
  3977. '/option',
  3978. array('option' => array('value' => 5)),
  3979. 'Five',
  3980. '/option',
  3981. '/optgroup',
  3982. '/select'
  3983. );
  3984. $this->assertTags($result, $expected);
  3985. $result = $this->Form->select(
  3986. 'Model.field',
  3987. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3988. array('showParents' => true, 'empty' => false)
  3989. );
  3990. $expected = array(
  3991. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  3992. array('option' => array('value' => 1)),
  3993. 'One',
  3994. '/option',
  3995. array('option' => array('value' => 2)),
  3996. 'Two',
  3997. '/option',
  3998. array('optgroup' => array('label' => 'Three')),
  3999. array('option' => array('value' => 3)),
  4000. 'Three',
  4001. '/option',
  4002. array('option' => array('value' => 4)),
  4003. 'Four',
  4004. '/option',
  4005. '/optgroup',
  4006. '/select'
  4007. );
  4008. $this->assertTags($result, $expected);
  4009. }
  4010. /**
  4011. * testSelectMultiple method
  4012. *
  4013. * test generation of multiple select elements
  4014. *
  4015. * @return void
  4016. */
  4017. public function testSelectMultiple() {
  4018. $options = array('first', 'second', 'third');
  4019. $result = $this->Form->select(
  4020. 'Model.multi_field', $options, array('multiple' => true)
  4021. );
  4022. $expected = array(
  4023. 'input' => array(
  4024. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4025. ),
  4026. 'select' => array(
  4027. 'name' => 'Model[multi_field][]',
  4028. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4029. ),
  4030. array('option' => array('value' => '0')),
  4031. 'first',
  4032. '/option',
  4033. array('option' => array('value' => '1')),
  4034. 'second',
  4035. '/option',
  4036. array('option' => array('value' => '2')),
  4037. 'third',
  4038. '/option',
  4039. '/select'
  4040. );
  4041. $this->assertTags($result, $expected);
  4042. $result = $this->Form->select(
  4043. 'Model.multi_field', $options, array('multiple' => 'multiple')
  4044. );
  4045. $expected = array(
  4046. 'input' => array(
  4047. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4048. ),
  4049. 'select' => array(
  4050. 'name' => 'Model[multi_field][]',
  4051. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  4052. ),
  4053. array('option' => array('value' => '0')),
  4054. 'first',
  4055. '/option',
  4056. array('option' => array('value' => '1')),
  4057. 'second',
  4058. '/option',
  4059. array('option' => array('value' => '2')),
  4060. 'third',
  4061. '/option',
  4062. '/select'
  4063. );
  4064. $this->assertTags($result, $expected);
  4065. $result = $this->Form->select(
  4066. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  4067. );
  4068. $expected = array(
  4069. 'input' => array(
  4070. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4071. ),
  4072. 'select' => array(
  4073. 'name' => 'Model[multi_field][]', 'id' => 'ModelMultiField',
  4074. 'multiple' => 'multiple'
  4075. ),
  4076. array('option' => array('value' => '0', 'selected' => 'selected')),
  4077. 'first',
  4078. '/option',
  4079. array('option' => array('value' => '1', 'selected' => 'selected')),
  4080. 'second',
  4081. '/option',
  4082. array('option' => array('value' => '2')),
  4083. 'third',
  4084. '/option',
  4085. '/select'
  4086. );
  4087. $this->assertTags($result, $expected);
  4088. $result = $this->Form->select(
  4089. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  4090. );
  4091. $expected = array(
  4092. 'select' => array(
  4093. 'name' => 'Model[multi_field]', 'id' => 'ModelMultiField'
  4094. ),
  4095. array('option' => array('value' => '0', 'selected' => 'selected')),
  4096. 'first',
  4097. '/option',
  4098. array('option' => array('value' => '1', 'selected' => 'selected')),
  4099. 'second',
  4100. '/option',
  4101. array('option' => array('value' => '2')),
  4102. 'third',
  4103. '/option',
  4104. '/select'
  4105. );
  4106. $this->assertTags($result, $expected);
  4107. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4108. $selected = array('2', '3x');
  4109. $result = $this->Form->select(
  4110. 'Model.multi_field', $options, array('multiple' => true, 'value' => $selected)
  4111. );
  4112. $expected = array(
  4113. 'input' => array(
  4114. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  4115. ),
  4116. 'select' => array(
  4117. 'name' => 'Model[multi_field][]', 'multiple' => 'multiple', 'id' => 'ModelMultiField'
  4118. ),
  4119. array('option' => array('value' => '1')),
  4120. 'One',
  4121. '/option',
  4122. array('option' => array('value' => '2', 'selected' => 'selected')),
  4123. 'Two',
  4124. '/option',
  4125. array('option' => array('value' => '3')),
  4126. 'Three',
  4127. '/option',
  4128. array('option' => array('value' => '3x', 'selected' => 'selected')),
  4129. 'Stringy',
  4130. '/option',
  4131. '/select'
  4132. );
  4133. $this->assertTags($result, $expected);
  4134. $result = $this->Form->select('Contact.required_one', array(
  4135. '1' => 'option A',
  4136. '2' => 'option B'
  4137. ), array('multiple' => true));
  4138. $expected = array(
  4139. 'input' => array(
  4140. 'type' => 'hidden', 'name' => 'data[Contact][required_one]', 'value' => '', 'id' => 'ContactRequiredOne_'
  4141. ),
  4142. 'select' => array(
  4143. 'name' => 'data[Contact][required_one][]',
  4144. 'id' => 'ContactRequiredOne',
  4145. 'required' => 'required',
  4146. 'multiple' => 'multiple'
  4147. ),
  4148. array('option' => array('value' => '1')), 'option A', '/option',
  4149. array('option' => array('value' => '2')), 'option B', '/option',
  4150. '/select'
  4151. );
  4152. $this->assertTags($result, $expected);
  4153. }
  4154. /**
  4155. * Test generating multiple select with disabled elements.
  4156. *
  4157. * @return void
  4158. */
  4159. public function testSelectMultipleWithDisabledElements() {
  4160. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4161. $disabled = array(2, 3);
  4162. $result = $this->Form->select('Contact.multiple', $options, array('multiple' => 'multiple', 'disabled' => $disabled));
  4163. $expected = array(
  4164. 'input' => array(
  4165. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4166. ),
  4167. 'select' => array(
  4168. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4169. ),
  4170. array('option' => array('value' => '1')),
  4171. 'One',
  4172. '/option',
  4173. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4174. 'Two',
  4175. '/option',
  4176. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4177. 'Three',
  4178. '/option',
  4179. array('option' => array('value' => '3x')),
  4180. 'Stringy',
  4181. '/option',
  4182. '/select'
  4183. );
  4184. $this->assertTags($result, $expected);
  4185. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4186. $disabled = array('2', '3x');
  4187. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4188. $expected = array(
  4189. array('div' => array('class' => 'input select')),
  4190. array('label' => array('for' => 'ContactMultiple')),
  4191. 'Multiple',
  4192. '/label',
  4193. 'input' => array(
  4194. 'type' => 'hidden', 'name' => 'Contact[multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4195. ),
  4196. 'select' => array(
  4197. 'name' => 'Contact[multiple][]', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4198. ),
  4199. array('option' => array('value' => '1')),
  4200. 'One',
  4201. '/option',
  4202. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4203. 'Two',
  4204. '/option',
  4205. array('option' => array('value' => '3')),
  4206. 'Three',
  4207. '/option',
  4208. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4209. 'Stringy',
  4210. '/option',
  4211. '/select',
  4212. '/div'
  4213. );
  4214. $this->assertTags($result, $expected);
  4215. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4216. $disabled = true;
  4217. $result = $this->Form->input('Contact.multiple', array('multiple' => 'multiple', 'disabled' => $disabled, 'options' => $options));
  4218. $expected = array(
  4219. array('div' => array('class' => 'input select')),
  4220. array('label' => array('for' => 'ContactMultiple')),
  4221. 'Multiple',
  4222. '/label',
  4223. 'input' => array(
  4224. 'type' => 'hidden', 'name' => 'data[Contact][multiple]', 'value' => '', 'id' => 'ContactMultiple_'
  4225. ),
  4226. 'select' => array(
  4227. 'name' => 'data[Contact][multiple][]', 'disabled' => 'disabled', 'multiple' => 'multiple', 'id' => 'ContactMultiple'
  4228. ),
  4229. array('option' => array('value' => '1')),
  4230. 'One',
  4231. '/option',
  4232. array('option' => array('value' => '2')),
  4233. 'Two',
  4234. '/option',
  4235. array('option' => array('value' => '3')),
  4236. 'Three',
  4237. '/option',
  4238. array('option' => array('value' => '3x')),
  4239. 'Stringy',
  4240. '/option',
  4241. '/select',
  4242. '/div'
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. }
  4246. /**
  4247. * Test generating select with disabled elements.
  4248. *
  4249. * @return void
  4250. */
  4251. public function testSelectWithDisabledElements() {
  4252. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4253. $disabled = array(2, 3);
  4254. $result = $this->Form->select('Model.field', $options, array('disabled' => $disabled));
  4255. $expected = array(
  4256. 'select' => array(
  4257. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4258. ),
  4259. array('option' => array('value' => '')),
  4260. '/option',
  4261. array('option' => array('value' => '1')),
  4262. 'One',
  4263. '/option',
  4264. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4265. 'Two',
  4266. '/option',
  4267. array('option' => array('value' => '3', 'disabled' => 'disabled')),
  4268. 'Three',
  4269. '/option',
  4270. array('option' => array('value' => '3x')),
  4271. 'Stringy',
  4272. '/option',
  4273. '/select'
  4274. );
  4275. $this->assertTags($result, $expected);
  4276. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4277. $disabled = array('2', '3x');
  4278. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4279. $expected = array(
  4280. array('div' => array('class' => 'input select')),
  4281. array('label' => array('for' => 'ModelField')),
  4282. 'Field',
  4283. '/label',
  4284. 'select' => array(
  4285. 'name' => 'data[Model][field]', 'id' => 'ModelField'
  4286. ),
  4287. array('option' => array('value' => '1')),
  4288. 'One',
  4289. '/option',
  4290. array('option' => array('value' => '2', 'disabled' => 'disabled')),
  4291. 'Two',
  4292. '/option',
  4293. array('option' => array('value' => '3')),
  4294. 'Three',
  4295. '/option',
  4296. array('option' => array('value' => '3x', 'disabled' => 'disabled')),
  4297. 'Stringy',
  4298. '/option',
  4299. '/select',
  4300. '/div'
  4301. );
  4302. $this->assertTags($result, $expected);
  4303. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three', '3x' => 'Stringy');
  4304. $disabled = true;
  4305. $result = $this->Form->input('Model.field', array('disabled' => $disabled, 'options' => $options));
  4306. $expected = array(
  4307. array('div' => array('class' => 'input select')),
  4308. array('label' => array('for' => 'ModelField')),
  4309. 'Field',
  4310. '/label',
  4311. 'select' => array(
  4312. 'name' => 'data[Model][field]', 'id' => 'ModelField', 'disabled' => 'disabled'
  4313. ),
  4314. array('option' => array('value' => '1')),
  4315. 'One',
  4316. '/option',
  4317. array('option' => array('value' => '2')),
  4318. 'Two',
  4319. '/option',
  4320. array('option' => array('value' => '3')),
  4321. 'Three',
  4322. '/option',
  4323. array('option' => array('value' => '3x')),
  4324. 'Stringy',
  4325. '/option',
  4326. '/select',
  4327. '/div'
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. }
  4331. /**
  4332. * test generation of habtm select boxes.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testHabtmSelectBox() {
  4337. $this->View->viewVars['contactTags'] = array(
  4338. 1 => 'blue',
  4339. 2 => 'red',
  4340. 3 => 'green'
  4341. );
  4342. $this->Form->request->data = array(
  4343. 'Contact' => array(),
  4344. 'ContactTag' => array(
  4345. array(
  4346. 'id' => '1',
  4347. 'name' => 'blue'
  4348. ),
  4349. array(
  4350. 'id' => 3,
  4351. 'name' => 'green'
  4352. )
  4353. )
  4354. );
  4355. $this->Form->create('Contact');
  4356. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4357. $expected = array(
  4358. 'input' => array(
  4359. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4360. ),
  4361. 'select' => array(
  4362. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4363. 'multiple' => 'multiple'
  4364. ),
  4365. array('option' => array('value' => '1', 'selected' => 'selected')),
  4366. 'blue',
  4367. '/option',
  4368. array('option' => array('value' => '2')),
  4369. 'red',
  4370. '/option',
  4371. array('option' => array('value' => '3', 'selected' => 'selected')),
  4372. 'green',
  4373. '/option',
  4374. '/select'
  4375. );
  4376. $this->assertTags($result, $expected);
  4377. // make sure only 50 is selected, and not 50f5c0cf
  4378. $this->View->viewVars['contactTags'] = array(
  4379. '1' => 'blue',
  4380. '50f5c0cf' => 'red',
  4381. '50' => 'green'
  4382. );
  4383. $this->Form->request->data = array(
  4384. 'Contact' => array(),
  4385. 'ContactTag' => array(
  4386. array(
  4387. 'id' => 1,
  4388. 'name' => 'blue'
  4389. ),
  4390. array(
  4391. 'id' => 50,
  4392. 'name' => 'green'
  4393. )
  4394. )
  4395. );
  4396. $this->Form->create('Contact');
  4397. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4398. $expected = array(
  4399. 'input' => array(
  4400. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4401. ),
  4402. 'select' => array(
  4403. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4404. 'multiple' => 'multiple'
  4405. ),
  4406. array('option' => array('value' => '1', 'selected' => 'selected')),
  4407. 'blue',
  4408. '/option',
  4409. array('option' => array('value' => '50f5c0cf')),
  4410. 'red',
  4411. '/option',
  4412. array('option' => array('value' => '50', 'selected' => 'selected')),
  4413. 'green',
  4414. '/option',
  4415. '/select'
  4416. );
  4417. $this->assertTags($result, $expected);
  4418. }
  4419. /**
  4420. * test generation of multi select elements in checkbox format
  4421. *
  4422. * @return void
  4423. */
  4424. public function testSelectMultipleCheckboxes() {
  4425. $result = $this->Form->select(
  4426. 'Model.multi_field',
  4427. array('first', 'second', 'third'),
  4428. array('multiple' => 'checkbox')
  4429. );
  4430. $expected = array(
  4431. 'input' => array(
  4432. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4433. ),
  4434. array('div' => array('class' => 'checkbox')),
  4435. array('input' => array(
  4436. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4437. 'value' => '0', 'id' => 'ModelMultiField0'
  4438. )),
  4439. array('label' => array('for' => 'ModelMultiField0')),
  4440. 'first',
  4441. '/label',
  4442. '/div',
  4443. array('div' => array('class' => 'checkbox')),
  4444. array('input' => array(
  4445. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4446. 'value' => '1', 'id' => 'ModelMultiField1'
  4447. )),
  4448. array('label' => array('for' => 'ModelMultiField1')),
  4449. 'second',
  4450. '/label',
  4451. '/div',
  4452. array('div' => array('class' => 'checkbox')),
  4453. array('input' => array(
  4454. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4455. 'value' => '2', 'id' => 'ModelMultiField2'
  4456. )),
  4457. array('label' => array('for' => 'ModelMultiField2')),
  4458. 'third',
  4459. '/label',
  4460. '/div'
  4461. );
  4462. $this->assertTags($result, $expected);
  4463. $result = $this->Form->select(
  4464. 'Model.multi_field',
  4465. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4466. array('multiple' => 'checkbox')
  4467. );
  4468. $expected = array(
  4469. 'input' => array(
  4470. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4471. ),
  4472. array('div' => array('class' => 'checkbox')),
  4473. array('input' => array(
  4474. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4475. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4476. )),
  4477. array('label' => array('for' => 'ModelMultiFieldA')),
  4478. 'first',
  4479. '/label',
  4480. '/div',
  4481. array('div' => array('class' => 'checkbox')),
  4482. array('input' => array(
  4483. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4484. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4485. )),
  4486. array('label' => array('for' => 'ModelMultiFieldB')),
  4487. 'second',
  4488. '/label',
  4489. '/div',
  4490. array('div' => array('class' => 'checkbox')),
  4491. array('input' => array(
  4492. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4493. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4494. )),
  4495. array('label' => array('for' => 'ModelMultiFieldC')),
  4496. 'third',
  4497. '/label',
  4498. '/div'
  4499. );
  4500. $this->assertTags($result, $expected);
  4501. $result = $this->Form->select(
  4502. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4503. );
  4504. $expected = array(
  4505. 'input' => array(
  4506. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4507. ),
  4508. array('div' => array('class' => 'checkbox')),
  4509. array('input' => array(
  4510. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4511. 'value' => '1', 'id' => 'ModelMultiField1'
  4512. )),
  4513. array('label' => array('for' => 'ModelMultiField1')),
  4514. 'first',
  4515. '/label',
  4516. '/div'
  4517. );
  4518. $this->assertTags($result, $expected);
  4519. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4520. $result = $this->Form->select(
  4521. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4522. );
  4523. $expected = array(
  4524. 'input' => array(
  4525. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4526. ),
  4527. array('div' => array('class' => 'checkbox')),
  4528. array('input' => array(
  4529. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4530. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4531. )),
  4532. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4533. 'first',
  4534. '/label',
  4535. '/div',
  4536. array('div' => array('class' => 'checkbox')),
  4537. array('input' => array(
  4538. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4539. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4540. )),
  4541. array('label' => array('for' => 'ModelTagsArray')),
  4542. 'Array',
  4543. '/label',
  4544. '/div'
  4545. );
  4546. $this->assertTags($result, $expected);
  4547. }
  4548. /**
  4549. * test multiple checkboxes with div styles.
  4550. *
  4551. * @return void
  4552. */
  4553. public function testSelectMultipleCheckboxDiv() {
  4554. $result = $this->Form->select(
  4555. 'Model.tags',
  4556. array('first', 'second'),
  4557. array('multiple' => 'checkbox', 'class' => 'my-class')
  4558. );
  4559. $expected = array(
  4560. 'input' => array(
  4561. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4562. ),
  4563. array('div' => array('class' => 'my-class')),
  4564. array('input' => array(
  4565. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4566. 'value' => '0', 'id' => 'ModelTags0'
  4567. )),
  4568. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4569. '/div',
  4570. array('div' => array('class' => 'my-class')),
  4571. array('input' => array(
  4572. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4573. 'value' => '1', 'id' => 'ModelTags1'
  4574. )),
  4575. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4576. '/div'
  4577. );
  4578. $this->assertTags($result, $expected);
  4579. $result = $this->Form->input('Model.tags', array(
  4580. 'options' => array('first', 'second'),
  4581. 'multiple' => 'checkbox',
  4582. 'class' => 'my-class',
  4583. 'div' => false,
  4584. 'label' => false
  4585. ));
  4586. $this->assertTags($result, $expected);
  4587. $Contact = ClassRegistry::getObject('Contact');
  4588. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4589. $result = $this->Form->input('Contact.tags', array(
  4590. 'options' => array('one'),
  4591. 'multiple' => 'checkbox',
  4592. 'label' => false,
  4593. 'div' => false
  4594. ));
  4595. $expected = array(
  4596. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4597. array('div' => array('class' => 'checkbox form-error')),
  4598. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4599. array('label' => array('for' => 'ContactTags0')),
  4600. 'one',
  4601. '/label',
  4602. '/div'
  4603. );
  4604. $this->assertTags($result, $expected);
  4605. $result = $this->Form->input('Contact.tags', array(
  4606. 'options' => array('one'),
  4607. 'multiple' => 'checkbox',
  4608. 'class' => 'mycheckbox',
  4609. 'label' => false,
  4610. 'div' => false
  4611. ));
  4612. $expected = array(
  4613. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4614. array('div' => array('class' => 'mycheckbox form-error')),
  4615. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4616. array('label' => array('for' => 'ContactTags0')),
  4617. 'one',
  4618. '/label',
  4619. '/div'
  4620. );
  4621. $this->assertTags($result, $expected);
  4622. }
  4623. /**
  4624. * Checks the security hash array generated for multiple-input checkbox elements
  4625. *
  4626. * @return void
  4627. */
  4628. public function testSelectMultipleCheckboxSecurity() {
  4629. $this->Form->request['_Token'] = array('key' => 'testKey');
  4630. $this->assertEquals(array(), $this->Form->fields);
  4631. $result = $this->Form->select(
  4632. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4633. array('multiple' => 'checkbox')
  4634. );
  4635. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4636. $result = $this->Form->secure($this->Form->fields);
  4637. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4638. $this->assertRegExp('/"' . $key . '"/', $result);
  4639. }
  4640. /**
  4641. * Multiple select elements should always be secured as they always participate
  4642. * in the POST data.
  4643. *
  4644. * @return void
  4645. */
  4646. public function testSelectMultipleSecureWithNoOptions() {
  4647. $this->Form->request['_Token'] = array('key' => 'testkey');
  4648. $this->assertEquals(array(), $this->Form->fields);
  4649. $this->Form->select(
  4650. 'Model.select',
  4651. array(),
  4652. array('multiple' => true)
  4653. );
  4654. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4655. }
  4656. /**
  4657. * When a select box has no options it should not be added to the fields list
  4658. * as it always fail post validation.
  4659. *
  4660. * @return void
  4661. */
  4662. public function testSelectNoSecureWithNoOptions() {
  4663. $this->Form->request['_Token'] = array('key' => 'testkey');
  4664. $this->assertEquals(array(), $this->Form->fields);
  4665. $this->Form->select(
  4666. 'Model.select',
  4667. array()
  4668. );
  4669. $this->assertEquals(array(), $this->Form->fields);
  4670. $this->Form->select(
  4671. 'Model.select',
  4672. array(),
  4673. array('empty' => true)
  4674. );
  4675. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4676. }
  4677. /**
  4678. * testInputMultipleCheckboxes method
  4679. *
  4680. * test input() resulting in multi select elements being generated.
  4681. *
  4682. * @return void
  4683. */
  4684. public function testInputMultipleCheckboxes() {
  4685. $result = $this->Form->input('Model.multi_field', array(
  4686. 'options' => array('first', 'second', 'third'),
  4687. 'multiple' => 'checkbox'
  4688. ));
  4689. $expected = array(
  4690. array('div' => array('class' => 'input select')),
  4691. array('label' => array('for' => 'ModelMultiField')),
  4692. 'Multi Field',
  4693. '/label',
  4694. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4695. array('div' => array('class' => 'checkbox')),
  4696. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4697. array('label' => array('for' => 'ModelMultiField0')),
  4698. 'first',
  4699. '/label',
  4700. '/div',
  4701. array('div' => array('class' => 'checkbox')),
  4702. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4703. array('label' => array('for' => 'ModelMultiField1')),
  4704. 'second',
  4705. '/label',
  4706. '/div',
  4707. array('div' => array('class' => 'checkbox')),
  4708. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4709. array('label' => array('for' => 'ModelMultiField2')),
  4710. 'third',
  4711. '/label',
  4712. '/div',
  4713. '/div'
  4714. );
  4715. $this->assertTags($result, $expected);
  4716. $result = $this->Form->input('Model.multi_field', array(
  4717. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4718. 'multiple' => 'checkbox'
  4719. ));
  4720. $expected = array(
  4721. array('div' => array('class' => 'input select')),
  4722. array('label' => array('for' => 'ModelMultiField')),
  4723. 'Multi Field',
  4724. '/label',
  4725. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4726. array('div' => array('class' => 'checkbox')),
  4727. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4728. array('label' => array('for' => 'ModelMultiFieldA')),
  4729. 'first',
  4730. '/label',
  4731. '/div',
  4732. array('div' => array('class' => 'checkbox')),
  4733. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4734. array('label' => array('for' => 'ModelMultiFieldB')),
  4735. 'second',
  4736. '/label',
  4737. '/div',
  4738. array('div' => array('class' => 'checkbox')),
  4739. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4740. array('label' => array('for' => 'ModelMultiFieldC')),
  4741. 'third',
  4742. '/label',
  4743. '/div',
  4744. '/div'
  4745. );
  4746. $this->assertTags($result, $expected);
  4747. $result = $this->Form->input('Model.multi_field', array(
  4748. 'options' => array('1' => 'first'),
  4749. 'multiple' => 'checkbox',
  4750. 'label' => false,
  4751. 'div' => false
  4752. ));
  4753. $expected = array(
  4754. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4755. array('div' => array('class' => 'checkbox')),
  4756. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4757. array('label' => array('for' => 'ModelMultiField1')),
  4758. 'first',
  4759. '/label',
  4760. '/div'
  4761. );
  4762. $this->assertTags($result, $expected);
  4763. $result = $this->Form->input('Model.multi_field', array(
  4764. 'options' => array('2' => 'second'),
  4765. 'multiple' => 'checkbox',
  4766. 'label' => false,
  4767. 'div' => false
  4768. ));
  4769. $expected = array(
  4770. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4771. array('div' => array('class' => 'checkbox')),
  4772. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4773. array('label' => array('for' => 'ModelMultiField2')),
  4774. 'second',
  4775. '/label',
  4776. '/div'
  4777. );
  4778. $this->assertTags($result, $expected);
  4779. }
  4780. /**
  4781. * testSelectHiddenFieldOmission method
  4782. *
  4783. * test that select() with 'hiddenField' => false omits the hidden field
  4784. *
  4785. * @return void
  4786. */
  4787. public function testSelectHiddenFieldOmission() {
  4788. $result = $this->Form->select('Model.multi_field',
  4789. array('first', 'second'),
  4790. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4791. );
  4792. $expected = array(
  4793. array('div' => array('class' => 'checkbox')),
  4794. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4795. array('label' => array('for' => 'ModelMultiField0')),
  4796. 'first',
  4797. '/label',
  4798. '/div',
  4799. array('div' => array('class' => 'checkbox')),
  4800. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4801. array('label' => array('for' => 'ModelMultiField1')),
  4802. 'second',
  4803. '/label',
  4804. '/div'
  4805. );
  4806. $this->assertTags($result, $expected);
  4807. $result = $this->Form->input('Model.multi_field', array(
  4808. 'options' => array('first', 'second'),
  4809. 'multiple' => 'checkbox',
  4810. 'hiddenField' => false
  4811. ));
  4812. $expected = array(
  4813. array('div' => array('class' => 'input select')),
  4814. array('label' => array('for' => 'ModelMultiField')),
  4815. 'Multi Field',
  4816. '/label',
  4817. array('div' => array('class' => 'checkbox')),
  4818. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4819. array('label' => array('for' => 'ModelMultiField0')),
  4820. 'first',
  4821. '/label',
  4822. '/div',
  4823. array('div' => array('class' => 'checkbox')),
  4824. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4825. array('label' => array('for' => 'ModelMultiField1')),
  4826. 'second',
  4827. '/label',
  4828. '/div',
  4829. '/div'
  4830. );
  4831. $this->assertTags($result, $expected);
  4832. }
  4833. /**
  4834. * test that select() with multiple = checkbox works with overriding name attribute.
  4835. *
  4836. * @return void
  4837. */
  4838. public function testSelectCheckboxMultipleOverrideName() {
  4839. $result = $this->Form->input('category', array(
  4840. 'type' => 'select',
  4841. 'multiple' => 'checkbox',
  4842. 'name' => 'fish',
  4843. 'options' => array('1', '2'),
  4844. 'div' => false,
  4845. 'label' => false,
  4846. ));
  4847. $expected = array(
  4848. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'),
  4849. array('div' => array('class' => 'checkbox')),
  4850. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'Category0')),
  4851. array('label' => array('for' => 'Category0')), '1', '/label',
  4852. '/div',
  4853. array('div' => array('class' => 'checkbox')),
  4854. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'Category1')),
  4855. array('label' => array('for' => 'Category1')), '2', '/label',
  4856. '/div'
  4857. );
  4858. $this->assertTags($result, $expected);
  4859. }
  4860. /**
  4861. * Test that 'id' overrides all the checkbox id's as well.
  4862. *
  4863. * @return void
  4864. */
  4865. public function testSelectCheckboxMultipleId() {
  4866. $result = $this->Form->select(
  4867. 'Model.multi_field',
  4868. array('first', 'second', 'third'),
  4869. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4870. );
  4871. $expected = array(
  4872. 'input' => array(
  4873. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'CustomId'
  4874. ),
  4875. array('div' => array('class' => 'checkbox')),
  4876. array('input' => array(
  4877. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4878. 'value' => '0', 'id' => 'CustomId0'
  4879. )),
  4880. array('label' => array('for' => 'CustomId0')),
  4881. 'first',
  4882. '/label',
  4883. '/div',
  4884. array('div' => array('class' => 'checkbox')),
  4885. array('input' => array(
  4886. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4887. 'value' => '1', 'id' => 'CustomId1'
  4888. )),
  4889. array('label' => array('for' => 'CustomId1')),
  4890. 'second',
  4891. '/label',
  4892. '/div',
  4893. array('div' => array('class' => 'checkbox')),
  4894. array('input' => array(
  4895. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4896. 'value' => '2', 'id' => 'CustomId2'
  4897. )),
  4898. array('label' => array('for' => 'CustomId2')),
  4899. 'third',
  4900. '/label',
  4901. '/div'
  4902. );
  4903. $this->assertTags($result, $expected);
  4904. }
  4905. /**
  4906. * testCheckbox method
  4907. *
  4908. * Test generation of checkboxes
  4909. *
  4910. * @return void
  4911. */
  4912. public function testCheckbox() {
  4913. $result = $this->Form->checkbox('Model.field');
  4914. $expected = array(
  4915. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  4916. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  4917. );
  4918. $this->assertTags($result, $expected);
  4919. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4920. $expected = array(
  4921. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'theID_'),
  4922. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4923. );
  4924. $this->assertTags($result, $expected);
  4925. $Contact = ClassRegistry::getObject('Contact');
  4926. $Contact->validationErrors['field'] = 1;
  4927. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4928. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4929. $expected = array(
  4930. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  4931. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4932. );
  4933. $this->assertTags($result, $expected);
  4934. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4935. $expected = array(
  4936. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  4937. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4938. );
  4939. $this->assertTags($result, $expected);
  4940. $this->Form->request->data['Contact']['field'] = '';
  4941. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4942. $expected = array(
  4943. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'theID_'),
  4944. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4945. );
  4946. $this->assertTags($result, $expected);
  4947. $Contact->validationErrors = array();
  4948. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4949. $expected = array(
  4950. 'input' => array('type' => 'hidden', 'name' => 'Contact[field]', 'value' => '0', 'id' => 'ContactField_'),
  4951. array('input' => array('type' => 'checkbox', 'name' => 'Contact[field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4952. );
  4953. $this->assertTags($result, $expected);
  4954. $this->Form->request->data['Contact']['published'] = 1;
  4955. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4956. $expected = array(
  4957. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  4958. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4959. );
  4960. $this->assertTags($result, $expected);
  4961. $this->Form->request->data['Contact']['published'] = 0;
  4962. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4963. $expected = array(
  4964. 'input' => array('type' => 'hidden', 'name' => 'Contact[published]', 'value' => '0', 'id' => 'theID_'),
  4965. array('input' => array('type' => 'checkbox', 'name' => 'Contact[published]', 'value' => '1', 'id' => 'theID'))
  4966. );
  4967. $this->assertTags($result, $expected);
  4968. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4969. $expected = array(
  4970. 'input' => array('type' => 'hidden', 'name' => 'Model[CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4971. array('input' => array('type' => 'checkbox', 'name' => 'Model[CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4972. );
  4973. $this->assertTags($result, $expected);
  4974. $result = $this->Form->checkbox('CustomField.1.value');
  4975. $expected = array(
  4976. 'input' => array('type' => 'hidden', 'name' => 'CustomField[1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4977. array('input' => array('type' => 'checkbox', 'name' => 'CustomField[1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4978. );
  4979. $this->assertTags($result, $expected);
  4980. }
  4981. /**
  4982. * test checkbox() with a custom name attribute
  4983. *
  4984. * @return void
  4985. */
  4986. public function testCheckboxCustomNameAttribute() {
  4987. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4988. $expected = array(
  4989. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4990. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4991. );
  4992. $this->assertTags($result, $expected);
  4993. }
  4994. /**
  4995. * test the checked option for checkboxes.
  4996. *
  4997. * @return void
  4998. */
  4999. public function testCheckboxCheckedOption() {
  5000. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  5001. $expected = array(
  5002. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5003. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5004. );
  5005. $this->assertTags($result, $expected);
  5006. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  5007. $expected = array(
  5008. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5009. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5010. );
  5011. $this->assertTags($result, $expected);
  5012. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  5013. $expected = array(
  5014. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5015. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  5016. );
  5017. $this->assertTags($result, $expected);
  5018. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5019. $expected = array(
  5020. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5021. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5022. );
  5023. $this->assertTags($result, $expected);
  5024. $this->Form->request->data['Model']['field'] = 1;
  5025. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  5026. $expected = array(
  5027. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'id' => 'ModelField_'),
  5028. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField'))
  5029. );
  5030. $this->assertTags($result, $expected);
  5031. }
  5032. /**
  5033. * Test that disabled attribute works on both the checkbox and hidden input.
  5034. *
  5035. * @return void
  5036. */
  5037. public function testCheckboxDisabling() {
  5038. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  5039. $expected = array(
  5040. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  5041. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  5042. );
  5043. $this->assertTags($result, $expected);
  5044. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  5045. $expected = array(
  5046. array('input' => array('type' => 'hidden', 'name' => 'Account[show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  5047. array('input' => array('type' => 'checkbox', 'name' => 'Account[show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  5048. );
  5049. $this->assertTags($result, $expected);
  5050. }
  5051. /**
  5052. * Test that the hidden input for checkboxes can be omitted or set to a
  5053. * specific value.
  5054. *
  5055. * @return void
  5056. */
  5057. public function testCheckboxHiddenField() {
  5058. $result = $this->Form->input('UserForm.something', array(
  5059. 'type' => 'checkbox',
  5060. 'hiddenField' => false
  5061. ));
  5062. $expected = array(
  5063. 'div' => array('class' => 'input checkbox'),
  5064. array('input' => array(
  5065. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5066. 'value' => '1', 'id' => 'UserFormSomething'
  5067. )),
  5068. 'label' => array('for' => 'UserFormSomething'),
  5069. 'Something',
  5070. '/label',
  5071. '/div'
  5072. );
  5073. $this->assertTags($result, $expected);
  5074. $result = $this->Form->input('UserForm.something', array(
  5075. 'type' => 'checkbox',
  5076. 'value' => 'Y',
  5077. 'hiddenField' => 'N',
  5078. ));
  5079. $expected = array(
  5080. 'div' => array('class' => 'input checkbox'),
  5081. array('input' => array(
  5082. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5083. 'value' => 'N', 'id' => 'UserFormSomething_'
  5084. )),
  5085. array('input' => array(
  5086. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5087. 'value' => 'Y', 'id' => 'UserFormSomething'
  5088. )),
  5089. 'label' => array('for' => 'UserFormSomething'),
  5090. 'Something',
  5091. '/label',
  5092. '/div'
  5093. );
  5094. $this->assertTags($result, $expected);
  5095. }
  5096. /**
  5097. * testDateTime method
  5098. *
  5099. * Test generation of date/time select elements
  5100. *
  5101. * @return void
  5102. */
  5103. public function testDateTime() {
  5104. extract($this->dateRegex);
  5105. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5106. $now = strtotime('now');
  5107. $expected = array(
  5108. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5109. $daysRegex,
  5110. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5111. date('j', $now),
  5112. '/option',
  5113. '*/select',
  5114. '-',
  5115. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5116. $monthsRegex,
  5117. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5118. date('F', $now),
  5119. '/option',
  5120. '*/select',
  5121. '-',
  5122. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5123. $yearsRegex,
  5124. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5125. date('Y', $now),
  5126. '/option',
  5127. '*/select',
  5128. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5129. $hoursRegex,
  5130. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5131. date('g', $now),
  5132. '/option',
  5133. '*/select',
  5134. ':',
  5135. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5136. $minutesRegex,
  5137. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5138. date('i', $now),
  5139. '/option',
  5140. '*/select',
  5141. ' ',
  5142. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5143. $meridianRegex,
  5144. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5145. date('a', $now),
  5146. '/option',
  5147. '*/select'
  5148. );
  5149. $this->assertTags($result, $expected);
  5150. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5151. $expected = array(
  5152. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5153. $daysRegex,
  5154. array('option' => array('value' => '')),
  5155. '/option',
  5156. '*/select',
  5157. '-',
  5158. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5159. $monthsRegex,
  5160. array('option' => array('value' => '')),
  5161. '/option',
  5162. '*/select',
  5163. '-',
  5164. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5165. $yearsRegex,
  5166. array('option' => array('value' => '')),
  5167. '/option',
  5168. '*/select',
  5169. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5170. $hoursRegex,
  5171. array('option' => array('value' => '')),
  5172. '/option',
  5173. '*/select',
  5174. ':',
  5175. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5176. $minutesRegex,
  5177. array('option' => array('value' => '')),
  5178. '/option',
  5179. '*/select',
  5180. ' ',
  5181. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5182. $meridianRegex,
  5183. array('option' => array('value' => '')),
  5184. '/option',
  5185. '*/select'
  5186. );
  5187. $this->assertTags($result, $expected);
  5188. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5189. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5190. $this->assertTags($result, $expected);
  5191. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5192. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5193. $this->assertTags($result, $expected);
  5194. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5195. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5196. $expected = array(
  5197. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5198. $daysRegex,
  5199. array('option' => array('value' => '')),
  5200. '/option',
  5201. '*/select',
  5202. '-',
  5203. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5204. $monthsRegex,
  5205. array('option' => array('value' => '')),
  5206. '/option',
  5207. '*/select',
  5208. '-',
  5209. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5210. $yearsRegex,
  5211. array('option' => array('value' => '')),
  5212. '/option',
  5213. '*/select',
  5214. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5215. $hoursRegex,
  5216. array('option' => array('value' => '')),
  5217. '/option',
  5218. '*/select',
  5219. ':',
  5220. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5221. $minutesRegex,
  5222. array('option' => array('value' => '')),
  5223. '/option',
  5224. array('option' => array('value' => '00')),
  5225. '00',
  5226. '/option',
  5227. array('option' => array('value' => '05')),
  5228. '05',
  5229. '/option',
  5230. array('option' => array('value' => '10')),
  5231. '10',
  5232. '/option',
  5233. '*/select',
  5234. ' ',
  5235. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5236. $meridianRegex,
  5237. array('option' => array('value' => '')),
  5238. '/option',
  5239. '*/select'
  5240. );
  5241. $this->assertTags($result, $expected);
  5242. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5243. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5244. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5245. $this->Form->request->data['Contact']['data'] = null;
  5246. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5247. $expected = array(
  5248. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5249. $daysRegex,
  5250. array('option' => array('value' => '')),
  5251. '/option',
  5252. '*/select',
  5253. '-',
  5254. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5255. $monthsRegex,
  5256. array('option' => array('value' => '')),
  5257. '/option',
  5258. '*/select',
  5259. '-',
  5260. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5261. $yearsRegex,
  5262. array('option' => array('value' => '')),
  5263. '/option',
  5264. '*/select',
  5265. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5266. $hoursRegex,
  5267. array('option' => array('value' => '')),
  5268. '/option',
  5269. '*/select',
  5270. ':',
  5271. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5272. $minutesRegex,
  5273. array('option' => array('value' => '')),
  5274. '/option',
  5275. '*/select',
  5276. ' ',
  5277. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5278. $meridianRegex,
  5279. array('option' => array('value' => '')),
  5280. '/option',
  5281. '*/select'
  5282. );
  5283. $this->assertTags($result, $expected);
  5284. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5285. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5286. $now = strtotime($this->Form->data['Model']['field']);
  5287. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5288. $expected = array(
  5289. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5290. $daysRegex,
  5291. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5292. date('j', $now),
  5293. '/option',
  5294. '*/select',
  5295. '-',
  5296. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5297. $monthsRegex,
  5298. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5299. date('F', $now),
  5300. '/option',
  5301. '*/select',
  5302. '-',
  5303. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  5304. $yearsRegex,
  5305. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5306. date('Y', $now),
  5307. '/option',
  5308. '*/select',
  5309. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5310. $hoursRegex,
  5311. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5312. date('g', $now),
  5313. '/option',
  5314. '*/select',
  5315. ':',
  5316. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5317. $minutesRegex,
  5318. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5319. date('i', $now),
  5320. '/option',
  5321. '*/select',
  5322. ' ',
  5323. array('select' => array('name' => 'Model[field][meridian]', 'id' => 'ModelFieldMeridian')),
  5324. $meridianRegex,
  5325. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5326. date('a', $now),
  5327. '/option',
  5328. '*/select'
  5329. );
  5330. $this->assertTags($result, $expected);
  5331. $selected = strtotime('2008-10-26 12:33:00');
  5332. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5333. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5334. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5335. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5336. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5337. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5338. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5339. $this->Form->create('Contact');
  5340. $result = $this->Form->input('published');
  5341. $now = strtotime('now');
  5342. $expected = array(
  5343. 'div' => array('class' => 'input date'),
  5344. 'label' => array('for' => 'ContactPublishedMonth'),
  5345. 'Published',
  5346. '/label',
  5347. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5348. $monthsRegex,
  5349. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5350. date('F', $now),
  5351. '/option',
  5352. '*/select',
  5353. '-',
  5354. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5355. $daysRegex,
  5356. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5357. date('j', $now),
  5358. '/option',
  5359. '*/select',
  5360. '-',
  5361. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5362. $yearsRegex,
  5363. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5364. date('Y', $now),
  5365. '/option',
  5366. '*/select',
  5367. '/div'
  5368. );
  5369. $this->assertTags($result, $expected);
  5370. $result = $this->Form->input('published2', array('type' => 'date'));
  5371. $now = strtotime('now');
  5372. $expected = array(
  5373. 'div' => array('class' => 'input date'),
  5374. 'label' => array('for' => 'ContactPublished2Month'),
  5375. 'Published2',
  5376. '/label',
  5377. array('select' => array('name' => 'Contact[published2][month]', 'id' => 'ContactPublished2Month')),
  5378. $monthsRegex,
  5379. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5380. date('F', $now),
  5381. '/option',
  5382. '*/select',
  5383. '-',
  5384. array('select' => array('name' => 'Contact[published2][day]', 'id' => 'ContactPublished2Day')),
  5385. $daysRegex,
  5386. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5387. date('j', $now),
  5388. '/option',
  5389. '*/select',
  5390. '-',
  5391. array('select' => array('name' => 'Contact[published2][year]', 'id' => 'ContactPublished2Year')),
  5392. $yearsRegex,
  5393. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5394. date('Y', $now),
  5395. '/option',
  5396. '*/select',
  5397. '/div'
  5398. );
  5399. $this->assertTags($result, $expected);
  5400. $this->Form->create('Contact');
  5401. $result = $this->Form->input('published', array('monthNames' => false));
  5402. $now = strtotime('now');
  5403. $expected = array(
  5404. 'div' => array('class' => 'input date'),
  5405. 'label' => array('for' => 'ContactPublishedMonth'),
  5406. 'Published',
  5407. '/label',
  5408. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5409. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5410. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5411. date('m', $now),
  5412. '/option',
  5413. '*/select',
  5414. '-',
  5415. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5416. $daysRegex,
  5417. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5418. date('j', $now),
  5419. '/option',
  5420. '*/select',
  5421. '-',
  5422. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5423. $yearsRegex,
  5424. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5425. date('Y', $now),
  5426. '/option',
  5427. '*/select',
  5428. '/div'
  5429. );
  5430. $this->assertTags($result, $expected);
  5431. $result = $this->Form->input('published', array('type' => 'time'));
  5432. $now = strtotime('now');
  5433. $expected = array(
  5434. 'div' => array('class' => 'input time'),
  5435. 'label' => array('for' => 'ContactPublishedHour'),
  5436. 'Published',
  5437. '/label',
  5438. array('select' => array('name' => 'Contact[published][hour]', 'id' => 'ContactPublishedHour')),
  5439. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5440. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5441. date('g', $now),
  5442. '/option',
  5443. '*/select',
  5444. ':',
  5445. );
  5446. $this->assertTags($result, $expected);
  5447. $result = $this->Form->input('published', array(
  5448. 'timeFormat' => 24,
  5449. 'interval' => 5,
  5450. 'selected' => strtotime('2009-09-03 13:37:00'),
  5451. 'type' => 'datetime'
  5452. ));
  5453. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5454. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5455. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5456. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5457. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5458. }
  5459. /**
  5460. * Test dateTime with rounding
  5461. *
  5462. * @return void
  5463. */
  5464. public function testDateTimeRounding() {
  5465. $this->Form->request->data['Contact'] = array(
  5466. 'date' => array(
  5467. 'day' => '13',
  5468. 'month' => '12',
  5469. 'year' => '2010',
  5470. 'hour' => '04',
  5471. 'min' => '19',
  5472. 'meridian' => 'AM'
  5473. )
  5474. );
  5475. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5476. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5477. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5478. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5479. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5480. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5481. }
  5482. /**
  5483. * Test that empty values don't trigger errors.
  5484. *
  5485. * @return void
  5486. */
  5487. public function testDateTimeNoErrorsOnEmptyData() {
  5488. $this->Form->request->data['Contact'] = array(
  5489. 'date' => array(
  5490. 'day' => '',
  5491. 'month' => '',
  5492. 'year' => '',
  5493. 'hour' => '',
  5494. 'min' => '',
  5495. 'meridian' => ''
  5496. )
  5497. );
  5498. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5499. $this->assertNotEmpty($result);
  5500. }
  5501. /**
  5502. * test that datetime() and default values work.
  5503. *
  5504. * @return void
  5505. */
  5506. public function testDatetimeWithDefault() {
  5507. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5508. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5509. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5510. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5511. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5512. 'default' => '2009-06-01 11:15:30'
  5513. ));
  5514. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5515. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5516. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5517. }
  5518. /**
  5519. * test that bogus non-date time data doesn't cause errors.
  5520. *
  5521. * @return void
  5522. */
  5523. public function testDateTimeWithBogusData() {
  5524. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5525. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5526. }
  5527. /**
  5528. * testDateTimeEmptyAsArray
  5529. *
  5530. * @return void
  5531. */
  5532. public function testDateTimeEmptyAsArray() {
  5533. $result = $this->Form->dateTime('Contact.date',
  5534. 'DMY',
  5535. '12',
  5536. array(
  5537. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5538. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5539. )
  5540. )
  5541. );
  5542. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5543. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5544. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5545. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5546. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5547. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5548. $result = $this->Form->dateTime('Contact.date',
  5549. 'DMY',
  5550. '12',
  5551. array(
  5552. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5553. )
  5554. );
  5555. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5556. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5557. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5558. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5559. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5560. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5561. }
  5562. /**
  5563. * testFormDateTimeMulti method
  5564. *
  5565. * test multiple datetime element generation
  5566. *
  5567. * @return void
  5568. */
  5569. public function testFormDateTimeMulti() {
  5570. extract($this->dateRegex);
  5571. $result = $this->Form->dateTime('Contact.1.updated');
  5572. $expected = array(
  5573. array('select' => array('name' => 'Contact[1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5574. $daysRegex,
  5575. array('option' => array('value' => '')),
  5576. '/option',
  5577. '*/select',
  5578. '-',
  5579. array('select' => array('name' => 'Contact[1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5580. $monthsRegex,
  5581. array('option' => array('value' => '')),
  5582. '/option',
  5583. '*/select',
  5584. '-',
  5585. array('select' => array('name' => 'Contact[1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5586. $yearsRegex,
  5587. array('option' => array('value' => '')),
  5588. '/option',
  5589. '*/select',
  5590. array('select' => array('name' => 'Contact[1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5591. $hoursRegex,
  5592. array('option' => array('value' => '')),
  5593. '/option',
  5594. '*/select',
  5595. ':',
  5596. array('select' => array('name' => 'Contact[1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5597. $minutesRegex,
  5598. array('option' => array('value' => '')),
  5599. '/option',
  5600. '*/select',
  5601. ' ',
  5602. array('select' => array('name' => 'Contact[1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5603. $meridianRegex,
  5604. array('option' => array('value' => '')),
  5605. '/option',
  5606. '*/select'
  5607. );
  5608. $this->assertTags($result, $expected);
  5609. $result = $this->Form->dateTime('Contact.2.updated');
  5610. $expected = array(
  5611. array('select' => array('name' => 'Contact[2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5612. $daysRegex,
  5613. array('option' => array('value' => '')),
  5614. '/option',
  5615. '*/select',
  5616. '-',
  5617. array('select' => array('name' => 'Contact[2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5618. $monthsRegex,
  5619. array('option' => array('value' => '')),
  5620. '/option',
  5621. '*/select',
  5622. '-',
  5623. array('select' => array('name' => 'Contact[2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5624. $yearsRegex,
  5625. array('option' => array('value' => '')),
  5626. '/option',
  5627. '*/select',
  5628. array('select' => array('name' => 'Contact[2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5629. $hoursRegex,
  5630. array('option' => array('value' => '')),
  5631. '/option',
  5632. '*/select',
  5633. ':',
  5634. array('select' => array('name' => 'Contact[2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5635. $minutesRegex,
  5636. array('option' => array('value' => '')),
  5637. '/option',
  5638. '*/select',
  5639. ' ',
  5640. array('select' => array('name' => 'Contact[2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5641. $meridianRegex,
  5642. array('option' => array('value' => '')),
  5643. '/option',
  5644. '*/select'
  5645. );
  5646. $this->assertTags($result, $expected);
  5647. }
  5648. /**
  5649. * When changing the date format, the label should always focus the first select box when
  5650. * clicked.
  5651. *
  5652. * @return void
  5653. */
  5654. public function testDateTimeLabelIdMatchesFirstInput() {
  5655. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5656. $this->assertContains('label for="ModelDateMonth"', $result);
  5657. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5658. $this->assertContains('label for="ModelDateDay"', $result);
  5659. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5660. $this->assertContains('label for="ModelDateYear"', $result);
  5661. }
  5662. /**
  5663. * testMonth method
  5664. *
  5665. * @return void
  5666. */
  5667. public function testMonth() {
  5668. $result = $this->Form->month('Model.field');
  5669. $expected = array(
  5670. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5671. array('option' => array('value' => '')),
  5672. '/option',
  5673. array('option' => array('value' => '01')),
  5674. date('F', strtotime('2008-01-01 00:00:00')),
  5675. '/option',
  5676. array('option' => array('value' => '02')),
  5677. date('F', strtotime('2008-02-01 00:00:00')),
  5678. '/option',
  5679. '*/select',
  5680. );
  5681. $this->assertTags($result, $expected);
  5682. $result = $this->Form->month('Model.field', array('empty' => true));
  5683. $expected = array(
  5684. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5685. array('option' => array('value' => '')),
  5686. '/option',
  5687. array('option' => array('value' => '01')),
  5688. date('F', strtotime('2008-01-01 00:00:00')),
  5689. '/option',
  5690. array('option' => array('value' => '02')),
  5691. date('F', strtotime('2008-02-01 00:00:00')),
  5692. '/option',
  5693. '*/select',
  5694. );
  5695. $this->assertTags($result, $expected);
  5696. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5697. $expected = array(
  5698. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5699. array('option' => array('value' => '')),
  5700. '/option',
  5701. array('option' => array('value' => '01')),
  5702. '01',
  5703. '/option',
  5704. array('option' => array('value' => '02')),
  5705. '02',
  5706. '/option',
  5707. '*/select',
  5708. );
  5709. $this->assertTags($result, $expected);
  5710. $monthNames = array(
  5711. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5712. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5713. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5714. $expected = array(
  5715. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5716. array('option' => array('value' => '')),
  5717. '/option',
  5718. array('option' => array('value' => '01')),
  5719. 'Jan',
  5720. '/option',
  5721. array('option' => array('value' => '02')),
  5722. 'Feb',
  5723. '/option',
  5724. '*/select',
  5725. );
  5726. $this->assertTags($result, $expected);
  5727. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5728. $result = $this->Form->month('Project.release');
  5729. $expected = array(
  5730. array('select' => array('name' => 'Project[release][month]', 'id' => 'ProjectReleaseMonth')),
  5731. array('option' => array('value' => '')),
  5732. '/option',
  5733. array('option' => array('value' => '01')),
  5734. 'January',
  5735. '/option',
  5736. array('option' => array('value' => '02', 'selected' => 'selected')),
  5737. 'February',
  5738. '/option',
  5739. '*/select',
  5740. );
  5741. $this->assertTags($result, $expected);
  5742. }
  5743. /**
  5744. * testDay method
  5745. *
  5746. * @return void
  5747. */
  5748. public function testDay() {
  5749. extract($this->dateRegex);
  5750. $result = $this->Form->day('Model.field', array('value' => false));
  5751. $expected = array(
  5752. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5753. array('option' => array('value' => '')),
  5754. '/option',
  5755. array('option' => array('value' => '01')),
  5756. '1',
  5757. '/option',
  5758. array('option' => array('value' => '02')),
  5759. '2',
  5760. '/option',
  5761. $daysRegex,
  5762. '/select',
  5763. );
  5764. $this->assertTags($result, $expected);
  5765. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5766. $result = $this->Form->day('Model.field');
  5767. $expected = array(
  5768. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5769. array('option' => array('value' => '')),
  5770. '/option',
  5771. array('option' => array('value' => '01')),
  5772. '1',
  5773. '/option',
  5774. array('option' => array('value' => '02')),
  5775. '2',
  5776. '/option',
  5777. $daysRegex,
  5778. array('option' => array('value' => '10', 'selected' => 'selected')),
  5779. '10',
  5780. '/option',
  5781. $daysRegex,
  5782. '/select',
  5783. );
  5784. $this->assertTags($result, $expected);
  5785. $this->Form->request->data['Model']['field'] = '';
  5786. $result = $this->Form->day('Model.field', array('value' => '10'));
  5787. $expected = array(
  5788. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5789. array('option' => array('value' => '')),
  5790. '/option',
  5791. array('option' => array('value' => '01')),
  5792. '1',
  5793. '/option',
  5794. array('option' => array('value' => '02')),
  5795. '2',
  5796. '/option',
  5797. $daysRegex,
  5798. array('option' => array('value' => '10', 'selected' => 'selected')),
  5799. '10',
  5800. '/option',
  5801. $daysRegex,
  5802. '/select',
  5803. );
  5804. $this->assertTags($result, $expected);
  5805. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5806. $result = $this->Form->day('Model.field', array('value' => true));
  5807. $expected = array(
  5808. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5809. array('option' => array('value' => '')),
  5810. '/option',
  5811. array('option' => array('value' => '01')),
  5812. '1',
  5813. '/option',
  5814. array('option' => array('value' => '02')),
  5815. '2',
  5816. '/option',
  5817. $daysRegex,
  5818. array('option' => array('value' => '10', 'selected' => 'selected')),
  5819. '10',
  5820. '/option',
  5821. $daysRegex,
  5822. '/select',
  5823. );
  5824. $this->assertTags($result, $expected);
  5825. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5826. $result = $this->Form->day('Project.release');
  5827. $expected = array(
  5828. array('select' => array('name' => 'Project[release][day]', 'id' => 'ProjectReleaseDay')),
  5829. array('option' => array('value' => '')),
  5830. '/option',
  5831. array('option' => array('value' => '01')),
  5832. '1',
  5833. '/option',
  5834. array('option' => array('value' => '02')),
  5835. '2',
  5836. '/option',
  5837. $daysRegex,
  5838. array('option' => array('value' => '10', 'selected' => 'selected')),
  5839. '10',
  5840. '/option',
  5841. $daysRegex,
  5842. '/select',
  5843. );
  5844. $this->assertTags($result, $expected);
  5845. }
  5846. /**
  5847. * testMinute method
  5848. *
  5849. * @return void
  5850. */
  5851. public function testMinute() {
  5852. extract($this->dateRegex);
  5853. $result = $this->Form->minute('Model.field');
  5854. $expected = array(
  5855. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5856. array('option' => array('value' => '')),
  5857. '/option',
  5858. array('option' => array('value' => '00')),
  5859. '00',
  5860. '/option',
  5861. array('option' => array('value' => '01')),
  5862. '01',
  5863. '/option',
  5864. array('option' => array('value' => '02')),
  5865. '02',
  5866. '/option',
  5867. $minutesRegex,
  5868. '/select',
  5869. );
  5870. $this->assertTags($result, $expected);
  5871. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5872. $result = $this->Form->minute('Model.field');
  5873. $expected = array(
  5874. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5875. array('option' => array('value' => '')),
  5876. '/option',
  5877. array('option' => array('value' => '00')),
  5878. '00',
  5879. '/option',
  5880. array('option' => array('value' => '01')),
  5881. '01',
  5882. '/option',
  5883. array('option' => array('value' => '02')),
  5884. '02',
  5885. '/option',
  5886. $minutesRegex,
  5887. array('option' => array('value' => '12', 'selected' => 'selected')),
  5888. '12',
  5889. '/option',
  5890. $minutesRegex,
  5891. '/select',
  5892. );
  5893. $this->assertTags($result, $expected);
  5894. $this->Form->request->data['Model']['field'] = '';
  5895. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5896. $expected = array(
  5897. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5898. array('option' => array('value' => '')),
  5899. '/option',
  5900. array('option' => array('value' => '00')),
  5901. '00',
  5902. '/option',
  5903. array('option' => array('value' => '05')),
  5904. '05',
  5905. '/option',
  5906. array('option' => array('value' => '10')),
  5907. '10',
  5908. '/option',
  5909. $minutesRegex,
  5910. '/select',
  5911. );
  5912. $this->assertTags($result, $expected);
  5913. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5914. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5915. $expected = array(
  5916. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5917. array('option' => array('value' => '')),
  5918. '/option',
  5919. array('option' => array('value' => '00')),
  5920. '00',
  5921. '/option',
  5922. array('option' => array('value' => '05')),
  5923. '05',
  5924. '/option',
  5925. array('option' => array('value' => '10', 'selected' => 'selected')),
  5926. '10',
  5927. '/option',
  5928. $minutesRegex,
  5929. '/select',
  5930. );
  5931. $this->assertTags($result, $expected);
  5932. }
  5933. /**
  5934. * testHour method
  5935. *
  5936. * @return void
  5937. */
  5938. public function testHour() {
  5939. extract($this->dateRegex);
  5940. $result = $this->Form->hour('Model.field', false);
  5941. $expected = array(
  5942. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5943. array('option' => array('value' => '')),
  5944. '/option',
  5945. array('option' => array('value' => '01')),
  5946. '1',
  5947. '/option',
  5948. array('option' => array('value' => '02')),
  5949. '2',
  5950. '/option',
  5951. $hoursRegex,
  5952. '/select',
  5953. );
  5954. $this->assertTags($result, $expected);
  5955. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5956. $result = $this->Form->hour('Model.field', false);
  5957. $expected = array(
  5958. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5959. array('option' => array('value' => '')),
  5960. '/option',
  5961. array('option' => array('value' => '01')),
  5962. '1',
  5963. '/option',
  5964. array('option' => array('value' => '02')),
  5965. '2',
  5966. '/option',
  5967. $hoursRegex,
  5968. array('option' => array('value' => '12', 'selected' => 'selected')),
  5969. '12',
  5970. '/option',
  5971. '/select',
  5972. );
  5973. $this->assertTags($result, $expected);
  5974. $this->Form->request->data['Model']['field'] = '';
  5975. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5976. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5977. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5978. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5979. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5980. $result = $this->Form->hour('Model.field', true);
  5981. $expected = array(
  5982. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5983. array('option' => array('value' => '')),
  5984. '/option',
  5985. array('option' => array('value' => '00', 'selected' => 'selected')),
  5986. '0',
  5987. '/option',
  5988. array('option' => array('value' => '01')),
  5989. '1',
  5990. '/option',
  5991. array('option' => array('value' => '02')),
  5992. '2',
  5993. '/option',
  5994. $hoursRegex,
  5995. '/select',
  5996. );
  5997. $this->assertTags($result, $expected);
  5998. unset($this->Form->request->data['Model']['field']);
  5999. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  6000. $thisHour = date('H');
  6001. $optValue = date('G');
  6002. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  6003. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  6004. $result = $this->Form->hour('Model.field', true);
  6005. $expected = array(
  6006. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  6007. array('option' => array('value' => '')),
  6008. '/option',
  6009. array('option' => array('value' => '00')),
  6010. '0',
  6011. '/option',
  6012. array('option' => array('value' => '01', 'selected' => 'selected')),
  6013. '1',
  6014. '/option',
  6015. array('option' => array('value' => '02')),
  6016. '2',
  6017. '/option',
  6018. $hoursRegex,
  6019. '/select',
  6020. );
  6021. $this->assertTags($result, $expected);
  6022. }
  6023. /**
  6024. * testYear method
  6025. *
  6026. * @return void
  6027. */
  6028. public function testYear() {
  6029. $result = $this->Form->year('Model.field', 2006, 2007);
  6030. $expected = array(
  6031. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6032. array('option' => array('value' => '')),
  6033. '/option',
  6034. array('option' => array('value' => '2007')),
  6035. '2007',
  6036. '/option',
  6037. array('option' => array('value' => '2006')),
  6038. '2006',
  6039. '/option',
  6040. '/select',
  6041. );
  6042. $this->assertTags($result, $expected);
  6043. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  6044. $expected = array(
  6045. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  6046. array('option' => array('value' => '')),
  6047. '/option',
  6048. array('option' => array('value' => '2006')),
  6049. '2006',
  6050. '/option',
  6051. array('option' => array('value' => '2007')),
  6052. '2007',
  6053. '/option',
  6054. '/select',
  6055. );
  6056. $this->assertTags($result, $expected);
  6057. $this->request->data['Contact']['published'] = '';
  6058. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  6059. $expected = array(
  6060. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  6061. array('option' => array('value' => '')),
  6062. '/option',
  6063. array('option' => array('value' => '2007')),
  6064. '2007',
  6065. '/option',
  6066. array('option' => array('value' => '2006')),
  6067. '2006',
  6068. '/option',
  6069. '/select',
  6070. );
  6071. $this->assertTags($result, $expected);
  6072. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6073. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  6074. $expected = array(
  6075. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6076. array('option' => array('value' => '2007')),
  6077. '2007',
  6078. '/option',
  6079. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6080. '2006',
  6081. '/option',
  6082. '/select',
  6083. );
  6084. $this->assertTags($result, $expected);
  6085. $this->Form->request->data['Contact']['published'] = '';
  6086. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  6087. $expected = array(
  6088. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6089. array('option' => array('value' => '')),
  6090. '/option',
  6091. array('option' => array('value' => '2007')),
  6092. '2007',
  6093. '/option',
  6094. array('option' => array('value' => '2006')),
  6095. '2006',
  6096. '/option',
  6097. '/select',
  6098. );
  6099. $this->assertTags($result, $expected);
  6100. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6101. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  6102. $expected = array(
  6103. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6104. array('option' => array('value' => '2007')),
  6105. '2007',
  6106. '/option',
  6107. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6108. '2006',
  6109. '/option',
  6110. '/select',
  6111. );
  6112. $this->assertTags($result, $expected);
  6113. $this->Form->request->data['Contact']['published'] = '';
  6114. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  6115. $expected = array(
  6116. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6117. array('option' => array('value' => '')),
  6118. '/option',
  6119. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6120. '2007',
  6121. '/option',
  6122. array('option' => array('value' => '2006')),
  6123. '2006',
  6124. '/option',
  6125. '/select',
  6126. );
  6127. $this->assertTags($result, $expected);
  6128. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6129. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6130. $expected = array(
  6131. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6132. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6133. '2007',
  6134. '/option',
  6135. array('option' => array('value' => '2006')),
  6136. '2006',
  6137. '/option',
  6138. '/select',
  6139. );
  6140. $this->assertTags($result, $expected);
  6141. $this->Form->request->data['Contact']['published'] = '';
  6142. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6143. $expected = array(
  6144. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6145. array('option' => array('value' => '2008')),
  6146. '2008',
  6147. '/option',
  6148. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6149. '2007',
  6150. '/option',
  6151. array('option' => array('value' => '2006')),
  6152. '2006',
  6153. '/option',
  6154. '/select',
  6155. );
  6156. $this->assertTags($result, $expected);
  6157. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6158. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6159. $expected = array(
  6160. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6161. array('option' => array('value' => '2008')),
  6162. '2008',
  6163. '/option',
  6164. array('option' => array('value' => '2007')),
  6165. '2007',
  6166. '/option',
  6167. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6168. '2006',
  6169. '/option',
  6170. '/select',
  6171. );
  6172. $this->assertTags($result, $expected);
  6173. $this->Form->request->data = array();
  6174. $this->Form->create('Contact');
  6175. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6176. $expected = array(
  6177. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6178. array('option' => array('value' => '2008')),
  6179. '2008',
  6180. '/option',
  6181. array('option' => array('value' => '2007')),
  6182. '2007',
  6183. '/option',
  6184. array('option' => array('value' => '2006')),
  6185. '2006',
  6186. '/option',
  6187. '/select',
  6188. );
  6189. $this->assertTags($result, $expected);
  6190. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6191. $this->assertContains('Contact[published][year]', $result);
  6192. }
  6193. /**
  6194. * testYearAutoExpandRange method
  6195. *
  6196. * @return void
  6197. */
  6198. public function testYearAutoExpandRange() {
  6199. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6200. $result = $this->Form->year('User.birthday');
  6201. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6202. $result = $matches[1];
  6203. $expected = range(date('Y') + 20, 1930);
  6204. $this->assertEquals($result, $expected);
  6205. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6206. $result = $this->Form->year('Project.release');
  6207. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6208. $result = $matches[1];
  6209. $expected = range(2050, date('Y') - 20);
  6210. $this->assertEquals($result, $expected);
  6211. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6212. $result = $this->Form->year('Project.release', 1890, 1900);
  6213. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6214. $result = $matches[1];
  6215. $expected = range(1900, 1881);
  6216. $this->assertEquals($result, $expected);
  6217. }
  6218. /**
  6219. * testInputDate method
  6220. *
  6221. * Test various inputs with type date and different dateFormat values.
  6222. * Failing to provide a dateFormat key should not error.
  6223. * It should simply not pre-select any value then.
  6224. *
  6225. * @return void
  6226. */
  6227. public function testInputDate() {
  6228. $this->Form->request->data = array(
  6229. 'User' => array(
  6230. 'month_year' => array('month' => date('m')),
  6231. 'just_year' => array('month' => date('m')),
  6232. 'just_month' => array('year' => date('Y')),
  6233. 'just_day' => array('month' => date('m')),
  6234. )
  6235. );
  6236. $this->Form->create('User');
  6237. $result = $this->Form->input('month_year',
  6238. array(
  6239. 'label' => false,
  6240. 'div' => false,
  6241. 'type' => 'date',
  6242. 'dateFormat' => 'MY',
  6243. 'minYear' => 2006,
  6244. 'maxYear' => 2008
  6245. )
  6246. );
  6247. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6248. $this->assertNotContains('value="2008" selected="selected"', $result);
  6249. $result = $this->Form->input('just_year',
  6250. array(
  6251. 'type' => 'date',
  6252. 'label' => false,
  6253. 'dateFormat' => 'Y',
  6254. 'minYear' => date('Y'),
  6255. 'maxYear' => date('Y', strtotime('+20 years'))
  6256. )
  6257. );
  6258. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6259. $result = $this->Form->input('just_month',
  6260. array(
  6261. 'type' => 'date',
  6262. 'label' => false,
  6263. 'dateFormat' => 'M',
  6264. 'empty' => false,
  6265. )
  6266. );
  6267. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6268. $result = $this->Form->input('just_day',
  6269. array(
  6270. 'type' => 'date',
  6271. 'label' => false,
  6272. 'dateFormat' => 'D',
  6273. 'empty' => false,
  6274. )
  6275. );
  6276. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6277. }
  6278. /**
  6279. * testInputDateMaxYear method
  6280. *
  6281. * Let's say we want to only allow users born from 2006 to 2008 to register
  6282. * This being the first singup page, we still don't have any data
  6283. *
  6284. * @return void
  6285. */
  6286. public function testInputDateMaxYear() {
  6287. $this->Form->request->data = array();
  6288. $this->Form->create('User');
  6289. $result = $this->Form->input('birthday',
  6290. array(
  6291. 'label' => false,
  6292. 'div' => false,
  6293. 'type' => 'date',
  6294. 'dateFormat' => 'DMY',
  6295. 'minYear' => 2006,
  6296. 'maxYear' => 2008
  6297. )
  6298. );
  6299. $this->assertContains('value="2008" selected="selected"', $result);
  6300. }
  6301. /**
  6302. * testTextArea method
  6303. *
  6304. * @return void
  6305. */
  6306. public function testTextArea() {
  6307. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  6308. $result = $this->Form->textarea('Model.field');
  6309. $expected = array(
  6310. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6311. 'some test data',
  6312. '/textarea',
  6313. );
  6314. $this->assertTags($result, $expected);
  6315. $result = $this->Form->textarea('Model.tmp');
  6316. $expected = array(
  6317. 'textarea' => array('name' => 'Model[tmp]', 'id' => 'ModelTmp'),
  6318. '/textarea',
  6319. );
  6320. $this->assertTags($result, $expected);
  6321. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6322. $result = $this->Form->textarea('Model.field');
  6323. $expected = array(
  6324. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6325. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6326. '/textarea',
  6327. );
  6328. $this->assertTags($result, $expected);
  6329. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  6330. $result = $this->Form->textarea('Model.field', array('escape' => false));
  6331. $expected = array(
  6332. 'textarea' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6333. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6334. '/textarea',
  6335. );
  6336. $this->assertTags($result, $expected);
  6337. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  6338. $result = $this->Form->textarea('Model.0.OtherModel.field');
  6339. $expected = array(
  6340. 'textarea' => array('name' => 'Model[0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  6341. '/textarea'
  6342. );
  6343. $this->assertTags($result, $expected);
  6344. }
  6345. /**
  6346. * testTextAreaWithStupidCharacters method
  6347. *
  6348. * test text area with non-ascii characters
  6349. *
  6350. * @return void
  6351. */
  6352. public function testTextAreaWithStupidCharacters() {
  6353. $this->loadFixtures('Post');
  6354. $result = $this->Form->input('Post.content', array(
  6355. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  6356. ));
  6357. $expected = array(
  6358. 'div' => array('class' => 'input textarea'),
  6359. 'label' => array('for' => 'PostContent'),
  6360. 'Current Text',
  6361. '/label',
  6362. 'textarea' => array('name' => 'Post[content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  6363. 'GREAT®',
  6364. '/textarea',
  6365. '/div'
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. }
  6369. /**
  6370. * testHiddenField method
  6371. *
  6372. * @return void
  6373. */
  6374. public function testHiddenField() {
  6375. $Contact = ClassRegistry::getObject('Contact');
  6376. $Contact->validationErrors['field'] = 1;
  6377. $this->Form->request->data['Contact']['field'] = 'test';
  6378. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  6379. $this->assertTags($result, array(
  6380. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[field]', 'id' => 'theID', 'value' => 'test'))
  6381. );
  6382. }
  6383. /**
  6384. * testFileUploadField method
  6385. *
  6386. * @return void
  6387. */
  6388. public function testFileUploadField() {
  6389. $result = $this->Form->file('Model.upload');
  6390. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6391. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6392. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6393. $expected = array(
  6394. 'div' => array('class' => 'input file'),
  6395. 'label' => array('for' => 'ModelUpload'),
  6396. 'Upload',
  6397. '/label',
  6398. 'input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload'),
  6399. '/div'
  6400. );
  6401. $this->assertTags($result, $expected);
  6402. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6403. $result = $this->Form->file('Model.upload');
  6404. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6405. }
  6406. /**
  6407. * test File upload input on a model not used in create();
  6408. *
  6409. * @return void
  6410. */
  6411. public function testFileUploadOnOtherModel() {
  6412. $this->Form->create('ValidateUser', array('type' => 'file'));
  6413. $result = $this->Form->file('ValidateProfile.city');
  6414. $expected = array(
  6415. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]', 'id' => 'ValidateProfileCity')
  6416. );
  6417. $this->assertTags($result, $expected);
  6418. }
  6419. /**
  6420. * testButton method
  6421. *
  6422. * @return void
  6423. */
  6424. public function testButton() {
  6425. $result = $this->Form->button('Hi');
  6426. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6427. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6428. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6429. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6430. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6431. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6432. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6433. $result = $this->Form->button('No type', array('type' => false));
  6434. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6435. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  6436. $this->assertNotRegExp('/\&039/', $result);
  6437. }
  6438. /**
  6439. * Test that button() makes unlocked fields by default.
  6440. *
  6441. * @return void
  6442. */
  6443. public function testButtonUnlockedByDefault() {
  6444. $this->Form->request->params['_Token']['key'] = 'secured';
  6445. $this->Form->button('Save', array('name' => 'save'));
  6446. $this->Form->button('Clear');
  6447. $result = $this->Form->unlockField();
  6448. $this->assertEquals(array('save'), $result);
  6449. }
  6450. /**
  6451. * testPostButton method
  6452. *
  6453. * @return void
  6454. */
  6455. public function testPostButton() {
  6456. $result = $this->Form->postButton('Hi', '/controller/action');
  6457. $this->assertTags($result, array(
  6458. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6459. 'div' => array('style' => 'display:none;'),
  6460. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6461. '/div',
  6462. 'button' => array('type' => 'submit'),
  6463. 'Hi',
  6464. '/button',
  6465. '/form'
  6466. ));
  6467. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6468. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"/>') !== false);
  6469. }
  6470. /**
  6471. * Test that postButton adds _Token fields.
  6472. *
  6473. * @return void
  6474. */
  6475. public function testSecurePostButton() {
  6476. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6477. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6478. $expected = array(
  6479. 'form' => array(
  6480. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6481. ),
  6482. array('div' => array('style' => 'display:none;')),
  6483. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6484. array('input' => array('type' => 'hidden', 'name' => '_Token[key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6485. '/div',
  6486. 'button' => array('type' => 'submit'),
  6487. 'Delete',
  6488. '/button',
  6489. array('div' => array('style' => 'display:none;')),
  6490. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6491. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6492. '/div',
  6493. '/form',
  6494. );
  6495. $this->assertTags($result, $expected);
  6496. }
  6497. /**
  6498. * testPostLink method
  6499. *
  6500. * @return void
  6501. */
  6502. public function testPostLink() {
  6503. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6504. $this->assertTags($result, array(
  6505. 'form' => array(
  6506. 'method' => 'post', 'action' => '/posts/delete/1',
  6507. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6508. ),
  6509. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6510. '/form',
  6511. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6512. 'Delete',
  6513. '/a'
  6514. ));
  6515. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6516. $this->assertTags($result, array(
  6517. 'form' => array(
  6518. 'method' => 'post', 'action' => '/posts/delete/1',
  6519. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6520. ),
  6521. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6522. '/form',
  6523. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6524. 'Delete',
  6525. '/a'
  6526. ));
  6527. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6528. $this->assertTags($result, array(
  6529. 'form' => array(
  6530. 'method' => 'post', 'action' => '/posts/delete/1',
  6531. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6532. ),
  6533. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6534. '/form',
  6535. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6536. 'Delete',
  6537. '/a'
  6538. ));
  6539. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6540. $this->assertTags($result, array(
  6541. 'form' => array(
  6542. 'method' => 'post', 'action' => '/posts/delete/1',
  6543. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6544. ),
  6545. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6546. '/form',
  6547. '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;/'),
  6548. 'Delete',
  6549. '/a'
  6550. ));
  6551. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6552. $this->assertContains('<input type="hidden" name="id" value="1"/>', $result);
  6553. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6554. $this->assertTags($result, array(
  6555. 'form' => array(
  6556. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6557. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6558. ),
  6559. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6560. '/form',
  6561. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6562. 'Delete',
  6563. '/a'
  6564. ));
  6565. $result = $this->Form->postLink(
  6566. '',
  6567. array('controller' => 'items', 'action' => 'delete', 10),
  6568. array('class' => 'btn btn-danger', 'escape' => false),
  6569. 'Confirm thing'
  6570. );
  6571. $this->assertTags($result, array(
  6572. 'form' => array(
  6573. 'method' => 'post', 'action' => '/items/delete/10',
  6574. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6575. ),
  6576. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6577. '/form',
  6578. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6579. '/a'
  6580. ));
  6581. }
  6582. /**
  6583. * Test that postLink adds _Token fields.
  6584. *
  6585. * @return void
  6586. */
  6587. public function testSecurePostLink() {
  6588. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  6589. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6590. $expected = array(
  6591. 'form' => array(
  6592. 'method' => 'post', 'action' => '/posts/delete/1',
  6593. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6594. ),
  6595. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6596. array('input' => array('type' => 'hidden', 'name' => '_Token[key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  6597. 'div' => array('style' => 'display:none;'),
  6598. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  6599. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  6600. '/div',
  6601. '/form',
  6602. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6603. 'Delete',
  6604. '/a'
  6605. );
  6606. $this->assertTags($result, $expected);
  6607. }
  6608. /**
  6609. * testSubmitButton method
  6610. *
  6611. * @return void
  6612. */
  6613. public function testSubmitButton() {
  6614. $result = $this->Form->submit('');
  6615. $expected = array(
  6616. 'div' => array('class' => 'submit'),
  6617. 'input' => array('type' => 'submit', 'value' => ''),
  6618. '/div'
  6619. );
  6620. $this->assertTags($result, $expected);
  6621. $result = $this->Form->submit('Test Submit');
  6622. $expected = array(
  6623. 'div' => array('class' => 'submit'),
  6624. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6625. '/div'
  6626. );
  6627. $this->assertTags($result, $expected);
  6628. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6629. $expected = array(
  6630. 'span' => array('class' => 'submit'),
  6631. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6632. '/span'
  6633. );
  6634. $this->assertTags($result, $expected);
  6635. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6636. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6637. $this->assertTags($result, $expected);
  6638. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6639. $expected = array(
  6640. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6641. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6642. '/div'
  6643. );
  6644. $this->assertTags($result, $expected);
  6645. $result = $this->Form->submit('Next >');
  6646. $expected = array(
  6647. 'div' => array('class' => 'submit'),
  6648. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6649. '/div'
  6650. );
  6651. $this->assertTags($result, $expected);
  6652. $result = $this->Form->submit('Next >', array('escape' => false));
  6653. $expected = array(
  6654. 'div' => array('class' => 'submit'),
  6655. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6656. '/div'
  6657. );
  6658. $this->assertTags($result, $expected);
  6659. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6660. $expected = array(
  6661. 'div' => array('class' => 'submit'),
  6662. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6663. '/div'
  6664. );
  6665. $this->assertTags($result, $expected);
  6666. $before = '--before--';
  6667. $after = '--after--';
  6668. $result = $this->Form->submit('Test', array('before' => $before));
  6669. $expected = array(
  6670. 'div' => array('class' => 'submit'),
  6671. '--before--',
  6672. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6673. '/div'
  6674. );
  6675. $this->assertTags($result, $expected);
  6676. $result = $this->Form->submit('Test', array('after' => $after));
  6677. $expected = array(
  6678. 'div' => array('class' => 'submit'),
  6679. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6680. '--after--',
  6681. '/div'
  6682. );
  6683. $this->assertTags($result, $expected);
  6684. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6685. $expected = array(
  6686. 'div' => array('class' => 'submit'),
  6687. '--before--',
  6688. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6689. '--after--',
  6690. '/div'
  6691. );
  6692. $this->assertTags($result, $expected);
  6693. }
  6694. /**
  6695. * test image submit types.
  6696. *
  6697. * @return void
  6698. */
  6699. public function testSubmitImage() {
  6700. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6701. $expected = array(
  6702. 'div' => array('class' => 'submit'),
  6703. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6704. '/div'
  6705. );
  6706. $this->assertTags($result, $expected);
  6707. $result = $this->Form->submit('/relative/cake.power.gif');
  6708. $expected = array(
  6709. 'div' => array('class' => 'submit'),
  6710. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6711. '/div'
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. $result = $this->Form->submit('cake.power.gif');
  6715. $expected = array(
  6716. 'div' => array('class' => 'submit'),
  6717. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6718. '/div'
  6719. );
  6720. $this->assertTags($result, $expected);
  6721. $result = $this->Form->submit('Not.an.image');
  6722. $expected = array(
  6723. 'div' => array('class' => 'submit'),
  6724. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6725. '/div'
  6726. );
  6727. $this->assertTags($result, $expected);
  6728. $after = '--after--';
  6729. $before = '--before--';
  6730. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6731. $expected = array(
  6732. 'div' => array('class' => 'submit'),
  6733. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6734. '--after--',
  6735. '/div'
  6736. );
  6737. $this->assertTags($result, $expected);
  6738. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6739. $expected = array(
  6740. 'div' => array('class' => 'submit'),
  6741. '--before--',
  6742. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6743. '/div'
  6744. );
  6745. $this->assertTags($result, $expected);
  6746. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6747. $expected = array(
  6748. 'div' => array('class' => 'submit'),
  6749. '--before--',
  6750. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6751. '--after--',
  6752. '/div'
  6753. );
  6754. $this->assertTags($result, $expected);
  6755. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6756. $expected = array(
  6757. 'div' => array('class' => 'submit'),
  6758. '--before--',
  6759. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6760. '--after--',
  6761. '/div'
  6762. );
  6763. $this->assertTags($result, $expected);
  6764. }
  6765. /**
  6766. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6767. * be submitted at a time.
  6768. *
  6769. * @return void
  6770. */
  6771. public function testSubmitUnlockedByDefault() {
  6772. $this->Form->request->params['_Token']['key'] = 'secured';
  6773. $this->Form->submit('Go go');
  6774. $this->Form->submit('Save', array('name' => 'save'));
  6775. $result = $this->Form->unlockField();
  6776. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6777. }
  6778. /**
  6779. * Test submit image with timestamps.
  6780. *
  6781. * @return void
  6782. */
  6783. public function testSubmitImageTimestamp() {
  6784. Configure::write('Asset.timestamp', 'force');
  6785. $result = $this->Form->submit('cake.power.gif');
  6786. $expected = array(
  6787. 'div' => array('class' => 'submit'),
  6788. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6789. '/div'
  6790. );
  6791. $this->assertTags($result, $expected);
  6792. }
  6793. /**
  6794. * test the create() method
  6795. *
  6796. * @return void
  6797. */
  6798. public function testCreate() {
  6799. $result = $this->Form->create('Contact');
  6800. $encoding = strtolower(Configure::read('App.encoding'));
  6801. $expected = array(
  6802. 'form' => array(
  6803. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6804. 'accept-charset' => $encoding
  6805. ),
  6806. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6807. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6808. '/div'
  6809. );
  6810. $this->assertTags($result, $expected);
  6811. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6812. $expected = array('form' => array(
  6813. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6814. 'accept-charset' => $encoding
  6815. ));
  6816. $this->assertTags($result, $expected);
  6817. $result = $this->Form->create('Contact', array('type' => 'get'));
  6818. $expected = array('form' => array(
  6819. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6820. 'accept-charset' => $encoding
  6821. ));
  6822. $this->assertTags($result, $expected);
  6823. $result = $this->Form->create('Contact', array('type' => 'put'));
  6824. $expected = array(
  6825. 'form' => array(
  6826. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6827. 'accept-charset' => $encoding
  6828. ),
  6829. 'div' => array('style' => 'display:none;'),
  6830. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6831. '/div'
  6832. );
  6833. $this->assertTags($result, $expected);
  6834. $result = $this->Form->create('Contact', array('type' => 'file'));
  6835. $expected = array(
  6836. 'form' => array(
  6837. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6838. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6839. ),
  6840. 'div' => array('style' => 'display:none;'),
  6841. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6842. '/div'
  6843. );
  6844. $this->assertTags($result, $expected);
  6845. $this->Form->request->data['Contact']['id'] = 1;
  6846. $this->Form->request->here = '/contacts/edit/1';
  6847. $this->Form->request['action'] = 'edit';
  6848. $result = $this->Form->create('Contact');
  6849. $expected = array(
  6850. 'form' => array(
  6851. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6852. 'accept-charset' => $encoding
  6853. ),
  6854. 'div' => array('style' => 'display:none;'),
  6855. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6856. '/div'
  6857. );
  6858. $this->assertTags($result, $expected);
  6859. $this->Form->request->data['Contact']['id'] = 1;
  6860. $this->Form->request->here = '/contacts/edit/1';
  6861. $this->Form->request['action'] = 'edit';
  6862. $result = $this->Form->create('Contact', array('type' => 'file'));
  6863. $expected = array(
  6864. 'form' => array(
  6865. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6866. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6867. ),
  6868. 'div' => array('style' => 'display:none;'),
  6869. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6870. '/div'
  6871. );
  6872. $this->assertTags($result, $expected);
  6873. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6874. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6875. $expected = array(
  6876. 'form' => array(
  6877. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6878. 'action' => '/contact_non_standard_pks/edit/1', 'accept-charset' => $encoding
  6879. ),
  6880. 'div' => array('style' => 'display:none;'),
  6881. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6882. '/div'
  6883. );
  6884. $this->assertTags($result, $expected);
  6885. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6886. $expected = array(
  6887. 'form' => array(
  6888. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6889. 'accept-charset' => $encoding
  6890. ),
  6891. 'div' => array('style' => 'display:none;'),
  6892. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6893. '/div'
  6894. );
  6895. $this->assertTags($result, $expected);
  6896. $this->Form->request['action'] = 'add';
  6897. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6898. $expected = array(
  6899. 'form' => array(
  6900. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6901. 'accept-charset' => $encoding
  6902. ),
  6903. 'div' => array('style' => 'display:none;'),
  6904. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6905. '/div'
  6906. );
  6907. $this->assertTags($result, $expected);
  6908. $result = $this->Form->create('User', array('action' => 'login'));
  6909. $expected = array(
  6910. 'form' => array(
  6911. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6912. 'accept-charset' => $encoding
  6913. ),
  6914. 'div' => array('style' => 'display:none;'),
  6915. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6916. '/div'
  6917. );
  6918. $this->assertTags($result, $expected);
  6919. $result = $this->Form->create('User', array('url' => '/users/login'));
  6920. $expected = array(
  6921. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6922. 'div' => array('style' => 'display:none;'),
  6923. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6924. '/div'
  6925. );
  6926. $this->assertTags($result, $expected);
  6927. $this->Form->request['controller'] = 'pages';
  6928. $result = $this->Form->create('User', array('action' => 'signup'));
  6929. $expected = array(
  6930. 'form' => array(
  6931. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6932. 'accept-charset' => $encoding
  6933. ),
  6934. 'div' => array('style' => 'display:none;'),
  6935. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6936. '/div'
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. $this->Form->request->data = array();
  6940. $this->Form->request['controller'] = 'contacts';
  6941. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6942. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6943. $expected = array(
  6944. 'form' => array(
  6945. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6946. 'accept-charset' => 'utf-8'
  6947. ),
  6948. 'div' => array('style' => 'display:none;'),
  6949. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6950. '/div'
  6951. );
  6952. $this->assertTags($result, $expected);
  6953. }
  6954. /**
  6955. * Test the onsubmit option for create()
  6956. *
  6957. * @return void
  6958. */
  6959. public function testCreateOnSubmit() {
  6960. $this->Form->request->data = array();
  6961. $this->Form->request['controller'] = 'contacts';
  6962. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6963. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6964. $expected = array(
  6965. 'form' => array(
  6966. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6967. 'accept-charset' => 'utf-8'
  6968. ),
  6969. 'div' => array('style' => 'display:none;'),
  6970. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6971. '/div'
  6972. );
  6973. $this->assertTags($result, $expected);
  6974. $this->Form->request->data = array();
  6975. $this->Form->request['controller'] = 'contacts';
  6976. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6977. $result = $this->Form->create(array(
  6978. 'url' => array('action' => 'index', 'param'),
  6979. 'default' => false,
  6980. 'onsubmit' => 'someFunction();'
  6981. ));
  6982. $expected = array(
  6983. 'form' => array(
  6984. 'id' => 'ContactAddForm', 'method' => 'post',
  6985. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6986. 'action' => '/contacts/index/param',
  6987. 'accept-charset' => 'utf-8'
  6988. ),
  6989. 'div' => array('style' => 'display:none;'),
  6990. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6991. '/div'
  6992. );
  6993. $this->assertTags($result, $expected);
  6994. }
  6995. /**
  6996. * test create() with automatic url generation
  6997. *
  6998. * @return void
  6999. */
  7000. public function testCreateAutoUrl() {
  7001. $this->Form->request['action'] = 'delete';
  7002. $this->Form->request->here = '/contacts/delete/10';
  7003. $this->Form->request->base = '';
  7004. $result = $this->Form->create('Contact');
  7005. $expected = array(
  7006. 'form' => array(
  7007. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/contacts/delete/10',
  7008. 'accept-charset' => 'utf-8'
  7009. ),
  7010. 'div' => array('style' => 'display:none;'),
  7011. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7012. '/div'
  7013. );
  7014. $this->assertTags($result, $expected);
  7015. }
  7016. /**
  7017. * test create() with a custom route
  7018. *
  7019. * @return void
  7020. */
  7021. public function testCreateCustomRoute() {
  7022. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  7023. $encoding = strtolower(Configure::read('App.encoding'));
  7024. $result = $this->Form->create('User', array('action' => 'login'));
  7025. $expected = array(
  7026. 'form' => array(
  7027. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  7028. 'accept-charset' => $encoding
  7029. ),
  7030. 'div' => array('style' => 'display:none;'),
  7031. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7032. '/div'
  7033. );
  7034. $this->assertTags($result, $expected);
  7035. }
  7036. /**
  7037. * test that inputDefaults are stored and used.
  7038. *
  7039. * @return void
  7040. */
  7041. public function testCreateWithInputDefaults() {
  7042. $this->Form->create('User', array(
  7043. 'inputDefaults' => array(
  7044. 'div' => false,
  7045. 'label' => false,
  7046. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  7047. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  7048. )
  7049. ));
  7050. $result = $this->Form->input('username');
  7051. $expected = array(
  7052. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  7053. );
  7054. $this->assertTags($result, $expected);
  7055. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  7056. $expected = array(
  7057. 'div' => array('class' => 'input text'),
  7058. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  7059. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7060. '/div'
  7061. );
  7062. $this->assertTags($result, $expected);
  7063. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  7064. $expected = array(
  7065. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7066. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  7067. );
  7068. $this->assertTags($result, $expected);
  7069. $this->Form->create('User', array(
  7070. 'inputDefaults' => array(
  7071. 'div' => false,
  7072. 'label' => array('class' => 'nice', 'for' => 'changed'),
  7073. )
  7074. ));
  7075. $result = $this->Form->input('username', array('div' => true));
  7076. $expected = array(
  7077. 'div' => array('class' => 'input text'),
  7078. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  7079. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  7080. '/div'
  7081. );
  7082. $this->assertTags($result, $expected);
  7083. }
  7084. /**
  7085. * test automatic accept-charset overriding
  7086. *
  7087. * @return void
  7088. */
  7089. public function testCreateWithAcceptCharset() {
  7090. $result = $this->Form->create('UserForm', array(
  7091. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  7092. )
  7093. );
  7094. $expected = array(
  7095. 'form' => array(
  7096. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  7097. 'accept-charset' => 'iso-8859-1'
  7098. ),
  7099. 'div' => array('style' => 'display:none;'),
  7100. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7101. '/div'
  7102. );
  7103. $this->assertTags($result, $expected);
  7104. }
  7105. /**
  7106. * Test base form url when url param is passed with multiple parameters (&)
  7107. *
  7108. */
  7109. public function testCreateQuerystringrequest() {
  7110. $encoding = strtolower(Configure::read('App.encoding'));
  7111. $result = $this->Form->create('Contact', array(
  7112. 'type' => 'post',
  7113. 'escape' => false,
  7114. 'url' => array(
  7115. 'controller' => 'controller',
  7116. 'action' => 'action',
  7117. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7118. )
  7119. ));
  7120. $expected = array(
  7121. 'form' => array(
  7122. 'id' => 'ContactAddForm',
  7123. 'method' => 'post',
  7124. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7125. 'accept-charset' => $encoding
  7126. ),
  7127. 'div' => array('style' => 'display:none;'),
  7128. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7129. '/div'
  7130. );
  7131. $this->assertTags($result, $expected);
  7132. $result = $this->Form->create('Contact', array(
  7133. 'type' => 'post',
  7134. 'url' => array(
  7135. 'controller' => 'controller',
  7136. 'action' => 'action',
  7137. '?' => array('param1' => 'value1', 'param2' => 'value2')
  7138. )
  7139. ));
  7140. $expected = array(
  7141. 'form' => array(
  7142. 'id' => 'ContactAddForm',
  7143. 'method' => 'post',
  7144. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  7145. 'accept-charset' => $encoding
  7146. ),
  7147. 'div' => array('style' => 'display:none;'),
  7148. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7149. '/div'
  7150. );
  7151. $this->assertTags($result, $expected);
  7152. }
  7153. /**
  7154. * test that create() doesn't cause errors by multiple id's being in the primary key
  7155. * as could happen with multiple select or checkboxes.
  7156. *
  7157. * @return void
  7158. */
  7159. public function testCreateWithMultipleIdInData() {
  7160. $encoding = strtolower(Configure::read('App.encoding'));
  7161. $this->Form->request->data['Contact']['id'] = array(1, 2);
  7162. $result = $this->Form->create('Contact');
  7163. $expected = array(
  7164. 'form' => array(
  7165. 'id' => 'ContactAddForm',
  7166. 'method' => 'post',
  7167. 'action' => '/contacts/add',
  7168. 'accept-charset' => $encoding
  7169. ),
  7170. 'div' => array('style' => 'display:none;'),
  7171. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7172. '/div'
  7173. );
  7174. $this->assertTags($result, $expected);
  7175. }
  7176. /**
  7177. * test that create() doesn't add in extra passed params.
  7178. *
  7179. * @return void
  7180. */
  7181. public function testCreatePassedArgs() {
  7182. $encoding = strtolower(Configure::read('App.encoding'));
  7183. $this->Form->request->data['Contact']['id'] = 1;
  7184. $result = $this->Form->create('Contact', array(
  7185. 'type' => 'post',
  7186. 'escape' => false,
  7187. 'url' => array(
  7188. 'action' => 'edit',
  7189. 'myparam'
  7190. )
  7191. ));
  7192. $expected = array(
  7193. 'form' => array(
  7194. 'id' => 'ContactAddForm',
  7195. 'method' => 'post',
  7196. 'action' => '/contacts/edit/myparam',
  7197. 'accept-charset' => $encoding
  7198. ),
  7199. 'div' => array('style' => 'display:none;'),
  7200. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7201. '/div'
  7202. );
  7203. $this->assertTags($result, $expected);
  7204. }
  7205. /**
  7206. * test creating a get form, and get form inputs.
  7207. *
  7208. * @return void
  7209. */
  7210. public function testGetFormCreate() {
  7211. $encoding = strtolower(Configure::read('App.encoding'));
  7212. $result = $this->Form->create('Contact', array('type' => 'get'));
  7213. $this->assertTags($result, array('form' => array(
  7214. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  7215. 'accept-charset' => $encoding
  7216. )));
  7217. $result = $this->Form->text('Contact.name');
  7218. $this->assertTags($result, array('input' => array(
  7219. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  7220. )));
  7221. $result = $this->Form->password('password');
  7222. $this->assertTags($result, array('input' => array(
  7223. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  7224. )));
  7225. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  7226. $result = $this->Form->text('user_form');
  7227. $this->assertTags($result, array('input' => array(
  7228. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  7229. )));
  7230. }
  7231. /**
  7232. * test get form, and inputs when the model param is false
  7233. *
  7234. * @return void
  7235. */
  7236. public function testGetFormWithFalseModel() {
  7237. $encoding = strtolower(Configure::read('App.encoding'));
  7238. $this->Form->request['controller'] = 'contact_test';
  7239. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  7240. $expected = array('form' => array(
  7241. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  7242. 'accept-charset' => $encoding
  7243. ));
  7244. $this->assertTags($result, $expected);
  7245. $result = $this->Form->text('reason');
  7246. $expected = array(
  7247. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  7248. );
  7249. $this->assertTags($result, $expected);
  7250. }
  7251. /**
  7252. * test that datetime() works with GET style forms.
  7253. *
  7254. * @return void
  7255. */
  7256. public function testDateTimeWithGetForms() {
  7257. extract($this->dateRegex);
  7258. $this->Form->create('Contact', array('type' => 'get'));
  7259. $result = $this->Form->datetime('created');
  7260. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  7261. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  7262. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  7263. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  7264. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  7265. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  7266. }
  7267. /**
  7268. * testEditFormWithData method
  7269. *
  7270. * test auto populating form elements from submitted data.
  7271. *
  7272. * @return void
  7273. */
  7274. public function testEditFormWithData() {
  7275. $this->Form->request->data = array('Person' => array(
  7276. 'id' => 1,
  7277. 'first_name' => 'Nate',
  7278. 'last_name' => 'Abele',
  7279. 'email' => 'nate@example.com'
  7280. ));
  7281. $this->Form->request->addParams(array(
  7282. 'models' => array('Person'),
  7283. 'controller' => 'people',
  7284. 'action' => 'add'
  7285. ));
  7286. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  7287. $this->Form->create();
  7288. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  7289. $expected = array(
  7290. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  7291. 'select' => array(
  7292. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  7293. ),
  7294. array('option' => array('value' => 1)), 'Nate', '/option',
  7295. array('option' => array('value' => 2)), 'Garrett', '/option',
  7296. array('option' => array('value' => 3)), 'Larry', '/option',
  7297. '/select'
  7298. );
  7299. $this->assertTags($result, $expected);
  7300. }
  7301. /**
  7302. * Test that required fields are created for various types of validation.
  7303. *
  7304. * @return void
  7305. */
  7306. public function testFormInputRequiredDetection() {
  7307. $this->Form->create('Contact');
  7308. $result = $this->Form->input('Contact.non_existing');
  7309. $expected = array(
  7310. 'div' => array('class' => 'input text'),
  7311. 'label' => array('for' => 'ContactNonExisting'),
  7312. 'Non Existing',
  7313. '/label',
  7314. 'input' => array(
  7315. 'type' => 'text', 'name' => 'Contact[non_existing]',
  7316. 'id' => 'ContactNonExisting'
  7317. ),
  7318. '/div'
  7319. );
  7320. $this->assertTags($result, $expected);
  7321. $result = $this->Form->input('Contact.imrequired');
  7322. $expected = array(
  7323. 'div' => array('class' => 'input text required'),
  7324. 'label' => array('for' => 'ContactImrequired'),
  7325. 'Imrequired',
  7326. '/label',
  7327. 'input' => array(
  7328. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7329. 'id' => 'ContactImrequired',
  7330. 'required' => 'required'
  7331. ),
  7332. '/div'
  7333. );
  7334. $this->assertTags($result, $expected);
  7335. $result = $this->Form->input('Contact.imalsorequired');
  7336. $expected = array(
  7337. 'div' => array('class' => 'input text required'),
  7338. 'label' => array('for' => 'ContactImalsorequired'),
  7339. 'Imalsorequired',
  7340. '/label',
  7341. 'input' => array(
  7342. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  7343. 'id' => 'ContactImalsorequired',
  7344. 'required' => 'required'
  7345. ),
  7346. '/div'
  7347. );
  7348. $this->assertTags($result, $expected);
  7349. $result = $this->Form->input('Contact.imrequiredtoo');
  7350. $expected = array(
  7351. 'div' => array('class' => 'input text required'),
  7352. 'label' => array('for' => 'ContactImrequiredtoo'),
  7353. 'Imrequiredtoo',
  7354. '/label',
  7355. 'input' => array(
  7356. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  7357. 'id' => 'ContactImrequiredtoo',
  7358. 'required' => 'required'
  7359. ),
  7360. '/div'
  7361. );
  7362. $this->assertTags($result, $expected);
  7363. $result = $this->Form->input('Contact.required_one');
  7364. $expected = array(
  7365. 'div' => array('class' => 'input text required'),
  7366. 'label' => array('for' => 'ContactRequiredOne'),
  7367. 'Required One',
  7368. '/label',
  7369. 'input' => array(
  7370. 'type' => 'text', 'name' => 'Contact[required_one]',
  7371. 'id' => 'ContactRequiredOne',
  7372. 'required' => 'required'
  7373. ),
  7374. '/div'
  7375. );
  7376. $this->assertTags($result, $expected);
  7377. $result = $this->Form->input('Contact.string_required');
  7378. $expected = array(
  7379. 'div' => array('class' => 'input text required'),
  7380. 'label' => array('for' => 'ContactStringRequired'),
  7381. 'String Required',
  7382. '/label',
  7383. 'input' => array(
  7384. 'type' => 'text', 'name' => 'Contact[string_required]',
  7385. 'id' => 'ContactStringRequired',
  7386. 'required' => 'required'
  7387. ),
  7388. '/div'
  7389. );
  7390. $this->assertTags($result, $expected);
  7391. $result = $this->Form->input('Contact.imnotrequired');
  7392. $expected = array(
  7393. 'div' => array('class' => 'input text'),
  7394. 'label' => array('for' => 'ContactImnotrequired'),
  7395. 'Imnotrequired',
  7396. '/label',
  7397. 'input' => array(
  7398. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  7399. 'id' => 'ContactImnotrequired'
  7400. ),
  7401. '/div'
  7402. );
  7403. $this->assertTags($result, $expected);
  7404. $result = $this->Form->input('Contact.imalsonotrequired');
  7405. $expected = array(
  7406. 'div' => array('class' => 'input text'),
  7407. 'label' => array('for' => 'ContactImalsonotrequired'),
  7408. 'Imalsonotrequired',
  7409. '/label',
  7410. 'input' => array(
  7411. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  7412. 'id' => 'ContactImalsonotrequired'
  7413. ),
  7414. '/div'
  7415. );
  7416. $this->assertTags($result, $expected);
  7417. $result = $this->Form->input('Contact.imalsonotrequired2');
  7418. $expected = array(
  7419. 'div' => array('class' => 'input text'),
  7420. 'label' => array('for' => 'ContactImalsonotrequired2'),
  7421. 'Imalsonotrequired2',
  7422. '/label',
  7423. 'input' => array(
  7424. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  7425. 'id' => 'ContactImalsonotrequired2'
  7426. ),
  7427. '/div'
  7428. );
  7429. $this->assertTags($result, $expected);
  7430. $result = $this->Form->input('Contact.imnotrequiredeither');
  7431. $expected = array(
  7432. 'div' => array('class' => 'input text'),
  7433. 'label' => array('for' => 'ContactImnotrequiredeither'),
  7434. 'Imnotrequiredeither',
  7435. '/label',
  7436. 'input' => array(
  7437. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  7438. 'id' => 'ContactImnotrequiredeither'
  7439. ),
  7440. '/div'
  7441. );
  7442. $this->assertTags($result, $expected);
  7443. $result = $this->Form->input('Contact.iamrequiredalways');
  7444. $expected = array(
  7445. 'div' => array('class' => 'input text required'),
  7446. 'label' => array('for' => 'ContactIamrequiredalways'),
  7447. 'Iamrequiredalways',
  7448. '/label',
  7449. 'input' => array(
  7450. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  7451. 'id' => 'ContactIamrequiredalways',
  7452. 'required' => 'required'
  7453. ),
  7454. '/div'
  7455. );
  7456. $this->assertTags($result, $expected);
  7457. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  7458. $expected = array(
  7459. 'div' => array('class' => 'input checkbox required'),
  7460. array('input' => array(
  7461. 'type' => 'hidden',
  7462. 'name' => 'Contact[boolean_field]',
  7463. 'id' => 'ContactBooleanField_',
  7464. 'value' => '0'
  7465. )),
  7466. array('input' => array(
  7467. 'type' => 'checkbox',
  7468. 'name' => 'Contact[boolean_field]',
  7469. 'value' => '1',
  7470. 'id' => 'ContactBooleanField'
  7471. )),
  7472. 'label' => array('for' => 'ContactBooleanField'),
  7473. 'Boolean Field',
  7474. '/label',
  7475. '/div'
  7476. );
  7477. $this->assertTags($result, $expected);
  7478. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  7479. $expected = array(
  7480. 'div' => array('class' => 'input checkbox required'),
  7481. array('input' => array(
  7482. 'type' => 'hidden',
  7483. 'name' => 'Contact[boolean_field]',
  7484. 'id' => 'ContactBooleanField_',
  7485. 'value' => '0'
  7486. )),
  7487. array('input' => array(
  7488. 'type' => 'checkbox',
  7489. 'name' => 'Contact[boolean_field]',
  7490. 'value' => '1',
  7491. 'id' => 'ContactBooleanField',
  7492. 'required' => 'required'
  7493. )),
  7494. 'label' => array('for' => 'ContactBooleanField'),
  7495. 'Boolean Field',
  7496. '/label',
  7497. '/div'
  7498. );
  7499. $this->assertTags($result, $expected);
  7500. }
  7501. /**
  7502. * Test that required fields are created when only using ModelValidator::add().
  7503. *
  7504. * @return void
  7505. */
  7506. public function testFormInputRequiredDetectionModelValidator() {
  7507. ClassRegistry::getObject('ContactTag')->validator()->add('iwillberequired', 'required', array('rule' => 'notEmpty'));
  7508. $this->Form->create('ContactTag');
  7509. $result = $this->Form->input('ContactTag.iwillberequired');
  7510. $expected = array(
  7511. 'div' => array('class' => 'input text required'),
  7512. 'label' => array('for' => 'ContactTagIwillberequired'),
  7513. 'Iwillberequired',
  7514. '/label',
  7515. 'input' => array(
  7516. 'name' => 'data[ContactTag][iwillberequired]',
  7517. 'type' => 'text',
  7518. 'id' => 'ContactTagIwillberequired',
  7519. 'required' => 'required'
  7520. ),
  7521. '/div'
  7522. );
  7523. $this->assertTags($result, $expected);
  7524. }
  7525. /**
  7526. * testFormMagicInput method
  7527. *
  7528. * @return void
  7529. */
  7530. public function testFormMagicInput() {
  7531. $encoding = strtolower(Configure::read('App.encoding'));
  7532. $result = $this->Form->create('Contact');
  7533. $expected = array(
  7534. 'form' => array(
  7535. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7536. 'accept-charset' => $encoding
  7537. ),
  7538. 'div' => array('style' => 'display:none;'),
  7539. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7540. '/div'
  7541. );
  7542. $this->assertTags($result, $expected);
  7543. $result = $this->Form->input('name');
  7544. $expected = array(
  7545. 'div' => array('class' => 'input text'),
  7546. 'label' => array('for' => 'ContactName'),
  7547. 'Name',
  7548. '/label',
  7549. 'input' => array(
  7550. 'type' => 'text', 'name' => 'Contact[name]',
  7551. 'id' => 'ContactName', 'maxlength' => '255'
  7552. ),
  7553. '/div'
  7554. );
  7555. $this->assertTags($result, $expected);
  7556. $result = $this->Form->input('non_existing_field_in_contact_model');
  7557. $expected = array(
  7558. 'div' => array('class' => 'input text'),
  7559. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7560. 'Non Existing Field In Contact Model',
  7561. '/label',
  7562. 'input' => array(
  7563. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  7564. 'id' => 'ContactNonExistingFieldInContactModel'
  7565. ),
  7566. '/div'
  7567. );
  7568. $this->assertTags($result, $expected);
  7569. $result = $this->Form->input('Address.street');
  7570. $expected = array(
  7571. 'div' => array('class' => 'input text'),
  7572. 'label' => array('for' => 'AddressStreet'),
  7573. 'Street',
  7574. '/label',
  7575. 'input' => array(
  7576. 'type' => 'text', 'name' => 'Address[street]',
  7577. 'id' => 'AddressStreet'
  7578. ),
  7579. '/div'
  7580. );
  7581. $this->assertTags($result, $expected);
  7582. $result = $this->Form->input('Address.non_existing_field_in_model');
  7583. $expected = array(
  7584. 'div' => array('class' => 'input text'),
  7585. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7586. 'Non Existing Field In Model',
  7587. '/label',
  7588. 'input' => array(
  7589. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  7590. 'id' => 'AddressNonExistingFieldInModel'
  7591. ),
  7592. '/div'
  7593. );
  7594. $this->assertTags($result, $expected);
  7595. $result = $this->Form->input('name', array('div' => false));
  7596. $expected = array(
  7597. 'label' => array('for' => 'ContactName'),
  7598. 'Name',
  7599. '/label',
  7600. 'input' => array(
  7601. 'type' => 'text', 'name' => 'Contact[name]',
  7602. 'id' => 'ContactName', 'maxlength' => '255'
  7603. )
  7604. );
  7605. $this->assertTags($result, $expected);
  7606. extract($this->dateRegex);
  7607. $now = strtotime('now');
  7608. $result = $this->Form->input('Contact.published', array('div' => false));
  7609. $expected = array(
  7610. 'label' => array('for' => 'ContactPublishedMonth'),
  7611. 'Published',
  7612. '/label',
  7613. array('select' => array(
  7614. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  7615. )),
  7616. $monthsRegex,
  7617. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7618. date('F', $now),
  7619. '/option',
  7620. '*/select',
  7621. '-',
  7622. array('select' => array(
  7623. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  7624. )),
  7625. $daysRegex,
  7626. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7627. date('j', $now),
  7628. '/option',
  7629. '*/select',
  7630. '-',
  7631. array('select' => array(
  7632. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  7633. )),
  7634. $yearsRegex,
  7635. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7636. date('Y', $now),
  7637. '*/select'
  7638. );
  7639. $this->assertTags($result, $expected);
  7640. $result = $this->Form->input('Contact.updated', array('div' => false));
  7641. $expected = array(
  7642. 'label' => array('for' => 'ContactUpdatedMonth'),
  7643. 'Updated',
  7644. '/label',
  7645. array('select' => array(
  7646. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  7647. )),
  7648. $monthsRegex,
  7649. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7650. date('F', $now),
  7651. '/option',
  7652. '*/select',
  7653. '-',
  7654. array('select' => array(
  7655. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  7656. )),
  7657. $daysRegex,
  7658. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7659. date('j', $now),
  7660. '/option',
  7661. '*/select',
  7662. '-',
  7663. array('select' => array(
  7664. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  7665. )),
  7666. $yearsRegex,
  7667. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7668. date('Y', $now),
  7669. '*/select'
  7670. );
  7671. $this->assertTags($result, $expected);
  7672. $result = $this->Form->input('UserForm.stuff');
  7673. $expected = array(
  7674. 'div' => array('class' => 'input text'),
  7675. 'label' => array('for' => 'UserFormStuff'),
  7676. 'Stuff',
  7677. '/label',
  7678. 'input' => array(
  7679. 'type' => 'text', 'name' => 'UserForm[stuff]',
  7680. 'id' => 'UserFormStuff', 'maxlength' => 10
  7681. ),
  7682. '/div'
  7683. );
  7684. $this->assertTags($result, $expected);
  7685. }
  7686. /**
  7687. * testForMagicInputNonExistingNorValidated method
  7688. *
  7689. * @return void
  7690. */
  7691. public function testForMagicInputNonExistingNorValidated() {
  7692. $encoding = strtolower(Configure::read('App.encoding'));
  7693. $result = $this->Form->create('Contact');
  7694. $expected = array(
  7695. 'form' => array(
  7696. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7697. 'accept-charset' => $encoding
  7698. ),
  7699. 'div' => array('style' => 'display:none;'),
  7700. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7701. '/div'
  7702. );
  7703. $this->assertTags($result, $expected);
  7704. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7705. $expected = array(
  7706. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7707. 'Non Existing Nor Validated',
  7708. '/label',
  7709. 'input' => array(
  7710. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7711. 'id' => 'ContactNonExistingNorValidated'
  7712. )
  7713. );
  7714. $this->assertTags($result, $expected);
  7715. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7716. 'div' => false, 'value' => 'my value'
  7717. ));
  7718. $expected = array(
  7719. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7720. 'Non Existing Nor Validated',
  7721. '/label',
  7722. 'input' => array(
  7723. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7724. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7725. )
  7726. );
  7727. $this->assertTags($result, $expected);
  7728. $this->Form->request->data = array(
  7729. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7730. ));
  7731. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7732. $expected = array(
  7733. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7734. 'Non Existing Nor Validated',
  7735. '/label',
  7736. 'input' => array(
  7737. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7738. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7739. )
  7740. );
  7741. $this->assertTags($result, $expected);
  7742. }
  7743. /**
  7744. * testFormMagicInputLabel method
  7745. *
  7746. * @return void
  7747. */
  7748. public function testFormMagicInputLabel() {
  7749. $encoding = strtolower(Configure::read('App.encoding'));
  7750. $result = $this->Form->create('Contact');
  7751. $expected = array(
  7752. 'form' => array(
  7753. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7754. 'accept-charset' => $encoding
  7755. ),
  7756. 'div' => array('style' => 'display:none;'),
  7757. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7758. '/div'
  7759. );
  7760. $this->assertTags($result, $expected);
  7761. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7762. $this->assertTags($result, array('input' => array(
  7763. 'name' => 'Contact[name]', 'type' => 'text',
  7764. 'id' => 'ContactName', 'maxlength' => '255')
  7765. ));
  7766. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7767. $expected = array(
  7768. 'label' => array('for' => 'ContactName'),
  7769. 'My label',
  7770. '/label',
  7771. 'input' => array(
  7772. 'type' => 'text', 'name' => 'Contact[name]',
  7773. 'id' => 'ContactName', 'maxlength' => '255'
  7774. )
  7775. );
  7776. $this->assertTags($result, $expected);
  7777. $result = $this->Form->input('Contact.name', array(
  7778. 'div' => false, 'label' => array('class' => 'mandatory')
  7779. ));
  7780. $expected = array(
  7781. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7782. 'Name',
  7783. '/label',
  7784. 'input' => array(
  7785. 'type' => 'text', 'name' => 'Contact[name]',
  7786. 'id' => 'ContactName', 'maxlength' => '255'
  7787. )
  7788. );
  7789. $this->assertTags($result, $expected);
  7790. $result = $this->Form->input('Contact.name', array(
  7791. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7792. ));
  7793. $expected = array(
  7794. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7795. 'My label',
  7796. '/label',
  7797. 'input' => array(
  7798. 'type' => 'text', 'name' => 'Contact[name]',
  7799. 'id' => 'ContactName', 'maxlength' => '255'
  7800. )
  7801. );
  7802. $this->assertTags($result, $expected);
  7803. $result = $this->Form->input('Contact.name', array(
  7804. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7805. ));
  7806. $expected = array(
  7807. 'label' => array('for' => 'my_id'),
  7808. 'Name',
  7809. '/label',
  7810. 'input' => array(
  7811. 'type' => 'text', 'name' => 'Contact[name]',
  7812. 'id' => 'my_id', 'maxlength' => '255'
  7813. )
  7814. );
  7815. $this->assertTags($result, $expected);
  7816. $result = $this->Form->input('1.id');
  7817. $this->assertTags($result, array('input' => array(
  7818. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  7819. 'id' => 'Contact1Id'
  7820. )));
  7821. $result = $this->Form->input("1.name");
  7822. $expected = array(
  7823. 'div' => array('class' => 'input text'),
  7824. 'label' => array('for' => 'Contact1Name'),
  7825. 'Name',
  7826. '/label',
  7827. 'input' => array(
  7828. 'type' => 'text', 'name' => 'Contact[1][name]',
  7829. 'id' => 'Contact1Name', 'maxlength' => '255'
  7830. ),
  7831. '/div'
  7832. );
  7833. $this->assertTags($result, $expected);
  7834. $result = $this->Form->input('Contact.1.id');
  7835. $this->assertTags($result, array(
  7836. 'input' => array(
  7837. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  7838. 'id' => 'Contact1Id'
  7839. )
  7840. ));
  7841. $result = $this->Form->input("Model.1.name");
  7842. $expected = array(
  7843. 'div' => array('class' => 'input text'),
  7844. 'label' => array('for' => 'Model1Name'),
  7845. 'Name',
  7846. '/label',
  7847. 'input' => array(
  7848. 'type' => 'text', 'name' => 'Model[1][name]',
  7849. 'id' => 'Model1Name'
  7850. ),
  7851. '/div'
  7852. );
  7853. $this->assertTags($result, $expected);
  7854. }
  7855. /**
  7856. * testFormEnd method
  7857. *
  7858. * @return void
  7859. */
  7860. public function testFormEnd() {
  7861. $this->assertEquals('</form>', $this->Form->end());
  7862. $result = $this->Form->end('');
  7863. $expected = array(
  7864. 'div' => array('class' => 'submit'),
  7865. 'input' => array('type' => 'submit', 'value' => ''),
  7866. '/div',
  7867. '/form'
  7868. );
  7869. $this->assertTags($result, $expected);
  7870. $result = $this->Form->end(array('label' => ''));
  7871. $expected = array(
  7872. 'div' => array('class' => 'submit'),
  7873. 'input' => array('type' => 'submit', 'value' => ''),
  7874. '/div',
  7875. '/form'
  7876. );
  7877. $this->assertTags($result, $expected);
  7878. $result = $this->Form->end('save');
  7879. $expected = array(
  7880. 'div' => array('class' => 'submit'),
  7881. 'input' => array('type' => 'submit', 'value' => 'save'),
  7882. '/div',
  7883. '/form'
  7884. );
  7885. $this->assertTags($result, $expected);
  7886. $result = $this->Form->end(array('label' => 'save'));
  7887. $expected = array(
  7888. 'div' => array('class' => 'submit'),
  7889. 'input' => array('type' => 'submit', 'value' => 'save'),
  7890. '/div',
  7891. '/form'
  7892. );
  7893. $this->assertTags($result, $expected);
  7894. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7895. $expected = array(
  7896. 'div' => array('class' => 'submit'),
  7897. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7898. '/div',
  7899. '/form'
  7900. );
  7901. $this->assertTags($result, $expected);
  7902. $result = $this->Form->end(array('name' => 'Whatever'));
  7903. $expected = array(
  7904. 'div' => array('class' => 'submit'),
  7905. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7906. '/div',
  7907. '/form'
  7908. );
  7909. $this->assertTags($result, $expected);
  7910. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7911. $expected = array(
  7912. 'div' => array('class' => 'good'),
  7913. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7914. '/div',
  7915. '/form'
  7916. );
  7917. $this->assertTags($result, $expected);
  7918. $result = $this->Form->end(array(
  7919. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7920. ));
  7921. $expected = array(
  7922. 'div' => array('class' => 'good'),
  7923. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7924. '/div',
  7925. '/form'
  7926. );
  7927. $this->assertTags($result, $expected);
  7928. }
  7929. /**
  7930. * testMultipleFormWithIdFields method
  7931. *
  7932. * @return void
  7933. */
  7934. public function testMultipleFormWithIdFields() {
  7935. $this->Form->create('UserForm');
  7936. $result = $this->Form->input('id');
  7937. $this->assertTags($result, array('input' => array(
  7938. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  7939. )));
  7940. $result = $this->Form->input('ValidateItem.id');
  7941. $this->assertTags($result, array('input' => array(
  7942. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  7943. 'id' => 'ValidateItemId'
  7944. )));
  7945. $result = $this->Form->input('ValidateUser.id');
  7946. $this->assertTags($result, array('input' => array(
  7947. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  7948. 'id' => 'ValidateUserId'
  7949. )));
  7950. }
  7951. /**
  7952. * testDbLessModel method
  7953. *
  7954. * @return void
  7955. */
  7956. public function testDbLessModel() {
  7957. $this->Form->create('TestMail');
  7958. $result = $this->Form->input('name');
  7959. $expected = array(
  7960. 'div' => array('class' => 'input text'),
  7961. 'label' => array('for' => 'TestMailName'),
  7962. 'Name',
  7963. '/label',
  7964. 'input' => array(
  7965. 'name' => 'TestMail[name]', 'type' => 'text',
  7966. 'id' => 'TestMailName'
  7967. ),
  7968. '/div'
  7969. );
  7970. $this->assertTags($result, $expected);
  7971. ClassRegistry::init('TestMail');
  7972. $this->Form->create('TestMail');
  7973. $result = $this->Form->input('name');
  7974. $expected = array(
  7975. 'div' => array('class' => 'input text'),
  7976. 'label' => array('for' => 'TestMailName'),
  7977. 'Name',
  7978. '/label',
  7979. 'input' => array(
  7980. 'name' => 'TestMail[name]', 'type' => 'text',
  7981. 'id' => 'TestMailName'
  7982. ),
  7983. '/div'
  7984. );
  7985. $this->assertTags($result, $expected);
  7986. }
  7987. /**
  7988. * testBrokenness method
  7989. *
  7990. * @return void
  7991. */
  7992. public function testBrokenness() {
  7993. /*
  7994. * #4 This test has two parents and four children. By default (as of r7117) both
  7995. * parents are show but the first parent is missing a child. This is the inconsistency
  7996. * in the default behaviour - one parent has all children, the other does not - dependent
  7997. * on the data values.
  7998. */
  7999. $result = $this->Form->select('Model.field', array(
  8000. 'Fred' => array(
  8001. 'freds_son_1' => 'Fred',
  8002. 'freds_son_2' => 'Freddie'
  8003. ),
  8004. 'Bert' => array(
  8005. 'berts_son_1' => 'Albert',
  8006. 'berts_son_2' => 'Bertie')
  8007. ),
  8008. array('showParents' => true, 'empty' => false)
  8009. );
  8010. $expected = array(
  8011. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8012. array('optgroup' => array('label' => 'Fred')),
  8013. array('option' => array('value' => 'freds_son_1')),
  8014. 'Fred',
  8015. '/option',
  8016. array('option' => array('value' => 'freds_son_2')),
  8017. 'Freddie',
  8018. '/option',
  8019. '/optgroup',
  8020. array('optgroup' => array('label' => 'Bert')),
  8021. array('option' => array('value' => 'berts_son_1')),
  8022. 'Albert',
  8023. '/option',
  8024. array('option' => array('value' => 'berts_son_2')),
  8025. 'Bertie',
  8026. '/option',
  8027. '/optgroup',
  8028. '/select'
  8029. );
  8030. $this->assertTags($result, $expected);
  8031. /*
  8032. * #2 This is structurally identical to the test above (#1) - only the parent name has
  8033. * changed, so we should expect the same select list data, just with a different name
  8034. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  8035. * This is where data corruption can occur, because when a select value is missing from
  8036. * a list a form will substitute the first value in the list - without the user knowing.
  8037. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  8038. * affect the availability of 3 => 'Three' as a valid option.
  8039. */
  8040. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  8041. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  8042. ));
  8043. $result = $this->Form->select(
  8044. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  8045. );
  8046. $expected = array(
  8047. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  8048. array('option' => array('value' => 1)),
  8049. 'One',
  8050. '/option',
  8051. array('option' => array('value' => 2)),
  8052. 'Two',
  8053. '/option',
  8054. array('optgroup' => array('label' => 'Three')),
  8055. array('option' => array('value' => 3)),
  8056. 'Three',
  8057. '/option',
  8058. array('option' => array('value' => 4)),
  8059. 'Four',
  8060. '/option',
  8061. array('option' => array('value' => 5)),
  8062. 'Five',
  8063. '/option',
  8064. '/optgroup',
  8065. '/select'
  8066. );
  8067. $this->assertTags($result, $expected);
  8068. }
  8069. /**
  8070. * Test the generation of fields for a multi record form.
  8071. *
  8072. * @return void
  8073. */
  8074. public function testMultiRecordForm() {
  8075. $this->Form->create('ValidateProfile');
  8076. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  8077. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  8078. $expected = array(
  8079. 'div' => array('class' => 'input textarea'),
  8080. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  8081. 'Name',
  8082. '/label',
  8083. 'textarea' => array(
  8084. 'id' => 'ValidateProfile1ValidateItem2Name',
  8085. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  8086. 'cols' => 30,
  8087. 'rows' => 6
  8088. ),
  8089. 'Value',
  8090. '/textarea',
  8091. '/div'
  8092. );
  8093. $this->assertTags($result, $expected);
  8094. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  8095. $expected = array(
  8096. 'div' => array('class' => 'input date'),
  8097. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  8098. 'Created',
  8099. '/label',
  8100. array('select' => array(
  8101. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  8102. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  8103. )
  8104. ),
  8105. array('option' => array('value' => '')), '/option',
  8106. $this->dateRegex['monthsRegex'],
  8107. '/select', '-',
  8108. array('select' => array(
  8109. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  8110. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  8111. )
  8112. ),
  8113. array('option' => array('value' => '')), '/option',
  8114. $this->dateRegex['daysRegex'],
  8115. '/select', '-',
  8116. array('select' => array(
  8117. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  8118. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  8119. )
  8120. ),
  8121. array('option' => array('value' => '')), '/option',
  8122. $this->dateRegex['yearsRegex'],
  8123. '/select',
  8124. '/div'
  8125. );
  8126. $this->assertTags($result, $expected);
  8127. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8128. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  8129. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  8130. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  8131. $expected = array(
  8132. 'div' => array('class' => 'input select error'),
  8133. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  8134. 'Profile',
  8135. '/label',
  8136. 'select' => array(
  8137. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  8138. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  8139. 'class' => 'form-error'
  8140. ),
  8141. '/select',
  8142. array('div' => array('class' => 'error-message')),
  8143. 'Error',
  8144. '/div',
  8145. '/div'
  8146. );
  8147. $this->assertTags($result, $expected);
  8148. }
  8149. /**
  8150. * test the correct display of multi-record form validation errors.
  8151. *
  8152. * @return void
  8153. */
  8154. public function testMultiRecordFormValidationErrors() {
  8155. $this->Form->create('ValidateProfile');
  8156. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  8157. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  8158. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  8159. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8160. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  8161. $result = $this->Form->error('ValidateProfile.2.city');
  8162. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8163. $result = $this->Form->error('2.city');
  8164. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8165. }
  8166. /**
  8167. * test the correct display of multi-record form validation errors.
  8168. *
  8169. * @return void
  8170. */
  8171. public function testSaveManyRecordFormValidationErrors() {
  8172. $this->Form->create('ValidateUser');
  8173. $ValidateUser = ClassRegistry::getObject('ValidateUser');
  8174. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  8175. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  8176. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  8177. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  8178. $result = $this->Form->error('ValidateUser.0.city');
  8179. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  8180. }
  8181. /**
  8182. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  8183. *
  8184. * @return void
  8185. */
  8186. public function testInputTemplate() {
  8187. $result = $this->Form->input('Contact.email', array(
  8188. 'type' => 'text', 'format' => array('input')
  8189. ));
  8190. $expected = array(
  8191. 'div' => array('class' => 'input text'),
  8192. 'input' => array(
  8193. 'type' => 'text', 'name' => 'Contact[email]',
  8194. 'id' => 'ContactEmail'
  8195. ),
  8196. '/div'
  8197. );
  8198. $this->assertTags($result, $expected);
  8199. $result = $this->Form->input('Contact.email', array(
  8200. 'type' => 'text', 'format' => array('input', 'label'),
  8201. 'label' => '<em>Email (required)</em>'
  8202. ));
  8203. $expected = array(
  8204. 'div' => array('class' => 'input text'),
  8205. array('input' => array(
  8206. 'type' => 'text', 'name' => 'Contact[email]',
  8207. 'id' => 'ContactEmail'
  8208. )),
  8209. 'label' => array('for' => 'ContactEmail'),
  8210. 'em' => array(),
  8211. 'Email (required)',
  8212. '/em',
  8213. '/label',
  8214. '/div'
  8215. );
  8216. $this->assertTags($result, $expected);
  8217. $result = $this->Form->input('Contact.email', array(
  8218. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  8219. 'between' => '<div>Something in the middle</div>',
  8220. 'after' => '<span>Some text at the end</span>'
  8221. ));
  8222. $expected = array(
  8223. 'div' => array('class' => 'input text'),
  8224. array('input' => array(
  8225. 'type' => 'text', 'name' => 'Contact[email]',
  8226. 'id' => 'ContactEmail'
  8227. )),
  8228. array('div' => array()),
  8229. 'Something in the middle',
  8230. '/div',
  8231. 'label' => array('for' => 'ContactEmail'),
  8232. 'Email',
  8233. '/label',
  8234. 'span' => array(),
  8235. 'Some text at the end',
  8236. '/span',
  8237. '/div'
  8238. );
  8239. $this->assertTags($result, $expected);
  8240. $result = $this->Form->input('Contact.method', array(
  8241. 'type' => 'radio',
  8242. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  8243. 'between' => 'I am between',
  8244. ));
  8245. $expected = array(
  8246. 'div' => array('class' => 'input radio'),
  8247. 'fieldset' => array(),
  8248. 'legend' => array(),
  8249. 'Method',
  8250. '/legend',
  8251. 'I am between',
  8252. 'input' => array(
  8253. 'type' => 'hidden', 'name' => 'Contact[method]',
  8254. 'value' => '', 'id' => 'ContactMethod_'
  8255. ),
  8256. array('input' => array(
  8257. 'type' => 'radio', 'name' => 'Contact[method]',
  8258. 'value' => 'email', 'id' => 'ContactMethodEmail'
  8259. )),
  8260. array('label' => array('for' => 'ContactMethodEmail')),
  8261. 'Email',
  8262. '/label',
  8263. array('input' => array(
  8264. 'type' => 'radio', 'name' => 'Contact[method]',
  8265. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  8266. )),
  8267. array('label' => array('for' => 'ContactMethodPigeon')),
  8268. 'Pigeon',
  8269. '/label',
  8270. '/fieldset',
  8271. '/div',
  8272. );
  8273. $this->assertTags($result, $expected);
  8274. }
  8275. /**
  8276. * test that some html5 inputs + FormHelper::__call() work
  8277. *
  8278. * @return void
  8279. */
  8280. public function testHtml5Inputs() {
  8281. $result = $this->Form->email('User.email');
  8282. $expected = array(
  8283. 'input' => array('type' => 'email', 'name' => 'User[email]', 'id' => 'UserEmail')
  8284. );
  8285. $this->assertTags($result, $expected);
  8286. $result = $this->Form->search('User.query');
  8287. $expected = array(
  8288. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery')
  8289. );
  8290. $this->assertTags($result, $expected);
  8291. $result = $this->Form->search('User.query', array('value' => 'test'));
  8292. $expected = array(
  8293. 'input' => array('type' => 'search', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8294. );
  8295. $this->assertTags($result, $expected);
  8296. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  8297. $expected = array(
  8298. 'input' => array('type' => 'text', 'name' => 'User[query]', 'id' => 'UserQuery', 'value' => 'test')
  8299. );
  8300. $this->assertTags($result, $expected);
  8301. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  8302. $expected = array(
  8303. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  8304. );
  8305. $this->assertTags($result, $expected);
  8306. }
  8307. /**
  8308. *
  8309. * @expectedException Cake\Error\Exception
  8310. * @return void
  8311. */
  8312. public function testHtml5InputException() {
  8313. $this->Form->email();
  8314. }
  8315. /**
  8316. * Tests that a model can be loaded from the model names passed in the request object
  8317. *
  8318. * @return void
  8319. */
  8320. public function testIntrospectModelFromRequest() {
  8321. $this->loadFixtures('Post');
  8322. App::build(array(
  8323. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  8324. ));
  8325. Plugin::load('TestPlugin');
  8326. $this->Form->request['models'] = array(
  8327. 'TestPluginPost' => array(
  8328. 'plugin' => 'TestPlugin',
  8329. 'className' => 'TestPluginPost'
  8330. )
  8331. );
  8332. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  8333. $this->Form->create('TestPluginPost');
  8334. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  8335. $this->assertInstanceOf('TestPlugin\Model\TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  8336. Plugin::unload();
  8337. }
  8338. /**
  8339. * Tests that it is possible to set the validation errors directly in the helper for a field
  8340. *
  8341. * @return void
  8342. */
  8343. public function testCustomValidationErrors() {
  8344. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  8345. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  8346. $this->assertEquals('Badness!', $result);
  8347. }
  8348. /**
  8349. * Tests that the 'on' key validates as expected on create
  8350. *
  8351. * @return void
  8352. */
  8353. public function testRequiredOnCreate() {
  8354. $this->Form->create('Contact');
  8355. $result = $this->Form->input('Contact.imrequiredonupdate');
  8356. $expected = array(
  8357. 'div' => array('class' => 'input text'),
  8358. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8359. 'Imrequiredonupdate',
  8360. '/label',
  8361. 'input' => array(
  8362. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8363. 'id' => 'ContactImrequiredonupdate'
  8364. ),
  8365. '/div'
  8366. );
  8367. $this->assertTags($result, $expected);
  8368. $result = $this->Form->input('Contact.imrequiredoncreate');
  8369. $expected = array(
  8370. 'div' => array('class' => 'input text required'),
  8371. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8372. 'Imrequiredoncreate',
  8373. '/label',
  8374. 'input' => array(
  8375. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8376. 'id' => 'ContactImrequiredoncreate',
  8377. 'required' => 'required'
  8378. ),
  8379. '/div'
  8380. );
  8381. $this->assertTags($result, $expected);
  8382. $result = $this->Form->input('Contact.imrequiredonboth');
  8383. $expected = array(
  8384. 'div' => array('class' => 'input text required'),
  8385. 'label' => array('for' => 'ContactImrequiredonboth'),
  8386. 'Imrequiredonboth',
  8387. '/label',
  8388. 'input' => array(
  8389. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8390. 'id' => 'ContactImrequiredonboth',
  8391. 'required' => 'required'
  8392. ),
  8393. '/div'
  8394. );
  8395. $this->assertTags($result, $expected);
  8396. $this->Form->inputDefaults(array('required' => false));
  8397. $result = $this->Form->input('Contact.imrequired');
  8398. $expected = array(
  8399. 'div' => array('class' => 'input text'),
  8400. 'label' => array('for' => 'ContactImrequired'),
  8401. 'Imrequired',
  8402. '/label',
  8403. 'input' => array(
  8404. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8405. 'id' => 'ContactImrequired'
  8406. ),
  8407. '/div'
  8408. );
  8409. $this->assertTags($result, $expected);
  8410. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  8411. $this->assertTags($result, $expected);
  8412. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  8413. $expected = array(
  8414. 'div' => array('class' => 'input text required'),
  8415. 'label' => array('for' => 'ContactImrequired'),
  8416. 'Imrequired',
  8417. '/label',
  8418. 'input' => array(
  8419. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  8420. 'id' => 'ContactImrequired'
  8421. ),
  8422. '/div'
  8423. );
  8424. $this->assertTags($result, $expected);
  8425. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  8426. $this->assertTags($result, $expected);
  8427. }
  8428. /**
  8429. * Tests that the 'on' key validates as expected on update
  8430. *
  8431. * @return void
  8432. */
  8433. public function testRequiredOnUpdate() {
  8434. $this->Form->request->data['Contact']['id'] = 1;
  8435. $this->Form->create('Contact');
  8436. $result = $this->Form->input('Contact.imrequiredonupdate');
  8437. $expected = array(
  8438. 'div' => array('class' => 'input text required'),
  8439. 'label' => array('for' => 'ContactImrequiredonupdate'),
  8440. 'Imrequiredonupdate',
  8441. '/label',
  8442. 'input' => array(
  8443. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  8444. 'id' => 'ContactImrequiredonupdate',
  8445. 'required' => 'required'
  8446. ),
  8447. '/div'
  8448. );
  8449. $this->assertTags($result, $expected);
  8450. $result = $this->Form->input('Contact.imrequiredoncreate');
  8451. $expected = array(
  8452. 'div' => array('class' => 'input text'),
  8453. 'label' => array('for' => 'ContactImrequiredoncreate'),
  8454. 'Imrequiredoncreate',
  8455. '/label',
  8456. 'input' => array(
  8457. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8458. 'id' => 'ContactImrequiredoncreate'
  8459. ),
  8460. '/div'
  8461. );
  8462. $this->assertTags($result, $expected);
  8463. $result = $this->Form->input('Contact.imrequiredonboth');
  8464. $expected = array(
  8465. 'div' => array('class' => 'input text required'),
  8466. 'label' => array('for' => 'ContactImrequiredonboth'),
  8467. 'Imrequiredonboth',
  8468. '/label',
  8469. 'input' => array(
  8470. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8471. 'id' => 'ContactImrequiredonboth',
  8472. 'required' => 'required'
  8473. ),
  8474. '/div'
  8475. );
  8476. $this->assertTags($result, $expected);
  8477. $result = $this->Form->input('Contact.imrequired');
  8478. $expected = array(
  8479. 'div' => array('class' => 'input text required'),
  8480. 'label' => array('for' => 'ContactImrequired'),
  8481. 'Imrequired',
  8482. '/label',
  8483. 'input' => array(
  8484. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8485. 'id' => 'ContactImrequired',
  8486. 'required' => 'required'
  8487. ),
  8488. '/div'
  8489. );
  8490. $this->assertTags($result, $expected);
  8491. }
  8492. /**
  8493. * Test inputDefaults setter and getter
  8494. *
  8495. * @return void
  8496. */
  8497. public function testInputDefaults() {
  8498. $this->Form->create('Contact');
  8499. $this->Form->inputDefaults(array(
  8500. 'label' => false,
  8501. 'div' => array(
  8502. 'style' => 'color: #000;'
  8503. )
  8504. ));
  8505. $result = $this->Form->input('Contact.field1');
  8506. $expected = array(
  8507. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8508. 'input' => array(
  8509. 'type' => 'text', 'name' => 'Contact[field1]',
  8510. 'id' => 'ContactField1'
  8511. ),
  8512. '/div'
  8513. );
  8514. $this->assertTags($result, $expected);
  8515. $this->Form->inputDefaults(array(
  8516. 'div' => false,
  8517. 'label' => 'Label',
  8518. ));
  8519. $result = $this->Form->input('Contact.field1');
  8520. $expected = array(
  8521. 'label' => array('for' => 'ContactField1'),
  8522. 'Label',
  8523. '/label',
  8524. 'input' => array(
  8525. 'type' => 'text', 'name' => 'Contact[field1]',
  8526. 'id' => 'ContactField1'
  8527. ),
  8528. );
  8529. $this->assertTags($result, $expected);
  8530. $this->Form->inputDefaults(array(
  8531. 'label' => false,
  8532. ), true);
  8533. $result = $this->Form->input('Contact.field1');
  8534. $expected = array(
  8535. 'input' => array(
  8536. 'type' => 'text', 'name' => 'Contact[field1]',
  8537. 'id' => 'ContactField1'
  8538. ),
  8539. );
  8540. $this->assertTags($result, $expected);
  8541. $result = $this->Form->inputDefaults();
  8542. $expected = array(
  8543. 'div' => false,
  8544. 'label' => false,
  8545. );
  8546. $this->assertEquals($expected, $result);
  8547. }
  8548. }