FormHelperTest.php 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632
  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 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View\Helper;
  16. use Cake\Collection\Collection;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Network\Request;
  22. use Cake\ORM\Entity;
  23. use Cake\ORM\Table;
  24. use Cake\ORM\TableRegistry;
  25. use Cake\Routing\Router;
  26. use Cake\TestSuite\TestCase;
  27. use Cake\Utility\Security;
  28. use Cake\View\Helper\FormHelper;
  29. use Cake\View\Helper\HtmlHelper;
  30. use Cake\View\View;
  31. /**
  32. * Test stub.
  33. */
  34. class Article extends Entity {
  35. }
  36. /**
  37. * Contact class
  38. *
  39. */
  40. class ContactsTable extends Table {
  41. /**
  42. * Default schema
  43. *
  44. * @var array
  45. */
  46. protected $_schema = array(
  47. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  48. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  49. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  50. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  51. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  52. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  53. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  54. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  55. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null),
  56. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  57. );
  58. /**
  59. * Initializes the schema
  60. *
  61. * @return void
  62. */
  63. public function initialize(array $config) {
  64. $this->schema($this->_schema);
  65. }
  66. }
  67. /**
  68. * ValidateUser class
  69. *
  70. */
  71. class ValidateUsersTable extends Table {
  72. /**
  73. * schema method
  74. *
  75. * @var array
  76. */
  77. protected $_schema = array(
  78. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  79. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  80. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'balance' => array('type' => 'float', 'null' => false, 'length' => 5, 'precision' => 2),
  82. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => 6, 'precision' => 3),
  83. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => 10, 'precision' => 6),
  84. 'population' => array('type' => 'decimal', 'null' => false, 'length' => 15, 'precision' => 0),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. '_constraints' => array('primary' => ['type' => 'primary', 'columns' => ['id']])
  88. );
  89. /**
  90. * Initializes the schema
  91. *
  92. * @return void
  93. */
  94. public function initialize(array $config) {
  95. $this->schema($this->_schema);
  96. }
  97. }
  98. /**
  99. * FormHelperTest class
  100. *
  101. * @property FormHelper $Form
  102. */
  103. class FormHelperTest extends TestCase {
  104. /**
  105. * Fixtures to be used
  106. *
  107. * @var array
  108. */
  109. public $fixtures = array('core.articles', 'core.comments');
  110. /**
  111. * Do not load the fixtures by default
  112. *
  113. * @var bool
  114. */
  115. public $autoFixtures = false;
  116. /**
  117. * setUp method
  118. *
  119. * @return void
  120. */
  121. public function setUp() {
  122. parent::setUp();
  123. Configure::write('Config.language', 'eng');
  124. Configure::write('App.base', '');
  125. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  126. Configure::delete('Asset');
  127. $this->View = new View();
  128. $this->Form = new FormHelper($this->View);
  129. $request = new Request('articles/add');
  130. $request->here = '/articles/add';
  131. $request['controller'] = 'articles';
  132. $request['action'] = 'add';
  133. $request->webroot = '';
  134. $request->base = '';
  135. $this->Form->Url->request = $this->Form->request = $request;
  136. $this->dateRegex = array(
  137. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  138. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  139. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  140. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  141. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  142. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  143. );
  144. $this->article = [
  145. 'schema' => [
  146. 'id' => ['type' => 'integer'],
  147. 'author_id' => ['type' => 'integer', 'null' => true],
  148. 'title' => ['type' => 'string', 'null' => true],
  149. 'body' => 'text',
  150. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  151. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  152. ],
  153. 'required' => [
  154. 'author_id' => true,
  155. 'title' => true,
  156. ]
  157. ];
  158. Security::salt('foo!');
  159. Router::connect('/:controller', array('action' => 'index'));
  160. Router::connect('/:controller/:action/*');
  161. }
  162. /**
  163. * tearDown method
  164. *
  165. * @return void
  166. */
  167. public function tearDown() {
  168. parent::tearDown();
  169. unset($this->Form, $this->Controller, $this->View);
  170. TableRegistry::clear();
  171. }
  172. /**
  173. * Test construct() with the templates option.
  174. *
  175. * @return void
  176. */
  177. public function testConstructTemplatesFile() {
  178. $helper = new FormHelper($this->View, [
  179. 'templates' => 'htmlhelper_tags.php'
  180. ]);
  181. $result = $helper->input('name');
  182. $this->assertContains('<input', $result);
  183. }
  184. /**
  185. * Test that when specifying custom widgets the config array for that widget
  186. * is overwritten instead of merged.
  187. *
  188. * @return void
  189. */
  190. public function testConstructWithWidgets() {
  191. $config = [
  192. 'widgets' => [
  193. 'datetime' => ['Cake\View\Widget\LabelWidget', 'select']
  194. ]
  195. ];
  196. $helper = new FormHelper($this->View, $config);
  197. $registry = $helper->widgetRegistry();
  198. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('datetime'));
  199. }
  200. /**
  201. * Test that when specifying custom widgets config file and it should be
  202. * added to widgets array. WidgetRegistry will load widgets in constructor.
  203. *
  204. * @return void
  205. */
  206. public function testConstructWithWidgetsConfig() {
  207. $helper = new FormHelper($this->View, ['widgets' => ['test_widgets']]);
  208. $registry = $helper->widgetRegistry();
  209. $this->assertInstanceOf('Cake\View\Widget\LabelWidget', $registry->get('text'));
  210. }
  211. /**
  212. * Test registering a new widget class and rendering it.
  213. *
  214. * @return void
  215. */
  216. public function testAddWidgetAndRenderWidget() {
  217. $data = [
  218. 'val' => 1
  219. ];
  220. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  221. $this->assertNull($this->Form->addWidget('test', $mock));
  222. $mock->expects($this->once())
  223. ->method('render')
  224. ->with($data)
  225. ->will($this->returnValue('HTML'));
  226. $result = $this->Form->widget('test', $data);
  227. $this->assertEquals('HTML', $result);
  228. }
  229. /**
  230. * Test registering an invalid widget class.
  231. *
  232. * @expectedException \RuntimeException
  233. * @return void
  234. */
  235. public function testAddWidgetInvalid() {
  236. $mock = new \StdClass();
  237. $this->Form->addWidget('test', $mock);
  238. $this->Form->widget('test');
  239. }
  240. /**
  241. * Test adding a new context class.
  242. *
  243. * @return void
  244. */
  245. public function testAddContextProvider() {
  246. $context = 'My data';
  247. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  248. $this->Form->addContextProvider('test', function ($request, $data) use ($context, $stub) {
  249. $this->assertInstanceOf('Cake\Network\Request', $request);
  250. $this->assertEquals($context, $data['entity']);
  251. return $stub;
  252. });
  253. $this->Form->create($context);
  254. $result = $this->Form->context();
  255. $this->assertSame($stub, $result);
  256. }
  257. /**
  258. * Test replacing a context class.
  259. *
  260. * @return void
  261. */
  262. public function testAddContextProviderReplace() {
  263. $entity = new Article();
  264. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  265. $this->Form->addContextProvider('orm', function ($request, $data) use ($stub) {
  266. return $stub;
  267. });
  268. $this->Form->create($entity);
  269. $result = $this->Form->context();
  270. $this->assertSame($stub, $result);
  271. }
  272. /**
  273. * Test overriding a context class.
  274. *
  275. * @return void
  276. */
  277. public function testAddContextProviderAdd() {
  278. $entity = new Article();
  279. $stub = $this->getMock('Cake\View\Form\ContextInterface');
  280. $this->Form->addContextProvider('newshiny', function ($request, $data) use ($stub) {
  281. if ($data['entity'] instanceof Entity) {
  282. return $stub;
  283. }
  284. });
  285. $this->Form->create($entity);
  286. $result = $this->Form->context();
  287. $this->assertSame($stub, $result);
  288. }
  289. /**
  290. * Test adding an invalid context class.
  291. *
  292. * @expectedException \RuntimeException
  293. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  294. * @return void
  295. */
  296. public function testAddContextProviderInvalid() {
  297. $context = 'My data';
  298. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  299. return new \StdClass();
  300. });
  301. $this->Form->create($context);
  302. }
  303. /**
  304. * Provides context options for create().
  305. *
  306. * @return array
  307. */
  308. public function contextSelectionProvider() {
  309. $entity = new Article();
  310. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  311. $emptyCollection = new Collection([]);
  312. $emptyArray = [];
  313. $arrayObject = new \ArrayObject([]);
  314. $data = [
  315. 'schema' => [
  316. 'title' => ['type' => 'string']
  317. ]
  318. ];
  319. return [
  320. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  321. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  322. 'empty_collection' => [$emptyCollection, 'Cake\View\Form\NullContext'],
  323. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  324. 'array_object' => [$arrayObject, 'Cake\View\Form\NullContext'],
  325. 'none' => [null, 'Cake\View\Form\NullContext'],
  326. 'false' => [false, 'Cake\View\Form\NullContext'],
  327. ];
  328. }
  329. /**
  330. * Test default context selection in create()
  331. *
  332. * @dataProvider contextSelectionProvider
  333. * @return void
  334. */
  335. public function testCreateContextSelectionBuiltIn($data, $class) {
  336. $this->loadFixtures('Articles');
  337. $this->Form->create($data);
  338. $this->assertInstanceOf($class, $this->Form->context());
  339. }
  340. /**
  341. * Data provider for type option.
  342. *
  343. * @return array
  344. */
  345. public static function requestTypeProvider() {
  346. return [
  347. // type, method, override
  348. ['post', 'post', 'POST'],
  349. ['put', 'post', 'PUT'],
  350. ['patch', 'post', 'PATCH'],
  351. ['delete', 'post', 'DELETE'],
  352. ];
  353. }
  354. /**
  355. * Test creating file forms.
  356. *
  357. * @return void
  358. */
  359. public function testCreateFile() {
  360. $encoding = strtolower(Configure::read('App.encoding'));
  361. $result = $this->Form->create(false, array('type' => 'file'));
  362. $expected = array(
  363. 'form' => array(
  364. 'method' => 'post', 'action' => '/articles/add',
  365. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  366. ),
  367. 'div' => array('style' => 'display:none;'),
  368. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  369. '/div'
  370. );
  371. $this->assertHtml($expected, $result);
  372. }
  373. /**
  374. * Test creating GET forms.
  375. *
  376. * @return void
  377. */
  378. public function testCreateGet() {
  379. $encoding = strtolower(Configure::read('App.encoding'));
  380. $result = $this->Form->create(false, array('type' => 'get'));
  381. $expected = array('form' => array(
  382. 'method' => 'get', 'action' => '/articles/add',
  383. 'accept-charset' => $encoding
  384. ));
  385. $this->assertHtml($expected, $result);
  386. }
  387. /**
  388. * Test create() with the templates option.
  389. *
  390. * @return void
  391. */
  392. public function testCreateTemplatesArray() {
  393. $result = $this->Form->create($this->article, [
  394. 'templates' => [
  395. 'formstart' => '<form class="form-horizontal"{{attrs}}>',
  396. ]
  397. ]);
  398. $expected = [
  399. 'form' => [
  400. 'class' => 'form-horizontal',
  401. 'method' => 'post',
  402. 'action' => '/articles/add',
  403. 'accept-charset' => 'utf-8'
  404. ]
  405. ];
  406. $this->assertHtml($expected, $result);
  407. }
  408. /**
  409. * Test create() with the templates option.
  410. *
  411. * @return void
  412. */
  413. public function testCreateTemplatesFile() {
  414. $result = $this->Form->create($this->article, [
  415. 'templates' => 'htmlhelper_tags.php',
  416. ]);
  417. $expected = [
  418. 'start form',
  419. 'div' => ['class' => 'hidden'],
  420. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  421. '/div'
  422. ];
  423. $this->assertHtml($expected, $result);
  424. }
  425. /**
  426. * Test that create() and end() restore templates.
  427. *
  428. * @return void
  429. */
  430. public function testCreateEndRestoreTemplates() {
  431. $this->Form->create($this->article, [
  432. 'templates' => ['input' => 'custom input element']
  433. ]);
  434. $this->Form->end();
  435. $this->assertNotEquals('custom input element', $this->Form->templater()->get('input'));
  436. }
  437. /**
  438. * test the create() method
  439. *
  440. * @dataProvider requestTypeProvider
  441. * @return void
  442. */
  443. public function testCreateTypeOptions($type, $method, $override) {
  444. $encoding = strtolower(Configure::read('App.encoding'));
  445. $result = $this->Form->create(false, array('type' => $type));
  446. $expected = array(
  447. 'form' => array(
  448. 'method' => $method, 'action' => '/articles/add',
  449. 'accept-charset' => $encoding
  450. ),
  451. 'div' => array('style' => 'display:none;'),
  452. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  453. '/div'
  454. );
  455. $this->assertHtml($expected, $result);
  456. }
  457. /**
  458. * Test opening a form for an update operation.
  459. *
  460. * @return void
  461. */
  462. public function testCreateUpdateForm() {
  463. $encoding = strtolower(Configure::read('App.encoding'));
  464. $this->Form->request->here = '/articles/edit/1';
  465. $this->Form->request['action'] = 'edit';
  466. $this->article['defaults']['id'] = 1;
  467. $result = $this->Form->create($this->article);
  468. $expected = array(
  469. 'form' => array(
  470. 'method' => 'post', 'action' => '/articles/edit/1',
  471. 'accept-charset' => $encoding
  472. ),
  473. 'div' => array('style' => 'display:none;'),
  474. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  475. '/div'
  476. );
  477. $this->assertHtml($expected, $result);
  478. }
  479. /**
  480. * test create() with automatic url generation
  481. *
  482. * @return void
  483. */
  484. public function testCreateAutoUrl() {
  485. $encoding = strtolower(Configure::read('App.encoding'));
  486. $this->Form->request['action'] = 'delete';
  487. $this->Form->request->here = '/articles/delete/10';
  488. $this->Form->request->base = '';
  489. $result = $this->Form->create($this->article);
  490. $expected = array(
  491. 'form' => array(
  492. 'method' => 'post', 'action' => '/articles/delete/10',
  493. 'accept-charset' => $encoding
  494. ),
  495. 'div' => array('style' => 'display:none;'),
  496. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  497. '/div'
  498. );
  499. $this->assertHtml($expected, $result);
  500. $this->article['defaults'] = ['id' => 1];
  501. $this->Form->request->here = '/articles/edit/1';
  502. $this->Form->request['action'] = 'delete';
  503. $result = $this->Form->create($this->article, ['action' => 'edit']);
  504. $expected = array(
  505. 'form' => array(
  506. 'method' => 'post',
  507. 'action' => '/articles/edit/1',
  508. 'accept-charset' => $encoding
  509. ),
  510. 'div' => array('style' => 'display:none;'),
  511. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  512. '/div'
  513. );
  514. $this->assertHtml($expected, $result);
  515. $this->Form->request['action'] = 'add';
  516. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  517. $expected = array(
  518. 'form' => array(
  519. 'method' => 'post',
  520. 'action' => '/articles/publish/1',
  521. 'accept-charset' => $encoding
  522. ),
  523. 'div' => array('style' => 'display:none;'),
  524. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  525. '/div'
  526. );
  527. $this->assertHtml($expected, $result);
  528. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  529. $expected = array(
  530. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  531. 'div' => array('style' => 'display:none;'),
  532. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  533. '/div'
  534. );
  535. $this->assertHtml($expected, $result);
  536. $this->Form->request['controller'] = 'Pages';
  537. $result = $this->Form->create($this->article, array('action' => 'signup'));
  538. $expected = array(
  539. 'form' => array(
  540. 'method' => 'post', 'action' => '/Pages/signup/1',
  541. 'accept-charset' => $encoding
  542. ),
  543. 'div' => array('style' => 'display:none;'),
  544. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  545. '/div'
  546. );
  547. $this->assertHtml($expected, $result);
  548. }
  549. /**
  550. * test create() with a custom route
  551. *
  552. * @return void
  553. */
  554. public function testCreateCustomRoute() {
  555. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  556. $encoding = strtolower(Configure::read('App.encoding'));
  557. $this->Form->request['controller'] = 'users';
  558. $result = $this->Form->create(false, array('action' => 'login'));
  559. $expected = array(
  560. 'form' => array(
  561. 'method' => 'post', 'action' => '/login',
  562. 'accept-charset' => $encoding
  563. ),
  564. 'div' => array('style' => 'display:none;'),
  565. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  566. '/div'
  567. );
  568. $this->assertHtml($expected, $result);
  569. Router::connect(
  570. '/new-article',
  571. ['controller' => 'articles', 'action' => 'myaction'],
  572. ['_name' => 'my-route']
  573. );
  574. $result = $this->Form->create(false, ['url' => ['_name' => 'my-route']]);
  575. $expected = array(
  576. 'form' => array(
  577. 'method' => 'post', 'action' => '/new-article',
  578. 'accept-charset' => $encoding,
  579. ),
  580. 'div' => array('style' => 'display:none;'),
  581. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  582. '/div'
  583. );
  584. $this->assertHtml($expected, $result);
  585. }
  586. /**
  587. * test automatic accept-charset overriding
  588. *
  589. * @return void
  590. */
  591. public function testCreateWithAcceptCharset() {
  592. $result = $this->Form->create($this->article, array(
  593. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  594. )
  595. );
  596. $expected = array(
  597. 'form' => array(
  598. 'method' => 'post', 'action' => '/articles',
  599. 'accept-charset' => 'iso-8859-1'
  600. ),
  601. 'div' => array('style' => 'display:none;'),
  602. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  603. '/div'
  604. );
  605. $this->assertHtml($expected, $result);
  606. }
  607. /**
  608. * Test base form URL when url param is passed with multiple parameters (&)
  609. *
  610. */
  611. public function testCreateQuerystringrequest() {
  612. $encoding = strtolower(Configure::read('App.encoding'));
  613. $result = $this->Form->create($this->article, array(
  614. 'type' => 'post',
  615. 'escape' => false,
  616. 'url' => array(
  617. 'controller' => 'controller',
  618. 'action' => 'action',
  619. '?' => array('param1' => 'value1', 'param2' => 'value2')
  620. )
  621. ));
  622. $expected = array(
  623. 'form' => array(
  624. 'method' => 'post',
  625. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  626. 'accept-charset' => $encoding
  627. ),
  628. 'div' => array('style' => 'display:none;'),
  629. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  630. '/div'
  631. );
  632. $this->assertHtml($expected, $result);
  633. $result = $this->Form->create($this->article, array(
  634. 'type' => 'post',
  635. 'url' => array(
  636. 'controller' => 'controller',
  637. 'action' => 'action',
  638. '?' => array('param1' => 'value1', 'param2' => 'value2')
  639. )
  640. ));
  641. $this->assertHtml($expected, $result);
  642. }
  643. /**
  644. * test that create() doesn't cause errors by multiple id's being in the primary key
  645. * as could happen with multiple select or checkboxes.
  646. *
  647. * @return void
  648. */
  649. public function testCreateWithMultipleIdInData() {
  650. $encoding = strtolower(Configure::read('App.encoding'));
  651. $this->Form->request->data['Article']['id'] = array(1, 2);
  652. $result = $this->Form->create($this->article);
  653. $expected = array(
  654. 'form' => array(
  655. 'method' => 'post',
  656. 'action' => '/articles/add',
  657. 'accept-charset' => $encoding
  658. ),
  659. 'div' => array('style' => 'display:none;'),
  660. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  661. '/div'
  662. );
  663. $this->assertHtml($expected, $result);
  664. }
  665. /**
  666. * test that create() doesn't add in extra passed params.
  667. *
  668. * @return void
  669. */
  670. public function testCreatePassedArgs() {
  671. $encoding = strtolower(Configure::read('App.encoding'));
  672. $this->Form->request->data['Article']['id'] = 1;
  673. $result = $this->Form->create($this->article, array(
  674. 'type' => 'post',
  675. 'escape' => false,
  676. 'url' => array(
  677. 'action' => 'edit',
  678. 'myparam'
  679. )
  680. ));
  681. $expected = array(
  682. 'form' => array(
  683. 'method' => 'post',
  684. 'action' => '/articles/edit/myparam',
  685. 'accept-charset' => $encoding
  686. ),
  687. 'div' => array('style' => 'display:none;'),
  688. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  689. '/div'
  690. );
  691. $this->assertHtml($expected, $result);
  692. }
  693. /**
  694. * test creating a get form, and get form inputs.
  695. *
  696. * @return void
  697. */
  698. public function testGetFormCreate() {
  699. $encoding = strtolower(Configure::read('App.encoding'));
  700. $result = $this->Form->create($this->article, array('type' => 'get'));
  701. $expected = array('form' => array(
  702. 'method' => 'get', 'action' => '/articles/add',
  703. 'accept-charset' => $encoding
  704. ));
  705. $this->assertHtml($expected, $result);
  706. $result = $this->Form->text('title');
  707. $expected = array('input' => array(
  708. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  709. ));
  710. $this->assertHtml($expected, $result);
  711. $result = $this->Form->password('password');
  712. $expected = array('input' => array(
  713. 'name' => 'password', 'type' => 'password'
  714. ));
  715. $this->assertHtml($expected, $result);
  716. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  717. $result = $this->Form->text('user_form');
  718. $expected = array('input' => array(
  719. 'name' => 'user_form', 'type' => 'text'
  720. ));
  721. $this->assertHtml($expected, $result);
  722. }
  723. /**
  724. * test get form, and inputs when the model param is false
  725. *
  726. * @return void
  727. */
  728. public function testGetFormWithFalseModel() {
  729. $encoding = strtolower(Configure::read('App.encoding'));
  730. $this->Form->request['controller'] = 'contact_test';
  731. $result = $this->Form->create(false, array(
  732. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  733. ));
  734. $expected = array('form' => array(
  735. 'method' => 'get', 'action' => '/contact_test/add',
  736. 'accept-charset' => $encoding
  737. ));
  738. $this->assertHtml($expected, $result);
  739. $result = $this->Form->text('reason');
  740. $expected = array(
  741. 'input' => array('type' => 'text', 'name' => 'reason')
  742. );
  743. $this->assertHtml($expected, $result);
  744. }
  745. /**
  746. * testFormCreateWithSecurity method
  747. *
  748. * Test form->create() with security key.
  749. *
  750. * @return void
  751. */
  752. public function testCreateWithSecurity() {
  753. $this->Form->request->params['_csrfToken'] = 'testKey';
  754. $encoding = strtolower(Configure::read('App.encoding'));
  755. $result = $this->Form->create($this->article, [
  756. 'url' => '/articles/publish',
  757. ]);
  758. $expected = array(
  759. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  760. 'div' => array('style' => 'display:none;'),
  761. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  762. array('input' => array(
  763. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  764. )),
  765. '/div'
  766. );
  767. $this->assertHtml($expected, $result);
  768. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  769. $expected['form']['id'] = 'MyForm';
  770. $this->assertHtml($expected, $result);
  771. }
  772. /**
  773. * testFormCreateGetNoSecurity method
  774. *
  775. * Test form->create() with no security key as its a get form
  776. *
  777. * @return void
  778. */
  779. public function testCreateEndGetNoSecurity() {
  780. $this->Form->request->params['_csrfToken'] = 'testKey';
  781. $encoding = strtolower(Configure::read('App.encoding'));
  782. $article = new Article();
  783. $result = $this->Form->create($article, [
  784. 'type' => 'get',
  785. 'url' => '/contacts/add'
  786. ]);
  787. $this->assertNotContains('testKey', $result);
  788. $result = $this->Form->end();
  789. $this->assertNotContains('testKey', $result);
  790. }
  791. /**
  792. * test that create() clears the fields property so it starts fresh
  793. *
  794. * @return void
  795. */
  796. public function testCreateClearingFields() {
  797. $this->Form->fields = array('model_id');
  798. $this->Form->create($this->article);
  799. $this->assertEquals(array(), $this->Form->fields);
  800. }
  801. /**
  802. * Tests form hash generation with model-less data
  803. *
  804. * @return void
  805. */
  806. public function testValidateHashNoModel() {
  807. $this->Form->request->params['_Token'] = 'foo';
  808. $result = $this->Form->secure(array('anything'));
  809. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  810. }
  811. /**
  812. * Tests that hidden fields generated for checkboxes don't get locked
  813. *
  814. * @return void
  815. */
  816. public function testNoCheckboxLocking() {
  817. $this->Form->request->params['_Token'] = 'foo';
  818. $this->assertSame([], $this->Form->fields);
  819. $this->Form->checkbox('check', array('value' => '1'));
  820. $this->assertSame($this->Form->fields, array('check'));
  821. }
  822. /**
  823. * testFormSecurityFields method
  824. *
  825. * Test generation of secure form hash generation.
  826. *
  827. * @return void
  828. */
  829. public function testFormSecurityFields() {
  830. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  831. $this->Form->request->params['_Token'] = 'testKey';
  832. $result = $this->Form->secure($fields);
  833. $hash = Security::hash(serialize($fields) . Security::salt());
  834. $hash .= ':' . 'Model.valid';
  835. $hash = urlencode($hash);
  836. $expected = array(
  837. 'div' => array('style' => 'display:none;'),
  838. array('input' => array(
  839. 'type' => 'hidden',
  840. 'name' => '_Token[fields]',
  841. 'value' => $hash
  842. )),
  843. array('input' => array(
  844. 'type' => 'hidden',
  845. 'name' => '_Token[unlocked]',
  846. 'value' => '',
  847. )),
  848. '/div'
  849. );
  850. $this->assertHtml($expected, $result);
  851. }
  852. /**
  853. * Tests correct generation of number fields for double and float fields
  854. *
  855. * @return void
  856. */
  857. public function testTextFieldGenerationForFloats() {
  858. $this->article['schema'] = [
  859. 'foo' => [
  860. 'type' => 'float',
  861. 'null' => false,
  862. 'default' => null,
  863. 'length' => 10
  864. ]
  865. ];
  866. $this->Form->create($this->article);
  867. $result = $this->Form->input('foo');
  868. $expected = array(
  869. 'div' => array('class' => 'input number'),
  870. 'label' => array('for' => 'foo'),
  871. 'Foo',
  872. '/label',
  873. array('input' => array(
  874. 'type' => 'number',
  875. 'name' => 'foo',
  876. 'id' => 'foo',
  877. 'step' => 'any'
  878. )),
  879. '/div'
  880. );
  881. $this->assertHtml($expected, $result);
  882. $result = $this->Form->input('foo', array('step' => 0.5));
  883. $expected = array(
  884. 'div' => array('class' => 'input number'),
  885. 'label' => array('for' => 'foo'),
  886. 'Foo',
  887. '/label',
  888. array('input' => array(
  889. 'type' => 'number',
  890. 'name' => 'foo',
  891. 'id' => 'foo',
  892. 'step' => '0.5'
  893. )),
  894. '/div'
  895. );
  896. $this->assertHtml($expected, $result);
  897. }
  898. /**
  899. * Tests correct generation of number fields for integer fields
  900. *
  901. * @return void
  902. */
  903. public function testTextFieldTypeNumberGenerationForIntegers() {
  904. TableRegistry::get('Contacts', [
  905. 'className' => __NAMESPACE__ . '\ContactsTable'
  906. ]);
  907. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  908. $result = $this->Form->input('age');
  909. $expected = array(
  910. 'div' => array('class' => 'input number'),
  911. 'label' => array('for' => 'age'),
  912. 'Age',
  913. '/label',
  914. array('input' => array(
  915. 'type' => 'number', 'name' => 'age',
  916. 'id' => 'age'
  917. )),
  918. '/div'
  919. );
  920. $this->assertHtml($expected, $result);
  921. }
  922. /**
  923. * Tests correct generation of file upload fields for binary fields
  924. *
  925. * @return void
  926. */
  927. public function testFileUploadFieldTypeGenerationForBinaries() {
  928. $table = TableRegistry::get('Contacts', [
  929. 'className' => __NAMESPACE__ . '\ContactsTable'
  930. ]);
  931. $table->schema(array('foo' => array(
  932. 'type' => 'binary',
  933. 'null' => false,
  934. 'default' => null,
  935. 'length' => 1024
  936. )));
  937. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  938. $result = $this->Form->input('foo');
  939. $expected = array(
  940. 'div' => array('class' => 'input file'),
  941. 'label' => array('for' => 'foo'),
  942. 'Foo',
  943. '/label',
  944. array('input' => array(
  945. 'type' => 'file', 'name' => 'foo',
  946. 'id' => 'foo'
  947. )),
  948. '/div'
  949. );
  950. $this->assertHtml($expected, $result);
  951. }
  952. /**
  953. * testFormSecurityMultipleFields method
  954. *
  955. * Test secure() with multiple row form. Ensure hash is correct.
  956. *
  957. * @return void
  958. */
  959. public function testFormSecurityMultipleFields() {
  960. $this->Form->request->params['_Token'] = 'foo';
  961. $fields = array(
  962. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  963. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  964. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  965. );
  966. $result = $this->Form->secure($fields);
  967. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  968. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  969. $expected = array(
  970. 'div' => array('style' => 'display:none;'),
  971. array('input' => array(
  972. 'type' => 'hidden', 'name' => '_Token[fields]',
  973. 'value' => $hash
  974. )),
  975. array('input' => array(
  976. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  977. 'value' => ''
  978. )),
  979. '/div'
  980. );
  981. $this->assertHtml($expected, $result);
  982. }
  983. /**
  984. * testFormSecurityMultipleSubmitButtons
  985. *
  986. * test form submit generation and ensure that _Token is only created on end()
  987. *
  988. * @return void
  989. */
  990. public function testFormSecurityMultipleSubmitButtons() {
  991. $this->Form->request->params['_Token'] = 'testKey';
  992. $this->Form->create($this->article);
  993. $this->Form->text('Address.title');
  994. $this->Form->text('Address.first_name');
  995. $result = $this->Form->submit('Save', array('name' => 'save'));
  996. $expected = array(
  997. 'div' => array('class' => 'submit'),
  998. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  999. '/div',
  1000. );
  1001. $this->assertHtml($expected, $result);
  1002. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1003. $expected = array(
  1004. 'div' => array('class' => 'submit'),
  1005. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1006. '/div',
  1007. );
  1008. $this->assertHtml($expected, $result);
  1009. $result = $this->Form->end();
  1010. $expected = array(
  1011. 'div' => array('style' => 'display:none;'),
  1012. array('input' => array(
  1013. 'type' => 'hidden',
  1014. 'name' => '_Token[fields]',
  1015. 'value'
  1016. )),
  1017. array('input' => array(
  1018. 'type' => 'hidden',
  1019. 'name' => '_Token[unlocked]',
  1020. 'value' => 'cancel%7Csave'
  1021. )),
  1022. '/div'
  1023. );
  1024. $this->assertHtml($expected, $result);
  1025. }
  1026. /**
  1027. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1028. *
  1029. * @return void
  1030. */
  1031. public function testSecurityButtonNestedNamed() {
  1032. $key = 'testKey';
  1033. $this->Form->request->params['_csrfToken'] = $key;
  1034. $this->Form->create('Addresses');
  1035. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1036. $result = $this->Form->unlockField();
  1037. $this->assertEquals(array('Address.button'), $result);
  1038. }
  1039. /**
  1040. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1041. *
  1042. * @return void
  1043. */
  1044. public function testSecuritySubmitNestedNamed() {
  1045. $this->Form->request->params['_Token'] = 'testKey';
  1046. $this->Form->create($this->article);
  1047. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1048. $result = $this->Form->unlockField();
  1049. $this->assertEquals(array('Address.button'), $result);
  1050. }
  1051. /**
  1052. * Test that the correct fields are unlocked for image submits with no names.
  1053. *
  1054. * @return void
  1055. */
  1056. public function testSecuritySubmitImageNoName() {
  1057. $key = 'testKey';
  1058. $this->Form->request->params['_Token'] = 'testKey';
  1059. $this->Form->create(false);
  1060. $result = $this->Form->submit('save.png');
  1061. $expected = array(
  1062. 'div' => array('class' => 'submit'),
  1063. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1064. '/div'
  1065. );
  1066. $this->assertHtml($expected, $result);
  1067. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1068. }
  1069. /**
  1070. * Test that the correct fields are unlocked for image submits with names.
  1071. *
  1072. * @return void
  1073. */
  1074. public function testSecuritySubmitImageName() {
  1075. $this->Form->request->params['_Token'] = 'testKey';
  1076. $this->Form->create(null);
  1077. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1078. $expected = array(
  1079. 'div' => array('class' => 'submit'),
  1080. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1081. '/div'
  1082. );
  1083. $this->assertHtml($expected, $result);
  1084. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1085. }
  1086. /**
  1087. * testFormSecurityMultipleInputFields method
  1088. *
  1089. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1090. *
  1091. * @return void
  1092. */
  1093. public function testFormSecurityMultipleInputFields() {
  1094. $this->Form->request->params['_Token'] = 'testKey';
  1095. $this->Form->create();
  1096. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1097. $this->Form->input('Addresses.0.title');
  1098. $this->Form->input('Addresses.0.first_name');
  1099. $this->Form->input('Addresses.0.last_name');
  1100. $this->Form->input('Addresses.0.address');
  1101. $this->Form->input('Addresses.0.city');
  1102. $this->Form->input('Addresses.0.phone');
  1103. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1104. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1105. $this->Form->input('Addresses.1.title');
  1106. $this->Form->input('Addresses.1.first_name');
  1107. $this->Form->input('Addresses.1.last_name');
  1108. $this->Form->input('Addresses.1.address');
  1109. $this->Form->input('Addresses.1.city');
  1110. $this->Form->input('Addresses.1.phone');
  1111. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1112. $result = $this->Form->secure($this->Form->fields);
  1113. $hash = '8bd3911b07b507408b1a969b31ee90c47b7d387e%3AAddresses.0.id%7CAddresses.1.id';
  1114. $expected = array(
  1115. 'div' => array('style' => 'display:none;'),
  1116. array('input' => array(
  1117. 'type' => 'hidden', 'name' => '_Token[fields]',
  1118. 'value' => $hash
  1119. )),
  1120. array('input' => array(
  1121. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1122. 'value' => ''
  1123. )),
  1124. '/div'
  1125. );
  1126. $this->assertHtml($expected, $result);
  1127. }
  1128. /**
  1129. * Test form security with Model.field.0 style inputs
  1130. *
  1131. * @return void
  1132. */
  1133. public function testFormSecurityArrayFields() {
  1134. $this->Form->request->params['_Token'] = 'testKey';
  1135. $this->Form->create();
  1136. $this->Form->text('Address.primary.1');
  1137. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1138. $this->Form->text('Address.secondary.1.0');
  1139. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1140. }
  1141. /**
  1142. * testFormSecurityMultipleInputDisabledFields method
  1143. *
  1144. * test secure form generation with multiple records and disabled fields.
  1145. *
  1146. * @return void
  1147. */
  1148. public function testFormSecurityMultipleInputDisabledFields() {
  1149. $this->Form->request->params['_Token'] = array(
  1150. 'unlockedFields' => array('first_name', 'address')
  1151. );
  1152. $this->Form->create();
  1153. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1154. $this->Form->text('Addresses.0.title');
  1155. $this->Form->text('Addresses.0.first_name');
  1156. $this->Form->text('Addresses.0.last_name');
  1157. $this->Form->text('Addresses.0.address');
  1158. $this->Form->text('Addresses.0.city');
  1159. $this->Form->text('Addresses.0.phone');
  1160. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1161. $this->Form->text('Addresses.1.title');
  1162. $this->Form->text('Addresses.1.first_name');
  1163. $this->Form->text('Addresses.1.last_name');
  1164. $this->Form->text('Addresses.1.address');
  1165. $this->Form->text('Addresses.1.city');
  1166. $this->Form->text('Addresses.1.phone');
  1167. $result = $this->Form->secure($this->Form->fields);
  1168. $hash = '4fb10b46873df4ddd4ef5c3a19944a2f29b38991%3AAddresses.0.id%7CAddresses.1.id';
  1169. $expected = array(
  1170. 'div' => array('style' => 'display:none;'),
  1171. array('input' => array(
  1172. 'type' => 'hidden',
  1173. 'name' => '_Token[fields]',
  1174. 'value' => $hash
  1175. )),
  1176. array('input' => array(
  1177. 'type' => 'hidden',
  1178. 'name' => '_Token[unlocked]',
  1179. 'value' => 'address%7Cfirst_name',
  1180. )),
  1181. '/div'
  1182. );
  1183. $this->assertHtml($expected, $result);
  1184. }
  1185. /**
  1186. * testFormSecurityInputDisabledFields method
  1187. *
  1188. * Test single record form with disabled fields.
  1189. *
  1190. * @return void
  1191. */
  1192. public function testFormSecurityInputUnlockedFields() {
  1193. $this->Form->request['_Token'] = array(
  1194. 'unlockedFields' => array('first_name', 'address')
  1195. );
  1196. $this->Form->create();
  1197. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1198. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1199. $this->Form->text('Addresses.title');
  1200. $this->Form->text('Addresses.first_name');
  1201. $this->Form->text('Addresses.last_name');
  1202. $this->Form->text('Addresses.address');
  1203. $this->Form->text('Addresses.city');
  1204. $this->Form->text('Addresses.phone');
  1205. $result = $this->Form->fields;
  1206. $expected = array(
  1207. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1208. 'Addresses.city', 'Addresses.phone'
  1209. );
  1210. $this->assertEquals($expected, $result);
  1211. $result = $this->Form->secure($expected, ['data-foo' => 'bar']);
  1212. $hash = 'a303becbdd99cb42ca14a1cf7e63dfd48696a3c5%3AAddresses.id';
  1213. $expected = array(
  1214. 'div' => array('style' => 'display:none;'),
  1215. array('input' => array(
  1216. 'type' => 'hidden',
  1217. 'name' => '_Token[fields]',
  1218. 'value' => $hash,
  1219. 'data-foo' => 'bar',
  1220. )),
  1221. array('input' => array(
  1222. 'type' => 'hidden',
  1223. 'name' => '_Token[unlocked]',
  1224. 'value' => 'address%7Cfirst_name',
  1225. 'data-foo' => 'bar',
  1226. )),
  1227. '/div'
  1228. );
  1229. $this->assertHtml($expected, $result);
  1230. }
  1231. /**
  1232. * test securing inputs with custom name attributes.
  1233. *
  1234. * @return void
  1235. */
  1236. public function testFormSecureWithCustomNameAttribute() {
  1237. $this->Form->request->params['_Token'] = 'testKey';
  1238. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1239. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1240. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1241. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1242. }
  1243. /**
  1244. * testFormSecuredInput method
  1245. *
  1246. * Test generation of entire secure form, assertions made on input() output.
  1247. *
  1248. * @return void
  1249. */
  1250. public function testFormSecuredInput() {
  1251. $this->Form->request->params['_csrfToken'] = 'testKey';
  1252. $this->Form->request->params['_Token'] = 'stuff';
  1253. $this->article['schema'] = [
  1254. 'ratio' => ['type' => 'decimal', 'length' => 5, 'precision' => 6],
  1255. 'population' => ['type' => 'decimal', 'length' => 15, 'precision' => 0],
  1256. ];
  1257. $result = $this->Form->create($this->article, array('url' => '/articles/add'));
  1258. $encoding = strtolower(Configure::read('App.encoding'));
  1259. $expected = array(
  1260. 'form' => array('method' => 'post', 'action' => '/articles/add', 'accept-charset' => $encoding),
  1261. 'div' => array('style' => 'display:none;'),
  1262. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1263. array('input' => array(
  1264. 'type' => 'hidden',
  1265. 'name' => '_csrfToken',
  1266. 'value' => 'testKey'
  1267. )),
  1268. '/div'
  1269. );
  1270. $this->assertHtml($expected, $result);
  1271. $result = $this->Form->input('ratio');
  1272. $expected = array(
  1273. 'div' => array('class'),
  1274. 'label' => array('for'),
  1275. 'Ratio',
  1276. '/label',
  1277. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1278. '/div',
  1279. );
  1280. $this->assertHtml($expected, $result);
  1281. $result = $this->Form->input('population');
  1282. $expected = array(
  1283. 'div' => array('class'),
  1284. 'label' => array('for'),
  1285. 'Population',
  1286. '/label',
  1287. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1288. '/div',
  1289. );
  1290. $this->assertHtml($expected, $result);
  1291. $result = $this->Form->input('published', array('type' => 'text'));
  1292. $expected = array(
  1293. 'div' => array('class' => 'input text'),
  1294. 'label' => array('for' => 'published'),
  1295. 'Published',
  1296. '/label',
  1297. array('input' => array(
  1298. 'type' => 'text',
  1299. 'name' => 'published',
  1300. 'id' => 'published'
  1301. )),
  1302. '/div'
  1303. );
  1304. $this->assertHtml($expected, $result);
  1305. $result = $this->Form->input('other', array('type' => 'text'));
  1306. $expected = array(
  1307. 'div' => array('class' => 'input text'),
  1308. 'label' => array('for' => 'other'),
  1309. 'Other',
  1310. '/label',
  1311. array('input' => array(
  1312. 'type' => 'text',
  1313. 'name' => 'other',
  1314. 'id',
  1315. )),
  1316. '/div'
  1317. );
  1318. $this->assertHtml($expected, $result);
  1319. $result = $this->Form->hidden('stuff');
  1320. $expected = array(
  1321. 'input' => array(
  1322. 'type' => 'hidden',
  1323. 'name' => 'stuff',
  1324. ));
  1325. $this->assertHtml($expected, $result);
  1326. $result = $this->Form->hidden('hidden', array('value' => '0'));
  1327. $expected = array('input' => array(
  1328. 'type' => 'hidden',
  1329. 'name' => 'hidden',
  1330. 'value' => '0'
  1331. ));
  1332. $this->assertHtml($expected, $result);
  1333. $result = $this->Form->input('something', array('type' => 'checkbox'));
  1334. $expected = array(
  1335. 'div' => array('class' => 'input checkbox'),
  1336. array('input' => array(
  1337. 'type' => 'hidden',
  1338. 'name' => 'something',
  1339. 'value' => '0'
  1340. )),
  1341. 'label' => array('for' => 'something'),
  1342. array('input' => array(
  1343. 'type' => 'checkbox',
  1344. 'name' => 'something',
  1345. 'value' => '1',
  1346. 'id' => 'something'
  1347. )),
  1348. 'Something',
  1349. '/label',
  1350. '/div'
  1351. );
  1352. $this->assertHtml($expected, $result);
  1353. $result = $this->Form->fields;
  1354. $expected = array(
  1355. 'ratio', 'population', 'published', 'other',
  1356. 'stuff' => '',
  1357. 'hidden' => '0',
  1358. 'something'
  1359. );
  1360. $this->assertEquals($expected, $result);
  1361. $result = $this->Form->secure($this->Form->fields);
  1362. $expected = array(
  1363. 'div' => array('style' => 'display:none;'),
  1364. array('input' => array(
  1365. 'type' => 'hidden',
  1366. 'name' => '_Token[fields]',
  1367. 'value'
  1368. )),
  1369. array('input' => array(
  1370. 'type' => 'hidden',
  1371. 'name' => '_Token[unlocked]',
  1372. 'value' => ''
  1373. )),
  1374. '/div'
  1375. );
  1376. $this->assertHtml($expected, $result);
  1377. }
  1378. /**
  1379. * Test secured inputs with custom names.
  1380. *
  1381. * @return void
  1382. */
  1383. public function testSecuredInputCustomName() {
  1384. $this->Form->request->params['_Token'] = 'testKey';
  1385. $this->assertEquals(array(), $this->Form->fields);
  1386. $this->Form->text('text_input', array(
  1387. 'name' => 'Option[General.default_role]',
  1388. ));
  1389. $expected = array('Option.General.default_role');
  1390. $this->assertEquals($expected, $this->Form->fields);
  1391. $this->Form->select('select_box', [1, 2], [
  1392. 'name' => 'Option[General.select_role]',
  1393. ]);
  1394. $expected = ['Option.General.default_role', 'Option.General.select_role'];
  1395. $this->assertEquals($expected, $this->Form->fields);
  1396. }
  1397. /**
  1398. * Tests that the correct keys are added to the field hash index
  1399. *
  1400. * @return void
  1401. */
  1402. public function testFormSecuredFileInput() {
  1403. $this->assertEquals(array(), $this->Form->fields);
  1404. $this->Form->file('Attachment.file');
  1405. $expected = array(
  1406. 'Attachment.file.name', 'Attachment.file.type',
  1407. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1408. 'Attachment.file.size'
  1409. );
  1410. $this->assertEquals($expected, $this->Form->fields);
  1411. }
  1412. /**
  1413. * test that multiple selects keys are added to field hash
  1414. *
  1415. * @return void
  1416. */
  1417. public function testFormSecuredMultipleSelect() {
  1418. $this->Form->request->params['_csrfToken'] = 'testKey';
  1419. $this->assertEquals(array(), $this->Form->fields);
  1420. $options = array('1' => 'one', '2' => 'two');
  1421. $this->Form->select('Model.select', $options);
  1422. $expected = array('Model.select');
  1423. $this->assertEquals($expected, $this->Form->fields);
  1424. $this->Form->fields = array();
  1425. $this->Form->select('Model.select', $options, array('multiple' => true));
  1426. $this->assertEquals($expected, $this->Form->fields);
  1427. }
  1428. /**
  1429. * testFormSecuredRadio method
  1430. *
  1431. * @return void
  1432. */
  1433. public function testFormSecuredRadio() {
  1434. $this->Form->request->params['_Token'] = 'testKey';
  1435. $this->assertEquals(array(), $this->Form->fields);
  1436. $options = array('1' => 'option1', '2' => 'option2');
  1437. $this->Form->radio('Test.test', $options);
  1438. $expected = array('Test.test');
  1439. $this->assertEquals($expected, $this->Form->fields);
  1440. }
  1441. /**
  1442. * Test that when disabled is in a list based attribute array it works.
  1443. *
  1444. * @return void
  1445. */
  1446. public function testFormSecuredAndDisabledNotAssoc() {
  1447. $this->Form->request->params['_Token'] = 'testKey';
  1448. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1449. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1450. $this->Form->text('Model.text', array('disabled'));
  1451. $this->Form->textarea('Model.textarea', array('disabled'));
  1452. $this->Form->password('Model.password', array('disabled'));
  1453. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1454. $expected = array(
  1455. 'Model.radio' => ''
  1456. );
  1457. $this->assertEquals($expected, $this->Form->fields);
  1458. }
  1459. /**
  1460. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1461. * hashing.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormSecuredAndDisabled() {
  1466. $this->Form->request->params['_Token'] = 'testKey';
  1467. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1468. $this->Form->text('Model.text', array('disabled' => true));
  1469. $this->Form->password('Model.text', array('disabled' => true));
  1470. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1471. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1472. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1473. $this->Form->year('Model.year', array('disabled' => true));
  1474. $this->Form->month('Model.month', array('disabled' => true));
  1475. $this->Form->day('Model.day', array('disabled' => true));
  1476. $this->Form->hour('Model.hour', array('disabled' => true));
  1477. $this->Form->minute('Model.minute', array('disabled' => true));
  1478. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1479. $expected = array(
  1480. 'Model.radio' => ''
  1481. );
  1482. $this->assertEquals($expected, $this->Form->fields);
  1483. }
  1484. /**
  1485. * testDisableSecurityUsingForm method
  1486. *
  1487. * @return void
  1488. */
  1489. public function testDisableSecurityUsingForm() {
  1490. $this->Form->request['_Token'] = [
  1491. 'disabledFields' => []
  1492. ];
  1493. $this->Form->create();
  1494. $this->Form->hidden('Addresses.id', ['value' => '123456']);
  1495. $this->Form->text('Addresses.title');
  1496. $this->Form->text('Addresses.first_name', ['secure' => false]);
  1497. $this->Form->textarea('Addresses.city', ['secure' => false]);
  1498. $this->Form->select('Addresses.zip', [1, 2], ['secure' => false]);
  1499. $result = $this->Form->fields;
  1500. $expected = [
  1501. 'Addresses.id' => '123456', 'Addresses.title',
  1502. ];
  1503. $this->assertEquals($expected, $result);
  1504. }
  1505. /**
  1506. * test disableField
  1507. *
  1508. * @return void
  1509. */
  1510. public function testUnlockFieldAddsToList() {
  1511. $this->Form->request['_Token'] = array(
  1512. 'unlockedFields' => array()
  1513. );
  1514. $this->Form->unlockField('Contact.name');
  1515. $this->Form->text('Contact.name');
  1516. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1517. $this->assertEquals(array(), $this->Form->fields);
  1518. }
  1519. /**
  1520. * test unlockField removing from fields array.
  1521. *
  1522. * @return void
  1523. */
  1524. public function testUnlockFieldRemovingFromFields() {
  1525. $this->Form->request['_Token'] = array(
  1526. 'unlockedFields' => array()
  1527. );
  1528. $this->Form->create($this->article);
  1529. $this->Form->hidden('Article.id', array('value' => 1));
  1530. $this->Form->text('Article.title');
  1531. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1532. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1533. $this->Form->unlockField('Article.title');
  1534. $this->Form->unlockField('Article.id');
  1535. $this->assertEquals(array(), $this->Form->fields);
  1536. }
  1537. /**
  1538. * Test that only the path + query elements of a form's URL show up in their hash.
  1539. *
  1540. * @return void
  1541. */
  1542. public function testSecuredFormUrlIgnoresHost() {
  1543. $this->Form->request['_Token'] = array('key' => 'testKey');
  1544. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  1545. $this->Form->create($this->article, array(
  1546. 'url' => array('controller' => 'articles', 'action' => 'view', 1, '?' => array('page' => 1))
  1547. ));
  1548. $result = $this->Form->secure();
  1549. $this->assertContains($expected, $result);
  1550. $this->Form->create($this->article, array('url' => 'http://localhost/articles/view/1?page=1'));
  1551. $result = $this->Form->secure();
  1552. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1553. $this->Form->create($this->article, array('url' => '/articles/view/1?page=1'));
  1554. $result = $this->Form->secure();
  1555. $this->assertContains($expected, $result, 'URL path + query should work.');
  1556. $this->Form->create($this->article, array('url' => '/articles/view/1'));
  1557. $result = $this->Form->secure();
  1558. $this->assertNotContains($expected, $result, 'URL is different');
  1559. }
  1560. /**
  1561. * Test that URL, HTML and identifer show up in their hashs.
  1562. *
  1563. * @return void
  1564. */
  1565. public function testSecuredFormUrlHasHtmlAndIdentifer() {
  1566. $this->Form->request['_Token'] = array('key' => 'testKey');
  1567. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  1568. $res = $this->Form->create($this->article, array(
  1569. 'url' => array(
  1570. 'controller' => 'articles',
  1571. 'action' => 'view',
  1572. '?' => array(
  1573. 'page' => 1,
  1574. 'limit' => 10,
  1575. 'html' => '<>"',
  1576. ),
  1577. '#' => 'result',
  1578. ),
  1579. ));
  1580. $result = $this->Form->secure();
  1581. $this->assertContains($expected, $result);
  1582. $this->Form->create($this->article, array(
  1583. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1584. ));
  1585. $result = $this->Form->secure();
  1586. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  1587. $this->Form->create($this->article, array(
  1588. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  1589. ));
  1590. $result = $this->Form->secure();
  1591. $this->assertContains($expected, $result, 'URL path + query should work.');
  1592. }
  1593. /**
  1594. * test error message display
  1595. *
  1596. * @return void
  1597. */
  1598. public function testErrorMessageDisplay() {
  1599. $this->article['errors'] = [
  1600. 'Article' => ['title' => 'error message']
  1601. ];
  1602. $this->Form->create($this->article);
  1603. $result = $this->Form->input('Article.title');
  1604. $expected = [
  1605. 'div' => ['class' => 'input text error'],
  1606. 'label' => ['for' => 'article-title'],
  1607. 'Title',
  1608. '/label',
  1609. 'input' => [
  1610. 'type' => 'text', 'name' => 'Article[title]',
  1611. 'id' => 'article-title', 'class' => 'form-error'
  1612. ],
  1613. ['div' => ['class' => 'error-message']],
  1614. 'error message',
  1615. '/div',
  1616. '/div'
  1617. ];
  1618. $this->assertHtml($expected, $result);
  1619. $result = $this->Form->input('Article.title', [
  1620. 'templates' => [
  1621. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  1622. ]
  1623. ]);
  1624. $expected = [
  1625. 'div' => ['class' => 'input text error'],
  1626. 'label' => ['for' => 'article-title'],
  1627. 'Title',
  1628. '/label',
  1629. 'input' => [
  1630. 'type' => 'text', 'name' => 'Article[title]',
  1631. 'id' => 'article-title', 'class' => 'form-error'
  1632. ],
  1633. '/div'
  1634. ];
  1635. $this->assertHtml($expected, $result);
  1636. }
  1637. /**
  1638. * Test validation errors, when validation message is an empty string.
  1639. *
  1640. * @return void
  1641. */
  1642. public function testEmptyErrorValidation() {
  1643. $this->article['errors'] = [
  1644. 'Article' => ['title' => '']
  1645. ];
  1646. $this->Form->create($this->article);
  1647. $result = $this->Form->input('Article.title');
  1648. $expected = [
  1649. 'div' => ['class' => 'input text error'],
  1650. 'label' => ['for' => 'article-title'],
  1651. 'Title',
  1652. '/label',
  1653. 'input' => [
  1654. 'type' => 'text', 'name' => 'Article[title]',
  1655. 'id' => 'article-title', 'class' => 'form-error'
  1656. ],
  1657. ['div' => ['class' => 'error-message']],
  1658. [],
  1659. '/div',
  1660. '/div'
  1661. ];
  1662. $this->assertHtml($expected, $result);
  1663. }
  1664. /**
  1665. * Test validation errors, when calling input() overriding validation message by an empty string.
  1666. *
  1667. * @return void
  1668. */
  1669. public function testEmptyInputErrorValidation() {
  1670. $this->article['errors'] = [
  1671. 'Article' => ['title' => 'error message']
  1672. ];
  1673. $this->Form->create($this->article);
  1674. $result = $this->Form->input('Article.title', array('error' => ''));
  1675. $expected = [
  1676. 'div' => ['class' => 'input text error'],
  1677. 'label' => ['for' => 'article-title'],
  1678. 'Title',
  1679. '/label',
  1680. 'input' => [
  1681. 'type' => 'text', 'name' => 'Article[title]',
  1682. 'id' => 'article-title', 'class' => 'form-error'
  1683. ],
  1684. ['div' => ['class' => 'error-message']],
  1685. [],
  1686. '/div',
  1687. '/div'
  1688. ];
  1689. $this->assertHtml($expected, $result);
  1690. }
  1691. /**
  1692. * Test validation errors, when calling input() overriding validation messages
  1693. *
  1694. * @return void
  1695. */
  1696. public function testInputErrorMessage() {
  1697. $this->article['errors'] = [
  1698. 'title' => ['error message']
  1699. ];
  1700. $this->Form->create($this->article);
  1701. $result = $this->Form->input('title', [
  1702. 'error' => 'Custom error!'
  1703. ]);
  1704. $expected = [
  1705. 'div' => ['class' => 'input text required error'],
  1706. 'label' => ['for' => 'title'],
  1707. 'Title',
  1708. '/label',
  1709. 'input' => [
  1710. 'type' => 'text', 'name' => 'title',
  1711. 'id' => 'title', 'class' => 'form-error',
  1712. 'required' => 'required',
  1713. ],
  1714. ['div' => ['class' => 'error-message']],
  1715. 'Custom error!',
  1716. '/div',
  1717. '/div'
  1718. ];
  1719. $this->assertHtml($expected, $result);
  1720. $result = $this->Form->input('title', [
  1721. 'error' => ['error message' => 'Custom error!']
  1722. ]);
  1723. $expected = [
  1724. 'div' => ['class' => 'input text required error'],
  1725. 'label' => ['for' => 'title'],
  1726. 'Title',
  1727. '/label',
  1728. 'input' => [
  1729. 'type' => 'text',
  1730. 'name' => 'title',
  1731. 'id' => 'title',
  1732. 'class' => 'form-error',
  1733. 'required' => 'required'
  1734. ],
  1735. ['div' => ['class' => 'error-message']],
  1736. 'Custom error!',
  1737. '/div',
  1738. '/div'
  1739. ];
  1740. $this->assertHtml($expected, $result);
  1741. }
  1742. /**
  1743. * Tests displaying errors for nested entities
  1744. *
  1745. * @return void
  1746. */
  1747. public function testFormValidationAssociated() {
  1748. $nested = new Entity(['foo' => 'bar']);
  1749. $nested->errors('foo', ['not a valid bar']);
  1750. $entity = new Entity(['nested' => $nested]);
  1751. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1752. $result = $this->Form->error('nested.foo');
  1753. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  1754. }
  1755. /**
  1756. * testFormValidationAssociatedSecondLevel method
  1757. *
  1758. * test form error display with associated model.
  1759. *
  1760. * @return void
  1761. */
  1762. public function testFormValidationAssociatedSecondLevel() {
  1763. $inner = new Entity(['bar' => 'baz']);
  1764. $nested = new Entity(['foo' => $inner]);
  1765. $entity = new Entity(['nested' => $nested]);
  1766. $inner->errors('bar', ['not a valid one']);
  1767. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  1768. $result = $this->Form->error('nested.foo.bar');
  1769. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  1770. }
  1771. /**
  1772. * testFormValidationMultiRecord method
  1773. *
  1774. * test form error display with multiple records.
  1775. *
  1776. * @return void
  1777. */
  1778. public function testFormValidationMultiRecord() {
  1779. $one = new Entity;
  1780. $two = new Entity;
  1781. TableRegistry::get('Contacts', [
  1782. 'className' => __NAMESPACE__ . '\ContactsTable'
  1783. ]);
  1784. $one->set('email', '');
  1785. $one->errors('email', ['invalid email']);
  1786. $two->set('name', '');
  1787. $two->errors('name', ['This is wrong']);
  1788. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  1789. $result = $this->Form->input('0.email');
  1790. $expected = array(
  1791. 'div' => array('class' => 'input email error'),
  1792. 'label' => array('for' => '0-email'),
  1793. 'Email',
  1794. '/label',
  1795. 'input' => array(
  1796. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  1797. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  1798. ),
  1799. array('div' => array('class' => 'error-message')),
  1800. 'invalid email',
  1801. '/div',
  1802. '/div'
  1803. );
  1804. $this->assertHtml($expected, $result);
  1805. $result = $this->Form->input('1.name');
  1806. $expected = array(
  1807. 'div' => array('class' => 'input text error'),
  1808. 'label' => array('for' => '1-name'),
  1809. 'Name',
  1810. '/label',
  1811. 'input' => array(
  1812. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  1813. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  1814. ),
  1815. array('div' => array('class' => 'error-message')),
  1816. 'This is wrong',
  1817. '/div',
  1818. '/div'
  1819. );
  1820. $this->assertHtml($expected, $result);
  1821. }
  1822. /**
  1823. * testInput method
  1824. *
  1825. * Test various incarnations of input().
  1826. *
  1827. * @return void
  1828. */
  1829. public function testInput() {
  1830. TableRegistry::get('ValidateUsers', [
  1831. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  1832. ]);
  1833. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  1834. $result = $this->Form->input('ValidateUsers.balance');
  1835. $expected = array(
  1836. 'div' => array('class'),
  1837. 'label' => array('for'),
  1838. 'Balance',
  1839. '/label',
  1840. 'input' => array('name', 'type' => 'number', 'id', 'step'),
  1841. '/div',
  1842. );
  1843. $this->assertHtml($expected, $result);
  1844. $result = $this->Form->input('ValidateUser.cost_decimal');
  1845. $expected = array(
  1846. 'div' => array('class'),
  1847. 'label' => array('for'),
  1848. 'Cost Decimal',
  1849. '/label',
  1850. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  1851. '/div',
  1852. );
  1853. $this->assertHtml($expected, $result);
  1854. }
  1855. /**
  1856. * Tests the input method and passing custom options
  1857. *
  1858. * @return void
  1859. */
  1860. public function testInputCustomization() {
  1861. TableRegistry::get('Contacts', [
  1862. 'className' => __NAMESPACE__ . '\ContactsTable'
  1863. ]);
  1864. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  1865. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1866. $expected = array(
  1867. 'div' => array('class' => 'input email'),
  1868. 'label' => array('for' => 'custom'),
  1869. 'Email',
  1870. '/label',
  1871. array('input' => array(
  1872. 'type' => 'email', 'name' => 'Contact[email]',
  1873. 'id' => 'custom', 'maxlength' => 255
  1874. )),
  1875. '/div'
  1876. );
  1877. $this->assertHtml($expected, $result);
  1878. $result = $this->Form->input('Contact.email', array(
  1879. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  1880. ));
  1881. $expected = array(
  1882. '<div',
  1883. 'label' => array('for' => 'contact-email'),
  1884. 'Email',
  1885. '/label',
  1886. array('input' => array(
  1887. 'type' => 'email', 'name' => 'Contact[email]',
  1888. 'id' => 'contact-email', 'maxlength' => 255
  1889. )),
  1890. '/div'
  1891. );
  1892. $this->assertHtml($expected, $result);
  1893. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1894. $expected = array(
  1895. 'div' => array('class' => 'input text'),
  1896. 'label' => array('for' => 'contact-email'),
  1897. 'Email',
  1898. '/label',
  1899. array('input' => array(
  1900. 'type' => 'text', 'name' => 'Contact[email]',
  1901. 'id' => 'contact-email', 'maxlength' => '255'
  1902. )),
  1903. '/div'
  1904. );
  1905. $this->assertHtml($expected, $result);
  1906. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1907. $expected = array(
  1908. 'div' => array('class' => 'input text'),
  1909. 'label' => array('for' => 'contact-5-email'),
  1910. 'Email',
  1911. '/label',
  1912. array('input' => array(
  1913. 'type' => 'text', 'name' => 'Contact[5][email]',
  1914. 'id' => 'contact-5-email', 'maxlength' => '255'
  1915. )),
  1916. '/div'
  1917. );
  1918. $this->assertHtml($expected, $result);
  1919. $result = $this->Form->input('Contact.password');
  1920. $expected = array(
  1921. 'div' => array('class' => 'input password'),
  1922. 'label' => array('for' => 'contact-password'),
  1923. 'Password',
  1924. '/label',
  1925. array('input' => array(
  1926. 'type' => 'password', 'name' => 'Contact[password]',
  1927. 'id' => 'contact-password'
  1928. )),
  1929. '/div'
  1930. );
  1931. $this->assertHtml($expected, $result);
  1932. $result = $this->Form->input('Contact.email', array(
  1933. 'type' => 'file', 'class' => 'textbox'
  1934. ));
  1935. $expected = array(
  1936. 'div' => array('class' => 'input file'),
  1937. 'label' => array('for' => 'contact-email'),
  1938. 'Email',
  1939. '/label',
  1940. array('input' => array(
  1941. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  1942. 'id' => 'contact-email'
  1943. )),
  1944. '/div'
  1945. );
  1946. $this->assertHtml($expected, $result);
  1947. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  1948. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1949. $result = $this->Form->input('phone');
  1950. $expected = array(
  1951. 'div' => array('class' => 'input tel'),
  1952. 'label' => array('for' => 'phone'),
  1953. 'Phone',
  1954. '/label',
  1955. array('input' => array(
  1956. 'type' => 'tel', 'name' => 'phone',
  1957. 'value' => 'Hello &amp; World &gt; weird chars',
  1958. 'id' => 'phone', 'maxlength' => 255
  1959. )),
  1960. '/div'
  1961. );
  1962. $this->assertHtml($expected, $result);
  1963. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1964. $this->Form->create();
  1965. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1966. $expected = array(
  1967. 'div' => array('class' => 'input text'),
  1968. 'label' => array('for' => 'myId'),
  1969. 'Field',
  1970. '/label',
  1971. 'input' => array(
  1972. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  1973. 'value' => 'My value', 'id' => 'myId'
  1974. ),
  1975. '/div'
  1976. );
  1977. $this->assertHtml($expected, $result);
  1978. $this->Form->request->data = [];
  1979. $entity->errors('field', 'Badness!');
  1980. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  1981. $result = $this->Form->input('field');
  1982. $expected = array(
  1983. 'div' => array('class' => 'input text error'),
  1984. 'label' => array('for' => 'field'),
  1985. 'Field',
  1986. '/label',
  1987. 'input' => array(
  1988. 'type' => 'text', 'name' => 'field',
  1989. 'id' => 'field', 'class' => 'form-error'
  1990. ),
  1991. array('div' => array('class' => 'error-message')),
  1992. 'Badness!',
  1993. '/div',
  1994. '/div'
  1995. );
  1996. $this->assertHtml($expected, $result);
  1997. $result = $this->Form->input('field', array(
  1998. 'templates' => [
  1999. 'inputContainerError' => '{{content}}{{error}}',
  2000. 'error' => '<span class="error-message">{{content}}</span>'
  2001. ]
  2002. ));
  2003. $expected = array(
  2004. 'label' => array('for' => 'field'),
  2005. 'Field',
  2006. '/label',
  2007. 'input' => array(
  2008. 'type' => 'text', 'name' => 'field',
  2009. 'id' => 'field', 'class' => 'form-error'
  2010. ),
  2011. array('span' => array('class' => 'error-message')),
  2012. 'Badness!',
  2013. '/span'
  2014. );
  2015. $this->assertHtml($expected, $result);
  2016. $entity->errors('field', ['minLength']);
  2017. $result = $this->Form->input('field', array(
  2018. 'error' => array(
  2019. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2020. 'maxLength' => 'login too large'
  2021. )
  2022. ));
  2023. $expected = array(
  2024. 'div' => array('class' => 'input text error'),
  2025. 'label' => array('for' => 'field'),
  2026. 'Field',
  2027. '/label',
  2028. 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'),
  2029. array('div' => array('class' => 'error-message')),
  2030. 'Le login doit contenir au moins 2 caractères',
  2031. '/div',
  2032. '/div'
  2033. );
  2034. $this->assertHtml($expected, $result);
  2035. $entity->errors('field', ['maxLength']);
  2036. $result = $this->Form->input('field', array(
  2037. 'error' => array(
  2038. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2039. 'maxLength' => 'login too large',
  2040. )
  2041. ));
  2042. $expected = array(
  2043. 'div' => array('class' => 'input text error'),
  2044. 'label' => array('for' => 'field'),
  2045. 'Field',
  2046. '/label',
  2047. 'input' => array('type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'),
  2048. array('div' => array('class' => 'error-message')),
  2049. 'login too large',
  2050. '/div',
  2051. '/div'
  2052. );
  2053. $this->assertHtml($expected, $result);
  2054. }
  2055. /**
  2056. * Test that input() accepts a template file.
  2057. *
  2058. * @return void
  2059. */
  2060. public function testInputWithTemplateFile() {
  2061. $result = $this->Form->input('field', array(
  2062. 'templates' => 'htmlhelper_tags'
  2063. ));
  2064. $expected = array(
  2065. 'label' => array('for' => 'field'),
  2066. 'Field',
  2067. '/label',
  2068. 'input' => array(
  2069. 'type' => 'text', 'name' => 'field',
  2070. 'id' => 'field'
  2071. ),
  2072. );
  2073. $this->assertHtml($expected, $result);
  2074. }
  2075. /**
  2076. * Test id prefix
  2077. *
  2078. * @return void
  2079. */
  2080. public function testCreateIdPrefix() {
  2081. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2082. $result = $this->Form->input('field');
  2083. $expected = [
  2084. 'div' => ['class' => 'input text'],
  2085. 'label' => ['for' => 'prefix-field'],
  2086. 'Field',
  2087. '/label',
  2088. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2089. '/div'
  2090. ];
  2091. $this->assertHtml($expected, $result);
  2092. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2093. $expected = [
  2094. 'div' => ['class' => 'input text'],
  2095. 'label' => ['for' => 'custom-id'],
  2096. 'Field',
  2097. '/label',
  2098. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2099. '/div'
  2100. ];
  2101. $this->assertHtml($expected, $result);
  2102. $result = $this->Form->radio('Model.field', ['option A']);
  2103. $expected = [
  2104. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2105. 'label' => ['for' => 'prefix-model-field-0'],
  2106. ['input' => [
  2107. 'type' => 'radio',
  2108. 'name' => 'Model[field]',
  2109. 'value' => '0',
  2110. 'id' => 'prefix-model-field-0'
  2111. ]],
  2112. 'option A',
  2113. '/label'
  2114. ];
  2115. $this->assertHtml($expected, $result);
  2116. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2117. $expected = [
  2118. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2119. 'label' => ['for' => 'prefix-model-field-0'],
  2120. ['input' => [
  2121. 'type' => 'radio',
  2122. 'name' => 'Model[field]',
  2123. 'value' => '0',
  2124. 'id' => 'prefix-model-field-0'
  2125. ]],
  2126. 'option A',
  2127. '/label'
  2128. ];
  2129. $this->assertHtml($expected, $result);
  2130. $result = $this->Form->select(
  2131. 'Model.multi_field',
  2132. ['first'],
  2133. ['multiple' => 'checkbox']
  2134. );
  2135. $expected = [
  2136. 'input' => [
  2137. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2138. ],
  2139. ['div' => ['class' => 'checkbox']],
  2140. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2141. ['input' => [
  2142. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2143. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2144. ]],
  2145. 'first',
  2146. '/label',
  2147. '/div',
  2148. ];
  2149. $this->assertHtml($expected, $result);
  2150. $this->Form->end();
  2151. $result = $this->Form->input('field');
  2152. $expected = [
  2153. 'div' => ['class' => 'input text'],
  2154. 'label' => ['for' => 'field'],
  2155. 'Field',
  2156. '/label',
  2157. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2158. '/div'
  2159. ];
  2160. $this->assertHtml($expected, $result);
  2161. }
  2162. /**
  2163. * Test that inputs with 0 can be created.
  2164. *
  2165. * @return void
  2166. */
  2167. public function testInputZero() {
  2168. TableRegistry::get('Contacts', [
  2169. 'className' => __NAMESPACE__ . '\ContactsTable'
  2170. ]);
  2171. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2172. $result = $this->Form->input('0');
  2173. $expected = array(
  2174. 'div' => array('class' => 'input text'),
  2175. 'label' => array('for' => '0'), '/label',
  2176. 'input' => array('type' => 'text', 'name' => '0', 'id' => '0'),
  2177. '/div'
  2178. );
  2179. $this->assertHtml($expected, $result);
  2180. }
  2181. /**
  2182. * test input() with checkbox creation
  2183. *
  2184. * @return void
  2185. */
  2186. public function testInputCheckbox() {
  2187. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2188. $expected = array(
  2189. 'div' => array('class' => 'input checkbox'),
  2190. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2191. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2192. '/div'
  2193. );
  2194. $this->assertHtml($expected, $result);
  2195. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2196. $expected = array(
  2197. 'div' => array('class' => 'input checkbox'),
  2198. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2199. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2200. '/div'
  2201. );
  2202. $this->assertHtml($expected, $result);
  2203. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2204. $expected = array(
  2205. 'div' => array('class' => 'input checkbox'),
  2206. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0'),
  2207. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked')),
  2208. '/div'
  2209. );
  2210. $this->assertHtml($expected, $result);
  2211. $result = $this->Form->input('User.disabled', array(
  2212. 'label' => 'Disabled',
  2213. 'type' => 'checkbox',
  2214. 'data-foo' => 'disabled'
  2215. ));
  2216. $expected = array(
  2217. 'div' => array('class' => 'input checkbox'),
  2218. 'input' => array('type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'),
  2219. 'label' => array('for' => 'user-disabled'),
  2220. array('input' => array(
  2221. 'type' => 'checkbox',
  2222. 'name' => 'User[disabled]',
  2223. 'value' => '1',
  2224. 'id' => 'user-disabled',
  2225. 'data-foo' => 'disabled'
  2226. )),
  2227. 'Disabled',
  2228. '/label',
  2229. '/div'
  2230. );
  2231. $this->assertHtml($expected, $result);
  2232. }
  2233. /**
  2234. * Test that input() does not create wrapping div and label tag for hidden fields
  2235. *
  2236. * @return void
  2237. */
  2238. public function testInputHidden() {
  2239. TableRegistry::get('ValidateUsers', [
  2240. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2241. ]);
  2242. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2243. $result = $this->Form->input('ValidateUser.id');
  2244. $expected = array(
  2245. 'input' => array('name', 'type' => 'hidden', 'id')
  2246. );
  2247. $this->assertHtml($expected, $result);
  2248. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2249. $expected = array(
  2250. 'input' => array('name', 'type' => 'hidden', 'id')
  2251. );
  2252. $this->assertHtml($expected, $result);
  2253. }
  2254. /**
  2255. * test form->input() with datetime
  2256. *
  2257. * @return void
  2258. */
  2259. public function testInputDatetime() {
  2260. $this->Form = $this->getMock(
  2261. 'Cake\View\Helper\FormHelper',
  2262. ['datetime'],
  2263. [new View()]
  2264. );
  2265. $this->Form->expects($this->once())->method('datetime')
  2266. ->with('prueba', [
  2267. 'type' => 'datetime',
  2268. 'timeFormat' => 24,
  2269. 'minYear' => 2008,
  2270. 'maxYear' => 2011,
  2271. 'interval' => 15,
  2272. 'options' => null,
  2273. 'empty' => false,
  2274. 'id' => 'prueba',
  2275. 'required' => false,
  2276. ])
  2277. ->will($this->returnValue('This is it!'));
  2278. $result = $this->Form->input('prueba', array(
  2279. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2280. 'maxYear' => 2011, 'interval' => 15
  2281. ));
  2282. $expected = array(
  2283. 'div' => array('class' => 'input datetime'),
  2284. '<label',
  2285. 'Prueba',
  2286. '/label',
  2287. 'This is it!',
  2288. '/div'
  2289. );
  2290. $this->assertHtml($expected, $result);
  2291. }
  2292. /**
  2293. * test form->input() with datetime with id prefix
  2294. *
  2295. * @return void
  2296. */
  2297. public function testInputDatetimeIdPrefix() {
  2298. $this->Form = $this->getMock(
  2299. 'Cake\View\Helper\FormHelper',
  2300. ['datetime'],
  2301. [new View()]
  2302. );
  2303. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2304. $this->Form->expects($this->once())->method('datetime')
  2305. ->with('prueba', [
  2306. 'type' => 'datetime',
  2307. 'timeFormat' => 24,
  2308. 'minYear' => 2008,
  2309. 'maxYear' => 2011,
  2310. 'interval' => 15,
  2311. 'options' => null,
  2312. 'empty' => false,
  2313. 'id' => 'prefix-prueba',
  2314. 'required' => false,
  2315. ])
  2316. ->will($this->returnValue('This is it!'));
  2317. $result = $this->Form->input('prueba', array(
  2318. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2319. 'maxYear' => 2011, 'interval' => 15
  2320. ));
  2321. $expected = array(
  2322. 'div' => array('class' => 'input datetime'),
  2323. '<label',
  2324. 'Prueba',
  2325. '/label',
  2326. 'This is it!',
  2327. '/div'
  2328. );
  2329. $this->assertHtml($expected, $result);
  2330. }
  2331. /**
  2332. * Test generating checkboxes with disabled elements.
  2333. *
  2334. * @return void
  2335. */
  2336. public function testInputCheckboxWithDisabledElements() {
  2337. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2338. $result = $this->Form->input('Contact.multiple', array(
  2339. 'multiple' => 'checkbox',
  2340. 'disabled' => 'disabled',
  2341. 'options' => $options
  2342. ));
  2343. $expected = array(
  2344. array('div' => array('class' => 'input select')),
  2345. array('label' => array('for' => "contact-multiple")),
  2346. 'Multiple',
  2347. '/label',
  2348. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '')),
  2349. array('div' => array('class' => 'checkbox')),
  2350. array('label' => array('for' => "contact-multiple-1")),
  2351. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1")),
  2352. 'One',
  2353. '/label',
  2354. '/div',
  2355. array('div' => array('class' => 'checkbox')),
  2356. array('label' => array('for' => "contact-multiple-2")),
  2357. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2")),
  2358. 'Two',
  2359. '/label',
  2360. '/div',
  2361. array('div' => array('class' => 'checkbox')),
  2362. array('label' => array('for' => "contact-multiple-3")),
  2363. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3")),
  2364. 'Three',
  2365. '/label',
  2366. '/div',
  2367. '/div'
  2368. );
  2369. $this->assertHtml($expected, $result);
  2370. // make sure 50 does only disable 50, and not 50f5c0cf
  2371. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2372. $disabled = array(50);
  2373. $expected = array(
  2374. array('div' => array('class' => 'input select')),
  2375. array('label' => array('for' => "contact-multiple")),
  2376. 'Multiple',
  2377. '/label',
  2378. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '')),
  2379. array('div' => array('class' => 'checkbox')),
  2380. array('label' => array('for' => "contact-multiple-50")),
  2381. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50")),
  2382. 'Fifty',
  2383. '/label',
  2384. '/div',
  2385. array('div' => array('class' => 'checkbox')),
  2386. array('label' => array('for' => "contact-multiple-50f5c0cf")),
  2387. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf")),
  2388. 'Stringy',
  2389. '/label',
  2390. '/div',
  2391. '/div'
  2392. );
  2393. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2394. $this->assertHtml($expected, $result);
  2395. }
  2396. /**
  2397. * test input name with leading integer, ensure attributes are generated correctly.
  2398. *
  2399. * @return void
  2400. */
  2401. public function testInputWithLeadingInteger() {
  2402. $result = $this->Form->text('0.Node.title');
  2403. $expected = array(
  2404. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2405. );
  2406. $this->assertHtml($expected, $result);
  2407. }
  2408. /**
  2409. * test form->input() with select type inputs.
  2410. *
  2411. * @return void
  2412. */
  2413. public function testInputSelectType() {
  2414. $result = $this->Form->input('email', array(
  2415. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2416. );
  2417. $expected = array(
  2418. 'div' => array('class' => 'input select'),
  2419. 'label' => array('for' => 'email'),
  2420. 'Email',
  2421. '/label',
  2422. array('select' => array('name' => 'email', 'id' => 'email')),
  2423. array('option' => array('value' => '')),
  2424. '/option',
  2425. array('option' => array('value' => 'è')),
  2426. 'Firést',
  2427. '/option',
  2428. array('option' => array('value' => 'é')),
  2429. 'Secoènd',
  2430. '/option',
  2431. '/select',
  2432. '/div'
  2433. );
  2434. $this->assertHtml($expected, $result);
  2435. $result = $this->Form->input('email', array(
  2436. 'options' => array('First', 'Second'), 'empty' => true)
  2437. );
  2438. $expected = array(
  2439. 'div' => array('class' => 'input select'),
  2440. 'label' => array('for' => 'email'),
  2441. 'Email',
  2442. '/label',
  2443. array('select' => array('name' => 'email', 'id' => 'email')),
  2444. array('option' => array('value' => '')),
  2445. '/option',
  2446. array('option' => array('value' => '0')),
  2447. 'First',
  2448. '/option',
  2449. array('option' => array('value' => '1')),
  2450. 'Second',
  2451. '/option',
  2452. '/select',
  2453. '/div'
  2454. );
  2455. $this->assertHtml($expected, $result);
  2456. $result = $this->Form->input('email', [
  2457. 'type' => 'select',
  2458. 'options' => new \ArrayObject(['First', 'Second']),
  2459. 'empty' => true
  2460. ]);
  2461. $this->assertHtml($expected, $result);
  2462. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2463. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2464. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2465. $expected = array(
  2466. 'div' => array('class' => 'input select'),
  2467. 'label' => array('for' => 'model-user-id'),
  2468. 'User',
  2469. '/label',
  2470. 'select' => array('name' => 'Model[user_id]', 'id' => 'model-user-id'),
  2471. array('option' => array('value' => '')),
  2472. '/option',
  2473. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2474. 'good',
  2475. '/option',
  2476. array('option' => array('value' => 'other')),
  2477. 'bad',
  2478. '/option',
  2479. '/select',
  2480. '/div'
  2481. );
  2482. $this->assertHtml($expected, $result);
  2483. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2484. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2485. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2486. $expected = array(
  2487. 'div' => array('class' => 'input select'),
  2488. 'label' => array('for' => 'thing-user-id'),
  2489. 'User',
  2490. '/label',
  2491. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2492. array('option' => array('value' => '')),
  2493. 'Some Empty',
  2494. '/option',
  2495. array('option' => array('value' => 'value')),
  2496. 'good',
  2497. '/option',
  2498. array('option' => array('value' => 'other')),
  2499. 'bad',
  2500. '/option',
  2501. '/select',
  2502. '/div'
  2503. );
  2504. $this->assertHtml($expected, $result);
  2505. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2506. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2507. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2508. $expected = array(
  2509. 'div' => array('class' => 'input select'),
  2510. 'label' => array('for' => 'thing-user-id'),
  2511. 'User',
  2512. '/label',
  2513. 'select' => array('name' => 'Thing[user_id]', 'id' => 'thing-user-id'),
  2514. array('option' => array('value' => '')),
  2515. 'Some Empty',
  2516. '/option',
  2517. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2518. 'good',
  2519. '/option',
  2520. array('option' => array('value' => 'other')),
  2521. 'bad',
  2522. '/option',
  2523. '/select',
  2524. '/div'
  2525. );
  2526. $this->assertHtml($expected, $result);
  2527. $this->Form->data = array();
  2528. $result = $this->Form->input('Publisher.id', array(
  2529. 'label' => 'Publisher',
  2530. 'type' => 'select',
  2531. 'multiple' => 'checkbox',
  2532. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2533. ));
  2534. $expected = array(
  2535. array('div' => array('class' => 'input select')),
  2536. array('label' => array('for' => 'publisher-id')),
  2537. 'Publisher',
  2538. '/label',
  2539. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''),
  2540. array('div' => array('class' => 'checkbox')),
  2541. array('label' => array('for' => 'publisher-id-value-1')),
  2542. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1')),
  2543. 'Label 1',
  2544. '/label',
  2545. '/div',
  2546. array('div' => array('class' => 'checkbox')),
  2547. array('label' => array('for' => 'publisher-id-value-2')),
  2548. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2')),
  2549. 'Label 2',
  2550. '/label',
  2551. '/div',
  2552. '/div'
  2553. );
  2554. $this->assertHtml($expected, $result);
  2555. }
  2556. /**
  2557. * test that input() and a non standard primary key makes a hidden input by default.
  2558. *
  2559. * @return void
  2560. */
  2561. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2562. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  2563. $this->Form->create($this->article);
  2564. $result = $this->Form->input('title');
  2565. $expected = array(
  2566. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2567. );
  2568. $this->assertHtml($expected, $result);
  2569. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  2570. $this->Form->create($this->article);
  2571. $result = $this->Form->input('title');
  2572. $expected = array(
  2573. 'input' => array('type' => 'hidden', 'name' => 'title', 'id' => 'title'),
  2574. );
  2575. $this->assertHtml($expected, $result);
  2576. $result = $this->Form->input('body');
  2577. $expected = array(
  2578. 'input' => array('type' => 'hidden', 'name' => 'body', 'id' => 'body'),
  2579. );
  2580. $this->assertHtml($expected, $result);
  2581. }
  2582. /**
  2583. * test that overriding the magic select type widget is possible
  2584. *
  2585. * @return void
  2586. */
  2587. public function testInputOverridingMagicSelectType() {
  2588. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2589. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2590. $expected = array(
  2591. 'div' => array('class' => 'input text'),
  2592. 'label' => array('for' => 'model-user-id'), 'User', '/label',
  2593. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'),
  2594. '/div'
  2595. );
  2596. $this->assertHtml($expected, $result);
  2597. //Check that magic types still work for plural/singular vars
  2598. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2599. $result = $this->Form->input('Model.type');
  2600. $expected = array(
  2601. 'div' => array('class' => 'input select'),
  2602. 'label' => array('for' => 'model-type'), 'Type', '/label',
  2603. 'select' => array('name' => 'Model[type]', 'id' => 'model-type'),
  2604. array('option' => array('value' => 'value')), 'good', '/option',
  2605. array('option' => array('value' => 'other')), 'bad', '/option',
  2606. '/select',
  2607. '/div'
  2608. );
  2609. $this->assertHtml($expected, $result);
  2610. }
  2611. /**
  2612. * Test that inferred types do not override developer input
  2613. *
  2614. * @return void
  2615. */
  2616. public function testInputMagicTypeDoesNotOverride() {
  2617. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2618. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  2619. $expected = array(
  2620. 'div' => array('class' => 'input checkbox'),
  2621. array('input' => array(
  2622. 'type' => 'hidden',
  2623. 'name' => 'Model[user]',
  2624. 'value' => 0,
  2625. )),
  2626. 'label' => array('for' => 'model-user'),
  2627. array('input' => array(
  2628. 'name' => 'Model[user]',
  2629. 'type' => 'checkbox',
  2630. 'id' => 'model-user',
  2631. 'value' => 1
  2632. )),
  2633. 'User',
  2634. '/label',
  2635. '/div'
  2636. );
  2637. $this->assertHtml($expected, $result);
  2638. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  2639. $options = array(
  2640. 1 => 'blue',
  2641. 2 => 'red'
  2642. );
  2643. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  2644. $expected = array(
  2645. 'div' => array('class' => 'input select'),
  2646. 'label' => array('for' => 'tags-ids'),
  2647. 'Tags',
  2648. '/label',
  2649. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  2650. array('div' => array('class' => 'checkbox')),
  2651. array('label' => array('for' => 'tags-ids-1')),
  2652. array('input' => array(
  2653. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  2654. 'value' => '1', 'name' => 'tags[_ids][]'
  2655. )),
  2656. 'blue',
  2657. '/label',
  2658. '/div',
  2659. array('div' => array('class' => 'checkbox')),
  2660. array('label' => array('for' => 'tags-ids-2')),
  2661. array('input' => array(
  2662. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  2663. 'value' => '2', 'name' => 'tags[_ids][]'
  2664. )),
  2665. 'red',
  2666. '/label',
  2667. '/div',
  2668. '/div'
  2669. );
  2670. $this->assertHtml($expected, $result);
  2671. }
  2672. /**
  2673. * Test that magic input() selects are created for type=number
  2674. *
  2675. * @return void
  2676. */
  2677. public function testInputMagicSelectForTypeNumber() {
  2678. TableRegistry::get('ValidateUsers', [
  2679. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2680. ]);
  2681. $entity = new Entity(['balance' => 1]);
  2682. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  2683. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2684. $result = $this->Form->input('balance');
  2685. $expected = array(
  2686. 'div' => array('class' => 'input select'),
  2687. 'label' => array('for' => 'balance'),
  2688. 'Balance',
  2689. '/label',
  2690. 'select' => array('name' => 'balance', 'id' => 'balance'),
  2691. array('option' => array('value' => '0')),
  2692. 'nothing',
  2693. '/option',
  2694. array('option' => array('value' => '1', 'selected' => 'selected')),
  2695. 'some',
  2696. '/option',
  2697. array('option' => array('value' => '100')),
  2698. 'a lot',
  2699. '/option',
  2700. '/select',
  2701. '/div'
  2702. );
  2703. $this->assertHtml($expected, $result);
  2704. }
  2705. /**
  2706. * Test that magic input() selects can easily be converted into radio types without error.
  2707. *
  2708. * @return void
  2709. */
  2710. public function testInputMagicSelectChangeToRadio() {
  2711. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2712. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2713. $this->assertContains('input type="radio"', $result);
  2714. }
  2715. /**
  2716. * testFormInputs method
  2717. *
  2718. * test correct results from form::inputs().
  2719. *
  2720. * @return void
  2721. */
  2722. public function testFormInputsLegendFieldset() {
  2723. $this->Form->create($this->article);
  2724. $result = $this->Form->allInputs([], array('legend' => 'The Legend'));
  2725. $expected = array(
  2726. '<fieldset',
  2727. '<legend',
  2728. 'The Legend',
  2729. '/legend',
  2730. '*/fieldset',
  2731. );
  2732. $this->assertHtml($expected, $result);
  2733. $result = $this->Form->allInputs([], array('fieldset' => true, 'legend' => 'Field of Dreams'));
  2734. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  2735. $this->assertContains('<fieldset>', $result);
  2736. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => false));
  2737. $this->assertNotContains('<legend>', $result);
  2738. $this->assertNotContains('<fieldset>', $result);
  2739. $result = $this->Form->allInputs([], array('fieldset' => false, 'legend' => 'Hello'));
  2740. $this->assertNotContains('<legend>', $result);
  2741. $this->assertNotContains('<fieldset>', $result);
  2742. $this->Form->create($this->article);
  2743. $this->Form->request->params['prefix'] = 'admin';
  2744. $this->Form->request->params['action'] = 'admin_edit';
  2745. $this->Form->request->params['controller'] = 'articles';
  2746. $result = $this->Form->allInputs();
  2747. $expected = [
  2748. '<fieldset',
  2749. '<legend',
  2750. 'New Article',
  2751. '/legend',
  2752. '*/fieldset',
  2753. ];
  2754. $this->assertHtml($expected, $result);
  2755. }
  2756. /**
  2757. * Test the inputs() method.
  2758. *
  2759. * @return void
  2760. */
  2761. public function testFormInputs() {
  2762. $this->Form->create($this->article);
  2763. $result = $this->Form->allInputs();
  2764. $expected = array(
  2765. '<fieldset',
  2766. '<legend', 'New Article', '/legend',
  2767. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2768. array('div' => array('class' => 'input select required')),
  2769. '*/div',
  2770. array('div' => array('class' => 'input text required')),
  2771. '*/div',
  2772. array('div' => array('class' => 'input text')),
  2773. '*/div',
  2774. array('div' => array('class' => 'input text')),
  2775. '*/div',
  2776. '/fieldset',
  2777. );
  2778. $this->assertHtml($expected, $result);
  2779. $result = $this->Form->allInputs([
  2780. 'published' => ['type' => 'boolean']
  2781. ]);
  2782. $expected = array(
  2783. '<fieldset',
  2784. '<legend', 'New Article', '/legend',
  2785. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2786. array('div' => array('class' => 'input select required')),
  2787. '*/div',
  2788. array('div' => array('class' => 'input text required')),
  2789. '*/div',
  2790. array('div' => array('class' => 'input text')),
  2791. '*/div',
  2792. array('div' => array('class' => 'input boolean')),
  2793. '*/div',
  2794. '/fieldset',
  2795. );
  2796. $this->assertHtml($expected, $result);
  2797. $this->Form->create($this->article);
  2798. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  2799. $expected = array(
  2800. 'fieldset' => array(),
  2801. 'legend' => array(),
  2802. 'Hello',
  2803. '/legend',
  2804. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2805. array('div' => array('class' => 'input select required')),
  2806. '*/div',
  2807. array('div' => array('class' => 'input text required')),
  2808. '*/div',
  2809. array('div' => array('class' => 'input text')),
  2810. '*/div',
  2811. array('div' => array('class' => 'input text')),
  2812. '*/div',
  2813. '/fieldset'
  2814. );
  2815. $this->assertHtml($expected, $result);
  2816. $this->Form->create(false);
  2817. $expected = array(
  2818. 'fieldset' => array(),
  2819. array('div' => array('class' => 'input text')),
  2820. 'label' => array('for' => 'foo'),
  2821. 'Foo',
  2822. '/label',
  2823. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  2824. '*/div',
  2825. '/fieldset'
  2826. );
  2827. $result = $this->Form->allInputs(
  2828. array('foo' => array('type' => 'text')),
  2829. array('legend' => false)
  2830. );
  2831. $this->assertHtml($expected, $result);
  2832. }
  2833. /**
  2834. * testFormInputsBlacklist
  2835. *
  2836. * @return void
  2837. */
  2838. public function testFormInputsBlacklist() {
  2839. $this->Form->create($this->article);
  2840. $result = $this->Form->allInputs([
  2841. 'id' => false
  2842. ]);
  2843. $expected = array(
  2844. '<fieldset',
  2845. '<legend', 'New Article', '/legend',
  2846. array('div' => array('class' => 'input select required')),
  2847. '*/div',
  2848. array('div' => array('class' => 'input text required')),
  2849. '*/div',
  2850. array('div' => array('class' => 'input text')),
  2851. '*/div',
  2852. array('div' => array('class' => 'input text')),
  2853. '*/div',
  2854. '/fieldset',
  2855. );
  2856. $this->assertHtml($expected, $result);
  2857. $this->Form->create($this->article);
  2858. $result = $this->Form->allInputs([
  2859. 'id' => []
  2860. ]);
  2861. $expected = array(
  2862. '<fieldset',
  2863. '<legend', 'New Article', '/legend',
  2864. 'input' => array('type' => 'hidden', 'name' => 'id', 'id' => 'id'),
  2865. array('div' => array('class' => 'input select required')),
  2866. '*/div',
  2867. array('div' => array('class' => 'input text required')),
  2868. '*/div',
  2869. array('div' => array('class' => 'input text')),
  2870. '*/div',
  2871. array('div' => array('class' => 'input text')),
  2872. '*/div',
  2873. '/fieldset',
  2874. );
  2875. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  2876. }
  2877. /**
  2878. * testSelectAsCheckbox method
  2879. *
  2880. * test multi-select widget with checkbox formatting.
  2881. *
  2882. * @return void
  2883. */
  2884. public function testSelectAsCheckbox() {
  2885. $result = $this->Form->select(
  2886. 'Model.multi_field',
  2887. array('first', 'second', 'third'),
  2888. array('multiple' => 'checkbox', 'value' => array(0, 1))
  2889. );
  2890. $expected = array(
  2891. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2892. array('div' => array('class' => 'checkbox')),
  2893. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  2894. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  2895. 'first',
  2896. '/label',
  2897. '/div',
  2898. array('div' => array('class' => 'checkbox')),
  2899. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  2900. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  2901. 'second',
  2902. '/label',
  2903. '/div',
  2904. array('div' => array('class' => 'checkbox')),
  2905. array('label' => array('for' => 'model-multi-field-2')),
  2906. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  2907. 'third',
  2908. '/label',
  2909. '/div',
  2910. );
  2911. $this->assertHtml($expected, $result);
  2912. $result = $this->Form->select(
  2913. 'Model.multi_field',
  2914. array('1/2' => 'half'),
  2915. array('multiple' => 'checkbox')
  2916. );
  2917. $expected = array(
  2918. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  2919. array('div' => array('class' => 'checkbox')),
  2920. array('label' => array('for' => 'model-multi-field-1-2')),
  2921. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  2922. 'half',
  2923. '/label',
  2924. '/div',
  2925. );
  2926. $this->assertHtml($expected, $result);
  2927. }
  2928. /**
  2929. * testLabel method
  2930. *
  2931. * test label generation.
  2932. *
  2933. * @return void
  2934. */
  2935. public function testLabel() {
  2936. $result = $this->Form->label('Person.name');
  2937. $expected = array('label' => array('for' => 'person-name'), 'Name', '/label');
  2938. $this->assertHtml($expected, $result);
  2939. $result = $this->Form->label('Person.name');
  2940. $expected = array('label' => array('for' => 'person-name'), 'Name', '/label');
  2941. $this->assertHtml($expected, $result);
  2942. $result = $this->Form->label('Person.first_name');
  2943. $expected = array('label' => array('for' => 'person-first-name'), 'First Name', '/label');
  2944. $this->assertHtml($expected, $result);
  2945. $result = $this->Form->label('Person.first_name', 'Your first name');
  2946. $expected = array('label' => array('for' => 'person-first-name'), 'Your first name', '/label');
  2947. $this->assertHtml($expected, $result);
  2948. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2949. $expected = array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label');
  2950. $this->assertHtml($expected, $result);
  2951. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2952. $expected = array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label');
  2953. $this->assertHtml($expected, $result);
  2954. $result = $this->Form->label('Person.first_name', '');
  2955. $expected = array('label' => array('for' => 'person-first-name'), '/label');
  2956. $this->assertHtml($expected, $result);
  2957. $result = $this->Form->label('Person.2.name', '');
  2958. $expected = array('label' => array('for' => 'person-2-name'), '/label');
  2959. $this->assertHtml($expected, $result);
  2960. }
  2961. /**
  2962. * Test that label() can accept an input with the correct template vars.
  2963. *
  2964. * @return void
  2965. */
  2966. public function testLabelContainInput() {
  2967. $this->Form->templates([
  2968. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  2969. ]);
  2970. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  2971. 'input' => '<input type="checkbox" name="accept_tos" >'
  2972. ]);
  2973. $expected = [
  2974. 'label' => ['for' => 'person-accept-terms'],
  2975. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  2976. 'Accept',
  2977. '/label',
  2978. ];
  2979. $this->assertHtml($expected, $result);
  2980. }
  2981. /**
  2982. * testTextbox method
  2983. *
  2984. * test textbox element generation
  2985. *
  2986. * @return void
  2987. */
  2988. public function testTextbox() {
  2989. $result = $this->Form->text('Model.field');
  2990. $expected = array('input' => array('type' => 'text', 'name' => 'Model[field]'));
  2991. $this->assertHtml($expected, $result);
  2992. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2993. $expected = array('input' => array('type' => 'password', 'name' => 'Model[field]'));
  2994. $this->assertHtml($expected, $result);
  2995. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2996. $expected = array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID'));
  2997. $this->assertHtml($expected, $result);
  2998. }
  2999. /**
  3000. * Test that text() hooks up with request data and error fields.
  3001. *
  3002. * @return void
  3003. */
  3004. public function testTextBoxDataAndError() {
  3005. $this->article['errors'] = [
  3006. 'Contact' => ['text' => 'wrong']
  3007. ];
  3008. $this->Form->create($this->article);
  3009. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3010. $result = $this->Form->text('Model.text');
  3011. $expected = [
  3012. 'input' => [
  3013. 'type' => 'text',
  3014. 'name' => 'Model[text]',
  3015. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3016. ]
  3017. ];
  3018. $this->assertHtml($expected, $result);
  3019. $this->Form->request->data['Contact']['text'] = 'test';
  3020. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3021. $expected = [
  3022. 'input' => [
  3023. 'type' => 'text',
  3024. 'name' => 'Contact[text]',
  3025. 'value' => 'test',
  3026. 'id' => 'theID',
  3027. 'class' => 'form-error'
  3028. ]
  3029. ];
  3030. $this->assertHtml($expected, $result);
  3031. }
  3032. /**
  3033. * testDefaultValue method
  3034. *
  3035. * Test default value setting
  3036. *
  3037. * @return void
  3038. */
  3039. public function testTextDefaultValue() {
  3040. $this->Form->request->data['Model']['field'] = 'test';
  3041. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3042. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3043. $this->assertHtml($expected, $result);
  3044. unset($this->Form->request->data['Model']['field']);
  3045. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3046. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3047. $this->assertHtml($expected, $result);
  3048. }
  3049. /**
  3050. * testError method
  3051. *
  3052. * Test field error generation
  3053. *
  3054. * @return void
  3055. */
  3056. public function testError() {
  3057. $this->article['errors'] = [
  3058. 'Article' => ['field' => 'email']
  3059. ];
  3060. $this->Form->create($this->article);
  3061. $result = $this->Form->error('Article.field');
  3062. $expected = [
  3063. ['div' => ['class' => 'error-message']],
  3064. 'email',
  3065. '/div',
  3066. ];
  3067. $this->assertHtml($expected, $result);
  3068. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3069. $expected = [
  3070. ['div' => ['class' => 'error-message']],
  3071. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3072. '/div',
  3073. ];
  3074. $this->assertHtml($expected, $result);
  3075. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3076. $expected = [
  3077. ['div' => ['class' => 'error-message']],
  3078. '<strong', 'Badness!', '/strong',
  3079. '/div',
  3080. ];
  3081. $this->assertHtml($expected, $result);
  3082. }
  3083. /**
  3084. * Test error with nested lists.
  3085. *
  3086. * @return void
  3087. */
  3088. public function testErrorMessages() {
  3089. $this->article['errors'] = [
  3090. 'Article' => ['field' => 'email']
  3091. ];
  3092. $this->Form->create($this->article);
  3093. $result = $this->Form->error('Article.field', array(
  3094. 'email' => 'No good!'
  3095. ));
  3096. $expected = array(
  3097. 'div' => array('class' => 'error-message'),
  3098. 'No good!',
  3099. '/div'
  3100. );
  3101. $this->assertHtml($expected, $result);
  3102. }
  3103. /**
  3104. * test error() with multiple messages.
  3105. *
  3106. * @return void
  3107. */
  3108. public function testErrorMultipleMessages() {
  3109. $this->article['errors'] = [
  3110. 'field' => ['notEmpty', 'email', 'Something else']
  3111. ];
  3112. $this->Form->create($this->article);
  3113. $result = $this->Form->error('field', array(
  3114. 'notEmpty' => 'Cannot be empty',
  3115. 'email' => 'No good!'
  3116. ));
  3117. $expected = array(
  3118. 'div' => array('class' => 'error-message'),
  3119. 'ul' => array(),
  3120. '<li', 'Cannot be empty', '/li',
  3121. '<li', 'No good!', '/li',
  3122. '<li', 'Something else', '/li',
  3123. '/ul',
  3124. '/div'
  3125. );
  3126. $this->assertHtml($expected, $result);
  3127. }
  3128. /**
  3129. * testPassword method
  3130. *
  3131. * Test password element generation
  3132. *
  3133. * @return void
  3134. */
  3135. public function testPassword() {
  3136. $this->article['errors'] = [
  3137. 'Contact' => [
  3138. 'passwd' => 1
  3139. ]
  3140. ];
  3141. $this->Form->create($this->article);
  3142. $result = $this->Form->password('Contact.field');
  3143. $expected = array('input' => array('type' => 'password', 'name' => 'Contact[field]'));
  3144. $this->assertHtml($expected, $result);
  3145. $this->Form->request->data['Contact']['passwd'] = 'test';
  3146. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3147. $expected = array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error'));
  3148. $this->assertHtml($expected, $result);
  3149. }
  3150. /**
  3151. * testRadio method
  3152. *
  3153. * Test radio element set generation
  3154. *
  3155. * @return void
  3156. */
  3157. public function testRadio() {
  3158. $result = $this->Form->radio('Model.field', array('option A'));
  3159. $expected = array(
  3160. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3161. 'label' => array('for' => 'model-field-0'),
  3162. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3163. 'option A',
  3164. '/label'
  3165. );
  3166. $this->assertHtml($expected, $result);
  3167. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  3168. $this->assertHtml($expected, $result);
  3169. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3170. $expected = array(
  3171. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3172. array('label' => array('for' => 'model-field-0')),
  3173. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3174. 'option A',
  3175. '/label',
  3176. array('label' => array('for' => 'model-field-1')),
  3177. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3178. 'option B',
  3179. '/label',
  3180. );
  3181. $this->assertHtml($expected, $result);
  3182. $result = $this->Form->radio(
  3183. 'Employee.gender',
  3184. array('male' => 'Male', 'female' => 'Female'),
  3185. ['form' => 'my-form']
  3186. );
  3187. $expected = array(
  3188. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'),
  3189. array('label' => array('for' => 'employee-gender-male')),
  3190. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form')),
  3191. 'Male',
  3192. '/label',
  3193. array('label' => array('for' => 'employee-gender-female')),
  3194. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form')),
  3195. 'Female',
  3196. '/label',
  3197. );
  3198. $this->assertHtml($expected, $result);
  3199. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3200. $expected = array(
  3201. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3202. array('label' => array('for' => 'model-custom-0')),
  3203. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3204. 'option A',
  3205. '/label',
  3206. array('label' => array('for' => 'model-custom-1')),
  3207. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3208. 'option B',
  3209. '/label',
  3210. );
  3211. $this->assertHtml($expected, $result);
  3212. }
  3213. /**
  3214. * Test that input works with radio types
  3215. *
  3216. * @return void
  3217. */
  3218. public function testInputRadio() {
  3219. $result = $this->Form->input('test', [
  3220. 'type' => 'radio',
  3221. 'options' => ['A', 'B'],
  3222. ]);
  3223. $expected = [
  3224. ['div' => ['class' => 'input radio']],
  3225. '<label',
  3226. 'Test',
  3227. '/label',
  3228. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3229. ['label' => ['for' => 'test-0']],
  3230. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3231. 'A',
  3232. '/label',
  3233. ['label' => ['for' => 'test-1']],
  3234. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3235. 'B',
  3236. '/label',
  3237. '/div',
  3238. ];
  3239. $this->assertHtml($expected, $result);
  3240. $result = $this->Form->input('test', [
  3241. 'type' => 'radio',
  3242. 'options' => ['A', 'B'],
  3243. 'label' => false
  3244. ]);
  3245. $expected = [
  3246. ['div' => ['class' => 'input radio']],
  3247. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3248. ['label' => ['for' => 'test-0']],
  3249. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3250. 'A',
  3251. '/label',
  3252. ['label' => ['for' => 'test-1']],
  3253. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3254. 'B',
  3255. '/label',
  3256. '/div',
  3257. ];
  3258. $this->assertHtml($expected, $result);
  3259. }
  3260. /**
  3261. * Test that radio() works with label = false
  3262. *
  3263. * @return void
  3264. */
  3265. public function testRadioNoLabel() {
  3266. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  3267. $expected = [
  3268. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3269. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3270. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  3271. ];
  3272. $this->assertHtml($expected, $result);
  3273. }
  3274. /**
  3275. * test generating radio input inside label ala twitter bootstrap
  3276. *
  3277. * @return void
  3278. */
  3279. public function testRadioInputInsideLabel() {
  3280. $this->Form->templates([
  3281. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3282. 'radioWrapper' => '{{label}}'
  3283. ]);
  3284. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3285. $expected = [
  3286. ['input' => [
  3287. 'type' => 'hidden',
  3288. 'name' => 'Model[field]',
  3289. 'value' => ''
  3290. ]],
  3291. ['label' => ['for' => 'model-field-0']],
  3292. ['input' => [
  3293. 'type' => 'radio',
  3294. 'name' => 'Model[field]',
  3295. 'value' => '0',
  3296. 'id' => 'model-field-0'
  3297. ]],
  3298. 'option A',
  3299. '/label',
  3300. ['label' => ['for' => 'model-field-1']],
  3301. ['input' => [
  3302. 'type' => 'radio',
  3303. 'name' => 'Model[field]',
  3304. 'value' => '1',
  3305. 'id' => 'model-field-1'
  3306. ]],
  3307. 'option B',
  3308. '/label',
  3309. ];
  3310. $this->assertHtml($expected, $result);
  3311. }
  3312. /**
  3313. * test disabling the hidden input for radio buttons
  3314. *
  3315. * @return void
  3316. */
  3317. public function testRadioHiddenInputDisabling() {
  3318. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3319. $expected = array(
  3320. 'label' => array('for' => 'model-1-field-0'),
  3321. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3322. 'option A',
  3323. '/label'
  3324. );
  3325. $this->assertHtml($expected, $result);
  3326. }
  3327. /**
  3328. * testSelect method
  3329. *
  3330. * Test select element generation.
  3331. *
  3332. * @return void
  3333. */
  3334. public function testSelect() {
  3335. $result = $this->Form->select('Model.field', array());
  3336. $expected = array(
  3337. 'select' => array('name' => 'Model[field]'),
  3338. '/select'
  3339. );
  3340. $this->assertHtml($expected, $result);
  3341. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3342. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3343. $expected = array(
  3344. 'select' => array('name' => 'Model[field]'),
  3345. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3346. 'good',
  3347. '/option',
  3348. array('option' => array('value' => 'other')),
  3349. 'bad',
  3350. '/option',
  3351. '/select'
  3352. );
  3353. $this->assertHtml($expected, $result);
  3354. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  3355. $this->assertHtml($expected, $result);
  3356. $this->Form->request->data = array();
  3357. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3358. $expected = array(
  3359. 'select' => array('name' => 'Model[field]'),
  3360. array('option' => array('value' => 'value')),
  3361. 'good',
  3362. '/option',
  3363. array('option' => array('value' => 'other')),
  3364. 'bad',
  3365. '/option',
  3366. '/select'
  3367. );
  3368. $this->assertHtml($expected, $result);
  3369. $options = array(
  3370. array('value' => 'first', 'text' => 'First'),
  3371. array('value' => 'first', 'text' => 'Another First'),
  3372. );
  3373. $result = $this->Form->select(
  3374. 'Model.field',
  3375. $options,
  3376. array('escape' => false, 'empty' => false)
  3377. );
  3378. $expected = array(
  3379. 'select' => array('name' => 'Model[field]'),
  3380. array('option' => array('value' => 'first')),
  3381. 'First',
  3382. '/option',
  3383. array('option' => array('value' => 'first')),
  3384. 'Another First',
  3385. '/option',
  3386. '/select'
  3387. );
  3388. $this->assertHtml($expected, $result);
  3389. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3390. $result = $this->Form->select(
  3391. 'Model.contact_id',
  3392. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3393. array('escape' => false, 'empty' => 'pick something')
  3394. );
  3395. $expected = array(
  3396. 'select' => array('name' => 'Model[contact_id]'),
  3397. array('option' => array('value' => '')), 'pick something', '/option',
  3398. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3399. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3400. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3401. '/select'
  3402. );
  3403. $this->assertHtml($expected, $result);
  3404. $this->Form->request->data['Model']['field'] = 0;
  3405. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3406. $expected = array(
  3407. 'select' => array('name' => 'Model[field]'),
  3408. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3409. array('option' => array('value' => '1')), 'Yes', '/option',
  3410. '/select'
  3411. );
  3412. $this->assertHtml($expected, $result);
  3413. }
  3414. /**
  3415. * Test that select() escapes HTML.
  3416. *
  3417. * @return void
  3418. */
  3419. public function testSelectEscapeHtml() {
  3420. $result = $this->Form->select(
  3421. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3422. array('empty' => false)
  3423. );
  3424. $expected = array(
  3425. 'select' => array('name' => 'Model[field]'),
  3426. array('option' => array('value' => 'first')),
  3427. 'first &quot;html&quot; &lt;chars&gt;',
  3428. '/option',
  3429. array('option' => array('value' => 'second')),
  3430. 'value',
  3431. '/option',
  3432. '/select'
  3433. );
  3434. $this->assertHtml($expected, $result);
  3435. $result = $this->Form->select(
  3436. 'Model.field',
  3437. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3438. array('escape' => false, 'empty' => false)
  3439. );
  3440. $expected = array(
  3441. 'select' => array('name' => 'Model[field]'),
  3442. array('option' => array('value' => 'first')),
  3443. 'first "html" <chars>',
  3444. '/option',
  3445. array('option' => array('value' => 'second')),
  3446. 'value',
  3447. '/option',
  3448. '/select'
  3449. );
  3450. $this->assertHtml($expected, $result);
  3451. }
  3452. /**
  3453. * test select() with required and disabled attributes.
  3454. *
  3455. * @return void
  3456. */
  3457. public function testSelectRequired() {
  3458. $this->article['required'] = [
  3459. 'user_id' => true
  3460. ];
  3461. $this->Form->create($this->article);
  3462. $result = $this->Form->select('user_id', array('option A'));
  3463. $expected = array(
  3464. 'select' => array(
  3465. 'name' => 'user_id',
  3466. 'required' => 'required'
  3467. ),
  3468. array('option' => array('value' => '0')), 'option A', '/option',
  3469. '/select'
  3470. );
  3471. $this->assertHtml($expected, $result);
  3472. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  3473. $expected = array(
  3474. 'select' => array(
  3475. 'name' => 'user_id',
  3476. 'disabled' => 'disabled'
  3477. ),
  3478. array('option' => array('value' => '0')), 'option A', '/option',
  3479. '/select'
  3480. );
  3481. $this->assertHtml($expected, $result);
  3482. }
  3483. /**
  3484. * testNestedSelect method
  3485. *
  3486. * test select element generation with optgroups
  3487. *
  3488. * @return void
  3489. */
  3490. public function testNestedSelect() {
  3491. $result = $this->Form->select(
  3492. 'Model.field',
  3493. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3494. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3495. )), array('empty' => false)
  3496. );
  3497. $expected = array(
  3498. 'select' => array('name' => 'Model[field]'),
  3499. array('option' => array('value' => 1)),
  3500. 'One',
  3501. '/option',
  3502. array('option' => array('value' => 2)),
  3503. 'Two',
  3504. '/option',
  3505. array('optgroup' => array('label' => 'Three')),
  3506. array('option' => array('value' => 3)),
  3507. 'Three',
  3508. '/option',
  3509. array('option' => array('value' => 4)),
  3510. 'Four',
  3511. '/option',
  3512. array('option' => array('value' => 5)),
  3513. 'Five',
  3514. '/option',
  3515. '/optgroup',
  3516. '/select'
  3517. );
  3518. $this->assertHtml($expected, $result);
  3519. }
  3520. /**
  3521. * testSelectMultiple method
  3522. *
  3523. * test generation of multiple select elements
  3524. *
  3525. * @return void
  3526. */
  3527. public function testSelectMultiple() {
  3528. $options = array('first', 'second', 'third');
  3529. $result = $this->Form->select(
  3530. 'Model.multi_field',
  3531. $options,
  3532. ['form' => 'my-form', 'multiple' => true]
  3533. );
  3534. $expected = array(
  3535. 'input' => array(
  3536. 'type' => 'hidden',
  3537. 'name' => 'Model[multi_field]',
  3538. 'value' => '',
  3539. 'form' => 'my-form',
  3540. ),
  3541. 'select' => array(
  3542. 'name' => 'Model[multi_field][]',
  3543. 'multiple' => 'multiple',
  3544. 'form' => 'my-form',
  3545. ),
  3546. array('option' => array('value' => '0')),
  3547. 'first',
  3548. '/option',
  3549. array('option' => array('value' => '1')),
  3550. 'second',
  3551. '/option',
  3552. array('option' => array('value' => '2')),
  3553. 'third',
  3554. '/option',
  3555. '/select'
  3556. );
  3557. $this->assertHtml($expected, $result);
  3558. $result = $this->Form->select(
  3559. 'Model.multi_field',
  3560. $options,
  3561. ['multiple' => 'multiple', 'form' => 'my-form']
  3562. );
  3563. $this->assertHtml($expected, $result);
  3564. }
  3565. /**
  3566. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  3567. *
  3568. * @return void
  3569. */
  3570. public function testCheckboxZeroValue() {
  3571. $result = $this->Form->input('User.get_spam', array(
  3572. 'type' => 'checkbox',
  3573. 'value' => '0',
  3574. 'hiddenField' => '1',
  3575. ));
  3576. $expected = array(
  3577. 'div' => array('class' => 'input checkbox'),
  3578. 'label' => array('for' => 'user-get-spam'),
  3579. array('input' => array(
  3580. 'type' => 'hidden', 'name' => 'User[get_spam]',
  3581. 'value' => '1'
  3582. )),
  3583. array('input' => array(
  3584. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  3585. 'value' => '0', 'id' => 'user-get-spam'
  3586. )),
  3587. 'Get Spam',
  3588. '/label',
  3589. '/div'
  3590. );
  3591. $this->assertHtml($expected, $result);
  3592. }
  3593. /**
  3594. * test generation of habtm select boxes.
  3595. *
  3596. * @return void
  3597. */
  3598. public function testHabtmSelectBox() {
  3599. $this->loadFixtures('Articles');
  3600. $options = array(
  3601. 1 => 'blue',
  3602. 2 => 'red',
  3603. 3 => 'green'
  3604. );
  3605. $tags = [
  3606. new Entity(['id' => 1, 'name' => 'blue']),
  3607. new Entity(['id' => 3, 'name' => 'green'])
  3608. ];
  3609. $article = new Article(['tags' => $tags]);
  3610. $this->Form->create($article);
  3611. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3612. $expected = array(
  3613. 'div' => array('class' => 'input select'),
  3614. 'label' => array('for' => 'tags-ids'),
  3615. 'Tags',
  3616. '/label',
  3617. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3618. 'select' => array(
  3619. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3620. 'multiple' => 'multiple'
  3621. ),
  3622. array('option' => array('value' => '1', 'selected' => 'selected')),
  3623. 'blue',
  3624. '/option',
  3625. array('option' => array('value' => '2')),
  3626. 'red',
  3627. '/option',
  3628. array('option' => array('value' => '3', 'selected' => 'selected')),
  3629. 'green',
  3630. '/option',
  3631. '/select',
  3632. '/div'
  3633. );
  3634. $this->assertHtml($expected, $result);
  3635. // make sure only 50 is selected, and not 50f5c0cf
  3636. $options = array(
  3637. '1' => 'blue',
  3638. '50f5c0cf' => 'red',
  3639. '50' => 'green'
  3640. );
  3641. $tags = [
  3642. new Entity(['id' => 1, 'name' => 'blue']),
  3643. new Entity(['id' => 50, 'name' => 'green'])
  3644. ];
  3645. $article = new Article(['tags' => $tags]);
  3646. $this->Form->create($article);
  3647. $result = $this->Form->input('tags._ids', ['options' => $options]);
  3648. $expected = array(
  3649. 'div' => array('class' => 'input select'),
  3650. 'label' => array('for' => 'tags-ids'),
  3651. 'Tags',
  3652. '/label',
  3653. 'input' => array('type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''),
  3654. 'select' => array(
  3655. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  3656. 'multiple' => 'multiple'
  3657. ),
  3658. array('option' => array('value' => '1', 'selected' => 'selected')),
  3659. 'blue',
  3660. '/option',
  3661. array('option' => array('value' => '50f5c0cf')),
  3662. 'red',
  3663. '/option',
  3664. array('option' => array('value' => '50', 'selected' => 'selected')),
  3665. 'green',
  3666. '/option',
  3667. '/select',
  3668. '/div'
  3669. );
  3670. $this->assertHtml($expected, $result);
  3671. }
  3672. /**
  3673. * test generation of multi select elements in checkbox format
  3674. *
  3675. * @return void
  3676. */
  3677. public function testSelectMultipleCheckboxes() {
  3678. $result = $this->Form->select(
  3679. 'Model.multi_field',
  3680. array('first', 'second', 'third'),
  3681. array('multiple' => 'checkbox')
  3682. );
  3683. $expected = array(
  3684. 'input' => array(
  3685. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3686. ),
  3687. array('div' => array('class' => 'checkbox')),
  3688. array('label' => array('for' => 'model-multi-field-0')),
  3689. array('input' => array(
  3690. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3691. 'value' => '0', 'id' => 'model-multi-field-0'
  3692. )),
  3693. 'first',
  3694. '/label',
  3695. '/div',
  3696. array('div' => array('class' => 'checkbox')),
  3697. array('label' => array('for' => 'model-multi-field-1')),
  3698. array('input' => array(
  3699. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3700. 'value' => '1', 'id' => 'model-multi-field-1'
  3701. )),
  3702. 'second',
  3703. '/label',
  3704. '/div',
  3705. array('div' => array('class' => 'checkbox')),
  3706. array('label' => array('for' => 'model-multi-field-2')),
  3707. array('input' => array(
  3708. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3709. 'value' => '2', 'id' => 'model-multi-field-2'
  3710. )),
  3711. 'third',
  3712. '/label',
  3713. '/div'
  3714. );
  3715. $this->assertHtml($expected, $result);
  3716. $result = $this->Form->select(
  3717. 'Model.multi_field',
  3718. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  3719. array('multiple' => 'checkbox')
  3720. );
  3721. $expected = array(
  3722. 'input' => array(
  3723. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3724. ),
  3725. array('div' => array('class' => 'checkbox')),
  3726. array('label' => array('for' => 'model-multi-field-a+')),
  3727. array('input' => array(
  3728. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3729. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  3730. )),
  3731. 'first',
  3732. '/label',
  3733. '/div',
  3734. array('div' => array('class' => 'checkbox')),
  3735. array('label' => array('for' => 'model-multi-field-a++')),
  3736. array('input' => array(
  3737. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3738. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  3739. )),
  3740. 'second',
  3741. '/label',
  3742. '/div',
  3743. array('div' => array('class' => 'checkbox')),
  3744. array('label' => array('for' => 'model-multi-field-a+++')),
  3745. array('input' => array(
  3746. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3747. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  3748. )),
  3749. 'third',
  3750. '/label',
  3751. '/div'
  3752. );
  3753. $this->assertHtml($expected, $result);
  3754. $result = $this->Form->select(
  3755. 'Model.multi_field',
  3756. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  3757. array('multiple' => 'checkbox')
  3758. );
  3759. $expected = array(
  3760. 'input' => array(
  3761. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  3762. ),
  3763. array('div' => array('class' => 'checkbox')),
  3764. array('label' => array('for' => 'model-multi-field-a-b')),
  3765. array('input' => array(
  3766. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3767. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  3768. )),
  3769. 'first',
  3770. '/label',
  3771. '/div',
  3772. array('div' => array('class' => 'checkbox')),
  3773. array('label' => array('for' => 'model-multi-field-a-b1')),
  3774. array('input' => array(
  3775. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3776. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  3777. )),
  3778. 'second',
  3779. '/label',
  3780. '/div',
  3781. array('div' => array('class' => 'checkbox')),
  3782. array('label' => array('for' => 'model-multi-field-a-b2')),
  3783. array('input' => array(
  3784. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  3785. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  3786. )),
  3787. 'third',
  3788. '/label',
  3789. '/div'
  3790. );
  3791. $this->assertHtml($expected, $result);
  3792. }
  3793. /**
  3794. * Ensure that multiCheckbox reads from the request data.
  3795. *
  3796. * @return void
  3797. */
  3798. public function testSelectMultipleCheckboxRequestData() {
  3799. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3800. $result = $this->Form->select(
  3801. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3802. );
  3803. $expected = array(
  3804. 'input' => array(
  3805. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  3806. ),
  3807. array('div' => array('class' => 'checkbox')),
  3808. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  3809. array('input' => array(
  3810. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3811. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  3812. )),
  3813. 'first',
  3814. '/label',
  3815. '/div',
  3816. array('div' => array('class' => 'checkbox')),
  3817. array('label' => array('for' => 'model-tags-array')),
  3818. array('input' => array(
  3819. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  3820. 'value' => 'Array', 'id' => 'model-tags-array'
  3821. )),
  3822. 'Array',
  3823. '/label',
  3824. '/div'
  3825. );
  3826. $this->assertHtml($expected, $result);
  3827. }
  3828. /**
  3829. * Checks the security hash array generated for multiple-input checkbox elements
  3830. *
  3831. * @return void
  3832. */
  3833. public function testSelectMultipleCheckboxSecurity() {
  3834. $this->Form->request->params['_Token'] = 'testKey';
  3835. $this->assertEquals(array(), $this->Form->fields);
  3836. $result = $this->Form->select(
  3837. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3838. array('multiple' => 'checkbox')
  3839. );
  3840. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3841. $result = $this->Form->secure($this->Form->fields);
  3842. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3843. $this->assertRegExp('/"' . $key . '"/', $result);
  3844. }
  3845. /**
  3846. * Multiple select elements should always be secured as they always participate
  3847. * in the POST data.
  3848. *
  3849. * @return void
  3850. */
  3851. public function testSelectMultipleSecureWithNoOptions() {
  3852. $this->assertEquals(array(), $this->Form->fields);
  3853. $this->Form->select(
  3854. 'Model.select',
  3855. array(),
  3856. array('multiple' => true)
  3857. );
  3858. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3859. }
  3860. /**
  3861. * When a select box has no options it should not be added to the fields list
  3862. * as it always fail post validation.
  3863. *
  3864. * @return void
  3865. */
  3866. public function testSelectNoSecureWithNoOptions() {
  3867. $this->Form->request->params['_Token'] = 'testkey';
  3868. $this->assertEquals([], $this->Form->fields);
  3869. $this->Form->select(
  3870. 'Model.select',
  3871. []
  3872. );
  3873. $this->assertEquals([], $this->Form->fields);
  3874. $this->Form->select(
  3875. 'Model.user_id',
  3876. [],
  3877. ['empty' => true]
  3878. );
  3879. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  3880. }
  3881. /**
  3882. * testInputMultipleCheckboxes method
  3883. *
  3884. * test input() resulting in multi select elements being generated.
  3885. *
  3886. * @return void
  3887. */
  3888. public function testInputMultipleCheckboxes() {
  3889. $result = $this->Form->input('Model.multi_field', array(
  3890. 'options' => array('first', 'second', 'third'),
  3891. 'multiple' => 'checkbox'
  3892. ));
  3893. $expected = array(
  3894. array('div' => array('class' => 'input select')),
  3895. array('label' => array('for' => 'model-multi-field')),
  3896. 'Multi Field',
  3897. '/label',
  3898. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3899. array('div' => array('class' => 'checkbox')),
  3900. array('label' => array('for' => 'model-multi-field-0')),
  3901. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0')),
  3902. 'first',
  3903. '/label',
  3904. '/div',
  3905. array('div' => array('class' => 'checkbox')),
  3906. array('label' => array('for' => 'model-multi-field-1')),
  3907. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1')),
  3908. 'second',
  3909. '/label',
  3910. '/div',
  3911. array('div' => array('class' => 'checkbox')),
  3912. array('label' => array('for' => 'model-multi-field-2')),
  3913. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3914. 'third',
  3915. '/label',
  3916. '/div',
  3917. '/div'
  3918. );
  3919. $this->assertHtml($expected, $result);
  3920. $result = $this->Form->input('Model.multi_field', array(
  3921. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3922. 'multiple' => 'checkbox'
  3923. ));
  3924. $expected = array(
  3925. array('div' => array('class' => 'input select')),
  3926. array('label' => array('for' => 'model-multi-field')),
  3927. 'Multi Field',
  3928. '/label',
  3929. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3930. array('div' => array('class' => 'checkbox')),
  3931. array('label' => array('for' => 'model-multi-field-a')),
  3932. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a')),
  3933. 'first',
  3934. '/label',
  3935. '/div',
  3936. array('div' => array('class' => 'checkbox')),
  3937. array('label' => array('for' => 'model-multi-field-b')),
  3938. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b')),
  3939. 'second',
  3940. '/label',
  3941. '/div',
  3942. array('div' => array('class' => 'checkbox')),
  3943. array('label' => array('for' => 'model-multi-field-c')),
  3944. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c')),
  3945. 'third',
  3946. '/label',
  3947. '/div',
  3948. '/div'
  3949. );
  3950. $this->assertHtml($expected, $result);
  3951. }
  3952. /**
  3953. * testSelectHiddenFieldOmission method
  3954. *
  3955. * test that select() with 'hiddenField' => false omits the hidden field
  3956. *
  3957. * @return void
  3958. */
  3959. public function testSelectHiddenFieldOmission() {
  3960. $result = $this->Form->select('Model.multi_field',
  3961. array('first', 'second'),
  3962. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3963. );
  3964. $this->assertNotContains('type="hidden"', $result);
  3965. }
  3966. /**
  3967. * test that select() with multiple = checkbox works with overriding name attribute.
  3968. *
  3969. * @return void
  3970. */
  3971. public function testSelectCheckboxMultipleOverrideName() {
  3972. $result = $this->Form->select('category', ['1', '2'], [
  3973. 'multiple' => 'checkbox',
  3974. 'name' => 'fish',
  3975. ]);
  3976. $expected = array(
  3977. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  3978. array('div' => array('class' => 'checkbox')),
  3979. array('label' => array('for' => 'fish-0')),
  3980. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  3981. '1',
  3982. '/label',
  3983. '/div',
  3984. array('div' => array('class' => 'checkbox')),
  3985. array('label' => array('for' => 'fish-1')),
  3986. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  3987. '2',
  3988. '/label',
  3989. '/div'
  3990. );
  3991. $this->assertHtml($expected, $result);
  3992. $result = $this->Form->multiCheckbox(
  3993. 'category',
  3994. new Collection(['1', '2']),
  3995. ['name' => 'fish']
  3996. );
  3997. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  3998. 'name' => 'fish',
  3999. ]);
  4000. $this->assertHtml($expected, $result);
  4001. }
  4002. /**
  4003. * Test that input() works with multicheckbox.
  4004. *
  4005. * @return void
  4006. */
  4007. public function testInputMultiCheckbox() {
  4008. $result = $this->Form->input('category', [
  4009. 'type' => 'multicheckbox',
  4010. 'options' => ['1', '2'],
  4011. ]);
  4012. $expected = [
  4013. ['div' => ['class' => 'input multicheckbox']],
  4014. '<label',
  4015. 'Category',
  4016. '/label',
  4017. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  4018. ['div' => ['class' => 'checkbox']],
  4019. ['label' => ['for' => 'category-0']],
  4020. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  4021. '1',
  4022. '/label',
  4023. '/div',
  4024. ['div' => ['class' => 'checkbox']],
  4025. ['label' => ['for' => 'category-1']],
  4026. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  4027. '2',
  4028. '/label',
  4029. '/div',
  4030. '/div',
  4031. ];
  4032. $this->assertHtml($expected, $result);
  4033. }
  4034. /**
  4035. * testCheckbox method
  4036. *
  4037. * Test generation of checkboxes
  4038. *
  4039. * @return void
  4040. */
  4041. public function testCheckbox() {
  4042. $result = $this->Form->checkbox('Model.field');
  4043. $expected = array(
  4044. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4045. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4046. );
  4047. $this->assertHtml($expected, $result);
  4048. $result = $this->Form->checkbox('Model.field', array(
  4049. 'id' => 'theID',
  4050. 'value' => 'myvalue',
  4051. 'form' => 'my-form',
  4052. ));
  4053. $expected = array(
  4054. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'),
  4055. array('input' => array(
  4056. 'type' => 'checkbox', 'name' => 'Model[field]',
  4057. 'value' => 'myvalue', 'id' => 'theID',
  4058. 'form' => 'my-form',
  4059. ))
  4060. );
  4061. $this->assertHtml($expected, $result);
  4062. }
  4063. /**
  4064. * testCheckboxDefaultValue method
  4065. *
  4066. * Test default value setting on checkbox() method
  4067. *
  4068. * @return void
  4069. */
  4070. public function testCheckboxDefaultValue() {
  4071. $this->Form->request->data['Model']['field'] = false;
  4072. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4073. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'));
  4074. $this->assertHtml($expected, $result);
  4075. unset($this->Form->request->data['Model']['field']);
  4076. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4077. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked'));
  4078. $this->assertHtml($expected, $result);
  4079. $this->Form->request->data['Model']['field'] = true;
  4080. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4081. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked'));
  4082. $this->assertHtml($expected, $result);
  4083. unset($this->Form->request->data['Model']['field']);
  4084. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4085. $expected = array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'));
  4086. $this->assertHtml($expected, $result);
  4087. }
  4088. /**
  4089. * Test checkbox being checked or having errors.
  4090. *
  4091. * @return void
  4092. */
  4093. public function testCheckboxCheckedAndError() {
  4094. $this->article['errors'] = [
  4095. 'published' => true
  4096. ];
  4097. $this->Form->request->data['published'] = 'myvalue';
  4098. $this->Form->create($this->article);
  4099. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4100. $expected = array(
  4101. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4102. array('input' => array(
  4103. 'type' => 'checkbox',
  4104. 'name' => 'published',
  4105. 'value' => 'myvalue',
  4106. 'id' => 'theID',
  4107. 'checked' => 'checked',
  4108. 'class' => 'form-error'
  4109. ))
  4110. );
  4111. $this->assertHtml($expected, $result);
  4112. $this->Form->request->data['published'] = '';
  4113. $result = $this->Form->checkbox('published');
  4114. $expected = array(
  4115. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4116. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4117. );
  4118. $this->assertHtml($expected, $result);
  4119. }
  4120. /**
  4121. * test checkbox() with a custom name attribute
  4122. *
  4123. * @return void
  4124. */
  4125. public function testCheckboxCustomNameAttribute() {
  4126. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4127. $expected = array(
  4128. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4129. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4130. );
  4131. $this->assertHtml($expected, $result);
  4132. }
  4133. /**
  4134. * Test that the hidden input for checkboxes can be omitted or set to a
  4135. * specific value.
  4136. *
  4137. * @return void
  4138. */
  4139. public function testCheckboxHiddenField() {
  4140. $result = $this->Form->checkbox('UserForm.something', array(
  4141. 'hiddenField' => false
  4142. ));
  4143. $expected = array(
  4144. 'input' => array(
  4145. 'type' => 'checkbox',
  4146. 'name' => 'UserForm[something]',
  4147. 'value' => '1'
  4148. ),
  4149. );
  4150. $this->assertHtml($expected, $result);
  4151. $result = $this->Form->checkbox('UserForm.something', array(
  4152. 'value' => 'Y',
  4153. 'hiddenField' => 'N',
  4154. ));
  4155. $expected = array(
  4156. array('input' => array(
  4157. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4158. 'value' => 'N'
  4159. )),
  4160. array('input' => array(
  4161. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4162. 'value' => 'Y'
  4163. )),
  4164. );
  4165. $this->assertHtml($expected, $result);
  4166. }
  4167. /**
  4168. * Test the time type.
  4169. *
  4170. * @return void
  4171. */
  4172. public function testTime() {
  4173. $result = $this->Form->time('start_time', array(
  4174. 'timeFormat' => 12,
  4175. 'interval' => 5,
  4176. 'value' => array('hour' => '4', 'minute' => '30', 'meridian' => 'pm')
  4177. ));
  4178. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4179. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4180. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4181. $this->assertNotContains('year', $result);
  4182. $this->assertNotContains('month', $result);
  4183. $this->assertNotContains('day', $result);
  4184. $result = $this->Form->time('start_time', array(
  4185. 'timeFormat' => 12,
  4186. 'interval' => 5,
  4187. 'value' => '2014-03-08 16:30:00'
  4188. ));
  4189. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  4190. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4191. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  4192. $this->assertNotContains('year', $result);
  4193. $this->assertNotContains('month', $result);
  4194. $this->assertNotContains('day', $result);
  4195. }
  4196. /**
  4197. * Ensure that timeFormat=24 has no merdian.
  4198. *
  4199. * @return void.
  4200. */
  4201. public function testTimeFormat24NoMeridian() {
  4202. $result = $this->Form->time('start_time', array(
  4203. 'timeFormat' => 24,
  4204. 'interval' => 5,
  4205. 'value' => '2014-03-08 16:30:00'
  4206. ));
  4207. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  4208. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  4209. $this->assertNotContains('meridian', $result);
  4210. $this->assertNotContains('pm', $result);
  4211. $this->assertNotContains('year', $result);
  4212. $this->assertNotContains('month', $result);
  4213. $this->assertNotContains('day', $result);
  4214. }
  4215. /**
  4216. * Test the date type.
  4217. *
  4218. * @return void
  4219. */
  4220. public function testDate() {
  4221. $result = $this->Form->date('start_day', array(
  4222. 'value' => array('year' => '2014', 'month' => '03', 'day' => '08')
  4223. ));
  4224. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  4225. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  4226. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  4227. $this->assertNotContains('hour', $result);
  4228. $this->assertNotContains('minute', $result);
  4229. $this->assertNotContains('second', $result);
  4230. $this->assertNotContains('meridian', $result);
  4231. }
  4232. /**
  4233. * testDateTime method
  4234. *
  4235. * Test generation of date/time select elements
  4236. *
  4237. * @return void
  4238. */
  4239. public function testDateTime() {
  4240. extract($this->dateRegex);
  4241. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4242. $now = strtotime('now');
  4243. $expected = array(
  4244. array('select' => array('name' => 'Contact[date][year]')),
  4245. $yearsRegex,
  4246. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4247. date('Y', $now),
  4248. '/option',
  4249. '*/select',
  4250. array('select' => array('name' => 'Contact[date][month]')),
  4251. $monthsRegex,
  4252. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4253. date('F', $now),
  4254. '/option',
  4255. '*/select',
  4256. array('select' => array('name' => 'Contact[date][day]')),
  4257. $daysRegex,
  4258. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4259. date('j', $now),
  4260. '/option',
  4261. '*/select',
  4262. array('select' => array('name' => 'Contact[date][hour]')),
  4263. $hoursRegex,
  4264. array('option' => array('value' => date('H', $now), 'selected' => 'selected')),
  4265. date('G', $now),
  4266. '/option',
  4267. '*/select',
  4268. array('select' => array('name' => 'Contact[date][minute]')),
  4269. $minutesRegex,
  4270. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4271. date('i', $now),
  4272. '/option',
  4273. '*/select',
  4274. );
  4275. $this->assertHtml($expected, $result);
  4276. }
  4277. /**
  4278. * Test that datetime fields are added to protected fields list.
  4279. *
  4280. * @return void
  4281. */
  4282. public function testDateTimeSecured() {
  4283. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4284. $this->Form->dateTime('Contact.date');
  4285. $expected = [
  4286. 'Contact.date.year',
  4287. 'Contact.date.month',
  4288. 'Contact.date.day',
  4289. 'Contact.date.hour',
  4290. 'Contact.date.minute',
  4291. ];
  4292. $this->assertEquals($expected, $this->Form->fields);
  4293. $this->Form->fields = [];
  4294. $this->Form->date('Contact.published');
  4295. $expected = [
  4296. 'Contact.published.year',
  4297. 'Contact.published.month',
  4298. 'Contact.published.day',
  4299. ];
  4300. $this->assertEquals($expected, $this->Form->fields);
  4301. }
  4302. /**
  4303. * Test that datetime fields are added to protected fields list.
  4304. *
  4305. * @return void
  4306. */
  4307. public function testDateTimeSecuredDisabled() {
  4308. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  4309. $this->Form->dateTime('Contact.date', ['secure' => false]);
  4310. $expected = [];
  4311. $this->assertEquals($expected, $this->Form->fields);
  4312. $this->Form->fields = [];
  4313. $this->Form->date('Contact.published', ['secure' => false]);
  4314. $expected = [];
  4315. $this->assertEquals($expected, $this->Form->fields);
  4316. }
  4317. /**
  4318. * Test empty defaulting to true for datetime.
  4319. *
  4320. * @return void
  4321. */
  4322. public function testDatetimeEmpty() {
  4323. extract($this->dateRegex);
  4324. $now = strtotime('now');
  4325. $result = $this->Form->dateTime('Contact.date', array(
  4326. 'timeFormat' => 12,
  4327. 'empty' => true,
  4328. ));
  4329. $expected = array(
  4330. array('select' => array('name' => 'Contact[date][year]')),
  4331. $yearsRegex,
  4332. array('option' => array('value' => '')),
  4333. '/option',
  4334. '*/select',
  4335. array('select' => array('name' => 'Contact[date][month]')),
  4336. $monthsRegex,
  4337. array('option' => array('value' => '')),
  4338. '/option',
  4339. '*/select',
  4340. array('select' => array('name' => 'Contact[date][day]')),
  4341. $daysRegex,
  4342. array('option' => array('value' => '')),
  4343. '/option',
  4344. '*/select',
  4345. array('select' => array('name' => 'Contact[date][hour]')),
  4346. $hoursRegex,
  4347. array('option' => array('value' => '')),
  4348. '/option',
  4349. '*/select',
  4350. array('select' => array('name' => 'Contact[date][minute]')),
  4351. $minutesRegex,
  4352. array('option' => array('value' => '')),
  4353. '/option',
  4354. '*/select',
  4355. array('select' => array('name' => 'Contact[date][meridian]')),
  4356. $meridianRegex,
  4357. array('option' => array('value' => '')),
  4358. '/option',
  4359. '*/select'
  4360. );
  4361. $this->assertHtml($expected, $result);
  4362. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4363. }
  4364. /**
  4365. * Test datetime with interval option.
  4366. *
  4367. * @return void
  4368. */
  4369. public function testDatetimeMinuteInterval() {
  4370. extract($this->dateRegex);
  4371. $now = strtotime('now');
  4372. $result = $this->Form->dateTime('Contact.date', array(
  4373. 'interval' => 5,
  4374. 'value' => ''
  4375. ));
  4376. $expected = array(
  4377. array('select' => array('name' => 'Contact[date][year]')),
  4378. $yearsRegex,
  4379. array('option' => array('selected' => 'selected', 'value' => '')),
  4380. '/option',
  4381. '*/select',
  4382. array('select' => array('name' => 'Contact[date][month]')),
  4383. $monthsRegex,
  4384. array('option' => array('selected' => 'selected', 'value' => '')),
  4385. '/option',
  4386. '*/select',
  4387. array('select' => array('name' => 'Contact[date][day]')),
  4388. $daysRegex,
  4389. array('option' => array('selected' => 'selected', 'value' => '')),
  4390. '/option',
  4391. '*/select',
  4392. array('select' => array('name' => 'Contact[date][hour]')),
  4393. $hoursRegex,
  4394. array('option' => array('selected' => 'selected', 'value' => '')),
  4395. '/option',
  4396. '*/select',
  4397. array('select' => array('name' => 'Contact[date][minute]')),
  4398. $minutesRegex,
  4399. array('option' => array('selected' => 'selected', 'value' => '')),
  4400. '/option',
  4401. array('option' => array('value' => '00')),
  4402. '00',
  4403. '/option',
  4404. array('option' => array('value' => '05')),
  4405. '05',
  4406. '/option',
  4407. array('option' => array('value' => '10')),
  4408. '10',
  4409. '/option',
  4410. '*/select',
  4411. );
  4412. $this->assertHtml($expected, $result);
  4413. }
  4414. /**
  4415. * Test dateTime with rounding
  4416. *
  4417. * @return void
  4418. */
  4419. public function testDateTimeRounding() {
  4420. $this->Form->request->data['Contact'] = array(
  4421. 'date' => array(
  4422. 'day' => '13',
  4423. 'month' => '12',
  4424. 'year' => '2010',
  4425. 'hour' => '04',
  4426. 'minute' => '19',
  4427. 'meridian' => 'AM'
  4428. )
  4429. );
  4430. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4431. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4432. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4433. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4434. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4435. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4436. }
  4437. /**
  4438. * test that datetime() and default values work.
  4439. *
  4440. * @return void
  4441. */
  4442. public function testDatetimeWithDefault() {
  4443. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4444. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4445. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4446. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4447. $result = $this->Form->dateTime('Contact.updated', array(
  4448. 'default' => '2009-06-01 11:15:30'
  4449. ));
  4450. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4451. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4452. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4453. }
  4454. /**
  4455. * testDateTime all zeros
  4456. *
  4457. * @return void
  4458. */
  4459. public function testDateTimeAllZeros() {
  4460. $result = $this->Form->dateTime('Contact.date', array(
  4461. 'timeFormat' => false,
  4462. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4463. 'value' => '0000-00-00'
  4464. ));
  4465. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4466. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4467. }
  4468. /**
  4469. * testDateTimeEmptyAsArray
  4470. *
  4471. * @return void
  4472. */
  4473. public function testDateTimeEmptyAsArray() {
  4474. $result = $this->Form->dateTime('Contact.date', array(
  4475. 'empty' => array(
  4476. 'day' => 'DAY',
  4477. 'month' => 'MONTH',
  4478. 'year' => 'YEAR',
  4479. 'hour' => 'HOUR',
  4480. 'minute' => 'MINUTE',
  4481. 'meridian' => false
  4482. )
  4483. ));
  4484. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4485. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4486. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4487. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4488. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4489. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4490. $result = $this->Form->dateTime('Contact.date', array(
  4491. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4492. ));
  4493. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4494. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4495. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4496. }
  4497. /**
  4498. * testFormDateTimeMulti method
  4499. *
  4500. * test multiple datetime element generation
  4501. *
  4502. * @return void
  4503. */
  4504. public function testFormDateTimeMulti() {
  4505. extract($this->dateRegex);
  4506. $result = $this->Form->dateTime('Contact.1.updated');
  4507. $this->assertContains('Contact[1][updated][month]', $result);
  4508. $this->assertContains('Contact[1][updated][day]', $result);
  4509. $this->assertContains('Contact[1][updated][year]', $result);
  4510. $this->assertContains('Contact[1][updated][hour]', $result);
  4511. $this->assertContains('Contact[1][updated][minute]', $result);
  4512. }
  4513. /**
  4514. * When changing the date format, the label should always focus the first select box when
  4515. * clicked.
  4516. *
  4517. * @return void
  4518. */
  4519. public function testDateTimeLabelIdMatchesFirstInput() {
  4520. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4521. $this->assertContains('<label>Date</label>', $result);
  4522. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4523. $this->assertContains('<label>Date</label>', $result);
  4524. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4525. $this->assertContains('<label>Date</label>', $result);
  4526. }
  4527. /**
  4528. * testMonth method
  4529. *
  4530. * @return void
  4531. */
  4532. public function testMonth() {
  4533. $result = $this->Form->month('Model.field', ['value' => '']);
  4534. $expected = array(
  4535. array('select' => array('name' => 'Model[field][month]')),
  4536. array('option' => array('value' => '', 'selected' => 'selected')),
  4537. '/option',
  4538. array('option' => array('value' => '01')),
  4539. date('F', strtotime('2008-01-01 00:00:00')),
  4540. '/option',
  4541. array('option' => array('value' => '02')),
  4542. date('F', strtotime('2008-02-01 00:00:00')),
  4543. '/option',
  4544. '*/select',
  4545. );
  4546. $this->assertHtml($expected, $result);
  4547. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4548. $expected = array(
  4549. array('select' => array('name' => 'Model[field][month]')),
  4550. array('option' => array('selected' => 'selected', 'value' => '')),
  4551. '/option',
  4552. array('option' => array('value' => '01')),
  4553. date('F', strtotime('2008-01-01 00:00:00')),
  4554. '/option',
  4555. array('option' => array('value' => '02')),
  4556. date('F', strtotime('2008-02-01 00:00:00')),
  4557. '/option',
  4558. '*/select',
  4559. );
  4560. $this->assertHtml($expected, $result);
  4561. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4562. $expected = array(
  4563. array('select' => array('name' => 'Model[field][month]')),
  4564. array('option' => array('selected' => 'selected', 'value' => '')),
  4565. '/option',
  4566. array('option' => array('value' => '01')),
  4567. '1',
  4568. '/option',
  4569. array('option' => array('value' => '02')),
  4570. '2',
  4571. '/option',
  4572. '*/select',
  4573. );
  4574. $this->assertHtml($expected, $result);
  4575. $monthNames = [
  4576. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4577. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4578. ];
  4579. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  4580. $expected = array(
  4581. array('select' => array('name' => 'Model[field][month]')),
  4582. array('option' => array('value' => '')),
  4583. '/option',
  4584. array('option' => array('value' => '01', 'selected' => 'selected')),
  4585. 'Jan',
  4586. '/option',
  4587. array('option' => array('value' => '02')),
  4588. 'Feb',
  4589. '/option',
  4590. '*/select',
  4591. );
  4592. $this->assertHtml($expected, $result);
  4593. $this->Form->request->data['Project']['release'] = '2050-02-10';
  4594. $result = $this->Form->month('Project.release');
  4595. $expected = array(
  4596. array('select' => array('name' => 'Project[release][month]')),
  4597. array('option' => array('value' => '')),
  4598. '/option',
  4599. array('option' => array('value' => '01')),
  4600. 'January',
  4601. '/option',
  4602. array('option' => array('value' => '02', 'selected' => 'selected')),
  4603. 'February',
  4604. '/option',
  4605. '*/select',
  4606. );
  4607. $this->assertHtml($expected, $result);
  4608. }
  4609. /**
  4610. * testDay method
  4611. *
  4612. * @return void
  4613. */
  4614. public function testDay() {
  4615. extract($this->dateRegex);
  4616. $result = $this->Form->day('Model.field', array('value' => false));
  4617. $expected = array(
  4618. array('select' => array('name' => 'Model[field][day]')),
  4619. array('option' => array('selected' => 'selected', 'value' => '')),
  4620. '/option',
  4621. array('option' => array('value' => '01')),
  4622. '1',
  4623. '/option',
  4624. array('option' => array('value' => '02')),
  4625. '2',
  4626. '/option',
  4627. $daysRegex,
  4628. '/select',
  4629. );
  4630. $this->assertHtml($expected, $result);
  4631. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4632. $result = $this->Form->day('Model.field');
  4633. $expected = array(
  4634. array('select' => array('name' => 'Model[field][day]')),
  4635. array('option' => array('value' => '')),
  4636. '/option',
  4637. array('option' => array('value' => '01')),
  4638. '1',
  4639. '/option',
  4640. array('option' => array('value' => '02')),
  4641. '2',
  4642. '/option',
  4643. $daysRegex,
  4644. array('option' => array('value' => '10', 'selected' => 'selected')),
  4645. '10',
  4646. '/option',
  4647. $daysRegex,
  4648. '/select',
  4649. );
  4650. $this->assertHtml($expected, $result);
  4651. $this->Form->request->data['Model']['field'] = '';
  4652. $result = $this->Form->day('Model.field', array('value' => '10'));
  4653. $expected = array(
  4654. array('select' => array('name' => 'Model[field][day]')),
  4655. array('option' => array('value' => '')),
  4656. '/option',
  4657. array('option' => array('value' => '01')),
  4658. '1',
  4659. '/option',
  4660. array('option' => array('value' => '02')),
  4661. '2',
  4662. '/option',
  4663. $daysRegex,
  4664. array('option' => array('value' => '10', 'selected' => 'selected')),
  4665. '10',
  4666. '/option',
  4667. $daysRegex,
  4668. '/select',
  4669. );
  4670. $this->assertHtml($expected, $result);
  4671. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4672. $result = $this->Form->day('Project.release');
  4673. $expected = array(
  4674. array('select' => array('name' => 'Project[release][day]')),
  4675. array('option' => array('value' => '')),
  4676. '/option',
  4677. array('option' => array('value' => '01')),
  4678. '1',
  4679. '/option',
  4680. array('option' => array('value' => '02')),
  4681. '2',
  4682. '/option',
  4683. $daysRegex,
  4684. array('option' => array('value' => '10', 'selected' => 'selected')),
  4685. '10',
  4686. '/option',
  4687. $daysRegex,
  4688. '/select',
  4689. );
  4690. $this->assertHtml($expected, $result);
  4691. }
  4692. /**
  4693. * testMinute method
  4694. *
  4695. * @return void
  4696. */
  4697. public function testMinute() {
  4698. extract($this->dateRegex);
  4699. $result = $this->Form->minute('Model.field', ['value' => '']);
  4700. $expected = array(
  4701. array('select' => array('name' => 'Model[field][minute]')),
  4702. array('option' => array('selected' => 'selected', 'value' => '')),
  4703. '/option',
  4704. array('option' => array('value' => '00')),
  4705. '00',
  4706. '/option',
  4707. array('option' => array('value' => '01')),
  4708. '01',
  4709. '/option',
  4710. array('option' => array('value' => '02')),
  4711. '02',
  4712. '/option',
  4713. $minutesRegex,
  4714. '/select',
  4715. );
  4716. $this->assertHtml($expected, $result);
  4717. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4718. $result = $this->Form->minute('Model.field');
  4719. $expected = array(
  4720. array('select' => array('name' => 'Model[field][minute]')),
  4721. array('option' => array('value' => '')),
  4722. '/option',
  4723. array('option' => array('value' => '00')),
  4724. '00',
  4725. '/option',
  4726. array('option' => array('value' => '01')),
  4727. '01',
  4728. '/option',
  4729. array('option' => array('value' => '02')),
  4730. '02',
  4731. '/option',
  4732. $minutesRegex,
  4733. array('option' => array('value' => '12', 'selected' => 'selected')),
  4734. '12',
  4735. '/option',
  4736. $minutesRegex,
  4737. '/select',
  4738. );
  4739. $this->assertHtml($expected, $result);
  4740. $this->Form->request->data['Model']['field'] = '';
  4741. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4742. $expected = array(
  4743. array('select' => array('name' => 'Model[field][minute]')),
  4744. array('option' => array('selected' => 'selected', 'value' => '')),
  4745. '/option',
  4746. array('option' => array('value' => '00')),
  4747. '00',
  4748. '/option',
  4749. array('option' => array('value' => '05')),
  4750. '05',
  4751. '/option',
  4752. array('option' => array('value' => '10')),
  4753. '10',
  4754. '/option',
  4755. $minutesRegex,
  4756. '/select',
  4757. );
  4758. $this->assertHtml($expected, $result);
  4759. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4760. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4761. $expected = array(
  4762. array('select' => array('name' => 'Model[field][minute]')),
  4763. array('option' => array('value' => '')),
  4764. '/option',
  4765. array('option' => array('value' => '00')),
  4766. '00',
  4767. '/option',
  4768. array('option' => array('value' => '05')),
  4769. '05',
  4770. '/option',
  4771. array('option' => array('value' => '10', 'selected' => 'selected')),
  4772. '10',
  4773. '/option',
  4774. $minutesRegex,
  4775. '/select',
  4776. );
  4777. $this->assertHtml($expected, $result);
  4778. }
  4779. /**
  4780. * Test generating an input for the meridian.
  4781. *
  4782. * @return void
  4783. */
  4784. public function testMeridian() {
  4785. extract($this->dateRegex);
  4786. $now = time();
  4787. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  4788. $expected = [
  4789. array('select' => array('name' => 'Model[field][meridian]')),
  4790. array('option' => array('value' => '')),
  4791. '/option',
  4792. $meridianRegex,
  4793. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4794. date('a', $now),
  4795. '/option',
  4796. '*/select'
  4797. ];
  4798. $this->assertHtml($expected, $result);
  4799. }
  4800. /**
  4801. * testHour method
  4802. *
  4803. * @return void
  4804. */
  4805. public function testHour() {
  4806. extract($this->dateRegex);
  4807. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  4808. $expected = array(
  4809. array('select' => array('name' => 'Model[field][hour]')),
  4810. array('option' => array('selected' => 'selected', 'value' => '')),
  4811. '/option',
  4812. array('option' => array('value' => '01')),
  4813. '1',
  4814. '/option',
  4815. array('option' => array('value' => '02')),
  4816. '2',
  4817. '/option',
  4818. $hoursRegex,
  4819. '/select',
  4820. );
  4821. $this->assertHtml($expected, $result);
  4822. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4823. $result = $this->Form->hour('Model.field', ['format' => 12]);
  4824. $expected = array(
  4825. array('select' => array('name' => 'Model[field][hour]')),
  4826. array('option' => array('value' => '')),
  4827. '/option',
  4828. array('option' => array('value' => '01')),
  4829. '1',
  4830. '/option',
  4831. array('option' => array('value' => '02')),
  4832. '2',
  4833. '/option',
  4834. $hoursRegex,
  4835. array('option' => array('value' => '12', 'selected' => 'selected')),
  4836. '12',
  4837. '/option',
  4838. '/select',
  4839. );
  4840. $this->assertHtml($expected, $result);
  4841. $this->Form->request->data['Model']['field'] = '';
  4842. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => '23'));
  4843. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  4844. $result = $this->Form->hour('Model.field', array('format' => 12, 'value' => '23'));
  4845. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  4846. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4847. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4848. $expected = array(
  4849. array('select' => array('name' => 'Model[field][hour]')),
  4850. array('option' => array('value' => '')),
  4851. '/option',
  4852. array('option' => array('value' => '00', 'selected' => 'selected')),
  4853. '0',
  4854. '/option',
  4855. array('option' => array('value' => '01')),
  4856. '1',
  4857. '/option',
  4858. array('option' => array('value' => '02')),
  4859. '2',
  4860. '/option',
  4861. $hoursRegex,
  4862. '/select',
  4863. );
  4864. $this->assertHtml($expected, $result);
  4865. unset($this->Form->request->data['Model']['field']);
  4866. $result = $this->Form->hour('Model.field', array('format' => 24, 'value' => 'now'));
  4867. $thisHour = date('H');
  4868. $optValue = date('G');
  4869. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  4870. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  4871. $result = $this->Form->hour('Model.field', ['format' => 24]);
  4872. $expected = array(
  4873. array('select' => array('name' => 'Model[field][hour]')),
  4874. array('option' => array('value' => '')),
  4875. '/option',
  4876. array('option' => array('value' => '00')),
  4877. '0',
  4878. '/option',
  4879. array('option' => array('value' => '01', 'selected' => 'selected')),
  4880. '1',
  4881. '/option',
  4882. array('option' => array('value' => '02')),
  4883. '2',
  4884. '/option',
  4885. $hoursRegex,
  4886. '/select',
  4887. );
  4888. $this->assertHtml($expected, $result);
  4889. }
  4890. /**
  4891. * testYear method
  4892. *
  4893. * @return void
  4894. */
  4895. public function testYear() {
  4896. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  4897. $expected = array(
  4898. array('select' => array('name' => 'Model[field][year]')),
  4899. array('option' => array('selected' => 'selected', 'value' => '')),
  4900. '/option',
  4901. array('option' => array('value' => '2007')),
  4902. '2007',
  4903. '/option',
  4904. array('option' => array('value' => '2006')),
  4905. '2006',
  4906. '/option',
  4907. '/select',
  4908. );
  4909. $this->assertHtml($expected, $result);
  4910. $result = $this->Form->year('Model.field', [
  4911. 'value' => '',
  4912. 'minYear' => 2006,
  4913. 'maxYear' => 2007,
  4914. 'orderYear' => 'asc'
  4915. ]);
  4916. $expected = array(
  4917. array('select' => array('name' => 'Model[field][year]')),
  4918. array('option' => array('selected' => 'selected', 'value' => '')),
  4919. '/option',
  4920. array('option' => array('value' => '2006')),
  4921. '2006',
  4922. '/option',
  4923. array('option' => array('value' => '2007')),
  4924. '2007',
  4925. '/option',
  4926. '/select',
  4927. );
  4928. $this->assertHtml($expected, $result);
  4929. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  4930. $result = $this->Form->year('Contact.published', [
  4931. 'empty' => false,
  4932. 'minYear' => 2006,
  4933. 'maxYear' => 2007,
  4934. ]);
  4935. $expected = array(
  4936. array('select' => array('name' => 'Contact[published][year]')),
  4937. array('option' => array('value' => '2007')),
  4938. '2007',
  4939. '/option',
  4940. array('option' => array('value' => '2006', 'selected' => 'selected')),
  4941. '2006',
  4942. '/option',
  4943. '/select',
  4944. );
  4945. $this->assertHtml($expected, $result);
  4946. }
  4947. /**
  4948. * testYearAutoExpandRange method
  4949. *
  4950. * @return void
  4951. */
  4952. public function testYearAutoExpandRange() {
  4953. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  4954. $result = $this->Form->year('User.birthday');
  4955. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4956. $result = $matches[1];
  4957. $expected = range(date('Y') + 5, 1930);
  4958. $this->assertEquals($expected, $result);
  4959. $this->Form->request->data['Project']['release'] = '2050-10-10';
  4960. $result = $this->Form->year('Project.release');
  4961. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4962. $result = $matches[1];
  4963. $expected = range(2050, date('Y') - 5);
  4964. $this->assertEquals($expected, $result);
  4965. $this->Form->request->data['Project']['release'] = '1881-10-10';
  4966. $result = $this->Form->year('Project.release', [
  4967. 'minYear' => 1890,
  4968. 'maxYear' => 1900
  4969. ]);
  4970. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  4971. $result = $matches[1];
  4972. $expected = range(1900, 1881);
  4973. $this->assertEquals($expected, $result);
  4974. }
  4975. /**
  4976. * Test that input() accepts the type of date and passes options in.
  4977. *
  4978. * @return void
  4979. */
  4980. public function testInputDate() {
  4981. $this->Form->request->data = array(
  4982. 'month_year' => array('month' => date('m')),
  4983. );
  4984. $this->Form->create($this->article);
  4985. $result = $this->Form->input('month_year', array(
  4986. 'label' => false,
  4987. 'type' => 'date',
  4988. 'minYear' => 2006,
  4989. 'maxYear' => 2008
  4990. ));
  4991. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  4992. $this->assertNotContains('value="2008" selected="selected"', $result);
  4993. }
  4994. /**
  4995. * Test the label option being set to false.
  4996. *
  4997. * @return void
  4998. */
  4999. public function testInputLabelFalse() {
  5000. $this->Form->create($this->article);
  5001. $result = $this->Form->input('title', ['label' => false]);
  5002. $expected = [
  5003. 'div' => ['class' => 'input text required'],
  5004. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  5005. '/div'
  5006. ];
  5007. $this->assertHtml($expected, $result);
  5008. }
  5009. /**
  5010. * testInputDateMaxYear method
  5011. *
  5012. * Let's say we want to only allow users born from 2006 to 2008 to register
  5013. * This being the first singup page, we still don't have any data
  5014. *
  5015. * @return void
  5016. */
  5017. public function testInputDateMaxYear() {
  5018. $this->Form->request->data = [];
  5019. $this->Form->create($this->article);
  5020. $result = $this->Form->input('birthday', array(
  5021. 'label' => false,
  5022. 'type' => 'date',
  5023. 'minYear' => 2006,
  5024. 'maxYear' => 2008
  5025. ));
  5026. $this->assertContains('value="2008" selected="selected"', $result);
  5027. }
  5028. /**
  5029. * testTextArea method
  5030. *
  5031. * @return void
  5032. */
  5033. public function testTextArea() {
  5034. $this->Form->request->data = array('field' => 'some test data');
  5035. $result = $this->Form->textarea('field');
  5036. $expected = array(
  5037. 'textarea' => array('name' => 'field'),
  5038. 'some test data',
  5039. '/textarea',
  5040. );
  5041. $this->assertHtml($expected, $result);
  5042. $result = $this->Form->textarea('user.bio');
  5043. $expected = array(
  5044. 'textarea' => array('name' => 'user[bio]'),
  5045. '/textarea',
  5046. );
  5047. $this->assertHtml($expected, $result);
  5048. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  5049. $result = $this->Form->textarea('field');
  5050. $expected = array(
  5051. 'textarea' => array('name' => 'field'),
  5052. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5053. '/textarea',
  5054. );
  5055. $this->assertHtml($expected, $result);
  5056. $this->Form->request->data = [
  5057. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5058. ];
  5059. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5060. $expected = array(
  5061. 'textarea' => array('name' => 'Model[field]'),
  5062. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5063. '/textarea',
  5064. );
  5065. $this->assertHtml($expected, $result);
  5066. $result = $this->Form->textarea('0.OtherModel.field');
  5067. $expected = array(
  5068. 'textarea' => array('name' => '0[OtherModel][field]'),
  5069. '/textarea'
  5070. );
  5071. $this->assertHtml($expected, $result);
  5072. }
  5073. /**
  5074. * testTextAreaWithStupidCharacters method
  5075. *
  5076. * test text area with non-ascii characters
  5077. *
  5078. * @return void
  5079. */
  5080. public function testTextAreaWithStupidCharacters() {
  5081. $result = $this->Form->textarea('Post.content', [
  5082. 'value' => "GREAT®",
  5083. 'rows' => '15',
  5084. 'cols' => '75'
  5085. ]);
  5086. $expected = [
  5087. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5088. 'GREAT®',
  5089. '/textarea',
  5090. ];
  5091. $this->assertHtml($expected, $result);
  5092. }
  5093. /**
  5094. * Test textareas waxlength read from schema.
  5095. *
  5096. * @return void
  5097. */
  5098. public function testTextAreaMaxLength() {
  5099. $this->Form->create([
  5100. 'schema' => [
  5101. 'stuff' => ['type' => 'string', 'length' => 10],
  5102. ]
  5103. ]);
  5104. $result = $this->Form->input('other', array('type' => 'textarea'));
  5105. $expected = array(
  5106. 'div' => array('class' => 'input textarea'),
  5107. 'label' => array('for' => 'other'),
  5108. 'Other',
  5109. '/label',
  5110. 'textarea' => array('name' => 'other', 'id' => 'other'),
  5111. '/textarea',
  5112. '/div'
  5113. );
  5114. $this->assertTags($result, $expected);
  5115. $result = $this->Form->input('stuff', array('type' => 'textarea'));
  5116. $expected = array(
  5117. 'div' => array('class' => 'input textarea'),
  5118. 'label' => array('for' => 'stuff'),
  5119. 'Stuff',
  5120. '/label',
  5121. 'textarea' => array('name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff'),
  5122. '/textarea',
  5123. '/div'
  5124. );
  5125. $this->assertTags($result, $expected);
  5126. }
  5127. /**
  5128. * testHiddenField method
  5129. *
  5130. * @return void
  5131. */
  5132. public function testHiddenField() {
  5133. $this->article['errors'] = [
  5134. 'field' => true
  5135. ];
  5136. $this->Form->request->data['field'] = 'test';
  5137. $this->Form->create($this->article);
  5138. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5139. $expected = array(
  5140. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'));
  5141. $this->assertHtml($expected, $result);
  5142. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5143. $expected = [
  5144. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5145. ];
  5146. $this->assertHtml($expected, $result);
  5147. }
  5148. /**
  5149. * testFileUploadField method
  5150. *
  5151. * @return void
  5152. */
  5153. public function testFileUploadField() {
  5154. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5155. $result = $this->Form->file('Model.upload');
  5156. $this->assertHtml($expected, $result);
  5157. $this->Form->request->data['Model']['upload'] = [
  5158. 'name' => '', 'type' => '', 'tmp_name' => '',
  5159. 'error' => 4, 'size' => 0
  5160. ];
  5161. $result = $this->Form->file('Model.upload');
  5162. $this->assertHtml($expected, $result);
  5163. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5164. $result = $this->Form->file('Model.upload');
  5165. $this->assertHtml($expected, $result);
  5166. }
  5167. /**
  5168. * test File upload input on a model not used in create();
  5169. *
  5170. * @return void
  5171. */
  5172. public function testFileUploadOnOtherModel() {
  5173. $this->Form->create($this->article, array('type' => 'file'));
  5174. $result = $this->Form->file('ValidateProfile.city');
  5175. $expected = array(
  5176. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5177. );
  5178. $this->assertHtml($expected, $result);
  5179. }
  5180. /**
  5181. * testButton method
  5182. *
  5183. * @return void
  5184. */
  5185. public function testButton() {
  5186. $result = $this->Form->button('Hi');
  5187. $expected = array('button' => array('type' => 'submit'), 'Hi', '/button');
  5188. $this->assertHtml($expected, $result);
  5189. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5190. $expected = array('button' => array('type' => 'reset'), 'Clear Form >', '/button');
  5191. $this->assertHtml($expected, $result);
  5192. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5193. $expected = array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button');
  5194. $this->assertHtml($expected, $result);
  5195. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5196. $expected = array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button');
  5197. $this->assertHtml($expected, $result);
  5198. $result = $this->Form->button('No type', array('type' => false));
  5199. $expected = array('button' => array(), 'No type', '/button');
  5200. $this->assertHtml($expected, $result);
  5201. $result = $this->Form->button('Upload Text', array(
  5202. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5203. 'escape' => false
  5204. ));
  5205. $this->assertNotRegExp('/\&039/', $result);
  5206. }
  5207. /**
  5208. * Test that button() makes unlocked fields by default.
  5209. *
  5210. * @return void
  5211. */
  5212. public function testButtonUnlockedByDefault() {
  5213. $this->Form->request->params['_csrfToken'] = 'secured';
  5214. $this->Form->button('Save', array('name' => 'save'));
  5215. $this->Form->button('Clear');
  5216. $result = $this->Form->unlockField();
  5217. $this->assertEquals(array('save'), $result);
  5218. }
  5219. /**
  5220. * testPostButton method
  5221. *
  5222. * @return void
  5223. */
  5224. public function testPostButton() {
  5225. $result = $this->Form->postButton('Hi', '/controller/action');
  5226. $expected = array(
  5227. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5228. 'div' => array('style' => 'display:none;'),
  5229. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5230. '/div',
  5231. 'button' => array('type' => 'submit'),
  5232. 'Hi',
  5233. '/button',
  5234. '/form'
  5235. );
  5236. $this->assertHtml($expected, $result);
  5237. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5238. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5239. }
  5240. /**
  5241. * Test using postButton with N dimensional data.
  5242. *
  5243. * @return void
  5244. */
  5245. public function testPostButtonNestedData() {
  5246. $data = array(
  5247. 'one' => array(
  5248. 'two' => array(
  5249. 3, 4, 5
  5250. )
  5251. )
  5252. );
  5253. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5254. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5255. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5256. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5257. }
  5258. /**
  5259. * Test that postButton adds _Token fields.
  5260. *
  5261. * @return void
  5262. */
  5263. public function testSecurePostButton() {
  5264. $this->Form->request->params['_csrfToken'] = 'testkey';
  5265. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5266. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5267. $expected = array(
  5268. 'form' => array(
  5269. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5270. ),
  5271. array('div' => array('style' => 'display:none;')),
  5272. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5273. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5274. '/div',
  5275. 'button' => array('type' => 'submit'),
  5276. 'Delete',
  5277. '/button',
  5278. array('div' => array('style' => 'display:none;')),
  5279. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5280. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5281. '/div',
  5282. '/form',
  5283. );
  5284. $this->assertHtml($expected, $result);
  5285. }
  5286. /**
  5287. * testPostLink method
  5288. *
  5289. * @return void
  5290. */
  5291. public function testPostLink() {
  5292. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5293. $expected = array(
  5294. 'form' => array(
  5295. 'method' => 'post', 'action' => '/posts/delete/1',
  5296. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5297. ),
  5298. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5299. '/form',
  5300. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5301. 'Delete',
  5302. '/a'
  5303. );
  5304. $this->assertHtml($expected, $result);
  5305. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5306. $expected = array(
  5307. 'form' => array(
  5308. 'method' => 'post', 'action' => '/posts/delete/1',
  5309. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5310. ),
  5311. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5312. '/form',
  5313. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5314. 'Delete',
  5315. '/a'
  5316. );
  5317. $this->assertHtml($expected, $result);
  5318. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('confirm' => 'Confirm?'));
  5319. $expected = array(
  5320. 'form' => array(
  5321. 'method' => 'post', 'action' => '/posts/delete/1',
  5322. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5323. ),
  5324. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5325. '/form',
  5326. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5327. 'Delete',
  5328. '/a'
  5329. );
  5330. $this->assertHtml($expected, $result);
  5331. $result = $this->Form->postLink(
  5332. 'Delete',
  5333. '/posts/delete/1',
  5334. array('escape' => false, 'confirm' => '\'Confirm\' this "deletion"?')
  5335. );
  5336. $expected = array(
  5337. 'form' => array(
  5338. 'method' => 'post', 'action' => '/posts/delete/1',
  5339. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5340. ),
  5341. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5342. '/form',
  5343. '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;/'),
  5344. 'Delete',
  5345. '/a'
  5346. );
  5347. $this->assertHtml($expected, $result);
  5348. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5349. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5350. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5351. $expected = array(
  5352. 'form' => array(
  5353. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5354. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5355. ),
  5356. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5357. '/form',
  5358. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5359. 'Delete',
  5360. '/a'
  5361. );
  5362. $this->assertHtml($expected, $result);
  5363. $result = $this->Form->postLink(
  5364. '',
  5365. array('controller' => 'items', 'action' => 'delete', 10),
  5366. array('class' => 'btn btn-danger', 'escape' => false, 'confirm' => 'Confirm thing')
  5367. );
  5368. $expected = array(
  5369. 'form' => array(
  5370. 'method' => 'post', 'action' => '/items/delete/10',
  5371. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5372. ),
  5373. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5374. '/form',
  5375. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5376. '/a'
  5377. );
  5378. $this->assertHtml($expected, $result);
  5379. }
  5380. /**
  5381. * Test that security hashes for postLink include the url.
  5382. *
  5383. * @return void
  5384. */
  5385. public function testPostLinkSecurityHash() {
  5386. $hash = Security::hash(
  5387. '/posts/delete/1' .
  5388. serialize(array()) .
  5389. '' .
  5390. Security::salt()
  5391. );
  5392. $hash .= '%3A';
  5393. $this->Form->request->params['_Token']['key'] = 'test';
  5394. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5395. $expected = array(
  5396. 'form' => array(
  5397. 'method' => 'post', 'action' => '/posts/delete/1',
  5398. 'name', 'style' => 'display:none;'
  5399. ),
  5400. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5401. 'div' => array('style' => 'display:none;'),
  5402. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash)),
  5403. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5404. '/div',
  5405. '/form',
  5406. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5407. 'Delete',
  5408. '/a'
  5409. );
  5410. $this->assertHtml($expected, $result);
  5411. }
  5412. /**
  5413. * Test using postLink with N dimensional data.
  5414. *
  5415. * @return void
  5416. */
  5417. public function testPostLinkNestedData() {
  5418. $data = array(
  5419. 'one' => array(
  5420. 'two' => array(
  5421. 3, 4, 5
  5422. )
  5423. )
  5424. );
  5425. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5426. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5427. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5428. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5429. }
  5430. /**
  5431. * test creating postLinks after a GET form.
  5432. *
  5433. * @return void
  5434. */
  5435. public function testPostLinkAfterGetForm() {
  5436. $this->Form->request->params['_csrfToken'] = 'testkey';
  5437. $this->Form->request->params['_Token'] = 'val';
  5438. $this->Form->create($this->article, array('type' => 'get'));
  5439. $this->Form->end();
  5440. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5441. $expected = array(
  5442. 'form' => array(
  5443. 'method' => 'post', 'action' => '/posts/delete/1',
  5444. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5445. ),
  5446. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5447. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5448. 'div' => array('style' => 'display:none;'),
  5449. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5450. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5451. '/div',
  5452. '/form',
  5453. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5454. 'Delete',
  5455. '/a'
  5456. );
  5457. $this->assertHtml($expected, $result);
  5458. }
  5459. /**
  5460. * Test that postLink adds form tags to view block
  5461. *
  5462. * @return void
  5463. */
  5464. public function testPostLinkFormBuffer() {
  5465. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => true));
  5466. $expected = array(
  5467. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5468. 'Delete',
  5469. '/a'
  5470. );
  5471. $this->assertHtml($expected, $result);
  5472. $result = $this->View->fetch('postLink');
  5473. $expected = array(
  5474. 'form' => array(
  5475. 'method' => 'post', 'action' => '/posts/delete/1',
  5476. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5477. ),
  5478. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5479. '/form'
  5480. );
  5481. $this->assertHtml($expected, $result);
  5482. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5483. array('block' => true, 'method' => 'DELETE')
  5484. );
  5485. $expected = array(
  5486. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5487. 'Delete',
  5488. '/a'
  5489. );
  5490. $this->assertHtml($expected, $result);
  5491. $result = $this->View->fetch('postLink');
  5492. $expected = array(
  5493. 'form' => array(
  5494. 'method' => 'post', 'action' => '/posts/delete/1',
  5495. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5496. ),
  5497. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5498. '/form',
  5499. array(
  5500. 'form' => array(
  5501. 'method' => 'post', 'action' => '/posts/delete/2',
  5502. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5503. ),
  5504. ),
  5505. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5506. '/form'
  5507. );
  5508. $this->assertHtml($expected, $result);
  5509. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5510. $expected = array(
  5511. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5512. 'Delete',
  5513. '/a'
  5514. );
  5515. $this->assertHtml($expected, $result);
  5516. $result = $this->View->fetch('foobar');
  5517. $expected = array(
  5518. 'form' => array(
  5519. 'method' => 'post', 'action' => '/posts/delete/1',
  5520. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5521. ),
  5522. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5523. '/form'
  5524. );
  5525. $this->assertHtml($expected, $result);
  5526. }
  5527. /**
  5528. * testSubmitButton method
  5529. *
  5530. * @return void
  5531. */
  5532. public function testSubmitButton() {
  5533. $result = $this->Form->submit('');
  5534. $expected = array(
  5535. 'div' => array('class' => 'submit'),
  5536. 'input' => array('type' => 'submit', 'value' => ''),
  5537. '/div'
  5538. );
  5539. $this->assertHtml($expected, $result);
  5540. $result = $this->Form->submit('Test Submit');
  5541. $expected = array(
  5542. 'div' => array('class' => 'submit'),
  5543. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5544. '/div'
  5545. );
  5546. $this->assertHtml($expected, $result);
  5547. $result = $this->Form->submit('Next >');
  5548. $expected = array(
  5549. 'div' => array('class' => 'submit'),
  5550. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5551. '/div'
  5552. );
  5553. $this->assertHtml($expected, $result);
  5554. $result = $this->Form->submit('Next >', array('escape' => false));
  5555. $expected = array(
  5556. 'div' => array('class' => 'submit'),
  5557. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5558. '/div'
  5559. );
  5560. $this->assertHtml($expected, $result);
  5561. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5562. $expected = array(
  5563. 'div' => array('class' => 'submit'),
  5564. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5565. '/div'
  5566. );
  5567. $this->assertHtml($expected, $result);
  5568. }
  5569. /**
  5570. * test image submit types.
  5571. *
  5572. * @return void
  5573. */
  5574. public function testSubmitImage() {
  5575. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5576. $expected = array(
  5577. 'div' => array('class' => 'submit'),
  5578. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5579. '/div'
  5580. );
  5581. $this->assertHtml($expected, $result);
  5582. $result = $this->Form->submit('/relative/cake.power.gif');
  5583. $expected = array(
  5584. 'div' => array('class' => 'submit'),
  5585. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5586. '/div'
  5587. );
  5588. $this->assertHtml($expected, $result);
  5589. $result = $this->Form->submit('cake.power.gif');
  5590. $expected = array(
  5591. 'div' => array('class' => 'submit'),
  5592. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5593. '/div'
  5594. );
  5595. $this->assertHtml($expected, $result);
  5596. $result = $this->Form->submit('Not.an.image');
  5597. $expected = array(
  5598. 'div' => array('class' => 'submit'),
  5599. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5600. '/div'
  5601. );
  5602. $this->assertHtml($expected, $result);
  5603. }
  5604. /**
  5605. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5606. * be submitted at a time.
  5607. *
  5608. * @return void
  5609. */
  5610. public function testSubmitUnlockedByDefault() {
  5611. $this->Form->request->params['_Token'] = 'secured';
  5612. $this->Form->submit('Go go');
  5613. $this->Form->submit('Save', array('name' => 'save'));
  5614. $result = $this->Form->unlockField();
  5615. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5616. }
  5617. /**
  5618. * Test submit image with timestamps.
  5619. *
  5620. * @return void
  5621. */
  5622. public function testSubmitImageTimestamp() {
  5623. Configure::write('Asset.timestamp', 'force');
  5624. $result = $this->Form->submit('cake.power.gif');
  5625. $expected = array(
  5626. 'div' => array('class' => 'submit'),
  5627. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5628. '/div'
  5629. );
  5630. $this->assertHtml($expected, $result);
  5631. }
  5632. /**
  5633. * test that datetime() works with GET style forms.
  5634. *
  5635. * @return void
  5636. */
  5637. public function testDateTimeWithGetForms() {
  5638. extract($this->dateRegex);
  5639. $this->Form->create($this->article, array('type' => 'get'));
  5640. $result = $this->Form->datetime('created');
  5641. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  5642. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  5643. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  5644. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  5645. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  5646. }
  5647. /**
  5648. * testForMagicInputNonExistingNorValidated method
  5649. *
  5650. * @return void
  5651. */
  5652. public function testForMagicInputNonExistingNorValidated() {
  5653. $result = $this->Form->create($this->article);
  5654. $this->Form->templates(['inputContainer' => '{{content}}']);
  5655. $result = $this->Form->input('non_existing_nor_validated');
  5656. $expected = array(
  5657. 'label' => array('for' => 'non-existing-nor-validated'),
  5658. 'Non Existing Nor Validated',
  5659. '/label',
  5660. 'input' => array(
  5661. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5662. 'id' => 'non-existing-nor-validated'
  5663. )
  5664. );
  5665. $this->assertHtml($expected, $result);
  5666. $result = $this->Form->input('non_existing_nor_validated', array(
  5667. 'val' => 'my value'
  5668. ));
  5669. $expected = array(
  5670. 'label' => array('for' => 'non-existing-nor-validated'),
  5671. 'Non Existing Nor Validated',
  5672. '/label',
  5673. 'input' => array(
  5674. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5675. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  5676. )
  5677. );
  5678. $this->assertHtml($expected, $result);
  5679. $this->Form->request->data = array('non_existing_nor_validated' => 'CakePHP magic');
  5680. $result = $this->Form->input('non_existing_nor_validated');
  5681. $expected = array(
  5682. 'label' => array('for' => 'non-existing-nor-validated'),
  5683. 'Non Existing Nor Validated',
  5684. '/label',
  5685. 'input' => array(
  5686. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  5687. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  5688. )
  5689. );
  5690. $this->assertHtml($expected, $result);
  5691. }
  5692. /**
  5693. * testFormMagicInputLabel method
  5694. *
  5695. * @return void
  5696. */
  5697. public function testFormMagicInputLabel() {
  5698. TableRegistry::get('Contacts', [
  5699. 'className' => __NAMESPACE__ . '\ContactsTable'
  5700. ]);
  5701. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  5702. $this->Form->templates(['inputContainer' => '{{content}}']);
  5703. $result = $this->Form->input('Contacts.name', array('label' => 'My label'));
  5704. $expected = array(
  5705. 'label' => array('for' => 'contacts-name'),
  5706. 'My label',
  5707. '/label',
  5708. 'input' => array(
  5709. 'type' => 'text', 'name' => 'Contacts[name]',
  5710. 'id' => 'contacts-name', 'maxlength' => '255'
  5711. )
  5712. );
  5713. $this->assertHtml($expected, $result);
  5714. $result = $this->Form->input('name', array(
  5715. 'label' => array('class' => 'mandatory')
  5716. ));
  5717. $expected = array(
  5718. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5719. 'Name',
  5720. '/label',
  5721. 'input' => array(
  5722. 'type' => 'text', 'name' => 'name',
  5723. 'id' => 'name', 'maxlength' => '255'
  5724. )
  5725. );
  5726. $this->assertHtml($expected, $result);
  5727. $result = $this->Form->input('name', array(
  5728. 'div' => false,
  5729. 'label' => array('class' => 'mandatory', 'text' => 'My label')
  5730. ));
  5731. $expected = array(
  5732. 'label' => array('for' => 'name', 'class' => 'mandatory'),
  5733. 'My label',
  5734. '/label',
  5735. 'input' => array(
  5736. 'type' => 'text', 'name' => 'name',
  5737. 'id' => 'name', 'maxlength' => '255'
  5738. )
  5739. );
  5740. $this->assertHtml($expected, $result);
  5741. $result = $this->Form->input('Contact.name', array(
  5742. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  5743. ));
  5744. $expected = array(
  5745. 'label' => array('for' => 'my_id'),
  5746. 'Name',
  5747. '/label',
  5748. 'input' => array(
  5749. 'type' => 'text', 'name' => 'Contact[name]',
  5750. 'id' => 'my_id', 'maxlength' => '255'
  5751. )
  5752. );
  5753. $this->assertHtml($expected, $result);
  5754. $result = $this->Form->input('1.id');
  5755. $expected = array('input' => array(
  5756. 'type' => 'hidden', 'name' => '1[id]',
  5757. 'id' => '1-id'
  5758. ));
  5759. $this->assertHtml($expected, $result);
  5760. $result = $this->Form->input("1.name");
  5761. $expected = array(
  5762. 'label' => array('for' => '1-name'),
  5763. 'Name',
  5764. '/label',
  5765. 'input' => array(
  5766. 'type' => 'text', 'name' => '1[name]',
  5767. 'id' => '1-name', 'maxlength' => '255'
  5768. )
  5769. );
  5770. $this->assertHtml($expected, $result);
  5771. }
  5772. /**
  5773. * testFormEnd method
  5774. *
  5775. * @return void
  5776. */
  5777. public function testFormEnd() {
  5778. $this->assertEquals('</form>', $this->Form->end());
  5779. }
  5780. /**
  5781. * Test the generation of fields for a multi record form.
  5782. *
  5783. * @return void
  5784. */
  5785. public function testMultiRecordForm() {
  5786. $this->loadFixtures('Articles', 'Comments');
  5787. $articles = TableRegistry::get('Articles');
  5788. $articles->hasMany('Comments');
  5789. $comment = new Entity(['comment' => 'Value']);
  5790. $article = new Article(['comments' => [$comment]]);
  5791. $this->Form->create([$article]);
  5792. $result = $this->Form->input('0.comments.1.comment');
  5793. $expected = array(
  5794. 'div' => array('class' => 'input textarea'),
  5795. 'label' => array('for' => '0-comments-1-comment'),
  5796. 'Comment',
  5797. '/label',
  5798. 'textarea' => array(
  5799. 'name',
  5800. 'id' => '0-comments-1-comment',
  5801. ),
  5802. '/textarea',
  5803. '/div'
  5804. );
  5805. $this->assertHtml($expected, $result);
  5806. $result = $this->Form->input('0.comments.0.comment');
  5807. $expected = array(
  5808. 'div' => array('class' => 'input textarea'),
  5809. 'label' => array('for' => '0-comments-0-comment'),
  5810. 'Comment',
  5811. '/label',
  5812. 'textarea' => array(
  5813. 'name',
  5814. 'id' => '0-comments-0-comment'
  5815. ),
  5816. 'Value',
  5817. '/textarea',
  5818. '/div'
  5819. );
  5820. $this->assertHtml($expected, $result);
  5821. $comment->errors('comment', ['Not valid']);
  5822. $result = $this->Form->input('0.comments.0.comment');
  5823. $expected = array(
  5824. 'div' => array('class' => 'input textarea error'),
  5825. 'label' => array('for' => '0-comments-0-comment'),
  5826. 'Comment',
  5827. '/label',
  5828. 'textarea' => array(
  5829. 'name',
  5830. 'class' => 'form-error',
  5831. 'id' => '0-comments-0-comment'
  5832. ),
  5833. 'Value',
  5834. '/textarea',
  5835. array('div' => array('class' => 'error-message')),
  5836. 'Not valid',
  5837. '/div',
  5838. '/div'
  5839. );
  5840. $this->assertHtml($expected, $result);
  5841. TableRegistry::get('Comments')
  5842. ->validator('default')
  5843. ->allowEmpty('comment', false);
  5844. $result = $this->Form->input('0.comments.1.comment');
  5845. $expected = array(
  5846. 'div' => array('class' => 'input textarea required'),
  5847. 'label' => array('for' => '0-comments-1-comment'),
  5848. 'Comment',
  5849. '/label',
  5850. 'textarea' => array(
  5851. 'name',
  5852. 'required' => 'required',
  5853. 'id' => '0-comments-1-comment'
  5854. ),
  5855. '/textarea',
  5856. '/div'
  5857. );
  5858. $this->assertHtml($expected, $result);
  5859. }
  5860. /**
  5861. * test that some html5 inputs + FormHelper::__call() work
  5862. *
  5863. * @return void
  5864. */
  5865. public function testHtml5Inputs() {
  5866. $result = $this->Form->email('User.email');
  5867. $expected = array(
  5868. 'input' => array('type' => 'email', 'name' => 'User[email]')
  5869. );
  5870. $this->assertHtml($expected, $result);
  5871. $result = $this->Form->search('User.query');
  5872. $expected = array(
  5873. 'input' => array('type' => 'search', 'name' => 'User[query]')
  5874. );
  5875. $this->assertHtml($expected, $result);
  5876. $result = $this->Form->search('User.query', array('value' => 'test'));
  5877. $expected = array(
  5878. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  5879. );
  5880. $this->assertHtml($expected, $result);
  5881. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  5882. $expected = array(
  5883. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  5884. );
  5885. $this->assertHtml($expected, $result);
  5886. }
  5887. /**
  5888. * Test accessing html5 inputs through input().
  5889. *
  5890. * @return void
  5891. */
  5892. public function testHtml5InputWithInput() {
  5893. $this->Form->create();
  5894. $this->Form->templates(['inputContainer' => '{{content}}']);
  5895. $result = $this->Form->input('website', array(
  5896. 'type' => 'url',
  5897. 'val' => 'http://domain.tld',
  5898. 'label' => false
  5899. ));
  5900. $expected = array(
  5901. 'input' => array('type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld')
  5902. );
  5903. $this->assertHtml($expected, $result);
  5904. }
  5905. /**
  5906. * Test errors when field name is missing.
  5907. *
  5908. * @expectedException \Cake\Core\Exception\Exception
  5909. * @return void
  5910. */
  5911. public function testHtml5InputException() {
  5912. $this->Form->email();
  5913. }
  5914. /**
  5915. * Tests that formhelper sets required attributes.
  5916. *
  5917. * @return void
  5918. */
  5919. public function testRequiredAttribute() {
  5920. $this->article['required'] = [
  5921. 'title' => true,
  5922. 'body' => false,
  5923. ];
  5924. $this->Form->create($this->article);
  5925. $result = $this->Form->input('title');
  5926. $expected = array(
  5927. 'div' => array('class' => 'input text required'),
  5928. 'label' => array('for' => 'title'),
  5929. 'Title',
  5930. '/label',
  5931. 'input' => array(
  5932. 'type' => 'text',
  5933. 'name' => 'title',
  5934. 'id' => 'title',
  5935. 'required' => 'required',
  5936. ),
  5937. '/div'
  5938. );
  5939. $this->assertHtml($expected, $result);
  5940. $result = $this->Form->input('title', ['required' => false]);
  5941. $this->assertNotContains('required', $result);
  5942. $result = $this->Form->input('body');
  5943. $expected = array(
  5944. 'div' => array('class' => 'input text'),
  5945. 'label' => array('for' => 'body'),
  5946. 'Body',
  5947. '/label',
  5948. 'input' => array(
  5949. 'type' => 'text',
  5950. 'name' => 'body',
  5951. 'id' => 'body',
  5952. ),
  5953. '/div'
  5954. );
  5955. $this->assertHtml($expected, $result);
  5956. $result = $this->Form->input('body', ['required' => true]);
  5957. $this->assertContains('required', $result);
  5958. }
  5959. /**
  5960. * Tests that it is possible to put inputs outside of the label
  5961. *
  5962. * @return void
  5963. */
  5964. public function testInputsNotNested() {
  5965. $this->Form->templates([
  5966. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  5967. 'formGroup' => '{{input}}{{label}}',
  5968. ]);
  5969. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  5970. $expected = [
  5971. 'div' => ['class' => 'input checkbox'],
  5972. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  5973. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  5974. 'label' => ['for' => 'foo'],
  5975. 'Foo',
  5976. '/label',
  5977. '/div'
  5978. ];
  5979. $this->assertHtml($expected, $result);
  5980. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  5981. $expected = [
  5982. 'div' => ['class' => 'input checkbox'],
  5983. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  5984. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  5985. '/div'
  5986. ];
  5987. $this->assertHtml($expected, $result);
  5988. $result = $this->Form->input('confirm', [
  5989. 'type' => 'radio',
  5990. 'options' => ['Y' => 'Yes', 'N' => 'No']
  5991. ]);
  5992. $expected = [
  5993. 'div' => ['class' => 'input radio'],
  5994. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  5995. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  5996. ['label' => ['for' => 'confirm-y']],
  5997. 'Yes',
  5998. '/label',
  5999. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  6000. ['label' => ['for' => 'confirm-n']],
  6001. 'No',
  6002. '/label',
  6003. '<label',
  6004. 'Confirm',
  6005. '/label',
  6006. '/div',
  6007. ];
  6008. $this->assertHtml($expected, $result);
  6009. $result = $this->Form->select('category', ['1', '2'], [
  6010. 'multiple' => 'checkbox',
  6011. 'name' => 'fish',
  6012. ]);
  6013. $expected = [
  6014. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  6015. ['div' => ['class' => 'checkbox']],
  6016. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  6017. ['label' => ['for' => 'fish-0']],
  6018. '1',
  6019. '/label',
  6020. '/div',
  6021. ['div' => ['class' => 'checkbox']],
  6022. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  6023. ['label' => ['for' => 'fish-1']],
  6024. '2',
  6025. '/label',
  6026. '/div'
  6027. ];
  6028. $this->assertHtml($expected, $result);
  6029. }
  6030. /**
  6031. * Test that *Container templates are used by input.
  6032. *
  6033. * @return void
  6034. */
  6035. public function testInputContainerTemplates() {
  6036. $this->Form->templates([
  6037. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  6038. 'radioContainer' => '<div class="rad">{{content}}</div>',
  6039. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  6040. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  6041. ]);
  6042. $this->article['errors'] = [
  6043. 'Article' => ['published' => 'error message']
  6044. ];
  6045. $this->Form->create($this->article);
  6046. $result = $this->Form->input('accept', [
  6047. 'type' => 'checkbox'
  6048. ]);
  6049. $expected = [
  6050. 'div' => ['class' => 'check'],
  6051. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  6052. 'label' => ['for' => 'accept'],
  6053. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  6054. 'Accept',
  6055. '/label',
  6056. '/div'
  6057. ];
  6058. $this->assertHtml($expected, $result);
  6059. $result = $this->Form->input('accept', [
  6060. 'type' => 'radio',
  6061. 'options' => ['Y', 'N']
  6062. ]);
  6063. $this->assertContains('<div class="rad">', $result);
  6064. $result = $this->Form->input('Article.published', [
  6065. 'type' => 'radio',
  6066. 'options' => ['Y', 'N']
  6067. ]);
  6068. $this->assertContains('<div class="rad err">', $result);
  6069. $result = $this->Form->input('Article.created', [
  6070. 'type' => 'datetime',
  6071. ]);
  6072. $this->assertContains('<div class="dt">', $result);
  6073. }
  6074. /**
  6075. * Test resetting templates.
  6076. *
  6077. * @return void
  6078. */
  6079. public function testResetTemplates() {
  6080. $this->Form->templates(['input' => '<input>']);
  6081. $this->assertEquals('<input>', $this->Form->templater()->get('input'));
  6082. $this->assertNull($this->Form->resetTemplates());
  6083. $this->assertNotEquals('<input>', $this->Form->templater()->get('input'));
  6084. }
  6085. /**
  6086. * Test the context method.
  6087. *
  6088. * @return void
  6089. */
  6090. public function testContext() {
  6091. $result = $this->Form->context();
  6092. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  6093. $mock = $this->getMock('Cake\View\Form\ContextInterface');
  6094. $this->assertSame($mock, $this->Form->context($mock));
  6095. $this->assertSame($mock, $this->Form->context());
  6096. }
  6097. }