FormHelperTest.php 259 KB

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