FormHelperTest.php 184 KB

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