FormHelperTest.php 262 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001
  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 reset unlockFields, when create new form.
  2089. *
  2090. * @return void
  2091. */
  2092. public function testResetUnlockFields()
  2093. {
  2094. $this->Form->request['_Token'] = [
  2095. 'key' => 'testKey',
  2096. 'unlockedFields' => []
  2097. ];
  2098. $this->Form->unlockField('Contact.id');
  2099. $this->Form->create('Contact');
  2100. $this->Form->hidden('Contact.id', ['value' => 1]);
  2101. $this->assertEmpty($this->Form->fields, 'Field should be unlocked');
  2102. $this->Form->end();
  2103. $this->Form->create('Contact');
  2104. $this->Form->hidden('Contact.id', ['value' => 1]);
  2105. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  2106. }
  2107. /**
  2108. * Test that only the path + query elements of a form's URL show up in their hash.
  2109. *
  2110. * @return void
  2111. */
  2112. public function testSecuredFormUrlIgnoresHost()
  2113. {
  2114. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2115. $expected = '0ff0c85cd70584d8fd18fa136846d22c66c21e2d%3A';
  2116. $this->Form->create($this->article, [
  2117. 'url' => ['controller' => 'articles', 'action' => 'view', 1, '?' => ['page' => 1]]
  2118. ]);
  2119. $result = $this->Form->secure();
  2120. $this->assertContains($expected, $result);
  2121. $this->Form->create($this->article, ['url' => 'http://localhost/articles/view/1?page=1']);
  2122. $result = $this->Form->secure();
  2123. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2124. $this->Form->create($this->article, ['url' => '/articles/view/1?page=1']);
  2125. $result = $this->Form->secure();
  2126. $this->assertContains($expected, $result, 'URL path + query should work.');
  2127. $this->Form->create($this->article, ['url' => '/articles/view/1']);
  2128. $result = $this->Form->secure();
  2129. $this->assertNotContains($expected, $result, 'URL is different');
  2130. }
  2131. /**
  2132. * Test that URL, HTML and identifer show up in their hashs.
  2133. *
  2134. * @return void
  2135. */
  2136. public function testSecuredFormUrlHasHtmlAndIdentifer()
  2137. {
  2138. $this->Form->request['_Token'] = ['key' => 'testKey'];
  2139. $expected = 'ece0693fb1b19ca116133db1832ac29baaf41ce5%3A';
  2140. $res = $this->Form->create($this->article, [
  2141. 'url' => [
  2142. 'controller' => 'articles',
  2143. 'action' => 'view',
  2144. '?' => [
  2145. 'page' => 1,
  2146. 'limit' => 10,
  2147. 'html' => '<>"',
  2148. ],
  2149. '#' => 'result',
  2150. ],
  2151. ]);
  2152. $result = $this->Form->secure();
  2153. $this->assertContains($expected, $result);
  2154. $this->Form->create($this->article, [
  2155. 'url' => 'http://localhost/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2156. ]);
  2157. $result = $this->Form->secure();
  2158. $this->assertContains($expected, $result, 'Full URL should only use path and query.');
  2159. $this->Form->create($this->article, [
  2160. 'url' => '/articles/view?page=1&limit=10&html=%3C%3E%22#result'
  2161. ]);
  2162. $result = $this->Form->secure();
  2163. $this->assertContains($expected, $result, 'URL path + query should work.');
  2164. }
  2165. /**
  2166. * test error message display
  2167. *
  2168. * @return void
  2169. */
  2170. public function testErrorMessageDisplay()
  2171. {
  2172. $this->article['errors'] = [
  2173. 'Article' => [
  2174. 'title' => 'error message',
  2175. 'content' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'
  2176. ]
  2177. ];
  2178. $this->Form->create($this->article);
  2179. $result = $this->Form->input('Article.title');
  2180. $expected = [
  2181. 'div' => ['class' => 'input text error'],
  2182. 'label' => ['for' => 'article-title'],
  2183. 'Title',
  2184. '/label',
  2185. 'input' => [
  2186. 'type' => 'text', 'name' => 'Article[title]',
  2187. 'id' => 'article-title', 'class' => 'form-error'
  2188. ],
  2189. ['div' => ['class' => 'error-message']],
  2190. 'error message',
  2191. '/div',
  2192. '/div'
  2193. ];
  2194. $this->assertHtml($expected, $result);
  2195. $result = $this->Form->input('Article.title', [
  2196. 'templates' => [
  2197. 'inputContainerError' => '<div class="input {{type}}{{required}} error">{{content}}</div>'
  2198. ]
  2199. ]);
  2200. $expected = [
  2201. 'div' => ['class' => 'input text error'],
  2202. 'label' => ['for' => 'article-title'],
  2203. 'Title',
  2204. '/label',
  2205. 'input' => [
  2206. 'type' => 'text', 'name' => 'Article[title]',
  2207. 'id' => 'article-title', 'class' => 'form-error'
  2208. ],
  2209. '/div'
  2210. ];
  2211. $this->assertHtml($expected, $result);
  2212. $result = $this->Form->input('Article.content');
  2213. $expected = [
  2214. 'div' => ['class' => 'input text error'],
  2215. 'label' => ['for' => 'article-content'],
  2216. 'Content',
  2217. '/label',
  2218. 'input' => [
  2219. 'type' => 'text', 'name' => 'Article[content]',
  2220. 'id' => 'article-content', 'class' => 'form-error'
  2221. ],
  2222. ['div' => ['class' => 'error-message']],
  2223. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2224. '/div',
  2225. '/div'
  2226. ];
  2227. $this->assertHtml($expected, $result);
  2228. $result = $this->Form->input('Article.content', ['error' => ['escape' => true]]);
  2229. $expected = [
  2230. 'div' => ['class' => 'input text error'],
  2231. 'label' => ['for' => 'article-content'],
  2232. 'Content',
  2233. '/label',
  2234. 'input' => [
  2235. 'type' => 'text', 'name' => 'Article[content]',
  2236. 'id' => 'article-content', 'class' => 'form-error'
  2237. ],
  2238. ['div' => ['class' => 'error-message']],
  2239. 'some &lt;strong&gt;test&lt;/strong&gt; data with &lt;a href=&quot;#&quot;&gt;HTML&lt;/a&gt; chars',
  2240. '/div',
  2241. '/div'
  2242. ];
  2243. $this->assertHtml($expected, $result);
  2244. $result = $this->Form->input('Article.content', ['error' => ['escape' => false]]);
  2245. $expected = [
  2246. 'div' => ['class' => 'input text error'],
  2247. 'label' => ['for' => 'article-content'],
  2248. 'Content',
  2249. '/label',
  2250. 'input' => [
  2251. 'type' => 'text', 'name' => 'Article[content]',
  2252. 'id' => 'article-content', 'class' => 'form-error'
  2253. ],
  2254. ['div' => ['class' => 'error-message']],
  2255. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  2256. '/div',
  2257. '/div'
  2258. ];
  2259. $this->assertHtml($expected, $result);
  2260. }
  2261. /**
  2262. * Test validation errors, when validation message is an empty string.
  2263. *
  2264. * @return void
  2265. */
  2266. public function testEmptyErrorValidation()
  2267. {
  2268. $this->article['errors'] = [
  2269. 'Article' => ['title' => '']
  2270. ];
  2271. $this->Form->create($this->article);
  2272. $result = $this->Form->input('Article.title');
  2273. $expected = [
  2274. 'div' => ['class' => 'input text error'],
  2275. 'label' => ['for' => 'article-title'],
  2276. 'Title',
  2277. '/label',
  2278. 'input' => [
  2279. 'type' => 'text', 'name' => 'Article[title]',
  2280. 'id' => 'article-title', 'class' => 'form-error'
  2281. ],
  2282. ['div' => ['class' => 'error-message']],
  2283. [],
  2284. '/div',
  2285. '/div'
  2286. ];
  2287. $this->assertHtml($expected, $result);
  2288. }
  2289. /**
  2290. * Test validation errors, when calling input() overriding validation message by an empty string.
  2291. *
  2292. * @return void
  2293. */
  2294. public function testEmptyInputErrorValidation()
  2295. {
  2296. $this->article['errors'] = [
  2297. 'Article' => ['title' => 'error message']
  2298. ];
  2299. $this->Form->create($this->article);
  2300. $result = $this->Form->input('Article.title', ['error' => '']);
  2301. $expected = [
  2302. 'div' => ['class' => 'input text error'],
  2303. 'label' => ['for' => 'article-title'],
  2304. 'Title',
  2305. '/label',
  2306. 'input' => [
  2307. 'type' => 'text', 'name' => 'Article[title]',
  2308. 'id' => 'article-title', 'class' => 'form-error'
  2309. ],
  2310. ['div' => ['class' => 'error-message']],
  2311. [],
  2312. '/div',
  2313. '/div'
  2314. ];
  2315. $this->assertHtml($expected, $result);
  2316. }
  2317. /**
  2318. * Test validation errors, when calling input() overriding validation messages
  2319. *
  2320. * @return void
  2321. */
  2322. public function testInputErrorMessage()
  2323. {
  2324. $this->article['errors'] = [
  2325. 'title' => ['error message']
  2326. ];
  2327. $this->Form->create($this->article);
  2328. $result = $this->Form->input('title', [
  2329. 'error' => 'Custom error!'
  2330. ]);
  2331. $expected = [
  2332. 'div' => ['class' => 'input text required error'],
  2333. 'label' => ['for' => 'title'],
  2334. 'Title',
  2335. '/label',
  2336. 'input' => [
  2337. 'type' => 'text', 'name' => 'title',
  2338. 'id' => 'title', 'class' => 'form-error',
  2339. 'required' => 'required',
  2340. ],
  2341. ['div' => ['class' => 'error-message']],
  2342. 'Custom error!',
  2343. '/div',
  2344. '/div'
  2345. ];
  2346. $this->assertHtml($expected, $result);
  2347. $result = $this->Form->input('title', [
  2348. 'error' => ['error message' => 'Custom error!']
  2349. ]);
  2350. $expected = [
  2351. 'div' => ['class' => 'input text required error'],
  2352. 'label' => ['for' => 'title'],
  2353. 'Title',
  2354. '/label',
  2355. 'input' => [
  2356. 'type' => 'text',
  2357. 'name' => 'title',
  2358. 'id' => 'title',
  2359. 'class' => 'form-error',
  2360. 'required' => 'required'
  2361. ],
  2362. ['div' => ['class' => 'error-message']],
  2363. 'Custom error!',
  2364. '/div',
  2365. '/div'
  2366. ];
  2367. $this->assertHtml($expected, $result);
  2368. }
  2369. /**
  2370. * Tests displaying errors for nested entities
  2371. *
  2372. * @return void
  2373. */
  2374. public function testFormValidationAssociated()
  2375. {
  2376. $nested = new Entity(['foo' => 'bar']);
  2377. $nested->errors('foo', ['not a valid bar']);
  2378. $entity = new Entity(['nested' => $nested]);
  2379. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2380. $result = $this->Form->error('nested.foo');
  2381. $this->assertEquals('<div class="error-message">not a valid bar</div>', $result);
  2382. }
  2383. /**
  2384. * testFormValidationAssociatedSecondLevel method
  2385. *
  2386. * test form error display with associated model.
  2387. *
  2388. * @return void
  2389. */
  2390. public function testFormValidationAssociatedSecondLevel()
  2391. {
  2392. $inner = new Entity(['bar' => 'baz']);
  2393. $nested = new Entity(['foo' => $inner]);
  2394. $entity = new Entity(['nested' => $nested]);
  2395. $inner->errors('bar', ['not a valid one']);
  2396. $this->Form->create($entity, ['context' => ['table' => 'Articles']]);
  2397. $result = $this->Form->error('nested.foo.bar');
  2398. $this->assertEquals('<div class="error-message">not a valid one</div>', $result);
  2399. }
  2400. /**
  2401. * testFormValidationMultiRecord method
  2402. *
  2403. * test form error display with multiple records.
  2404. *
  2405. * @return void
  2406. */
  2407. public function testFormValidationMultiRecord()
  2408. {
  2409. $one = new Entity;
  2410. $two = new Entity;
  2411. TableRegistry::get('Contacts', [
  2412. 'className' => __NAMESPACE__ . '\ContactsTable'
  2413. ]);
  2414. $one->set('email', '');
  2415. $one->errors('email', ['invalid email']);
  2416. $two->set('name', '');
  2417. $two->errors('name', ['This is wrong']);
  2418. $this->Form->create([$one, $two], ['context' => ['table' => 'Contacts']]);
  2419. $result = $this->Form->input('0.email');
  2420. $expected = [
  2421. 'div' => ['class' => 'input email error'],
  2422. 'label' => ['for' => '0-email'],
  2423. 'Email',
  2424. '/label',
  2425. 'input' => [
  2426. 'type' => 'email', 'name' => '0[email]', 'id' => '0-email',
  2427. 'class' => 'form-error', 'maxlength' => 255, 'value' => '',
  2428. ],
  2429. ['div' => ['class' => 'error-message']],
  2430. 'invalid email',
  2431. '/div',
  2432. '/div'
  2433. ];
  2434. $this->assertHtml($expected, $result);
  2435. $result = $this->Form->input('1.name');
  2436. $expected = [
  2437. 'div' => ['class' => 'input text error'],
  2438. 'label' => ['for' => '1-name'],
  2439. 'Name',
  2440. '/label',
  2441. 'input' => [
  2442. 'type' => 'text', 'name' => '1[name]', 'id' => '1-name',
  2443. 'class' => 'form-error', 'maxlength' => 255, 'value' => ''
  2444. ],
  2445. ['div' => ['class' => 'error-message']],
  2446. 'This is wrong',
  2447. '/div',
  2448. '/div'
  2449. ];
  2450. $this->assertHtml($expected, $result);
  2451. }
  2452. /**
  2453. * testInput method
  2454. *
  2455. * Test various incarnations of input().
  2456. *
  2457. * @return void
  2458. */
  2459. public function testInput()
  2460. {
  2461. TableRegistry::get('ValidateUsers', [
  2462. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2463. ]);
  2464. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2465. $result = $this->Form->input('ValidateUsers.balance');
  2466. $expected = [
  2467. 'div' => ['class'],
  2468. 'label' => ['for'],
  2469. 'Balance',
  2470. '/label',
  2471. 'input' => ['name', 'type' => 'number', 'id', 'step'],
  2472. '/div',
  2473. ];
  2474. $this->assertHtml($expected, $result);
  2475. $result = $this->Form->input('ValidateUser.cost_decimal');
  2476. $expected = [
  2477. 'div' => ['class'],
  2478. 'label' => ['for'],
  2479. 'Cost Decimal',
  2480. '/label',
  2481. 'input' => ['name', 'type' => 'number', 'step' => '0.001', 'id'],
  2482. '/div',
  2483. ];
  2484. $this->assertHtml($expected, $result);
  2485. $result = $this->Form->input('ValidateUser.null_decimal');
  2486. $expected = [
  2487. 'div' => ['class'],
  2488. 'label' => ['for'],
  2489. 'Null Decimal',
  2490. '/label',
  2491. 'input' => ['name', 'type' => 'number', 'id'],
  2492. '/div',
  2493. ];
  2494. $this->assertHtml($expected, $result);
  2495. }
  2496. /**
  2497. * Tests the input method and passing custom options
  2498. *
  2499. * @return void
  2500. */
  2501. public function testInputCustomization()
  2502. {
  2503. TableRegistry::get('Contacts', [
  2504. 'className' => __NAMESPACE__ . '\ContactsTable'
  2505. ]);
  2506. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2507. $result = $this->Form->input('Contact.email', ['id' => 'custom']);
  2508. $expected = [
  2509. 'div' => ['class' => 'input email'],
  2510. 'label' => ['for' => 'custom'],
  2511. 'Email',
  2512. '/label',
  2513. ['input' => [
  2514. 'type' => 'email', 'name' => 'Contact[email]',
  2515. 'id' => 'custom', 'maxlength' => 255
  2516. ]],
  2517. '/div'
  2518. ];
  2519. $this->assertHtml($expected, $result);
  2520. $result = $this->Form->input('Contact.email', [
  2521. 'templates' => ['inputContainer' => '<div>{{content}}</div>']
  2522. ]);
  2523. $expected = [
  2524. '<div',
  2525. 'label' => ['for' => 'contact-email'],
  2526. 'Email',
  2527. '/label',
  2528. ['input' => [
  2529. 'type' => 'email', 'name' => 'Contact[email]',
  2530. 'id' => 'contact-email', 'maxlength' => 255
  2531. ]],
  2532. '/div'
  2533. ];
  2534. $this->assertHtml($expected, $result);
  2535. $result = $this->Form->input('Contact.email', ['type' => 'text']);
  2536. $expected = [
  2537. 'div' => ['class' => 'input text'],
  2538. 'label' => ['for' => 'contact-email'],
  2539. 'Email',
  2540. '/label',
  2541. ['input' => [
  2542. 'type' => 'text', 'name' => 'Contact[email]',
  2543. 'id' => 'contact-email', 'maxlength' => '255'
  2544. ]],
  2545. '/div'
  2546. ];
  2547. $this->assertHtml($expected, $result);
  2548. $result = $this->Form->input('Contact.5.email', ['type' => 'text']);
  2549. $expected = [
  2550. 'div' => ['class' => 'input text'],
  2551. 'label' => ['for' => 'contact-5-email'],
  2552. 'Email',
  2553. '/label',
  2554. ['input' => [
  2555. 'type' => 'text', 'name' => 'Contact[5][email]',
  2556. 'id' => 'contact-5-email', 'maxlength' => '255'
  2557. ]],
  2558. '/div'
  2559. ];
  2560. $this->assertHtml($expected, $result);
  2561. $result = $this->Form->input('Contact.password');
  2562. $expected = [
  2563. 'div' => ['class' => 'input password'],
  2564. 'label' => ['for' => 'contact-password'],
  2565. 'Password',
  2566. '/label',
  2567. ['input' => [
  2568. 'type' => 'password', 'name' => 'Contact[password]',
  2569. 'id' => 'contact-password'
  2570. ]],
  2571. '/div'
  2572. ];
  2573. $this->assertHtml($expected, $result);
  2574. $result = $this->Form->input('Contact.email', [
  2575. 'type' => 'file', 'class' => 'textbox'
  2576. ]);
  2577. $expected = [
  2578. 'div' => ['class' => 'input file'],
  2579. 'label' => ['for' => 'contact-email'],
  2580. 'Email',
  2581. '/label',
  2582. ['input' => [
  2583. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2584. 'id' => 'contact-email'
  2585. ]],
  2586. '/div'
  2587. ];
  2588. $this->assertHtml($expected, $result);
  2589. $entity = new Entity(['phone' => 'Hello & World > weird chars']);
  2590. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2591. $result = $this->Form->input('phone');
  2592. $expected = [
  2593. 'div' => ['class' => 'input tel'],
  2594. 'label' => ['for' => 'phone'],
  2595. 'Phone',
  2596. '/label',
  2597. ['input' => [
  2598. 'type' => 'tel', 'name' => 'phone',
  2599. 'value' => 'Hello &amp; World &gt; weird chars',
  2600. 'id' => 'phone', 'maxlength' => 255
  2601. ]],
  2602. '/div'
  2603. ];
  2604. $this->assertHtml($expected, $result);
  2605. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2606. $this->Form->create();
  2607. $result = $this->Form->input('Model.0.OtherModel.field', ['id' => 'myId']);
  2608. $expected = [
  2609. 'div' => ['class' => 'input text'],
  2610. 'label' => ['for' => 'myId'],
  2611. 'Field',
  2612. '/label',
  2613. 'input' => [
  2614. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2615. 'value' => 'My value', 'id' => 'myId'
  2616. ],
  2617. '/div'
  2618. ];
  2619. $this->assertHtml($expected, $result);
  2620. $this->Form->request->data = [];
  2621. $entity->errors('field', 'Badness!');
  2622. $this->Form->create($entity, ['context' => ['table' => 'Contacts']]);
  2623. $result = $this->Form->input('field');
  2624. $expected = [
  2625. 'div' => ['class' => 'input text error'],
  2626. 'label' => ['for' => 'field'],
  2627. 'Field',
  2628. '/label',
  2629. 'input' => [
  2630. 'type' => 'text', 'name' => 'field',
  2631. 'id' => 'field', 'class' => 'form-error'
  2632. ],
  2633. ['div' => ['class' => 'error-message']],
  2634. 'Badness!',
  2635. '/div',
  2636. '/div'
  2637. ];
  2638. $this->assertHtml($expected, $result);
  2639. $result = $this->Form->input('field', [
  2640. 'templates' => [
  2641. 'inputContainerError' => '{{content}}{{error}}',
  2642. 'error' => '<span class="error-message">{{content}}</span>'
  2643. ]
  2644. ]);
  2645. $expected = [
  2646. 'label' => ['for' => 'field'],
  2647. 'Field',
  2648. '/label',
  2649. 'input' => [
  2650. 'type' => 'text', 'name' => 'field',
  2651. 'id' => 'field', 'class' => 'form-error'
  2652. ],
  2653. ['span' => ['class' => 'error-message']],
  2654. 'Badness!',
  2655. '/span'
  2656. ];
  2657. $this->assertHtml($expected, $result);
  2658. $entity->errors('field', ['minLength'], true);
  2659. $result = $this->Form->input('field', [
  2660. 'error' => [
  2661. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2662. 'maxLength' => 'login too large'
  2663. ]
  2664. ]);
  2665. $expected = [
  2666. 'div' => ['class' => 'input text error'],
  2667. 'label' => ['for' => 'field'],
  2668. 'Field',
  2669. '/label',
  2670. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2671. ['div' => ['class' => 'error-message']],
  2672. 'Le login doit contenir au moins 2 caractères',
  2673. '/div',
  2674. '/div'
  2675. ];
  2676. $this->assertHtml($expected, $result);
  2677. $entity->errors('field', ['maxLength'], true);
  2678. $result = $this->Form->input('field', [
  2679. 'error' => [
  2680. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2681. 'maxLength' => 'login too large',
  2682. ]
  2683. ]);
  2684. $expected = [
  2685. 'div' => ['class' => 'input text error'],
  2686. 'label' => ['for' => 'field'],
  2687. 'Field',
  2688. '/label',
  2689. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field', 'class' => 'form-error'],
  2690. ['div' => ['class' => 'error-message']],
  2691. 'login too large',
  2692. '/div',
  2693. '/div'
  2694. ];
  2695. $this->assertHtml($expected, $result);
  2696. }
  2697. /**
  2698. * Test that input() accepts a template file.
  2699. *
  2700. * @return void
  2701. */
  2702. public function testInputWithTemplateFile()
  2703. {
  2704. $result = $this->Form->input('field', [
  2705. 'templates' => 'htmlhelper_tags'
  2706. ]);
  2707. $expected = [
  2708. 'label' => ['for' => 'field'],
  2709. 'Field',
  2710. '/label',
  2711. 'input' => [
  2712. 'type' => 'text', 'name' => 'field',
  2713. 'id' => 'field'
  2714. ],
  2715. ];
  2716. $this->assertHtml($expected, $result);
  2717. }
  2718. /**
  2719. * Test that nested inputs end with brackets
  2720. *
  2721. * @return void
  2722. */
  2723. public function testNestedInputsEndWithBrackets()
  2724. {
  2725. $result = $this->Form->text('nested.text[]');
  2726. $expected = [
  2727. 'input' => [
  2728. 'type' => 'text', 'name' => 'nested[text][]'
  2729. ],
  2730. ];
  2731. $result = $this->Form->file('nested.file[]');
  2732. $expected = [
  2733. 'input' => [
  2734. 'type' => 'file', 'name' => 'nested[file][]'
  2735. ],
  2736. ];
  2737. $this->assertHtml($expected, $result);
  2738. }
  2739. /**
  2740. * Test id prefix
  2741. *
  2742. * @return void
  2743. */
  2744. public function testCreateIdPrefix()
  2745. {
  2746. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2747. $result = $this->Form->input('field');
  2748. $expected = [
  2749. 'div' => ['class' => 'input text'],
  2750. 'label' => ['for' => 'prefix-field'],
  2751. 'Field',
  2752. '/label',
  2753. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'prefix-field'],
  2754. '/div'
  2755. ];
  2756. $this->assertHtml($expected, $result);
  2757. $result = $this->Form->input('field', ['id' => 'custom-id']);
  2758. $expected = [
  2759. 'div' => ['class' => 'input text'],
  2760. 'label' => ['for' => 'custom-id'],
  2761. 'Field',
  2762. '/label',
  2763. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'custom-id'],
  2764. '/div'
  2765. ];
  2766. $this->assertHtml($expected, $result);
  2767. $result = $this->Form->radio('Model.field', ['option A']);
  2768. $expected = [
  2769. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2770. 'label' => ['for' => 'prefix-model-field-0'],
  2771. ['input' => [
  2772. 'type' => 'radio',
  2773. 'name' => 'Model[field]',
  2774. 'value' => '0',
  2775. 'id' => 'prefix-model-field-0'
  2776. ]],
  2777. 'option A',
  2778. '/label'
  2779. ];
  2780. $this->assertHtml($expected, $result);
  2781. $result = $this->Form->radio('Model.field', ['option A', 'option']);
  2782. $expected = [
  2783. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  2784. 'label' => ['for' => 'prefix-model-field-0'],
  2785. ['input' => [
  2786. 'type' => 'radio',
  2787. 'name' => 'Model[field]',
  2788. 'value' => '0',
  2789. 'id' => 'prefix-model-field-0'
  2790. ]],
  2791. 'option A',
  2792. '/label'
  2793. ];
  2794. $this->assertHtml($expected, $result);
  2795. $result = $this->Form->select(
  2796. 'Model.multi_field',
  2797. ['first'],
  2798. ['multiple' => 'checkbox']
  2799. );
  2800. $expected = [
  2801. 'input' => [
  2802. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  2803. ],
  2804. ['div' => ['class' => 'checkbox']],
  2805. ['label' => ['for' => 'prefix-model-multi-field-0']],
  2806. ['input' => [
  2807. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  2808. 'value' => '0', 'id' => 'prefix-model-multi-field-0'
  2809. ]],
  2810. 'first',
  2811. '/label',
  2812. '/div',
  2813. ];
  2814. $this->assertHtml($expected, $result);
  2815. $this->Form->end();
  2816. $result = $this->Form->input('field');
  2817. $expected = [
  2818. 'div' => ['class' => 'input text'],
  2819. 'label' => ['for' => 'field'],
  2820. 'Field',
  2821. '/label',
  2822. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  2823. '/div'
  2824. ];
  2825. $this->assertHtml($expected, $result);
  2826. }
  2827. /**
  2828. * Test that inputs with 0 can be created.
  2829. *
  2830. * @return void
  2831. */
  2832. public function testInputZero()
  2833. {
  2834. TableRegistry::get('Contacts', [
  2835. 'className' => __NAMESPACE__ . '\ContactsTable'
  2836. ]);
  2837. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  2838. $result = $this->Form->input('0');
  2839. $expected = [
  2840. 'div' => ['class' => 'input text'],
  2841. 'label' => ['for' => '0'], '/label',
  2842. 'input' => ['type' => 'text', 'name' => '0', 'id' => '0'],
  2843. '/div'
  2844. ];
  2845. $this->assertHtml($expected, $result);
  2846. }
  2847. /**
  2848. * test input() with checkbox creation
  2849. *
  2850. * @return void
  2851. */
  2852. public function testInputCheckbox()
  2853. {
  2854. $result = $this->Form->input('User.active', ['label' => false, 'checked' => true]);
  2855. $expected = [
  2856. 'div' => ['class' => 'input checkbox'],
  2857. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2858. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2859. '/div'
  2860. ];
  2861. $this->assertHtml($expected, $result);
  2862. $result = $this->Form->input('User.active', ['label' => false, 'checked' => 1]);
  2863. $expected = [
  2864. 'div' => ['class' => 'input checkbox'],
  2865. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2866. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2867. '/div'
  2868. ];
  2869. $this->assertHtml($expected, $result);
  2870. $result = $this->Form->input('User.active', ['label' => false, 'checked' => '1']);
  2871. $expected = [
  2872. 'div' => ['class' => 'input checkbox'],
  2873. 'input' => ['type' => 'hidden', 'name' => 'User[active]', 'value' => '0'],
  2874. ['input' => ['type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'user-active', 'checked' => 'checked']],
  2875. '/div'
  2876. ];
  2877. $this->assertHtml($expected, $result);
  2878. $result = $this->Form->input('User.disabled', [
  2879. 'label' => 'Disabled',
  2880. 'type' => 'checkbox',
  2881. 'data-foo' => 'disabled'
  2882. ]);
  2883. $expected = [
  2884. 'div' => ['class' => 'input checkbox'],
  2885. 'input' => ['type' => 'hidden', 'name' => 'User[disabled]', 'value' => '0'],
  2886. 'label' => ['for' => 'user-disabled'],
  2887. ['input' => [
  2888. 'type' => 'checkbox',
  2889. 'name' => 'User[disabled]',
  2890. 'value' => '1',
  2891. 'id' => 'user-disabled',
  2892. 'data-foo' => 'disabled'
  2893. ]],
  2894. 'Disabled',
  2895. '/label',
  2896. '/div'
  2897. ];
  2898. $this->assertHtml($expected, $result);
  2899. }
  2900. /**
  2901. * Test that input() does not create wrapping div and label tag for hidden fields
  2902. *
  2903. * @return void
  2904. */
  2905. public function testInputHidden()
  2906. {
  2907. TableRegistry::get('ValidateUsers', [
  2908. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  2909. ]);
  2910. $this->Form->create([], ['context' => ['table' => 'ValidateUsers']]);
  2911. $result = $this->Form->input('ValidateUser.id');
  2912. $expected = [
  2913. 'input' => ['name', 'type' => 'hidden', 'id']
  2914. ];
  2915. $this->assertHtml($expected, $result);
  2916. $result = $this->Form->input('ValidateUser.custom', ['type' => 'hidden']);
  2917. $expected = [
  2918. 'input' => ['name', 'type' => 'hidden', 'id']
  2919. ];
  2920. $this->assertHtml($expected, $result);
  2921. }
  2922. /**
  2923. * test form->input() with datetime
  2924. *
  2925. * @return void
  2926. */
  2927. public function testInputDatetime()
  2928. {
  2929. $this->Form = $this->getMock(
  2930. 'Cake\View\Helper\FormHelper',
  2931. ['datetime'],
  2932. [new View()]
  2933. );
  2934. $this->Form->expects($this->once())->method('datetime')
  2935. ->with('prueba', [
  2936. 'type' => 'datetime',
  2937. 'timeFormat' => 24,
  2938. 'minYear' => 2008,
  2939. 'maxYear' => 2011,
  2940. 'interval' => 15,
  2941. 'options' => null,
  2942. 'empty' => false,
  2943. 'id' => 'prueba',
  2944. 'required' => false,
  2945. 'templateVars' => []
  2946. ])
  2947. ->will($this->returnValue('This is it!'));
  2948. $result = $this->Form->input('prueba', [
  2949. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2950. 'maxYear' => 2011, 'interval' => 15
  2951. ]);
  2952. $expected = [
  2953. 'div' => ['class' => 'input datetime'],
  2954. '<label',
  2955. 'Prueba',
  2956. '/label',
  2957. 'This is it!',
  2958. '/div'
  2959. ];
  2960. $this->assertHtml($expected, $result);
  2961. }
  2962. /**
  2963. * test form->input() with datetime with id prefix
  2964. *
  2965. * @return void
  2966. */
  2967. public function testInputDatetimeIdPrefix()
  2968. {
  2969. $this->Form = $this->getMock(
  2970. 'Cake\View\Helper\FormHelper',
  2971. ['datetime'],
  2972. [new View()]
  2973. );
  2974. $this->Form->create(false, ['idPrefix' => 'prefix']);
  2975. $this->Form->expects($this->once())->method('datetime')
  2976. ->with('prueba', [
  2977. 'type' => 'datetime',
  2978. 'timeFormat' => 24,
  2979. 'minYear' => 2008,
  2980. 'maxYear' => 2011,
  2981. 'interval' => 15,
  2982. 'options' => null,
  2983. 'empty' => false,
  2984. 'id' => 'prefix-prueba',
  2985. 'required' => false,
  2986. 'templateVars' => []
  2987. ])
  2988. ->will($this->returnValue('This is it!'));
  2989. $result = $this->Form->input('prueba', [
  2990. 'type' => 'datetime', 'timeFormat' => 24, 'minYear' => 2008,
  2991. 'maxYear' => 2011, 'interval' => 15
  2992. ]);
  2993. $expected = [
  2994. 'div' => ['class' => 'input datetime'],
  2995. '<label',
  2996. 'Prueba',
  2997. '/label',
  2998. 'This is it!',
  2999. '/div'
  3000. ];
  3001. $this->assertHtml($expected, $result);
  3002. }
  3003. /**
  3004. * Test generating checkboxes with disabled elements.
  3005. *
  3006. * @return void
  3007. */
  3008. public function testInputCheckboxWithDisabledElements()
  3009. {
  3010. $options = [1 => 'One', 2 => 'Two', '3' => 'Three'];
  3011. $result = $this->Form->input('Contact.multiple', [
  3012. 'multiple' => 'checkbox',
  3013. 'disabled' => 'disabled',
  3014. 'options' => $options
  3015. ]);
  3016. $expected = [
  3017. ['div' => ['class' => 'input select']],
  3018. ['label' => ['for' => "contact-multiple"]],
  3019. 'Multiple',
  3020. '/label',
  3021. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'disabled' => 'disabled', 'value' => '']],
  3022. ['div' => ['class' => 'checkbox']],
  3023. ['label' => ['for' => "contact-multiple-1"]],
  3024. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "contact-multiple-1"]],
  3025. 'One',
  3026. '/label',
  3027. '/div',
  3028. ['div' => ['class' => 'checkbox']],
  3029. ['label' => ['for' => "contact-multiple-2"]],
  3030. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "contact-multiple-2"]],
  3031. 'Two',
  3032. '/label',
  3033. '/div',
  3034. ['div' => ['class' => 'checkbox']],
  3035. ['label' => ['for' => "contact-multiple-3"]],
  3036. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "contact-multiple-3"]],
  3037. 'Three',
  3038. '/label',
  3039. '/div',
  3040. '/div'
  3041. ];
  3042. $this->assertHtml($expected, $result);
  3043. // make sure 50 does only disable 50, and not 50f5c0cf
  3044. $options = ['50' => 'Fifty', '50f5c0cf' => 'Stringy'];
  3045. $disabled = [50];
  3046. $expected = [
  3047. ['div' => ['class' => 'input select']],
  3048. ['label' => ['for' => "contact-multiple"]],
  3049. 'Multiple',
  3050. '/label',
  3051. ['input' => ['type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '']],
  3052. ['div' => ['class' => 'checkbox']],
  3053. ['label' => ['for' => "contact-multiple-50"]],
  3054. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "contact-multiple-50"]],
  3055. 'Fifty',
  3056. '/label',
  3057. '/div',
  3058. ['div' => ['class' => 'checkbox']],
  3059. ['label' => ['for' => "contact-multiple-50f5c0cf"]],
  3060. ['input' => ['type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "contact-multiple-50f5c0cf"]],
  3061. 'Stringy',
  3062. '/label',
  3063. '/div',
  3064. '/div'
  3065. ];
  3066. $result = $this->Form->input('Contact.multiple', ['multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options]);
  3067. $this->assertHtml($expected, $result);
  3068. }
  3069. /**
  3070. * test input name with leading integer, ensure attributes are generated correctly.
  3071. *
  3072. * @return void
  3073. */
  3074. public function testInputWithLeadingInteger()
  3075. {
  3076. $result = $this->Form->text('0.Node.title');
  3077. $expected = [
  3078. 'input' => ['name' => '0[Node][title]', 'type' => 'text']
  3079. ];
  3080. $this->assertHtml($expected, $result);
  3081. }
  3082. /**
  3083. * test form->input() with select type inputs.
  3084. *
  3085. * @return void
  3086. */
  3087. public function testInputSelectType()
  3088. {
  3089. $result = $this->Form->input(
  3090. 'email',
  3091. [
  3092. 'options' => ['è' => 'Firést', 'é' => 'Secoènd'], 'empty' => true]
  3093. );
  3094. $expected = [
  3095. 'div' => ['class' => 'input select'],
  3096. 'label' => ['for' => 'email'],
  3097. 'Email',
  3098. '/label',
  3099. ['select' => ['name' => 'email', 'id' => 'email']],
  3100. ['option' => ['value' => '']],
  3101. '/option',
  3102. ['option' => ['value' => 'è']],
  3103. 'Firést',
  3104. '/option',
  3105. ['option' => ['value' => 'é']],
  3106. 'Secoènd',
  3107. '/option',
  3108. '/select',
  3109. '/div'
  3110. ];
  3111. $this->assertHtml($expected, $result);
  3112. $result = $this->Form->input(
  3113. 'email',
  3114. [
  3115. 'options' => ['First', 'Second'], 'empty' => true]
  3116. );
  3117. $expected = [
  3118. 'div' => ['class' => 'input select'],
  3119. 'label' => ['for' => 'email'],
  3120. 'Email',
  3121. '/label',
  3122. ['select' => ['name' => 'email', 'id' => 'email']],
  3123. ['option' => ['value' => '']],
  3124. '/option',
  3125. ['option' => ['value' => '0']],
  3126. 'First',
  3127. '/option',
  3128. ['option' => ['value' => '1']],
  3129. 'Second',
  3130. '/option',
  3131. '/select',
  3132. '/div'
  3133. ];
  3134. $this->assertHtml($expected, $result);
  3135. $result = $this->Form->input('email', [
  3136. 'type' => 'select',
  3137. 'options' => new \ArrayObject(['First', 'Second']),
  3138. 'empty' => true
  3139. ]);
  3140. $this->assertHtml($expected, $result);
  3141. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3142. $this->Form->request->data = ['Model' => ['user_id' => 'value']];
  3143. $result = $this->Form->input('Model.user_id', ['empty' => true]);
  3144. $expected = [
  3145. 'div' => ['class' => 'input select'],
  3146. 'label' => ['for' => 'model-user-id'],
  3147. 'User',
  3148. '/label',
  3149. 'select' => ['name' => 'Model[user_id]', 'id' => 'model-user-id'],
  3150. ['option' => ['value' => '']],
  3151. '/option',
  3152. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3153. 'good',
  3154. '/option',
  3155. ['option' => ['value' => 'other']],
  3156. 'bad',
  3157. '/option',
  3158. '/select',
  3159. '/div'
  3160. ];
  3161. $this->assertHtml($expected, $result);
  3162. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3163. $this->Form->request->data = ['Thing' => ['user_id' => null]];
  3164. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3165. $expected = [
  3166. 'div' => ['class' => 'input select'],
  3167. 'label' => ['for' => 'thing-user-id'],
  3168. 'User',
  3169. '/label',
  3170. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3171. ['option' => ['value' => '']],
  3172. 'Some Empty',
  3173. '/option',
  3174. ['option' => ['value' => 'value']],
  3175. 'good',
  3176. '/option',
  3177. ['option' => ['value' => 'other']],
  3178. 'bad',
  3179. '/option',
  3180. '/select',
  3181. '/div'
  3182. ];
  3183. $this->assertHtml($expected, $result);
  3184. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3185. $this->Form->request->data = ['Thing' => ['user_id' => 'value']];
  3186. $result = $this->Form->input('Thing.user_id', ['empty' => 'Some Empty']);
  3187. $expected = [
  3188. 'div' => ['class' => 'input select'],
  3189. 'label' => ['for' => 'thing-user-id'],
  3190. 'User',
  3191. '/label',
  3192. 'select' => ['name' => 'Thing[user_id]', 'id' => 'thing-user-id'],
  3193. ['option' => ['value' => '']],
  3194. 'Some Empty',
  3195. '/option',
  3196. ['option' => ['value' => 'value', 'selected' => 'selected']],
  3197. 'good',
  3198. '/option',
  3199. ['option' => ['value' => 'other']],
  3200. 'bad',
  3201. '/option',
  3202. '/select',
  3203. '/div'
  3204. ];
  3205. $this->assertHtml($expected, $result);
  3206. $this->Form->data = [];
  3207. $result = $this->Form->input('Publisher.id', [
  3208. 'label' => 'Publisher',
  3209. 'type' => 'select',
  3210. 'multiple' => 'checkbox',
  3211. 'options' => ['Value 1' => 'Label 1', 'Value 2' => 'Label 2']
  3212. ]);
  3213. $expected = [
  3214. ['div' => ['class' => 'input select']],
  3215. ['label' => ['for' => 'publisher-id']],
  3216. 'Publisher',
  3217. '/label',
  3218. 'input' => ['type' => 'hidden', 'name' => 'Publisher[id]', 'value' => ''],
  3219. ['div' => ['class' => 'checkbox']],
  3220. ['label' => ['for' => 'publisher-id-value-1']],
  3221. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'publisher-id-value-1']],
  3222. 'Label 1',
  3223. '/label',
  3224. '/div',
  3225. ['div' => ['class' => 'checkbox']],
  3226. ['label' => ['for' => 'publisher-id-value-2']],
  3227. ['input' => ['type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'publisher-id-value-2']],
  3228. 'Label 2',
  3229. '/label',
  3230. '/div',
  3231. '/div'
  3232. ];
  3233. $this->assertHtml($expected, $result);
  3234. }
  3235. /**
  3236. * test that input() and a non standard primary key makes a hidden input by default.
  3237. *
  3238. * @return void
  3239. */
  3240. public function testInputWithNonStandardPrimaryKeyMakesHidden()
  3241. {
  3242. $this->article['schema']['_constraints']['primary']['columns'] = ['title'];
  3243. $this->Form->create($this->article);
  3244. $result = $this->Form->input('title');
  3245. $expected = [
  3246. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3247. ];
  3248. $this->assertHtml($expected, $result);
  3249. $this->article['schema']['_constraints']['primary']['columns'] = ['title', 'body'];
  3250. $this->Form->create($this->article);
  3251. $result = $this->Form->input('title');
  3252. $expected = [
  3253. 'input' => ['type' => 'hidden', 'name' => 'title', 'id' => 'title'],
  3254. ];
  3255. $this->assertHtml($expected, $result);
  3256. $result = $this->Form->input('body');
  3257. $expected = [
  3258. 'input' => ['type' => 'hidden', 'name' => 'body', 'id' => 'body'],
  3259. ];
  3260. $this->assertHtml($expected, $result);
  3261. }
  3262. /**
  3263. * test that overriding the magic select type widget is possible
  3264. *
  3265. * @return void
  3266. */
  3267. public function testInputOverridingMagicSelectType()
  3268. {
  3269. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3270. $result = $this->Form->input('Model.user_id', ['type' => 'text']);
  3271. $expected = [
  3272. 'div' => ['class' => 'input text'],
  3273. 'label' => ['for' => 'model-user-id'], 'User', '/label',
  3274. 'input' => ['name' => 'Model[user_id]', 'type' => 'text', 'id' => 'model-user-id'],
  3275. '/div'
  3276. ];
  3277. $this->assertHtml($expected, $result);
  3278. //Check that magic types still work for plural/singular vars
  3279. $this->View->viewVars['types'] = ['value' => 'good', 'other' => 'bad'];
  3280. $result = $this->Form->input('Model.type');
  3281. $expected = [
  3282. 'div' => ['class' => 'input select'],
  3283. 'label' => ['for' => 'model-type'], 'Type', '/label',
  3284. 'select' => ['name' => 'Model[type]', 'id' => 'model-type'],
  3285. ['option' => ['value' => 'value']], 'good', '/option',
  3286. ['option' => ['value' => 'other']], 'bad', '/option',
  3287. '/select',
  3288. '/div'
  3289. ];
  3290. $this->assertHtml($expected, $result);
  3291. }
  3292. /**
  3293. * Test that inferred types do not override developer input
  3294. *
  3295. * @return void
  3296. */
  3297. public function testInputMagicTypeDoesNotOverride()
  3298. {
  3299. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3300. $result = $this->Form->input('Model.user', ['type' => 'checkbox']);
  3301. $expected = [
  3302. 'div' => ['class' => 'input checkbox'],
  3303. ['input' => [
  3304. 'type' => 'hidden',
  3305. 'name' => 'Model[user]',
  3306. 'value' => 0,
  3307. ]],
  3308. 'label' => ['for' => 'model-user'],
  3309. ['input' => [
  3310. 'name' => 'Model[user]',
  3311. 'type' => 'checkbox',
  3312. 'id' => 'model-user',
  3313. 'value' => 1
  3314. ]],
  3315. 'User',
  3316. '/label',
  3317. '/div'
  3318. ];
  3319. $this->assertHtml($expected, $result);
  3320. // make sure that for HABTM the multiple option is not being overwritten in case it's truly
  3321. $options = [
  3322. 1 => 'blue',
  3323. 2 => 'red'
  3324. ];
  3325. $result = $this->Form->input('tags._ids', ['options' => $options, 'multiple' => 'checkbox']);
  3326. $expected = [
  3327. 'div' => ['class' => 'input select'],
  3328. 'label' => ['for' => 'tags-ids'],
  3329. 'Tags',
  3330. '/label',
  3331. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  3332. ['div' => ['class' => 'checkbox']],
  3333. ['label' => ['for' => 'tags-ids-1']],
  3334. ['input' => [
  3335. 'id' => 'tags-ids-1', 'type' => 'checkbox',
  3336. 'value' => '1', 'name' => 'tags[_ids][]'
  3337. ]],
  3338. 'blue',
  3339. '/label',
  3340. '/div',
  3341. ['div' => ['class' => 'checkbox']],
  3342. ['label' => ['for' => 'tags-ids-2']],
  3343. ['input' => [
  3344. 'id' => 'tags-ids-2', 'type' => 'checkbox',
  3345. 'value' => '2', 'name' => 'tags[_ids][]'
  3346. ]],
  3347. 'red',
  3348. '/label',
  3349. '/div',
  3350. '/div'
  3351. ];
  3352. $this->assertHtml($expected, $result);
  3353. }
  3354. /**
  3355. * Test that magic input() selects are created for type=number
  3356. *
  3357. * @return void
  3358. */
  3359. public function testInputMagicSelectForTypeNumber()
  3360. {
  3361. TableRegistry::get('ValidateUsers', [
  3362. 'className' => __NAMESPACE__ . '\ValidateUsersTable'
  3363. ]);
  3364. $entity = new Entity(['balance' => 1]);
  3365. $this->Form->create($entity, ['context' => ['table' => 'ValidateUsers']]);
  3366. $this->View->viewVars['balances'] = [0 => 'nothing', 1 => 'some', 100 => 'a lot'];
  3367. $result = $this->Form->input('balance');
  3368. $expected = [
  3369. 'div' => ['class' => 'input select'],
  3370. 'label' => ['for' => 'balance'],
  3371. 'Balance',
  3372. '/label',
  3373. 'select' => ['name' => 'balance', 'id' => 'balance'],
  3374. ['option' => ['value' => '0']],
  3375. 'nothing',
  3376. '/option',
  3377. ['option' => ['value' => '1', 'selected' => 'selected']],
  3378. 'some',
  3379. '/option',
  3380. ['option' => ['value' => '100']],
  3381. 'a lot',
  3382. '/option',
  3383. '/select',
  3384. '/div'
  3385. ];
  3386. $this->assertHtml($expected, $result);
  3387. }
  3388. /**
  3389. * Test that magic input() selects can easily be converted into radio types without error.
  3390. *
  3391. * @return void
  3392. */
  3393. public function testInputMagicSelectChangeToRadio()
  3394. {
  3395. $this->View->viewVars['users'] = ['value' => 'good', 'other' => 'bad'];
  3396. $result = $this->Form->input('Model.user_id', ['type' => 'radio']);
  3397. $this->assertContains('input type="radio"', $result);
  3398. }
  3399. /**
  3400. * testFormInputSubmit method
  3401. *
  3402. * test correct results for form::input() and type submit.
  3403. *
  3404. * @return void
  3405. */
  3406. public function testFormInputSubmit()
  3407. {
  3408. $result = $this->Form->input('Test Submit', ['type' => 'submit', 'class' => 'foobar']);
  3409. $expected = [
  3410. 'div' => ['class' => 'submit'],
  3411. 'input' => ['type' => 'submit', 'class' => 'foobar', 'id' => 'test-submit', 'value' => 'Test Submit'],
  3412. '/div'
  3413. ];
  3414. $this->assertHtml($expected, $result);
  3415. }
  3416. /**
  3417. * testFormInputs method
  3418. *
  3419. * test correct results from form::inputs().
  3420. *
  3421. * @return void
  3422. */
  3423. public function testFormInputsLegendFieldset()
  3424. {
  3425. $this->Form->create($this->article);
  3426. $result = $this->Form->allInputs([], ['legend' => 'The Legend']);
  3427. $expected = [
  3428. '<fieldset',
  3429. '<legend',
  3430. 'The Legend',
  3431. '/legend',
  3432. '*/fieldset',
  3433. ];
  3434. $this->assertHtml($expected, $result);
  3435. $result = $this->Form->allInputs([], ['fieldset' => true, 'legend' => 'Field of Dreams']);
  3436. $this->assertContains('<legend>Field of Dreams</legend>', $result);
  3437. $this->assertContains('<fieldset>', $result);
  3438. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => false]);
  3439. $this->assertNotContains('<legend>', $result);
  3440. $this->assertNotContains('<fieldset>', $result);
  3441. $result = $this->Form->allInputs([], ['fieldset' => false, 'legend' => 'Hello']);
  3442. $this->assertNotContains('<legend>', $result);
  3443. $this->assertNotContains('<fieldset>', $result);
  3444. $this->Form->create($this->article);
  3445. $this->Form->request->params['prefix'] = 'admin';
  3446. $this->Form->request->params['action'] = 'admin_edit';
  3447. $this->Form->request->params['controller'] = 'articles';
  3448. $result = $this->Form->allInputs();
  3449. $expected = [
  3450. '<fieldset',
  3451. '<legend',
  3452. 'New Article',
  3453. '/legend',
  3454. '*/fieldset',
  3455. ];
  3456. $this->assertHtml($expected, $result);
  3457. $this->Form->create($this->article);
  3458. $result = $this->Form->allInputs([], ['fieldset' => [], 'legend' => 'The Legend']);
  3459. $expected = [
  3460. '<fieldset',
  3461. '<legend',
  3462. 'The Legend',
  3463. '/legend',
  3464. '*/fieldset',
  3465. ];
  3466. $this->assertHtml($expected, $result);
  3467. $this->Form->create($this->article);
  3468. $result = $this->Form->allInputs([], [
  3469. 'fieldset' => [
  3470. 'class' => 'some-class some-other-class',
  3471. 'disabled' => true,
  3472. 'data-param' => 'a-param'
  3473. ],
  3474. 'legend' => 'The Legend'
  3475. ]);
  3476. $expected = [
  3477. '<fieldset class="some-class some-other-class" disabled="disabled" data-param="a-param"',
  3478. '<legend',
  3479. 'The Legend',
  3480. '/legend',
  3481. '*/fieldset',
  3482. ];
  3483. $this->assertHtml($expected, $result);
  3484. }
  3485. /**
  3486. * Test the inputs() method.
  3487. *
  3488. * @return void
  3489. */
  3490. public function testFormInputs()
  3491. {
  3492. $this->Form->create($this->article);
  3493. $result = $this->Form->allInputs();
  3494. $expected = [
  3495. '<fieldset',
  3496. '<legend', 'New Article', '/legend',
  3497. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3498. ['div' => ['class' => 'input select required']],
  3499. '*/div',
  3500. ['div' => ['class' => 'input text required']],
  3501. '*/div',
  3502. ['div' => ['class' => 'input text']],
  3503. '*/div',
  3504. ['div' => ['class' => 'input text']],
  3505. '*/div',
  3506. '/fieldset',
  3507. ];
  3508. $this->assertHtml($expected, $result);
  3509. $result = $this->Form->allInputs([
  3510. 'published' => ['type' => 'boolean']
  3511. ]);
  3512. $expected = [
  3513. '<fieldset',
  3514. '<legend', 'New Article', '/legend',
  3515. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3516. ['div' => ['class' => 'input select required']],
  3517. '*/div',
  3518. ['div' => ['class' => 'input text required']],
  3519. '*/div',
  3520. ['div' => ['class' => 'input text']],
  3521. '*/div',
  3522. ['div' => ['class' => 'input boolean']],
  3523. '*/div',
  3524. '/fieldset',
  3525. ];
  3526. $this->assertHtml($expected, $result);
  3527. $this->Form->create($this->article);
  3528. $result = $this->Form->allInputs([], ['legend' => 'Hello']);
  3529. $expected = [
  3530. 'fieldset' => [],
  3531. 'legend' => [],
  3532. 'Hello',
  3533. '/legend',
  3534. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3535. ['div' => ['class' => 'input select required']],
  3536. '*/div',
  3537. ['div' => ['class' => 'input text required']],
  3538. '*/div',
  3539. ['div' => ['class' => 'input text']],
  3540. '*/div',
  3541. ['div' => ['class' => 'input text']],
  3542. '*/div',
  3543. '/fieldset'
  3544. ];
  3545. $this->assertHtml($expected, $result);
  3546. $this->Form->create(false);
  3547. $expected = [
  3548. 'fieldset' => [],
  3549. ['div' => ['class' => 'input text']],
  3550. 'label' => ['for' => 'foo'],
  3551. 'Foo',
  3552. '/label',
  3553. 'input' => ['type' => 'text', 'name' => 'foo', 'id' => 'foo'],
  3554. '*/div',
  3555. '/fieldset'
  3556. ];
  3557. $result = $this->Form->allInputs(
  3558. ['foo' => ['type' => 'text']],
  3559. ['legend' => false]
  3560. );
  3561. $this->assertHtml($expected, $result);
  3562. }
  3563. /**
  3564. * testFormInputsBlacklist
  3565. *
  3566. * @return void
  3567. */
  3568. public function testFormInputsBlacklist()
  3569. {
  3570. $this->Form->create($this->article);
  3571. $result = $this->Form->allInputs([
  3572. 'id' => false
  3573. ]);
  3574. $expected = [
  3575. '<fieldset',
  3576. '<legend', 'New Article', '/legend',
  3577. ['div' => ['class' => 'input select required']],
  3578. '*/div',
  3579. ['div' => ['class' => 'input text required']],
  3580. '*/div',
  3581. ['div' => ['class' => 'input text']],
  3582. '*/div',
  3583. ['div' => ['class' => 'input text']],
  3584. '*/div',
  3585. '/fieldset',
  3586. ];
  3587. $this->assertHtml($expected, $result);
  3588. $this->Form->create($this->article);
  3589. $result = $this->Form->allInputs([
  3590. 'id' => []
  3591. ]);
  3592. $expected = [
  3593. '<fieldset',
  3594. '<legend', 'New Article', '/legend',
  3595. 'input' => ['type' => 'hidden', 'name' => 'id', 'id' => 'id'],
  3596. ['div' => ['class' => 'input select required']],
  3597. '*/div',
  3598. ['div' => ['class' => 'input text required']],
  3599. '*/div',
  3600. ['div' => ['class' => 'input text']],
  3601. '*/div',
  3602. ['div' => ['class' => 'input text']],
  3603. '*/div',
  3604. '/fieldset',
  3605. ];
  3606. $this->assertHtml($expected, $result, 'A falsey value (array) should not remove the input');
  3607. }
  3608. /**
  3609. * testSelectAsCheckbox method
  3610. *
  3611. * test multi-select widget with checkbox formatting.
  3612. *
  3613. * @return void
  3614. */
  3615. public function testSelectAsCheckbox()
  3616. {
  3617. $result = $this->Form->select(
  3618. 'Model.multi_field',
  3619. ['first', 'second', 'third'],
  3620. ['multiple' => 'checkbox', 'value' => [0, 1]]
  3621. );
  3622. $expected = [
  3623. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3624. ['div' => ['class' => 'checkbox']],
  3625. ['label' => ['for' => 'model-multi-field-0', 'class' => 'selected']],
  3626. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0']],
  3627. 'first',
  3628. '/label',
  3629. '/div',
  3630. ['div' => ['class' => 'checkbox']],
  3631. ['label' => ['for' => 'model-multi-field-1', 'class' => 'selected']],
  3632. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1']],
  3633. 'second',
  3634. '/label',
  3635. '/div',
  3636. ['div' => ['class' => 'checkbox']],
  3637. ['label' => ['for' => 'model-multi-field-2']],
  3638. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  3639. 'third',
  3640. '/label',
  3641. '/div',
  3642. ];
  3643. $this->assertHtml($expected, $result);
  3644. $result = $this->Form->select(
  3645. 'Model.multi_field',
  3646. ['1/2' => 'half'],
  3647. ['multiple' => 'checkbox']
  3648. );
  3649. $expected = [
  3650. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  3651. ['div' => ['class' => 'checkbox']],
  3652. ['label' => ['for' => 'model-multi-field-1-2']],
  3653. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2']],
  3654. 'half',
  3655. '/label',
  3656. '/div',
  3657. ];
  3658. $this->assertHtml($expected, $result);
  3659. }
  3660. /**
  3661. * testLabel method
  3662. *
  3663. * test label generation.
  3664. *
  3665. * @return void
  3666. */
  3667. public function testLabel()
  3668. {
  3669. $result = $this->Form->label('Person.name');
  3670. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3671. $this->assertHtml($expected, $result);
  3672. $result = $this->Form->label('Person.name');
  3673. $expected = ['label' => ['for' => 'person-name'], 'Name', '/label'];
  3674. $this->assertHtml($expected, $result);
  3675. $result = $this->Form->label('Person.first_name');
  3676. $expected = ['label' => ['for' => 'person-first-name'], 'First Name', '/label'];
  3677. $this->assertHtml($expected, $result);
  3678. $result = $this->Form->label('Person.first_name', 'Your first name');
  3679. $expected = ['label' => ['for' => 'person-first-name'], 'Your first name', '/label'];
  3680. $this->assertHtml($expected, $result);
  3681. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class']);
  3682. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class'], 'Your first name', '/label'];
  3683. $this->assertHtml($expected, $result);
  3684. $result = $this->Form->label('Person.first_name', 'Your first name', ['class' => 'my-class', 'id' => 'LabelID']);
  3685. $expected = ['label' => ['for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'], 'Your first name', '/label'];
  3686. $this->assertHtml($expected, $result);
  3687. $result = $this->Form->label('Person.first_name', '');
  3688. $expected = ['label' => ['for' => 'person-first-name'], '/label'];
  3689. $this->assertHtml($expected, $result);
  3690. $result = $this->Form->label('Person.2.name', '');
  3691. $expected = ['label' => ['for' => 'person-2-name'], '/label'];
  3692. $this->assertHtml($expected, $result);
  3693. }
  3694. /**
  3695. * Test that label() can accept an input with the correct template vars.
  3696. *
  3697. * @return void
  3698. */
  3699. public function testLabelContainInput()
  3700. {
  3701. $this->Form->templates([
  3702. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3703. ]);
  3704. $result = $this->Form->label('Person.accept_terms', 'Accept', [
  3705. 'input' => '<input type="checkbox" name="accept_tos"/>'
  3706. ]);
  3707. $expected = [
  3708. 'label' => ['for' => 'person-accept-terms'],
  3709. 'input' => ['type' => 'checkbox', 'name' => 'accept_tos'],
  3710. 'Accept',
  3711. '/label',
  3712. ];
  3713. $this->assertHtml($expected, $result);
  3714. }
  3715. /**
  3716. * testTextbox method
  3717. *
  3718. * test textbox element generation
  3719. *
  3720. * @return void
  3721. */
  3722. public function testTextbox()
  3723. {
  3724. $result = $this->Form->text('Model.field');
  3725. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]']];
  3726. $this->assertHtml($expected, $result);
  3727. $result = $this->Form->text('Model.field', ['type' => 'password']);
  3728. $expected = ['input' => ['type' => 'password', 'name' => 'Model[field]']];
  3729. $this->assertHtml($expected, $result);
  3730. $result = $this->Form->text('Model.field', ['id' => 'theID']);
  3731. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'id' => 'theID']];
  3732. $this->assertHtml($expected, $result);
  3733. }
  3734. /**
  3735. * Test that text() hooks up with request data and error fields.
  3736. *
  3737. * @return void
  3738. */
  3739. public function testTextBoxDataAndError()
  3740. {
  3741. $this->article['errors'] = [
  3742. 'Contact' => ['text' => 'wrong']
  3743. ];
  3744. $this->Form->create($this->article);
  3745. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3746. $result = $this->Form->text('Model.text');
  3747. $expected = [
  3748. 'input' => [
  3749. 'type' => 'text',
  3750. 'name' => 'Model[text]',
  3751. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3752. ]
  3753. ];
  3754. $this->assertHtml($expected, $result);
  3755. $this->Form->request->data['Contact']['text'] = 'test';
  3756. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3757. $expected = [
  3758. 'input' => [
  3759. 'type' => 'text',
  3760. 'name' => 'Contact[text]',
  3761. 'value' => 'test',
  3762. 'id' => 'theID',
  3763. 'class' => 'form-error'
  3764. ]
  3765. ];
  3766. $this->assertHtml($expected, $result);
  3767. }
  3768. /**
  3769. * testDefaultValue method
  3770. *
  3771. * Test default value setting
  3772. *
  3773. * @return void
  3774. */
  3775. public function testTextDefaultValue()
  3776. {
  3777. $this->Form->request->data['Model']['field'] = 'test';
  3778. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3779. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3780. $this->assertHtml($expected, $result);
  3781. unset($this->Form->request->data['Model']['field']);
  3782. $result = $this->Form->text('Model.field', ['default' => 'default value']);
  3783. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3784. $this->assertHtml($expected, $result);
  3785. }
  3786. /**
  3787. * testError method
  3788. *
  3789. * Test field error generation
  3790. *
  3791. * @return void
  3792. */
  3793. public function testError()
  3794. {
  3795. $this->article['errors'] = [
  3796. 'Article' => ['field' => 'email']
  3797. ];
  3798. $this->Form->create($this->article);
  3799. $result = $this->Form->error('Article.field');
  3800. $expected = [
  3801. ['div' => ['class' => 'error-message']],
  3802. 'email',
  3803. '/div',
  3804. ];
  3805. $this->assertHtml($expected, $result);
  3806. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3807. $expected = [
  3808. ['div' => ['class' => 'error-message']],
  3809. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3810. '/div',
  3811. ];
  3812. $this->assertHtml($expected, $result);
  3813. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3814. $expected = [
  3815. ['div' => ['class' => 'error-message']],
  3816. '<strong', 'Badness!', '/strong',
  3817. '/div',
  3818. ];
  3819. $this->assertHtml($expected, $result);
  3820. }
  3821. /**
  3822. * Test error with nested lists.
  3823. *
  3824. * @return void
  3825. */
  3826. public function testErrorMessages()
  3827. {
  3828. $this->article['errors'] = [
  3829. 'Article' => ['field' => 'email']
  3830. ];
  3831. $this->Form->create($this->article);
  3832. $result = $this->Form->error('Article.field', [
  3833. 'email' => 'No good!'
  3834. ]);
  3835. $expected = [
  3836. 'div' => ['class' => 'error-message'],
  3837. 'No good!',
  3838. '/div'
  3839. ];
  3840. $this->assertHtml($expected, $result);
  3841. }
  3842. /**
  3843. * test error() with multiple messages.
  3844. *
  3845. * @return void
  3846. */
  3847. public function testErrorMultipleMessages()
  3848. {
  3849. $this->article['errors'] = [
  3850. 'field' => ['notBlank', 'email', 'Something else']
  3851. ];
  3852. $this->Form->create($this->article);
  3853. $result = $this->Form->error('field', [
  3854. 'notBlank' => 'Cannot be empty',
  3855. 'email' => 'No good!'
  3856. ]);
  3857. $expected = [
  3858. 'div' => ['class' => 'error-message'],
  3859. 'ul' => [],
  3860. '<li', 'Cannot be empty', '/li',
  3861. '<li', 'No good!', '/li',
  3862. '<li', 'Something else', '/li',
  3863. '/ul',
  3864. '/div'
  3865. ];
  3866. $this->assertHtml($expected, $result);
  3867. }
  3868. /**
  3869. * testPassword method
  3870. *
  3871. * Test password element generation
  3872. *
  3873. * @return void
  3874. */
  3875. public function testPassword()
  3876. {
  3877. $this->article['errors'] = [
  3878. 'Contact' => [
  3879. 'passwd' => 1
  3880. ]
  3881. ];
  3882. $this->Form->create($this->article);
  3883. $result = $this->Form->password('Contact.field');
  3884. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[field]']];
  3885. $this->assertHtml($expected, $result);
  3886. $this->Form->request->data['Contact']['passwd'] = 'test';
  3887. $result = $this->Form->password('Contact.passwd', ['id' => 'theID']);
  3888. $expected = ['input' => ['type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error']];
  3889. $this->assertHtml($expected, $result);
  3890. }
  3891. /**
  3892. * testRadio method
  3893. *
  3894. * Test radio element set generation
  3895. *
  3896. * @return void
  3897. */
  3898. public function testRadio()
  3899. {
  3900. $result = $this->Form->radio('Model.field', ['option A']);
  3901. $expected = [
  3902. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3903. 'label' => ['for' => 'model-field-0'],
  3904. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3905. 'option A',
  3906. '/label'
  3907. ];
  3908. $this->assertHtml($expected, $result);
  3909. $result = $this->Form->radio('Model.field', new Collection(['option A']));
  3910. $this->assertHtml($expected, $result);
  3911. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3912. $expected = [
  3913. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  3914. ['label' => ['for' => 'model-field-0']],
  3915. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  3916. 'option A',
  3917. '/label',
  3918. ['label' => ['for' => 'model-field-1']],
  3919. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  3920. 'option B',
  3921. '/label',
  3922. ];
  3923. $this->assertHtml($expected, $result);
  3924. $result = $this->Form->radio(
  3925. 'Employee.gender',
  3926. ['male' => 'Male', 'female' => 'Female'],
  3927. ['form' => 'my-form']
  3928. );
  3929. $expected = [
  3930. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => '', 'form' => 'my-form'],
  3931. ['label' => ['for' => 'employee-gender-male']],
  3932. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male', 'form' => 'my-form']],
  3933. 'Male',
  3934. '/label',
  3935. ['label' => ['for' => 'employee-gender-female']],
  3936. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female', 'form' => 'my-form']],
  3937. 'Female',
  3938. '/label',
  3939. ];
  3940. $this->assertHtml($expected, $result);
  3941. $result = $this->Form->radio('Model.field', ['option A', 'option B'], ['name' => 'Model[custom]']);
  3942. $expected = [
  3943. ['input' => ['type' => 'hidden', 'name' => 'Model[custom]', 'value' => '']],
  3944. ['label' => ['for' => 'model-custom-0']],
  3945. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0']],
  3946. 'option A',
  3947. '/label',
  3948. ['label' => ['for' => 'model-custom-1']],
  3949. ['input' => ['type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1']],
  3950. 'option B',
  3951. '/label',
  3952. ];
  3953. $this->assertHtml($expected, $result);
  3954. $result = $this->Form->radio(
  3955. 'Employee.gender',
  3956. [
  3957. ['value' => 'male', 'text' => 'Male', 'style' => 'width:20px'],
  3958. ['value' => 'female', 'text' => 'Female', 'style' => 'width:20px'],
  3959. ]
  3960. );
  3961. $expected = [
  3962. 'input' => ['type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''],
  3963. ['label' => ['for' => 'employee-gender-male']],
  3964. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male',
  3965. 'id' => 'employee-gender-male', 'style' => 'width:20px']],
  3966. 'Male',
  3967. '/label',
  3968. ['label' => ['for' => 'employee-gender-female']],
  3969. ['input' => ['type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female',
  3970. 'id' => 'employee-gender-female', 'style' => 'width:20px']],
  3971. 'Female',
  3972. '/label',
  3973. ];
  3974. $this->assertHtml($expected, $result);
  3975. }
  3976. /**
  3977. * Test that input works with radio types
  3978. *
  3979. * @return void
  3980. */
  3981. public function testInputRadio()
  3982. {
  3983. $result = $this->Form->input('test', [
  3984. 'type' => 'radio',
  3985. 'options' => ['A', 'B'],
  3986. ]);
  3987. $expected = [
  3988. ['div' => ['class' => 'input radio']],
  3989. '<label',
  3990. 'Test',
  3991. '/label',
  3992. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  3993. ['label' => ['for' => 'test-0']],
  3994. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  3995. 'A',
  3996. '/label',
  3997. ['label' => ['for' => 'test-1']],
  3998. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  3999. 'B',
  4000. '/label',
  4001. '/div',
  4002. ];
  4003. $this->assertHtml($expected, $result);
  4004. $result = $this->Form->input('test', [
  4005. 'type' => 'radio',
  4006. 'options' => ['A', 'B'],
  4007. 'value' => '0'
  4008. ]);
  4009. $expected = [
  4010. ['div' => ['class' => 'input radio']],
  4011. '<label',
  4012. 'Test',
  4013. '/label',
  4014. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4015. ['label' => ['for' => 'test-0']],
  4016. ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4017. 'A',
  4018. '/label',
  4019. ['label' => ['for' => 'test-1']],
  4020. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4021. 'B',
  4022. '/label',
  4023. '/div',
  4024. ];
  4025. $this->assertHtml($expected, $result);
  4026. $result = $this->Form->input('test', [
  4027. 'type' => 'radio',
  4028. 'options' => ['A', 'B'],
  4029. 'label' => false
  4030. ]);
  4031. $expected = [
  4032. ['div' => ['class' => 'input radio']],
  4033. ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
  4034. ['label' => ['for' => 'test-0']],
  4035. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
  4036. 'A',
  4037. '/label',
  4038. ['label' => ['for' => 'test-1']],
  4039. ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
  4040. 'B',
  4041. '/label',
  4042. '/div',
  4043. ];
  4044. $this->assertHtml($expected, $result);
  4045. }
  4046. /**
  4047. * Test that radio() works with label = false
  4048. *
  4049. * @return void
  4050. */
  4051. public function testRadioNoLabel()
  4052. {
  4053. $result = $this->Form->radio('Model.field', ['A', 'B'], ['label' => false]);
  4054. $expected = [
  4055. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4056. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0']],
  4057. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1']],
  4058. ];
  4059. $this->assertHtml($expected, $result);
  4060. }
  4061. /**
  4062. * test generating radio input inside label ala twitter bootstrap
  4063. *
  4064. * @return void
  4065. */
  4066. public function testRadioInputInsideLabel()
  4067. {
  4068. $this->Form->templates([
  4069. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  4070. 'radioWrapper' => '{{label}}'
  4071. ]);
  4072. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  4073. //@codingStandardsIgnoreStart
  4074. $expected = [
  4075. ['input' => [
  4076. 'type' => 'hidden',
  4077. 'name' => 'Model[field]',
  4078. 'value' => ''
  4079. ]],
  4080. ['label' => ['for' => 'model-field-0']],
  4081. ['input' => [
  4082. 'type' => 'radio',
  4083. 'name' => 'Model[field]',
  4084. 'value' => '0',
  4085. 'id' => 'model-field-0'
  4086. ]],
  4087. 'option A',
  4088. '/label',
  4089. ['label' => ['for' => 'model-field-1']],
  4090. ['input' => [
  4091. 'type' => 'radio',
  4092. 'name' => 'Model[field]',
  4093. 'value' => '1',
  4094. 'id' => 'model-field-1'
  4095. ]],
  4096. 'option B',
  4097. '/label',
  4098. ];
  4099. //@codingStandardsIgnoreEnd
  4100. $this->assertHtml($expected, $result);
  4101. }
  4102. /**
  4103. * test disabling the hidden input for radio buttons
  4104. *
  4105. * @return void
  4106. */
  4107. public function testRadioHiddenInputDisabling()
  4108. {
  4109. $result = $this->Form->radio('Model.1.field', ['option A'], ['hiddenField' => false]);
  4110. $expected = [
  4111. 'label' => ['for' => 'model-1-field-0'],
  4112. 'input' => ['type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'],
  4113. 'option A',
  4114. '/label'
  4115. ];
  4116. $this->assertHtml($expected, $result);
  4117. }
  4118. /**
  4119. * testRadio method
  4120. *
  4121. * Test radio element set generation
  4122. *
  4123. * @return void
  4124. */
  4125. public function testRadioOutOfRange()
  4126. {
  4127. $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
  4128. $expected = [
  4129. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
  4130. 'label' => ['for' => 'model-field-v'],
  4131. ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
  4132. 'value',
  4133. '/label'
  4134. ];
  4135. $this->assertHtml($expected, $result);
  4136. }
  4137. /**
  4138. * testSelect method
  4139. *
  4140. * Test select element generation.
  4141. *
  4142. * @return void
  4143. */
  4144. public function testSelect()
  4145. {
  4146. $result = $this->Form->select('Model.field', []);
  4147. $expected = [
  4148. 'select' => ['name' => 'Model[field]'],
  4149. '/select'
  4150. ];
  4151. $this->assertHtml($expected, $result);
  4152. $this->Form->request->data = ['Model' => ['field' => 'value']];
  4153. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4154. $expected = [
  4155. 'select' => ['name' => 'Model[field]'],
  4156. ['option' => ['value' => 'value', 'selected' => 'selected']],
  4157. 'good',
  4158. '/option',
  4159. ['option' => ['value' => 'other']],
  4160. 'bad',
  4161. '/option',
  4162. '/select'
  4163. ];
  4164. $this->assertHtml($expected, $result);
  4165. $result = $this->Form->select('Model.field', new Collection(['value' => 'good', 'other' => 'bad']));
  4166. $this->assertHtml($expected, $result);
  4167. $this->Form->request->data = [];
  4168. $result = $this->Form->select('Model.field', ['value' => 'good', 'other' => 'bad']);
  4169. $expected = [
  4170. 'select' => ['name' => 'Model[field]'],
  4171. ['option' => ['value' => 'value']],
  4172. 'good',
  4173. '/option',
  4174. ['option' => ['value' => 'other']],
  4175. 'bad',
  4176. '/option',
  4177. '/select'
  4178. ];
  4179. $this->assertHtml($expected, $result);
  4180. $options = [
  4181. ['value' => 'first', 'text' => 'First'],
  4182. ['value' => 'first', 'text' => 'Another First'],
  4183. ];
  4184. $result = $this->Form->select(
  4185. 'Model.field',
  4186. $options,
  4187. ['escape' => false, 'empty' => false]
  4188. );
  4189. $expected = [
  4190. 'select' => ['name' => 'Model[field]'],
  4191. ['option' => ['value' => 'first']],
  4192. 'First',
  4193. '/option',
  4194. ['option' => ['value' => 'first']],
  4195. 'Another First',
  4196. '/option',
  4197. '/select'
  4198. ];
  4199. $this->assertHtml($expected, $result);
  4200. $this->Form->request->data = ['Model' => ['contact_id' => 228]];
  4201. $result = $this->Form->select(
  4202. 'Model.contact_id',
  4203. ['228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'],
  4204. ['escape' => false, 'empty' => 'pick something']
  4205. );
  4206. $expected = [
  4207. 'select' => ['name' => 'Model[contact_id]'],
  4208. ['option' => ['value' => '']], 'pick something', '/option',
  4209. ['option' => ['value' => '228', 'selected' => 'selected']], '228 value', '/option',
  4210. ['option' => ['value' => '228-1']], '228-1 value', '/option',
  4211. ['option' => ['value' => '228-2']], '228-2 value', '/option',
  4212. '/select'
  4213. ];
  4214. $this->assertHtml($expected, $result);
  4215. $this->Form->request->data['Model']['field'] = 0;
  4216. $result = $this->Form->select('Model.field', ['0' => 'No', '1' => 'Yes']);
  4217. $expected = [
  4218. 'select' => ['name' => 'Model[field]'],
  4219. ['option' => ['value' => '0', 'selected' => 'selected']], 'No', '/option',
  4220. ['option' => ['value' => '1']], 'Yes', '/option',
  4221. '/select'
  4222. ];
  4223. $this->assertHtml($expected, $result);
  4224. }
  4225. /**
  4226. * Test that select() escapes HTML.
  4227. *
  4228. * @return void
  4229. */
  4230. public function testSelectEscapeHtml()
  4231. {
  4232. $result = $this->Form->select(
  4233. 'Model.field',
  4234. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4235. ['empty' => false]
  4236. );
  4237. $expected = [
  4238. 'select' => ['name' => 'Model[field]'],
  4239. ['option' => ['value' => 'first']],
  4240. 'first &quot;html&quot; &lt;chars&gt;',
  4241. '/option',
  4242. ['option' => ['value' => 'second']],
  4243. 'value',
  4244. '/option',
  4245. '/select'
  4246. ];
  4247. $this->assertHtml($expected, $result);
  4248. $result = $this->Form->select(
  4249. 'Model.field',
  4250. ['first' => 'first "html" <chars>', 'second' => 'value'],
  4251. ['escape' => false, 'empty' => false]
  4252. );
  4253. $expected = [
  4254. 'select' => ['name' => 'Model[field]'],
  4255. ['option' => ['value' => 'first']],
  4256. 'first "html" <chars>',
  4257. '/option',
  4258. ['option' => ['value' => 'second']],
  4259. 'value',
  4260. '/option',
  4261. '/select'
  4262. ];
  4263. $this->assertHtml($expected, $result);
  4264. }
  4265. /**
  4266. * test select() with required and disabled attributes.
  4267. *
  4268. * @return void
  4269. */
  4270. public function testSelectRequired()
  4271. {
  4272. $this->article['required'] = [
  4273. 'user_id' => true
  4274. ];
  4275. $this->Form->create($this->article);
  4276. $result = $this->Form->select('user_id', ['option A']);
  4277. $expected = [
  4278. 'select' => [
  4279. 'name' => 'user_id',
  4280. 'required' => 'required'
  4281. ],
  4282. ['option' => ['value' => '0']], 'option A', '/option',
  4283. '/select'
  4284. ];
  4285. $this->assertHtml($expected, $result);
  4286. $result = $this->Form->select('user_id', ['option A'], ['disabled' => true]);
  4287. $expected = [
  4288. 'select' => [
  4289. 'name' => 'user_id',
  4290. 'disabled' => 'disabled'
  4291. ],
  4292. ['option' => ['value' => '0']], 'option A', '/option',
  4293. '/select'
  4294. ];
  4295. $this->assertHtml($expected, $result);
  4296. }
  4297. /**
  4298. * testNestedSelect method
  4299. *
  4300. * test select element generation with optgroups
  4301. *
  4302. * @return void
  4303. */
  4304. public function testNestedSelect()
  4305. {
  4306. $result = $this->Form->select(
  4307. 'Model.field',
  4308. [1 => 'One', 2 => 'Two', 'Three' => [
  4309. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4310. ]],
  4311. ['empty' => false]
  4312. );
  4313. $expected = [
  4314. 'select' => ['name' => 'Model[field]'],
  4315. ['option' => ['value' => 1]],
  4316. 'One',
  4317. '/option',
  4318. ['option' => ['value' => 2]],
  4319. 'Two',
  4320. '/option',
  4321. ['optgroup' => ['label' => 'Three']],
  4322. ['option' => ['value' => 3]],
  4323. 'Three',
  4324. '/option',
  4325. ['option' => ['value' => 4]],
  4326. 'Four',
  4327. '/option',
  4328. ['option' => ['value' => 5]],
  4329. 'Five',
  4330. '/option',
  4331. '/optgroup',
  4332. '/select'
  4333. ];
  4334. $this->assertHtml($expected, $result);
  4335. }
  4336. /**
  4337. * testSelectMultiple method
  4338. *
  4339. * test generation of multiple select elements
  4340. *
  4341. * @return void
  4342. */
  4343. public function testSelectMultiple()
  4344. {
  4345. $options = ['first', 'second', 'third'];
  4346. $result = $this->Form->select(
  4347. 'Model.multi_field',
  4348. $options,
  4349. ['form' => 'my-form', 'multiple' => true]
  4350. );
  4351. $expected = [
  4352. 'input' => [
  4353. 'type' => 'hidden',
  4354. 'name' => 'Model[multi_field]',
  4355. 'value' => '',
  4356. 'form' => 'my-form',
  4357. ],
  4358. 'select' => [
  4359. 'name' => 'Model[multi_field][]',
  4360. 'multiple' => 'multiple',
  4361. 'form' => 'my-form',
  4362. ],
  4363. ['option' => ['value' => '0']],
  4364. 'first',
  4365. '/option',
  4366. ['option' => ['value' => '1']],
  4367. 'second',
  4368. '/option',
  4369. ['option' => ['value' => '2']],
  4370. 'third',
  4371. '/option',
  4372. '/select'
  4373. ];
  4374. $this->assertHtml($expected, $result);
  4375. $result = $this->Form->select(
  4376. 'Model.multi_field',
  4377. $options,
  4378. ['multiple' => 'multiple', 'form' => 'my-form']
  4379. );
  4380. $this->assertHtml($expected, $result);
  4381. }
  4382. /**
  4383. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4384. *
  4385. * @return void
  4386. */
  4387. public function testCheckboxZeroValue()
  4388. {
  4389. $result = $this->Form->input('User.get_spam', [
  4390. 'type' => 'checkbox',
  4391. 'value' => '0',
  4392. 'hiddenField' => '1',
  4393. ]);
  4394. $expected = [
  4395. 'div' => ['class' => 'input checkbox'],
  4396. 'label' => ['for' => 'user-get-spam'],
  4397. ['input' => [
  4398. 'type' => 'hidden', 'name' => 'User[get_spam]',
  4399. 'value' => '1'
  4400. ]],
  4401. ['input' => [
  4402. 'type' => 'checkbox', 'name' => 'User[get_spam]',
  4403. 'value' => '0', 'id' => 'user-get-spam'
  4404. ]],
  4405. 'Get Spam',
  4406. '/label',
  4407. '/div'
  4408. ];
  4409. $this->assertHtml($expected, $result);
  4410. }
  4411. /**
  4412. * test generation of habtm select boxes.
  4413. *
  4414. * @return void
  4415. */
  4416. public function testHabtmSelectBox()
  4417. {
  4418. $this->loadFixtures('Articles');
  4419. $options = [
  4420. 1 => 'blue',
  4421. 2 => 'red',
  4422. 3 => 'green'
  4423. ];
  4424. $tags = [
  4425. new Entity(['id' => 1, 'name' => 'blue']),
  4426. new Entity(['id' => 3, 'name' => 'green'])
  4427. ];
  4428. $article = new Article(['tags' => $tags]);
  4429. $this->Form->create($article);
  4430. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4431. $expected = [
  4432. 'div' => ['class' => 'input select'],
  4433. 'label' => ['for' => 'tags-ids'],
  4434. 'Tags',
  4435. '/label',
  4436. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4437. 'select' => [
  4438. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4439. 'multiple' => 'multiple'
  4440. ],
  4441. ['option' => ['value' => '1', 'selected' => 'selected']],
  4442. 'blue',
  4443. '/option',
  4444. ['option' => ['value' => '2']],
  4445. 'red',
  4446. '/option',
  4447. ['option' => ['value' => '3', 'selected' => 'selected']],
  4448. 'green',
  4449. '/option',
  4450. '/select',
  4451. '/div'
  4452. ];
  4453. $this->assertHtml($expected, $result);
  4454. // make sure only 50 is selected, and not 50f5c0cf
  4455. $options = [
  4456. '1' => 'blue',
  4457. '50f5c0cf' => 'red',
  4458. '50' => 'green'
  4459. ];
  4460. $tags = [
  4461. new Entity(['id' => 1, 'name' => 'blue']),
  4462. new Entity(['id' => 50, 'name' => 'green'])
  4463. ];
  4464. $article = new Article(['tags' => $tags]);
  4465. $this->Form->create($article);
  4466. $result = $this->Form->input('tags._ids', ['options' => $options]);
  4467. $expected = [
  4468. 'div' => ['class' => 'input select'],
  4469. 'label' => ['for' => 'tags-ids'],
  4470. 'Tags',
  4471. '/label',
  4472. 'input' => ['type' => 'hidden', 'name' => 'tags[_ids]', 'value' => ''],
  4473. 'select' => [
  4474. 'name' => 'tags[_ids][]', 'id' => 'tags-ids',
  4475. 'multiple' => 'multiple'
  4476. ],
  4477. ['option' => ['value' => '1', 'selected' => 'selected']],
  4478. 'blue',
  4479. '/option',
  4480. ['option' => ['value' => '50f5c0cf']],
  4481. 'red',
  4482. '/option',
  4483. ['option' => ['value' => '50', 'selected' => 'selected']],
  4484. 'green',
  4485. '/option',
  4486. '/select',
  4487. '/div'
  4488. ];
  4489. $this->assertHtml($expected, $result);
  4490. $spacecraft = [
  4491. 1 => 'Orion',
  4492. 2 => 'Helios'
  4493. ];
  4494. $this->View->viewVars['spacecraft'] = $spacecraft;
  4495. $this->Form->create();
  4496. $result = $this->Form->input('spacecraft._ids');
  4497. $expected = [
  4498. 'div' => ['class' => 'input select'],
  4499. 'label' => ['for' => 'spacecraft-ids'],
  4500. 'Spacecraft',
  4501. '/label',
  4502. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4503. 'select' => [
  4504. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4505. 'multiple' => 'multiple'
  4506. ],
  4507. ['option' => ['value' => '1']],
  4508. 'Orion',
  4509. '/option',
  4510. ['option' => ['value' => '2']],
  4511. 'Helios',
  4512. '/option',
  4513. '/select',
  4514. '/div'
  4515. ];
  4516. $this->assertHtml($expected, $result);
  4517. }
  4518. /**
  4519. * Tests that errors for belongsToMany select fields are being
  4520. * picked up properly.
  4521. *
  4522. * @return void
  4523. */
  4524. public function testErrorsForBelongsToManySelect()
  4525. {
  4526. $spacecraft = [
  4527. 1 => 'Orion',
  4528. 2 => 'Helios'
  4529. ];
  4530. $this->View->viewVars['spacecraft'] = $spacecraft;
  4531. $article = new Article();
  4532. $article->errors('spacecraft', ['Invalid']);
  4533. $this->Form->create($article);
  4534. $result = $this->Form->input('spacecraft._ids');
  4535. $expected = [
  4536. ['div' => ['class' => 'input select error']],
  4537. 'label' => ['for' => 'spacecraft-ids'],
  4538. 'Spacecraft',
  4539. '/label',
  4540. 'input' => ['type' => 'hidden', 'name' => 'spacecraft[_ids]', 'value' => ''],
  4541. 'select' => [
  4542. 'name' => 'spacecraft[_ids][]', 'id' => 'spacecraft-ids',
  4543. 'multiple' => 'multiple'
  4544. ],
  4545. ['option' => ['value' => '1']],
  4546. 'Orion',
  4547. '/option',
  4548. ['option' => ['value' => '2']],
  4549. 'Helios',
  4550. '/option',
  4551. '/select',
  4552. ['div' => ['class' => 'error-message']],
  4553. 'Invalid',
  4554. '/div',
  4555. '/div'
  4556. ];
  4557. $this->assertHtml($expected, $result);
  4558. }
  4559. /**
  4560. * test generation of multi select elements in checkbox format
  4561. *
  4562. * @return void
  4563. */
  4564. public function testSelectMultipleCheckboxes()
  4565. {
  4566. $result = $this->Form->select(
  4567. 'Model.multi_field',
  4568. ['first', 'second', 'third'],
  4569. ['multiple' => 'checkbox']
  4570. );
  4571. $expected = [
  4572. 'input' => [
  4573. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4574. ],
  4575. ['div' => ['class' => 'checkbox']],
  4576. ['label' => ['for' => 'model-multi-field-0']],
  4577. ['input' => [
  4578. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4579. 'value' => '0', 'id' => 'model-multi-field-0'
  4580. ]],
  4581. 'first',
  4582. '/label',
  4583. '/div',
  4584. ['div' => ['class' => 'checkbox']],
  4585. ['label' => ['for' => 'model-multi-field-1']],
  4586. ['input' => [
  4587. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4588. 'value' => '1', 'id' => 'model-multi-field-1'
  4589. ]],
  4590. 'second',
  4591. '/label',
  4592. '/div',
  4593. ['div' => ['class' => 'checkbox']],
  4594. ['label' => ['for' => 'model-multi-field-2']],
  4595. ['input' => [
  4596. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4597. 'value' => '2', 'id' => 'model-multi-field-2'
  4598. ]],
  4599. 'third',
  4600. '/label',
  4601. '/div'
  4602. ];
  4603. $this->assertHtml($expected, $result);
  4604. $result = $this->Form->select(
  4605. 'Model.multi_field',
  4606. ['a+' => 'first', 'a++' => 'second', 'a+++' => 'third'],
  4607. ['multiple' => 'checkbox']
  4608. );
  4609. $expected = [
  4610. 'input' => [
  4611. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4612. ],
  4613. ['div' => ['class' => 'checkbox']],
  4614. ['label' => ['for' => 'model-multi-field-a+']],
  4615. ['input' => [
  4616. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4617. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4618. ]],
  4619. 'first',
  4620. '/label',
  4621. '/div',
  4622. ['div' => ['class' => 'checkbox']],
  4623. ['label' => ['for' => 'model-multi-field-a++']],
  4624. ['input' => [
  4625. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4626. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4627. ]],
  4628. 'second',
  4629. '/label',
  4630. '/div',
  4631. ['div' => ['class' => 'checkbox']],
  4632. ['label' => ['for' => 'model-multi-field-a+++']],
  4633. ['input' => [
  4634. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4635. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4636. ]],
  4637. 'third',
  4638. '/label',
  4639. '/div'
  4640. ];
  4641. $this->assertHtml($expected, $result);
  4642. $result = $this->Form->select(
  4643. 'Model.multi_field',
  4644. ['a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'],
  4645. ['multiple' => 'checkbox']
  4646. );
  4647. $expected = [
  4648. 'input' => [
  4649. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4650. ],
  4651. ['div' => ['class' => 'checkbox']],
  4652. ['label' => ['for' => 'model-multi-field-a-b']],
  4653. ['input' => [
  4654. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4655. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4656. ]],
  4657. 'first',
  4658. '/label',
  4659. '/div',
  4660. ['div' => ['class' => 'checkbox']],
  4661. ['label' => ['for' => 'model-multi-field-a-b1']],
  4662. ['input' => [
  4663. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4664. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4665. ]],
  4666. 'second',
  4667. '/label',
  4668. '/div',
  4669. ['div' => ['class' => 'checkbox']],
  4670. ['label' => ['for' => 'model-multi-field-a-b2']],
  4671. ['input' => [
  4672. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4673. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4674. ]],
  4675. 'third',
  4676. '/label',
  4677. '/div'
  4678. ];
  4679. $this->assertHtml($expected, $result);
  4680. }
  4681. /**
  4682. * Ensure that multiCheckbox reads from the request data.
  4683. *
  4684. * @return void
  4685. */
  4686. public function testSelectMultipleCheckboxRequestData()
  4687. {
  4688. $this->Form->request->data = ['Model' => ['tags' => [1]]];
  4689. $result = $this->Form->select(
  4690. 'Model.tags',
  4691. ['1' => 'first', 'Array' => 'Array'],
  4692. ['multiple' => 'checkbox']
  4693. );
  4694. $expected = [
  4695. 'input' => [
  4696. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4697. ],
  4698. ['div' => ['class' => 'checkbox']],
  4699. ['label' => ['for' => 'model-tags-1', 'class' => 'selected']],
  4700. ['input' => [
  4701. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4702. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4703. ]],
  4704. 'first',
  4705. '/label',
  4706. '/div',
  4707. ['div' => ['class' => 'checkbox']],
  4708. ['label' => ['for' => 'model-tags-array']],
  4709. ['input' => [
  4710. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4711. 'value' => 'Array', 'id' => 'model-tags-array'
  4712. ]],
  4713. 'Array',
  4714. '/label',
  4715. '/div'
  4716. ];
  4717. $this->assertHtml($expected, $result);
  4718. }
  4719. /**
  4720. * Checks the security hash array generated for multiple-input checkbox elements
  4721. *
  4722. * @return void
  4723. */
  4724. public function testSelectMultipleCheckboxSecurity()
  4725. {
  4726. $this->Form->request->params['_Token'] = 'testKey';
  4727. $this->assertEquals([], $this->Form->fields);
  4728. $result = $this->Form->select(
  4729. 'Model.multi_field',
  4730. ['1' => 'first', '2' => 'second', '3' => 'third'],
  4731. ['multiple' => 'checkbox']
  4732. );
  4733. $this->assertEquals(['Model.multi_field'], $this->Form->fields);
  4734. $result = $this->Form->secure($this->Form->fields);
  4735. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4736. $this->assertRegExp('/"' . $key . '"/', $result);
  4737. }
  4738. /**
  4739. * Multiple select elements should always be secured as they always participate
  4740. * in the POST data.
  4741. *
  4742. * @return void
  4743. */
  4744. public function testSelectMultipleSecureWithNoOptions()
  4745. {
  4746. $this->assertEquals([], $this->Form->fields);
  4747. $this->Form->select(
  4748. 'Model.select',
  4749. [],
  4750. ['multiple' => true]
  4751. );
  4752. $this->assertEquals(['Model.select'], $this->Form->fields);
  4753. }
  4754. /**
  4755. * When a select box has no options it should not be added to the fields list
  4756. * as it always fail post validation.
  4757. *
  4758. * @return void
  4759. */
  4760. public function testSelectNoSecureWithNoOptions()
  4761. {
  4762. $this->Form->request->params['_Token'] = 'testkey';
  4763. $this->assertEquals([], $this->Form->fields);
  4764. $this->Form->select(
  4765. 'Model.select',
  4766. []
  4767. );
  4768. $this->assertEquals([], $this->Form->fields);
  4769. $this->Form->select(
  4770. 'Model.user_id',
  4771. [],
  4772. ['empty' => true]
  4773. );
  4774. $this->assertEquals(['Model.user_id'], $this->Form->fields);
  4775. }
  4776. /**
  4777. * testInputMultipleCheckboxes method
  4778. *
  4779. * test input() resulting in multi select elements being generated.
  4780. *
  4781. * @return void
  4782. */
  4783. public function testInputMultipleCheckboxes()
  4784. {
  4785. $result = $this->Form->input('Model.multi_field', [
  4786. 'options' => ['first', 'second', 'third'],
  4787. 'multiple' => 'checkbox'
  4788. ]);
  4789. $expected = [
  4790. ['div' => ['class' => 'input select']],
  4791. ['label' => ['for' => 'model-multi-field']],
  4792. 'Multi Field',
  4793. '/label',
  4794. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4795. ['div' => ['class' => 'checkbox']],
  4796. ['label' => ['for' => 'model-multi-field-0']],
  4797. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'model-multi-field-0']],
  4798. 'first',
  4799. '/label',
  4800. '/div',
  4801. ['div' => ['class' => 'checkbox']],
  4802. ['label' => ['for' => 'model-multi-field-1']],
  4803. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'model-multi-field-1']],
  4804. 'second',
  4805. '/label',
  4806. '/div',
  4807. ['div' => ['class' => 'checkbox']],
  4808. ['label' => ['for' => 'model-multi-field-2']],
  4809. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2']],
  4810. 'third',
  4811. '/label',
  4812. '/div',
  4813. '/div'
  4814. ];
  4815. $this->assertHtml($expected, $result);
  4816. $result = $this->Form->input('Model.multi_field', [
  4817. 'options' => ['a' => 'first', 'b' => 'second', 'c' => 'third'],
  4818. 'multiple' => 'checkbox'
  4819. ]);
  4820. $expected = [
  4821. ['div' => ['class' => 'input select']],
  4822. ['label' => ['for' => 'model-multi-field']],
  4823. 'Multi Field',
  4824. '/label',
  4825. 'input' => ['type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''],
  4826. ['div' => ['class' => 'checkbox']],
  4827. ['label' => ['for' => 'model-multi-field-a']],
  4828. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'model-multi-field-a']],
  4829. 'first',
  4830. '/label',
  4831. '/div',
  4832. ['div' => ['class' => 'checkbox']],
  4833. ['label' => ['for' => 'model-multi-field-b']],
  4834. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'model-multi-field-b']],
  4835. 'second',
  4836. '/label',
  4837. '/div',
  4838. ['div' => ['class' => 'checkbox']],
  4839. ['label' => ['for' => 'model-multi-field-c']],
  4840. ['input' => ['type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'model-multi-field-c']],
  4841. 'third',
  4842. '/label',
  4843. '/div',
  4844. '/div'
  4845. ];
  4846. $this->assertHtml($expected, $result);
  4847. }
  4848. /**
  4849. * testSelectHiddenFieldOmission method
  4850. *
  4851. * test that select() with 'hiddenField' => false omits the hidden field
  4852. *
  4853. * @return void
  4854. */
  4855. public function testSelectHiddenFieldOmission()
  4856. {
  4857. $result = $this->Form->select(
  4858. 'Model.multi_field',
  4859. ['first', 'second'],
  4860. ['multiple' => 'checkbox', 'hiddenField' => false, 'value' => null]
  4861. );
  4862. $this->assertNotContains('type="hidden"', $result);
  4863. }
  4864. /**
  4865. * test that select() with multiple = checkbox works with overriding name attribute.
  4866. *
  4867. * @return void
  4868. */
  4869. public function testSelectCheckboxMultipleOverrideName()
  4870. {
  4871. $result = $this->Form->select('category', ['1', '2'], [
  4872. 'multiple' => 'checkbox',
  4873. 'name' => 'fish',
  4874. ]);
  4875. $expected = [
  4876. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  4877. ['div' => ['class' => 'checkbox']],
  4878. ['label' => ['for' => 'fish-0']],
  4879. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  4880. '1',
  4881. '/label',
  4882. '/div',
  4883. ['div' => ['class' => 'checkbox']],
  4884. ['label' => ['for' => 'fish-1']],
  4885. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  4886. '2',
  4887. '/label',
  4888. '/div'
  4889. ];
  4890. $this->assertHtml($expected, $result);
  4891. $result = $this->Form->multiCheckbox(
  4892. 'category',
  4893. new Collection(['1', '2']),
  4894. ['name' => 'fish']
  4895. );
  4896. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4897. 'name' => 'fish',
  4898. ]);
  4899. $this->assertHtml($expected, $result);
  4900. }
  4901. /**
  4902. * Test that input() works with multicheckbox.
  4903. *
  4904. * @return void
  4905. */
  4906. public function testInputMultiCheckbox()
  4907. {
  4908. $result = $this->Form->input('category', [
  4909. 'type' => 'multicheckbox',
  4910. 'options' => ['1', '2'],
  4911. ]);
  4912. $expected = [
  4913. ['div' => ['class' => 'input multicheckbox']],
  4914. '<label',
  4915. 'Category',
  4916. '/label',
  4917. 'input' => ['type' => 'hidden', 'name' => 'category', 'value' => ''],
  4918. ['div' => ['class' => 'checkbox']],
  4919. ['label' => ['for' => 'category-0']],
  4920. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '0', 'id' => 'category-0']],
  4921. '1',
  4922. '/label',
  4923. '/div',
  4924. ['div' => ['class' => 'checkbox']],
  4925. ['label' => ['for' => 'category-1']],
  4926. ['input' => ['type' => 'checkbox', 'name' => 'category[]', 'value' => '1', 'id' => 'category-1']],
  4927. '2',
  4928. '/label',
  4929. '/div',
  4930. '/div',
  4931. ];
  4932. $this->assertHtml($expected, $result);
  4933. }
  4934. /**
  4935. * testCheckbox method
  4936. *
  4937. * Test generation of checkboxes
  4938. *
  4939. * @return void
  4940. */
  4941. public function testCheckbox()
  4942. {
  4943. $result = $this->Form->checkbox('Model.field');
  4944. $expected = [
  4945. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'],
  4946. ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']]
  4947. ];
  4948. $this->assertHtml($expected, $result);
  4949. $result = $this->Form->checkbox('Model.field', [
  4950. 'id' => 'theID',
  4951. 'value' => 'myvalue',
  4952. 'form' => 'my-form',
  4953. ]);
  4954. $expected = [
  4955. 'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => '0', 'form' => 'my-form'],
  4956. ['input' => [
  4957. 'type' => 'checkbox', 'name' => 'Model[field]',
  4958. 'value' => 'myvalue', 'id' => 'theID',
  4959. 'form' => 'my-form',
  4960. ]]
  4961. ];
  4962. $this->assertHtml($expected, $result);
  4963. }
  4964. /**
  4965. * testCheckboxDefaultValue method
  4966. *
  4967. * Test default value setting on checkbox() method
  4968. *
  4969. * @return void
  4970. */
  4971. public function testCheckboxDefaultValue()
  4972. {
  4973. $this->Form->request->data['Model']['field'] = false;
  4974. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  4975. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  4976. $this->assertHtml($expected, $result);
  4977. unset($this->Form->request->data['Model']['field']);
  4978. $result = $this->Form->checkbox('Model.field', ['default' => true, 'hiddenField' => false]);
  4979. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  4980. $this->assertHtml($expected, $result);
  4981. $this->Form->request->data['Model']['field'] = true;
  4982. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  4983. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked']];
  4984. $this->assertHtml($expected, $result);
  4985. unset($this->Form->request->data['Model']['field']);
  4986. $result = $this->Form->checkbox('Model.field', ['default' => false, 'hiddenField' => false]);
  4987. $expected = ['input' => ['type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1']];
  4988. $this->assertHtml($expected, $result);
  4989. }
  4990. /**
  4991. * Test checkbox being checked or having errors.
  4992. *
  4993. * @return void
  4994. */
  4995. public function testCheckboxCheckedAndError()
  4996. {
  4997. $this->article['errors'] = [
  4998. 'published' => true
  4999. ];
  5000. $this->Form->request->data['published'] = 'myvalue';
  5001. $this->Form->create($this->article);
  5002. $result = $this->Form->checkbox('published', ['id' => 'theID', 'value' => 'myvalue']);
  5003. $expected = [
  5004. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5005. ['input' => [
  5006. 'type' => 'checkbox',
  5007. 'name' => 'published',
  5008. 'value' => 'myvalue',
  5009. 'id' => 'theID',
  5010. 'checked' => 'checked',
  5011. 'class' => 'form-error'
  5012. ]]
  5013. ];
  5014. $this->assertHtml($expected, $result);
  5015. $this->Form->request->data['published'] = '';
  5016. $result = $this->Form->checkbox('published');
  5017. $expected = [
  5018. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'],
  5019. ['input' => ['type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error']]
  5020. ];
  5021. $this->assertHtml($expected, $result);
  5022. }
  5023. /**
  5024. * test checkbox() with a custom name attribute
  5025. *
  5026. * @return void
  5027. */
  5028. public function testCheckboxCustomNameAttribute()
  5029. {
  5030. $result = $this->Form->checkbox('Test.test', ['name' => 'myField']);
  5031. $expected = [
  5032. 'input' => ['type' => 'hidden', 'name' => 'myField', 'value' => '0'],
  5033. ['input' => ['type' => 'checkbox', 'name' => 'myField', 'value' => '1']]
  5034. ];
  5035. $this->assertHtml($expected, $result);
  5036. }
  5037. /**
  5038. * Test that the hidden input for checkboxes can be omitted or set to a
  5039. * specific value.
  5040. *
  5041. * @return void
  5042. */
  5043. public function testCheckboxHiddenField()
  5044. {
  5045. $result = $this->Form->checkbox('UserForm.something', [
  5046. 'hiddenField' => false
  5047. ]);
  5048. $expected = [
  5049. 'input' => [
  5050. 'type' => 'checkbox',
  5051. 'name' => 'UserForm[something]',
  5052. 'value' => '1'
  5053. ],
  5054. ];
  5055. $this->assertHtml($expected, $result);
  5056. $result = $this->Form->checkbox('UserForm.something', [
  5057. 'value' => 'Y',
  5058. 'hiddenField' => 'N',
  5059. ]);
  5060. $expected = [
  5061. ['input' => [
  5062. 'type' => 'hidden', 'name' => 'UserForm[something]',
  5063. 'value' => 'N'
  5064. ]],
  5065. ['input' => [
  5066. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  5067. 'value' => 'Y'
  5068. ]],
  5069. ];
  5070. $this->assertHtml($expected, $result);
  5071. }
  5072. /**
  5073. * Test the time type.
  5074. *
  5075. * @return void
  5076. */
  5077. public function testTime()
  5078. {
  5079. $result = $this->Form->time('start_time', [
  5080. 'timeFormat' => 12,
  5081. 'interval' => 5,
  5082. 'value' => ['hour' => '4', 'minute' => '30', 'meridian' => 'pm']
  5083. ]);
  5084. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5085. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5086. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5087. $this->assertNotContains('year', $result);
  5088. $this->assertNotContains('month', $result);
  5089. $this->assertNotContains('day', $result);
  5090. $result = $this->Form->time('start_time', [
  5091. 'timeFormat' => 12,
  5092. 'interval' => 5,
  5093. 'value' => '2014-03-08 16:30:00'
  5094. ]);
  5095. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  5096. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5097. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  5098. $this->assertNotContains('year', $result);
  5099. $this->assertNotContains('month', $result);
  5100. $this->assertNotContains('day', $result);
  5101. }
  5102. /**
  5103. * Ensure that timeFormat=24 has no merdian.
  5104. *
  5105. * @return void.
  5106. */
  5107. public function testTimeFormat24NoMeridian()
  5108. {
  5109. $result = $this->Form->time('start_time', [
  5110. 'timeFormat' => 24,
  5111. 'interval' => 5,
  5112. 'value' => '2014-03-08 16:30:00'
  5113. ]);
  5114. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  5115. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  5116. $this->assertNotContains('meridian', $result);
  5117. $this->assertNotContains('pm', $result);
  5118. $this->assertNotContains('year', $result);
  5119. $this->assertNotContains('month', $result);
  5120. $this->assertNotContains('day', $result);
  5121. }
  5122. /**
  5123. * Test the date type.
  5124. *
  5125. * @return void
  5126. */
  5127. public function testDate()
  5128. {
  5129. $result = $this->Form->date('start_day', [
  5130. 'value' => ['year' => '2014', 'month' => '03', 'day' => '08']
  5131. ]);
  5132. $this->assertContains('<option value="2014" selected="selected">2014</option>', $result);
  5133. $this->assertContains('<option value="03" selected="selected">March</option>', $result);
  5134. $this->assertContains('<option value="08" selected="selected">8</option>', $result);
  5135. $this->assertNotContains('hour', $result);
  5136. $this->assertNotContains('minute', $result);
  5137. $this->assertNotContains('second', $result);
  5138. $this->assertNotContains('meridian', $result);
  5139. }
  5140. /**
  5141. * testDateTime method
  5142. *
  5143. * Test generation of date/time select elements
  5144. *
  5145. * @return void
  5146. */
  5147. public function testDateTime()
  5148. {
  5149. extract($this->dateRegex);
  5150. $result = $this->Form->dateTime('Contact.date', ['default' => true]);
  5151. $now = strtotime('now');
  5152. $expected = [
  5153. ['select' => ['name' => 'Contact[date][year]']],
  5154. ['option' => ['value' => '']],
  5155. '/option',
  5156. $yearsRegex,
  5157. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5158. date('Y', $now),
  5159. '/option',
  5160. '*/select',
  5161. ['select' => ['name' => 'Contact[date][month]']],
  5162. ['option' => ['value' => '']],
  5163. '/option',
  5164. $monthsRegex,
  5165. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5166. date('F', $now),
  5167. '/option',
  5168. '*/select',
  5169. ['select' => ['name' => 'Contact[date][day]']],
  5170. ['option' => ['value' => '']],
  5171. '/option',
  5172. $daysRegex,
  5173. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5174. date('j', $now),
  5175. '/option',
  5176. '*/select',
  5177. ['select' => ['name' => 'Contact[date][hour]']],
  5178. ['option' => ['value' => '']],
  5179. '/option',
  5180. $hoursRegex,
  5181. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5182. date('G', $now),
  5183. '/option',
  5184. '*/select',
  5185. ['select' => ['name' => 'Contact[date][minute]']],
  5186. ['option' => ['value' => '']],
  5187. '/option',
  5188. $minutesRegex,
  5189. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5190. date('i', $now),
  5191. '/option',
  5192. '*/select',
  5193. ];
  5194. $this->assertHtml($expected, $result);
  5195. // Empty=>false implies Default=>true, as selecting the "first" dropdown value is useless
  5196. $result = $this->Form->dateTime('Contact.date', ['empty' => false]);
  5197. $now = strtotime('now');
  5198. $expected = [
  5199. ['select' => ['name' => 'Contact[date][year]']],
  5200. $yearsRegex,
  5201. ['option' => ['value' => date('Y', $now), 'selected' => 'selected']],
  5202. date('Y', $now),
  5203. '/option',
  5204. '*/select',
  5205. ['select' => ['name' => 'Contact[date][month]']],
  5206. $monthsRegex,
  5207. ['option' => ['value' => date('m', $now), 'selected' => 'selected']],
  5208. date('F', $now),
  5209. '/option',
  5210. '*/select',
  5211. ['select' => ['name' => 'Contact[date][day]']],
  5212. $daysRegex,
  5213. ['option' => ['value' => date('d', $now), 'selected' => 'selected']],
  5214. date('j', $now),
  5215. '/option',
  5216. '*/select',
  5217. ['select' => ['name' => 'Contact[date][hour]']],
  5218. $hoursRegex,
  5219. ['option' => ['value' => date('H', $now), 'selected' => 'selected']],
  5220. date('G', $now),
  5221. '/option',
  5222. '*/select',
  5223. ['select' => ['name' => 'Contact[date][minute]']],
  5224. $minutesRegex,
  5225. ['option' => ['value' => date('i', $now), 'selected' => 'selected']],
  5226. date('i', $now),
  5227. '/option',
  5228. '*/select',
  5229. ];
  5230. $this->assertHtml($expected, $result);
  5231. }
  5232. /**
  5233. * Test that datetime fields are added to protected fields list.
  5234. *
  5235. * @return void
  5236. */
  5237. public function testDateTimeSecured()
  5238. {
  5239. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5240. $this->Form->dateTime('Contact.date');
  5241. $expected = [
  5242. 'Contact.date.year',
  5243. 'Contact.date.month',
  5244. 'Contact.date.day',
  5245. 'Contact.date.hour',
  5246. 'Contact.date.minute',
  5247. ];
  5248. $this->assertEquals($expected, $this->Form->fields);
  5249. $this->Form->fields = [];
  5250. $this->Form->date('Contact.published');
  5251. $expected = [
  5252. 'Contact.published.year',
  5253. 'Contact.published.month',
  5254. 'Contact.published.day',
  5255. ];
  5256. $this->assertEquals($expected, $this->Form->fields);
  5257. }
  5258. /**
  5259. * Test that datetime fields are added to protected fields list.
  5260. *
  5261. * @return void
  5262. */
  5263. public function testDateTimeSecuredDisabled()
  5264. {
  5265. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5266. $this->Form->dateTime('Contact.date', ['secure' => false]);
  5267. $expected = [];
  5268. $this->assertEquals($expected, $this->Form->fields);
  5269. $this->Form->fields = [];
  5270. $this->Form->date('Contact.published', ['secure' => false]);
  5271. $expected = [];
  5272. $this->assertEquals($expected, $this->Form->fields);
  5273. }
  5274. /**
  5275. * Test empty defaulting to true for datetime.
  5276. *
  5277. * @return void
  5278. */
  5279. public function testDatetimeEmpty()
  5280. {
  5281. extract($this->dateRegex);
  5282. $now = strtotime('now');
  5283. $result = $this->Form->dateTime('Contact.date', [
  5284. 'timeFormat' => 12,
  5285. 'empty' => true,
  5286. 'default' => true
  5287. ]);
  5288. $expected = [
  5289. ['select' => ['name' => 'Contact[date][year]']],
  5290. $yearsRegex,
  5291. ['option' => ['value' => '']],
  5292. '/option',
  5293. '*/select',
  5294. ['select' => ['name' => 'Contact[date][month]']],
  5295. $monthsRegex,
  5296. ['option' => ['value' => '']],
  5297. '/option',
  5298. '*/select',
  5299. ['select' => ['name' => 'Contact[date][day]']],
  5300. $daysRegex,
  5301. ['option' => ['value' => '']],
  5302. '/option',
  5303. '*/select',
  5304. ['select' => ['name' => 'Contact[date][hour]']],
  5305. $hoursRegex,
  5306. ['option' => ['value' => '']],
  5307. '/option',
  5308. '*/select',
  5309. ['select' => ['name' => 'Contact[date][minute]']],
  5310. $minutesRegex,
  5311. ['option' => ['value' => '']],
  5312. '/option',
  5313. '*/select',
  5314. ['select' => ['name' => 'Contact[date][meridian]']],
  5315. $meridianRegex,
  5316. ['option' => ['value' => '']],
  5317. '/option',
  5318. '*/select'
  5319. ];
  5320. $this->assertHtml($expected, $result);
  5321. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5322. }
  5323. /**
  5324. * Test datetime with interval option.
  5325. *
  5326. * @return void
  5327. */
  5328. public function testDatetimeMinuteInterval()
  5329. {
  5330. extract($this->dateRegex);
  5331. $now = strtotime('now');
  5332. $result = $this->Form->dateTime('Contact.date', [
  5333. 'interval' => 5,
  5334. 'value' => ''
  5335. ]);
  5336. $expected = [
  5337. ['select' => ['name' => 'Contact[date][year]']],
  5338. $yearsRegex,
  5339. ['option' => ['selected' => 'selected', 'value' => '']],
  5340. '/option',
  5341. '*/select',
  5342. ['select' => ['name' => 'Contact[date][month]']],
  5343. $monthsRegex,
  5344. ['option' => ['selected' => 'selected', 'value' => '']],
  5345. '/option',
  5346. '*/select',
  5347. ['select' => ['name' => 'Contact[date][day]']],
  5348. $daysRegex,
  5349. ['option' => ['selected' => 'selected', 'value' => '']],
  5350. '/option',
  5351. '*/select',
  5352. ['select' => ['name' => 'Contact[date][hour]']],
  5353. $hoursRegex,
  5354. ['option' => ['selected' => 'selected', 'value' => '']],
  5355. '/option',
  5356. '*/select',
  5357. ['select' => ['name' => 'Contact[date][minute]']],
  5358. $minutesRegex,
  5359. ['option' => ['selected' => 'selected', 'value' => '']],
  5360. '/option',
  5361. ['option' => ['value' => '00']],
  5362. '00',
  5363. '/option',
  5364. ['option' => ['value' => '05']],
  5365. '05',
  5366. '/option',
  5367. ['option' => ['value' => '10']],
  5368. '10',
  5369. '/option',
  5370. '*/select',
  5371. ];
  5372. $this->assertHtml($expected, $result);
  5373. }
  5374. /**
  5375. * Test dateTime with rounding
  5376. *
  5377. * @return void
  5378. */
  5379. public function testDateTimeRounding()
  5380. {
  5381. $this->Form->request->data['Contact'] = [
  5382. 'date' => [
  5383. 'day' => '13',
  5384. 'month' => '12',
  5385. 'year' => '2010',
  5386. 'hour' => '04',
  5387. 'minute' => '19',
  5388. 'meridian' => 'AM'
  5389. ]
  5390. ];
  5391. $result = $this->Form->dateTime('Contact.date', ['interval' => 15]);
  5392. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5393. $result = $this->Form->dateTime('Contact.date', ['interval' => 15, 'round' => 'up']);
  5394. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5395. $result = $this->Form->dateTime('Contact.date', ['interval' => 5, 'round' => 'down']);
  5396. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5397. }
  5398. /**
  5399. * test that datetime() and default values work.
  5400. *
  5401. * @return void
  5402. */
  5403. public function testDatetimeWithDefault()
  5404. {
  5405. $result = $this->Form->dateTime('Contact.updated', ['value' => '2009-06-01 11:15:30']);
  5406. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5407. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5408. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5409. $result = $this->Form->dateTime('Contact.updated', [
  5410. 'default' => '2009-06-01 11:15:30'
  5411. ]);
  5412. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5413. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5414. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5415. }
  5416. /**
  5417. * testDateTime all zeros
  5418. *
  5419. * @return void
  5420. */
  5421. public function testDateTimeAllZeros()
  5422. {
  5423. $result = $this->Form->dateTime('Contact.date', [
  5424. 'timeFormat' => false,
  5425. 'empty' => ['day' => '-', 'month' => '-', 'year' => '-'],
  5426. 'value' => '0000-00-00'
  5427. ]);
  5428. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5429. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5430. }
  5431. /**
  5432. * testDateTimeEmptyAsArray
  5433. *
  5434. * @return void
  5435. */
  5436. public function testDateTimeEmptyAsArray()
  5437. {
  5438. $result = $this->Form->dateTime('Contact.date', [
  5439. 'empty' => [
  5440. 'day' => 'DAY',
  5441. 'month' => 'MONTH',
  5442. 'year' => 'YEAR',
  5443. 'hour' => 'HOUR',
  5444. 'minute' => 'MINUTE',
  5445. 'meridian' => false
  5446. ],
  5447. 'default' => true
  5448. ]);
  5449. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5450. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5451. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5452. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5453. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5454. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5455. $result = $this->Form->dateTime('Contact.date', [
  5456. 'empty' => ['day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR'],
  5457. 'default' => true
  5458. ]);
  5459. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5460. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5461. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5462. }
  5463. /**
  5464. * testFormDateTimeMulti method
  5465. *
  5466. * test multiple datetime element generation
  5467. *
  5468. * @return void
  5469. */
  5470. public function testFormDateTimeMulti()
  5471. {
  5472. extract($this->dateRegex);
  5473. $result = $this->Form->dateTime('Contact.1.updated');
  5474. $this->assertContains('Contact[1][updated][month]', $result);
  5475. $this->assertContains('Contact[1][updated][day]', $result);
  5476. $this->assertContains('Contact[1][updated][year]', $result);
  5477. $this->assertContains('Contact[1][updated][hour]', $result);
  5478. $this->assertContains('Contact[1][updated][minute]', $result);
  5479. }
  5480. /**
  5481. * When changing the date format, the label should always focus the first select box when
  5482. * clicked.
  5483. *
  5484. * @return void
  5485. */
  5486. public function testDateTimeLabelIdMatchesFirstInput()
  5487. {
  5488. $result = $this->Form->input('Model.date', ['type' => 'date']);
  5489. $this->assertContains('<label>Date</label>', $result);
  5490. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'DMY']);
  5491. $this->assertContains('<label>Date</label>', $result);
  5492. $result = $this->Form->input('Model.date', ['type' => 'date', 'dateFormat' => 'YMD']);
  5493. $this->assertContains('<label>Date</label>', $result);
  5494. }
  5495. /**
  5496. * test datetime second=true
  5497. *
  5498. * @return void
  5499. */
  5500. public function testDateTimeSecondOptions()
  5501. {
  5502. $result = $this->Form->dateTime('updated', ['second' => true]);
  5503. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5504. $result = $this->Form->dateTime('updated', ['second' => []]);
  5505. $this->assertContains('updated[second]', $result, 'Should have seconds');
  5506. $result = $this->Form->dateTime('updated', ['second' => null]);
  5507. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5508. $result = $this->Form->dateTime('updated', ['second' => false]);
  5509. $this->assertNotContains('updated[second]', $result, 'Should not have seconds');
  5510. }
  5511. /**
  5512. * testMonth method
  5513. *
  5514. * @return void
  5515. */
  5516. public function testMonth()
  5517. {
  5518. $result = $this->Form->month('Model.field', ['value' => '']);
  5519. $expected = [
  5520. ['select' => ['name' => 'Model[field][month]']],
  5521. ['option' => ['value' => '', 'selected' => 'selected']],
  5522. '/option',
  5523. ['option' => ['value' => '01']],
  5524. date('F', strtotime('2008-01-01 00:00:00')),
  5525. '/option',
  5526. ['option' => ['value' => '02']],
  5527. date('F', strtotime('2008-02-01 00:00:00')),
  5528. '/option',
  5529. '*/select',
  5530. ];
  5531. $this->assertHtml($expected, $result);
  5532. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  5533. $expected = [
  5534. ['select' => ['name' => 'Model[field][month]']],
  5535. ['option' => ['selected' => 'selected', 'value' => '']],
  5536. '/option',
  5537. ['option' => ['value' => '01']],
  5538. date('F', strtotime('2008-01-01 00:00:00')),
  5539. '/option',
  5540. ['option' => ['value' => '02']],
  5541. date('F', strtotime('2008-02-01 00:00:00')),
  5542. '/option',
  5543. '*/select',
  5544. ];
  5545. $this->assertHtml($expected, $result);
  5546. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  5547. $expected = [
  5548. ['select' => ['name' => 'Model[field][month]']],
  5549. ['option' => ['selected' => 'selected', 'value' => '']],
  5550. '/option',
  5551. ['option' => ['value' => '01']],
  5552. '1',
  5553. '/option',
  5554. ['option' => ['value' => '02']],
  5555. '2',
  5556. '/option',
  5557. '*/select',
  5558. ];
  5559. $this->assertHtml($expected, $result);
  5560. $monthNames = [
  5561. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5562. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  5563. ];
  5564. $result = $this->Form->month('Model.field', ['value' => '1', 'monthNames' => $monthNames]);
  5565. $expected = [
  5566. ['select' => ['name' => 'Model[field][month]']],
  5567. ['option' => ['value' => '']],
  5568. '/option',
  5569. ['option' => ['value' => '01', 'selected' => 'selected']],
  5570. 'Jan',
  5571. '/option',
  5572. ['option' => ['value' => '02']],
  5573. 'Feb',
  5574. '/option',
  5575. '*/select',
  5576. ];
  5577. $this->assertHtml($expected, $result);
  5578. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5579. $result = $this->Form->month('Project.release');
  5580. $expected = [
  5581. ['select' => ['name' => 'Project[release][month]']],
  5582. ['option' => ['value' => '']],
  5583. '/option',
  5584. ['option' => ['value' => '01']],
  5585. 'January',
  5586. '/option',
  5587. ['option' => ['value' => '02', 'selected' => 'selected']],
  5588. 'February',
  5589. '/option',
  5590. '*/select',
  5591. ];
  5592. $this->assertHtml($expected, $result);
  5593. $result = $this->Form->month('Contact.published', [
  5594. 'empty' => 'Published on',
  5595. ]);
  5596. $this->assertContains('Published on', $result);
  5597. }
  5598. /**
  5599. * testDay method
  5600. *
  5601. * @return void
  5602. */
  5603. public function testDay()
  5604. {
  5605. extract($this->dateRegex);
  5606. $result = $this->Form->day('Model.field', ['value' => '', 'class' => 'form-control']);
  5607. $expected = [
  5608. ['select' => ['name' => 'Model[field][day]', 'class' => 'form-control']],
  5609. ['option' => ['selected' => 'selected', 'value' => '']],
  5610. '/option',
  5611. ['option' => ['value' => '01']],
  5612. '1',
  5613. '/option',
  5614. ['option' => ['value' => '02']],
  5615. '2',
  5616. '/option',
  5617. $daysRegex,
  5618. '/select',
  5619. ];
  5620. $this->assertHtml($expected, $result);
  5621. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5622. $result = $this->Form->day('Model.field');
  5623. $expected = [
  5624. ['select' => ['name' => 'Model[field][day]']],
  5625. ['option' => ['value' => '']],
  5626. '/option',
  5627. ['option' => ['value' => '01']],
  5628. '1',
  5629. '/option',
  5630. ['option' => ['value' => '02']],
  5631. '2',
  5632. '/option',
  5633. $daysRegex,
  5634. ['option' => ['value' => '10', 'selected' => 'selected']],
  5635. '10',
  5636. '/option',
  5637. $daysRegex,
  5638. '/select',
  5639. ];
  5640. $this->assertHtml($expected, $result);
  5641. $this->Form->request->data['Model']['field'] = '';
  5642. $result = $this->Form->day('Model.field', ['value' => '10']);
  5643. $expected = [
  5644. ['select' => ['name' => 'Model[field][day]']],
  5645. ['option' => ['value' => '']],
  5646. '/option',
  5647. ['option' => ['value' => '01']],
  5648. '1',
  5649. '/option',
  5650. ['option' => ['value' => '02']],
  5651. '2',
  5652. '/option',
  5653. $daysRegex,
  5654. ['option' => ['value' => '10', 'selected' => 'selected']],
  5655. '10',
  5656. '/option',
  5657. $daysRegex,
  5658. '/select',
  5659. ];
  5660. $this->assertHtml($expected, $result);
  5661. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5662. $result = $this->Form->day('Project.release');
  5663. $expected = [
  5664. ['select' => ['name' => 'Project[release][day]']],
  5665. ['option' => ['value' => '']],
  5666. '/option',
  5667. ['option' => ['value' => '01']],
  5668. '1',
  5669. '/option',
  5670. ['option' => ['value' => '02']],
  5671. '2',
  5672. '/option',
  5673. $daysRegex,
  5674. ['option' => ['value' => '10', 'selected' => 'selected']],
  5675. '10',
  5676. '/option',
  5677. $daysRegex,
  5678. '/select',
  5679. ];
  5680. $this->assertHtml($expected, $result);
  5681. $result = $this->Form->day('Contact.published', [
  5682. 'empty' => 'Published on',
  5683. ]);
  5684. $this->assertContains('Published on', $result);
  5685. }
  5686. /**
  5687. * testMinute method
  5688. *
  5689. * @return void
  5690. */
  5691. public function testMinute()
  5692. {
  5693. extract($this->dateRegex);
  5694. $result = $this->Form->minute('Model.field', ['value' => '']);
  5695. $expected = [
  5696. ['select' => ['name' => 'Model[field][minute]']],
  5697. ['option' => ['selected' => 'selected', 'value' => '']],
  5698. '/option',
  5699. ['option' => ['value' => '00']],
  5700. '00',
  5701. '/option',
  5702. ['option' => ['value' => '01']],
  5703. '01',
  5704. '/option',
  5705. ['option' => ['value' => '02']],
  5706. '02',
  5707. '/option',
  5708. $minutesRegex,
  5709. '/select',
  5710. ];
  5711. $this->assertHtml($expected, $result);
  5712. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5713. $result = $this->Form->minute('Model.field');
  5714. $expected = [
  5715. ['select' => ['name' => 'Model[field][minute]']],
  5716. ['option' => ['value' => '']],
  5717. '/option',
  5718. ['option' => ['value' => '00']],
  5719. '00',
  5720. '/option',
  5721. ['option' => ['value' => '01']],
  5722. '01',
  5723. '/option',
  5724. ['option' => ['value' => '02']],
  5725. '02',
  5726. '/option',
  5727. $minutesRegex,
  5728. ['option' => ['value' => '12', 'selected' => 'selected']],
  5729. '12',
  5730. '/option',
  5731. $minutesRegex,
  5732. '/select',
  5733. ];
  5734. $this->assertHtml($expected, $result);
  5735. $this->Form->request->data['Model']['field'] = '';
  5736. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5737. $expected = [
  5738. ['select' => ['name' => 'Model[field][minute]']],
  5739. ['option' => ['selected' => 'selected', 'value' => '']],
  5740. '/option',
  5741. ['option' => ['value' => '00']],
  5742. '00',
  5743. '/option',
  5744. ['option' => ['value' => '05']],
  5745. '05',
  5746. '/option',
  5747. ['option' => ['value' => '10']],
  5748. '10',
  5749. '/option',
  5750. $minutesRegex,
  5751. '/select',
  5752. ];
  5753. $this->assertHtml($expected, $result);
  5754. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5755. $result = $this->Form->minute('Model.field', ['interval' => 5]);
  5756. $expected = [
  5757. ['select' => ['name' => 'Model[field][minute]']],
  5758. ['option' => ['value' => '']],
  5759. '/option',
  5760. ['option' => ['value' => '00']],
  5761. '00',
  5762. '/option',
  5763. ['option' => ['value' => '05']],
  5764. '05',
  5765. '/option',
  5766. ['option' => ['value' => '10', 'selected' => 'selected']],
  5767. '10',
  5768. '/option',
  5769. $minutesRegex,
  5770. '/select',
  5771. ];
  5772. $this->assertHtml($expected, $result);
  5773. }
  5774. /**
  5775. * Test generating an input for the meridian.
  5776. *
  5777. * @return void
  5778. */
  5779. public function testMeridian()
  5780. {
  5781. extract($this->dateRegex);
  5782. $now = new \DateTime();
  5783. $result = $this->Form->meridian('Model.field', ['value' => 'am']);
  5784. $expected = [
  5785. ['select' => ['name' => 'Model[field][meridian]']],
  5786. ['option' => ['value' => '']],
  5787. '/option',
  5788. $meridianRegex,
  5789. ['option' => ['value' => $now->format('a'), 'selected' => 'selected']],
  5790. $now->format('a'),
  5791. '/option',
  5792. '*/select'
  5793. ];
  5794. $this->assertHtml($expected, $result);
  5795. }
  5796. /**
  5797. * testHour method
  5798. *
  5799. * @return void
  5800. */
  5801. public function testHour()
  5802. {
  5803. extract($this->dateRegex);
  5804. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '']);
  5805. $expected = [
  5806. ['select' => ['name' => 'Model[field][hour]']],
  5807. ['option' => ['selected' => 'selected', 'value' => '']],
  5808. '/option',
  5809. ['option' => ['value' => '01']],
  5810. '1',
  5811. '/option',
  5812. ['option' => ['value' => '02']],
  5813. '2',
  5814. '/option',
  5815. $hoursRegex,
  5816. '/select',
  5817. ];
  5818. $this->assertHtml($expected, $result);
  5819. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5820. $result = $this->Form->hour('Model.field', ['format' => 12]);
  5821. $expected = [
  5822. ['select' => ['name' => 'Model[field][hour]']],
  5823. ['option' => ['value' => '']],
  5824. '/option',
  5825. ['option' => ['value' => '01']],
  5826. '1',
  5827. '/option',
  5828. ['option' => ['value' => '02']],
  5829. '2',
  5830. '/option',
  5831. $hoursRegex,
  5832. ['option' => ['value' => '12', 'selected' => 'selected']],
  5833. '12',
  5834. '/option',
  5835. '/select',
  5836. ];
  5837. $this->assertHtml($expected, $result);
  5838. $this->Form->request->data['Model']['field'] = '';
  5839. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => '23']);
  5840. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5841. $result = $this->Form->hour('Model.field', ['format' => 12, 'value' => '23']);
  5842. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5843. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5844. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5845. $expected = [
  5846. ['select' => ['name' => 'Model[field][hour]']],
  5847. ['option' => ['value' => '']],
  5848. '/option',
  5849. ['option' => ['value' => '00', 'selected' => 'selected']],
  5850. '0',
  5851. '/option',
  5852. ['option' => ['value' => '01']],
  5853. '1',
  5854. '/option',
  5855. ['option' => ['value' => '02']],
  5856. '2',
  5857. '/option',
  5858. $hoursRegex,
  5859. '/select',
  5860. ];
  5861. $this->assertHtml($expected, $result);
  5862. unset($this->Form->request->data['Model']['field']);
  5863. $result = $this->Form->hour('Model.field', ['format' => 24, 'value' => 'now']);
  5864. $thisHour = date('H');
  5865. $optValue = date('G');
  5866. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5867. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5868. $result = $this->Form->hour('Model.field', ['format' => 24]);
  5869. $expected = [
  5870. ['select' => ['name' => 'Model[field][hour]']],
  5871. ['option' => ['value' => '']],
  5872. '/option',
  5873. ['option' => ['value' => '00']],
  5874. '0',
  5875. '/option',
  5876. ['option' => ['value' => '01', 'selected' => 'selected']],
  5877. '1',
  5878. '/option',
  5879. ['option' => ['value' => '02']],
  5880. '2',
  5881. '/option',
  5882. $hoursRegex,
  5883. '/select',
  5884. ];
  5885. $this->assertHtml($expected, $result);
  5886. }
  5887. /**
  5888. * testYear method
  5889. *
  5890. * @return void
  5891. */
  5892. public function testYear()
  5893. {
  5894. $result = $this->Form->year('Model.field', ['value' => '', 'minYear' => 2006, 'maxYear' => 2007]);
  5895. $expected = [
  5896. ['select' => ['name' => 'Model[field][year]']],
  5897. ['option' => ['selected' => 'selected', 'value' => '']],
  5898. '/option',
  5899. ['option' => ['value' => '2007']],
  5900. '2007',
  5901. '/option',
  5902. ['option' => ['value' => '2006']],
  5903. '2006',
  5904. '/option',
  5905. '/select',
  5906. ];
  5907. $this->assertHtml($expected, $result);
  5908. $result = $this->Form->year('Model.field', [
  5909. 'value' => '',
  5910. 'minYear' => 2006,
  5911. 'maxYear' => 2007,
  5912. 'orderYear' => 'asc'
  5913. ]);
  5914. $expected = [
  5915. ['select' => ['name' => 'Model[field][year]']],
  5916. ['option' => ['selected' => 'selected', 'value' => '']],
  5917. '/option',
  5918. ['option' => ['value' => '2006']],
  5919. '2006',
  5920. '/option',
  5921. ['option' => ['value' => '2007']],
  5922. '2007',
  5923. '/option',
  5924. '/select',
  5925. ];
  5926. $this->assertHtml($expected, $result);
  5927. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5928. $result = $this->Form->year('Contact.published', [
  5929. 'empty' => false,
  5930. 'minYear' => 2006,
  5931. 'maxYear' => 2007,
  5932. ]);
  5933. $expected = [
  5934. ['select' => ['name' => 'Contact[published][year]']],
  5935. ['option' => ['value' => '2007']],
  5936. '2007',
  5937. '/option',
  5938. ['option' => ['value' => '2006', 'selected' => 'selected']],
  5939. '2006',
  5940. '/option',
  5941. '/select',
  5942. ];
  5943. $this->assertHtml($expected, $result);
  5944. $result = $this->Form->year('Contact.published', [
  5945. 'empty' => 'Published on',
  5946. ]);
  5947. $this->assertContains('Published on', $result);
  5948. }
  5949. /**
  5950. * Test minYear being prior to the unix epoch
  5951. *
  5952. * @return void
  5953. */
  5954. public function testInputDatetimePreEpoch()
  5955. {
  5956. $start = date('Y') - 80;
  5957. $end = date('Y') - 18;
  5958. $result = $this->Form->input('birth_year', [
  5959. 'type' => 'date',
  5960. 'label' => 'Birth Year',
  5961. 'minYear' => $start,
  5962. 'maxYear' => $end,
  5963. 'month' => false,
  5964. 'day' => false,
  5965. ]);
  5966. $this->assertContains('value="' . $start . '">' . $start, $result);
  5967. $this->assertContains('value="' . $end . '" selected="selected">' . $end, $result);
  5968. $this->assertNotContains('value="00">00', $result);
  5969. }
  5970. /**
  5971. * testYearAutoExpandRange method
  5972. *
  5973. * @return void
  5974. */
  5975. public function testYearAutoExpandRange()
  5976. {
  5977. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5978. $result = $this->Form->year('User.birthday');
  5979. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5980. $result = $matches[1];
  5981. $expected = range(date('Y') + 5, 1930);
  5982. $this->assertEquals($expected, $result);
  5983. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5984. $result = $this->Form->year('Project.release');
  5985. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5986. $result = $matches[1];
  5987. $expected = range(2050, date('Y') - 5);
  5988. $this->assertEquals($expected, $result);
  5989. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5990. $result = $this->Form->year('Project.release', [
  5991. 'minYear' => 1890,
  5992. 'maxYear' => 1900
  5993. ]);
  5994. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5995. $result = $matches[1];
  5996. $expected = range(1900, 1881);
  5997. $this->assertEquals($expected, $result);
  5998. }
  5999. /**
  6000. * Test that input() accepts the type of date and passes options in.
  6001. *
  6002. * @return void
  6003. */
  6004. public function testInputDate()
  6005. {
  6006. $this->Form->request->data = [
  6007. 'month_year' => ['month' => date('m')],
  6008. ];
  6009. $this->Form->create($this->article);
  6010. $result = $this->Form->input('month_year', [
  6011. 'label' => false,
  6012. 'type' => 'date',
  6013. 'minYear' => 2006,
  6014. 'maxYear' => 2008
  6015. ]);
  6016. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6017. $this->assertNotContains('value="2008" selected="selected"', $result);
  6018. }
  6019. /**
  6020. * Test the label option being set to false.
  6021. *
  6022. * @return void
  6023. */
  6024. public function testInputLabelFalse()
  6025. {
  6026. $this->Form->create($this->article);
  6027. $result = $this->Form->input('title', ['label' => false]);
  6028. $expected = [
  6029. 'div' => ['class' => 'input text required'],
  6030. 'input' => ['type' => 'text', 'required' => 'required', 'id' => 'title', 'name' => 'title'],
  6031. '/div'
  6032. ];
  6033. $this->assertHtml($expected, $result);
  6034. }
  6035. /**
  6036. * testInputDateMaxYear method
  6037. *
  6038. * Let's say we want to only allow users born from 2006 to 2008 to register
  6039. * This being the first signup page, we still don't have any data
  6040. *
  6041. * @return void
  6042. */
  6043. public function testInputDateMaxYear()
  6044. {
  6045. $this->Form->request->data = [];
  6046. $this->Form->create($this->article);
  6047. $result = $this->Form->input('birthday', [
  6048. 'label' => false,
  6049. 'type' => 'date',
  6050. 'minYear' => 2006,
  6051. 'maxYear' => 2008,
  6052. 'default' => true
  6053. ]);
  6054. $this->assertContains('value="2008" selected="selected"', $result);
  6055. $this->assertContains('value="2006"', $result);
  6056. $this->assertNotContains('value="2005"', $result);
  6057. $this->assertNotContains('value="2009"', $result);
  6058. }
  6059. /**
  6060. * testTextArea method
  6061. *
  6062. * @return void
  6063. */
  6064. public function testTextArea()
  6065. {
  6066. $this->Form->request->data = ['field' => 'some test data'];
  6067. $result = $this->Form->textarea('field');
  6068. $expected = [
  6069. 'textarea' => ['name' => 'field', 'rows' => 5],
  6070. 'some test data',
  6071. '/textarea',
  6072. ];
  6073. $this->assertHtml($expected, $result);
  6074. $result = $this->Form->textarea('user.bio');
  6075. $expected = [
  6076. 'textarea' => ['name' => 'user[bio]', 'rows' => 5],
  6077. '/textarea',
  6078. ];
  6079. $this->assertHtml($expected, $result);
  6080. $this->Form->request->data = ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'];
  6081. $result = $this->Form->textarea('field');
  6082. $expected = [
  6083. 'textarea' => ['name' => 'field', 'rows' => 5],
  6084. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6085. '/textarea',
  6086. ];
  6087. $this->assertHtml($expected, $result);
  6088. $this->Form->request->data = [
  6089. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6090. ];
  6091. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6092. $expected = [
  6093. 'textarea' => ['name' => 'Model[field]', 'rows' => 5],
  6094. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6095. '/textarea',
  6096. ];
  6097. $this->assertHtml($expected, $result);
  6098. $result = $this->Form->textarea('0.OtherModel.field');
  6099. $expected = [
  6100. 'textarea' => ['name' => '0[OtherModel][field]', 'rows' => 5],
  6101. '/textarea'
  6102. ];
  6103. $this->assertHtml($expected, $result);
  6104. }
  6105. /**
  6106. * testTextAreaWithStupidCharacters method
  6107. *
  6108. * test text area with non-ascii characters
  6109. *
  6110. * @return void
  6111. */
  6112. public function testTextAreaWithStupidCharacters()
  6113. {
  6114. $result = $this->Form->textarea('Post.content', [
  6115. 'value' => "GREAT®",
  6116. 'rows' => '15',
  6117. 'cols' => '75'
  6118. ]);
  6119. $expected = [
  6120. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6121. 'GREAT®',
  6122. '/textarea',
  6123. ];
  6124. $this->assertHtml($expected, $result);
  6125. }
  6126. /**
  6127. * Test textareas waxlength read from schema.
  6128. *
  6129. * @return void
  6130. */
  6131. public function testTextAreaMaxLength()
  6132. {
  6133. $this->Form->create([
  6134. 'schema' => [
  6135. 'stuff' => ['type' => 'string', 'length' => 10],
  6136. ]
  6137. ]);
  6138. $result = $this->Form->input('other', ['type' => 'textarea']);
  6139. $expected = [
  6140. 'div' => ['class' => 'input textarea'],
  6141. 'label' => ['for' => 'other'],
  6142. 'Other',
  6143. '/label',
  6144. 'textarea' => ['name' => 'other', 'id' => 'other', 'rows' => 5],
  6145. '/textarea',
  6146. '/div'
  6147. ];
  6148. $this->assertHtml($expected, $result);
  6149. $result = $this->Form->input('stuff', ['type' => 'textarea']);
  6150. $expected = [
  6151. 'div' => ['class' => 'input textarea'],
  6152. 'label' => ['for' => 'stuff'],
  6153. 'Stuff',
  6154. '/label',
  6155. 'textarea' => ['name' => 'stuff', 'maxlength' => 10, 'id' => 'stuff', 'rows' => 5],
  6156. '/textarea',
  6157. '/div'
  6158. ];
  6159. $this->assertHtml($expected, $result);
  6160. }
  6161. /**
  6162. * testHiddenField method
  6163. *
  6164. * @return void
  6165. */
  6166. public function testHiddenField()
  6167. {
  6168. $this->article['errors'] = [
  6169. 'field' => true
  6170. ];
  6171. $this->Form->request->data['field'] = 'test';
  6172. $this->Form->create($this->article);
  6173. $result = $this->Form->hidden('field', ['id' => 'theID']);
  6174. $expected = [
  6175. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test']];
  6176. $this->assertHtml($expected, $result);
  6177. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6178. $expected = [
  6179. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6180. ];
  6181. $this->assertHtml($expected, $result);
  6182. }
  6183. /**
  6184. * testFileUploadField method
  6185. *
  6186. * @return void
  6187. */
  6188. public function testFileUploadField()
  6189. {
  6190. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  6191. $result = $this->Form->file('Model.upload');
  6192. $this->assertHtml($expected, $result);
  6193. $this->Form->request->data['Model']['upload'] = [
  6194. 'name' => '', 'type' => '', 'tmp_name' => '',
  6195. 'error' => 4, 'size' => 0
  6196. ];
  6197. $result = $this->Form->file('Model.upload');
  6198. $this->assertHtml($expected, $result);
  6199. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6200. $result = $this->Form->file('Model.upload');
  6201. $this->assertHtml($expected, $result);
  6202. }
  6203. /**
  6204. * test File upload input on a model not used in create();
  6205. *
  6206. * @return void
  6207. */
  6208. public function testFileUploadOnOtherModel()
  6209. {
  6210. $this->Form->create($this->article, ['type' => 'file']);
  6211. $result = $this->Form->file('ValidateProfile.city');
  6212. $expected = [
  6213. 'input' => ['type' => 'file', 'name' => 'ValidateProfile[city]']
  6214. ];
  6215. $this->assertHtml($expected, $result);
  6216. }
  6217. /**
  6218. * testButton method
  6219. *
  6220. * @return void
  6221. */
  6222. public function testButton()
  6223. {
  6224. $result = $this->Form->button('Hi');
  6225. $expected = ['button' => ['type' => 'submit'], 'Hi', '/button'];
  6226. $this->assertHtml($expected, $result);
  6227. $result = $this->Form->button('Clear Form >', ['type' => 'reset']);
  6228. $expected = ['button' => ['type' => 'reset'], 'Clear Form >', '/button'];
  6229. $this->assertHtml($expected, $result);
  6230. $result = $this->Form->button('Clear Form >', ['type' => 'reset', 'id' => 'clearForm']);
  6231. $expected = ['button' => ['type' => 'reset', 'id' => 'clearForm'], 'Clear Form >', '/button'];
  6232. $this->assertHtml($expected, $result);
  6233. $result = $this->Form->button('<Clear Form>', ['type' => 'reset', 'escape' => true]);
  6234. $expected = ['button' => ['type' => 'reset'], '&lt;Clear Form&gt;', '/button'];
  6235. $this->assertHtml($expected, $result);
  6236. $result = $this->Form->button('No type', ['type' => false]);
  6237. $expected = ['button' => [], 'No type', '/button'];
  6238. $this->assertHtml($expected, $result);
  6239. $result = $this->Form->button('Upload Text', [
  6240. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6241. 'escape' => false
  6242. ]);
  6243. $this->assertNotRegExp('/\&039/', $result);
  6244. }
  6245. /**
  6246. * Test that button() makes unlocked fields by default.
  6247. *
  6248. * @return void
  6249. */
  6250. public function testButtonUnlockedByDefault()
  6251. {
  6252. $this->Form->request->params['_csrfToken'] = 'secured';
  6253. $this->Form->button('Save', ['name' => 'save']);
  6254. $this->Form->button('Clear');
  6255. $result = $this->Form->unlockField();
  6256. $this->assertEquals(['save'], $result);
  6257. }
  6258. /**
  6259. * testPostButton method
  6260. *
  6261. * @return void
  6262. */
  6263. public function testPostButton()
  6264. {
  6265. $result = $this->Form->postButton('Hi', '/controller/action');
  6266. $expected = [
  6267. 'form' => ['method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'],
  6268. 'div' => ['style' => 'display:none;'],
  6269. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6270. '/div',
  6271. 'button' => ['type' => 'submit'],
  6272. 'Hi',
  6273. '/button',
  6274. '/form'
  6275. ];
  6276. $this->assertHtml($expected, $result);
  6277. $result = $this->Form->postButton('Send', '/', ['data' => ['extra' => 'value']]);
  6278. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6279. }
  6280. /**
  6281. * Test using postButton with N dimensional data.
  6282. *
  6283. * @return void
  6284. */
  6285. public function testPostButtonNestedData()
  6286. {
  6287. $data = [
  6288. 'one' => [
  6289. 'two' => [
  6290. 3, 4, 5
  6291. ]
  6292. ]
  6293. ];
  6294. $result = $this->Form->postButton('Send', '/', ['data' => $data]);
  6295. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6296. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6297. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6298. }
  6299. /**
  6300. * Test that postButton adds _Token fields.
  6301. *
  6302. * @return void
  6303. */
  6304. public function testSecurePostButton()
  6305. {
  6306. $this->Form->request->params['_csrfToken'] = 'testkey';
  6307. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6308. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6309. $tokenDebug = urlencode(json_encode([
  6310. '/posts/delete/1',
  6311. [],
  6312. []
  6313. ]));
  6314. $expected = [
  6315. 'form' => [
  6316. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6317. ],
  6318. ['div' => ['style' => 'display:none;']],
  6319. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6320. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6321. '/div',
  6322. 'button' => ['type' => 'submit'],
  6323. 'Delete',
  6324. '/button',
  6325. ['div' => ['style' => 'display:none;']],
  6326. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6327. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6328. ['input' => [
  6329. 'type' => 'hidden', 'name' => '_Token[debug]',
  6330. 'value' => $tokenDebug,
  6331. ]],
  6332. '/div',
  6333. '/form',
  6334. ];
  6335. $this->assertHtml($expected, $result);
  6336. }
  6337. /**
  6338. * testPostLink method
  6339. *
  6340. * @return void
  6341. */
  6342. public function testPostLink()
  6343. {
  6344. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6345. $expected = [
  6346. 'form' => [
  6347. 'method' => 'post', 'action' => '/posts/delete/1',
  6348. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6349. ],
  6350. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6351. '/form',
  6352. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6353. 'Delete',
  6354. '/a'
  6355. ];
  6356. $this->assertHtml($expected, $result);
  6357. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['method' => 'delete']);
  6358. $expected = [
  6359. 'form' => [
  6360. 'method' => 'post', 'action' => '/posts/delete/1',
  6361. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6362. ],
  6363. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'],
  6364. '/form',
  6365. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6366. 'Delete',
  6367. '/a'
  6368. ];
  6369. $this->assertHtml($expected, $result);
  6370. $result = $this->Form->postLink(
  6371. 'Delete',
  6372. '/posts/delete/1',
  6373. ['target' => '_blank', 'class' => 'btn btn-danger']
  6374. );
  6375. $expected = [
  6376. 'form' => [
  6377. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6378. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6379. ],
  6380. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6381. '/form',
  6382. 'a' => ['class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6383. 'Delete',
  6384. '/a'
  6385. ];
  6386. $this->assertHtml($expected, $result);
  6387. }
  6388. /**
  6389. * Test the confirm option for postLink()
  6390. *
  6391. * @return void
  6392. */
  6393. public function testPostLinkWithConfirm()
  6394. {
  6395. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['confirm' => 'Confirm?']);
  6396. $expected = [
  6397. 'form' => [
  6398. 'method' => 'post', 'action' => '/posts/delete/1',
  6399. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6400. ],
  6401. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6402. '/form',
  6403. 'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'],
  6404. 'Delete',
  6405. '/a'
  6406. ];
  6407. $this->assertHtml($expected, $result);
  6408. $result = $this->Form->postLink(
  6409. 'Delete',
  6410. '/posts/delete/1',
  6411. ['escape' => false, 'confirm' => "'Confirm'\nthis \"deletion\"?"]
  6412. );
  6413. $expected = [
  6414. 'form' => [
  6415. 'method' => 'post', 'action' => '/posts/delete/1',
  6416. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6417. ],
  6418. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6419. '/form',
  6420. 'a' => ['href' => '#', 'onclick' => "preg:/if \(confirm\(&quot;&#039;Confirm&#039;\\nthis \\\\&quot;deletion\\\\&quot;\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/"],
  6421. 'Delete',
  6422. '/a'
  6423. ];
  6424. $this->assertHtml($expected, $result);
  6425. }
  6426. /**
  6427. * test postLink() with query string args.
  6428. *
  6429. * @return void
  6430. */
  6431. public function testPostLinkWithQuery()
  6432. {
  6433. $result = $this->Form->postLink(
  6434. 'Delete',
  6435. ['controller' => 'posts', 'action' => 'delete', 1, '?' => ['a' => 'b', 'c' => 'd']]
  6436. );
  6437. $expected = [
  6438. 'form' => [
  6439. 'method' => 'post', 'action' => '/posts/delete/1?a=b&amp;c=d',
  6440. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6441. ],
  6442. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6443. '/form',
  6444. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6445. 'Delete',
  6446. '/a'
  6447. ];
  6448. $this->assertHtml($expected, $result);
  6449. }
  6450. /**
  6451. * Test postLink with additional data.
  6452. *
  6453. * @return void
  6454. */
  6455. public function testPostLinkWithData()
  6456. {
  6457. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['id' => 1]]);
  6458. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6459. $entity = new Entity(['name' => 'no show'], ['source' => 'Articles']);
  6460. $this->Form->create($entity);
  6461. $this->Form->end();
  6462. $result = $this->Form->postLink('Delete', '/posts/delete', ['data' => ['name' => 'show']]);
  6463. $this->assertContains(
  6464. '<input type="hidden" name="name" value="show"',
  6465. $result,
  6466. 'should not contain entity data.'
  6467. );
  6468. }
  6469. /**
  6470. * Test that security hashes for postLink include the url.
  6471. *
  6472. * @return void
  6473. */
  6474. public function testPostLinkSecurityHash()
  6475. {
  6476. $hash = Security::hash(
  6477. '/posts/delete/1' .
  6478. serialize(['id' => '1']) .
  6479. '' .
  6480. Security::salt()
  6481. );
  6482. $hash .= '%3Aid';
  6483. $this->Form->request->params['_Token']['key'] = 'test';
  6484. $result = $this->Form->postLink(
  6485. 'Delete',
  6486. '/posts/delete/1',
  6487. ['data' => ['id' => 1]]
  6488. );
  6489. $tokenDebug = urlencode(json_encode([
  6490. '/posts/delete/1',
  6491. [
  6492. 'id' => 1
  6493. ],
  6494. []
  6495. ]));
  6496. $expected = [
  6497. 'form' => [
  6498. 'method' => 'post', 'action' => '/posts/delete/1',
  6499. 'name', 'style' => 'display:none;'
  6500. ],
  6501. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6502. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6503. 'div' => ['style' => 'display:none;'],
  6504. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6505. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6506. ['input' => [
  6507. 'type' => 'hidden', 'name' => '_Token[debug]',
  6508. 'value' => $tokenDebug,
  6509. ]],
  6510. '/div',
  6511. '/form',
  6512. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6513. 'Delete',
  6514. '/a'
  6515. ];
  6516. $this->assertHtml($expected, $result);
  6517. }
  6518. /**
  6519. * Test that postLink doesn't modify the fields in the containing form.
  6520. *
  6521. * postLink() calls inside open forms should not modify the field list
  6522. * for the form.
  6523. *
  6524. * @return void
  6525. */
  6526. public function testPostLinkSecurityHashBlockMode()
  6527. {
  6528. $hash = Security::hash(
  6529. '/posts/delete/1' .
  6530. serialize([]) .
  6531. '' .
  6532. Security::salt()
  6533. );
  6534. $hash .= '%3A';
  6535. $this->Form->request->params['_Token']['key'] = 'test';
  6536. $this->Form->create('Post', ['url' => ['action' => 'add']]);
  6537. $this->Form->input('title');
  6538. $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6539. $result = $this->View->fetch('postLink');
  6540. $this->assertEquals(['title'], $this->Form->fields);
  6541. $this->assertContains($hash, $result, 'Should contain the correct hash.');
  6542. $this->assertAttributeEquals('/articles/add', '_lastAction', $this->Form, 'lastAction was should be restored.');
  6543. }
  6544. /**
  6545. * Test that security does not include debug token if debug is false.
  6546. *
  6547. * @return void
  6548. */
  6549. public function testPostLinkSecurityHashNoDebugMode()
  6550. {
  6551. Configure::write('debug', false);
  6552. $hash = Security::hash(
  6553. '/posts/delete/1' .
  6554. serialize(['id' => '1']) .
  6555. '' .
  6556. Security::salt()
  6557. );
  6558. $hash .= '%3Aid';
  6559. $this->Form->request->params['_Token']['key'] = 'test';
  6560. $result = $this->Form->postLink(
  6561. 'Delete',
  6562. '/posts/delete/1',
  6563. ['data' => ['id' => 1]]
  6564. );
  6565. $expected = [
  6566. 'form' => [
  6567. 'method' => 'post', 'action' => '/posts/delete/1',
  6568. 'name', 'style' => 'display:none;'
  6569. ],
  6570. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6571. ['input' => ['type' => 'hidden', 'name' => 'id', 'value' => '1']],
  6572. 'div' => ['style' => 'display:none;'],
  6573. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => $hash]],
  6574. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6575. '/div',
  6576. '/form',
  6577. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6578. 'Delete',
  6579. '/a'
  6580. ];
  6581. $this->assertHtml($expected, $result);
  6582. }
  6583. /**
  6584. * Test using postLink with N dimensional data.
  6585. *
  6586. * @return void
  6587. */
  6588. public function testPostLinkNestedData()
  6589. {
  6590. $data = [
  6591. 'one' => [
  6592. 'two' => [
  6593. 3, 4, 5
  6594. ]
  6595. ]
  6596. ];
  6597. $result = $this->Form->postLink('Send', '/', ['data' => $data]);
  6598. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  6599. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  6600. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  6601. }
  6602. /**
  6603. * test creating postLinks after a GET form.
  6604. *
  6605. * @return void
  6606. */
  6607. public function testPostLinkAfterGetForm()
  6608. {
  6609. $this->Form->request->params['_csrfToken'] = 'testkey';
  6610. $this->Form->request->params['_Token'] = 'val';
  6611. $this->Form->create($this->article, ['type' => 'get']);
  6612. $this->Form->end();
  6613. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6614. $tokenDebug = urlencode(json_encode([
  6615. '/posts/delete/1',
  6616. [],
  6617. []
  6618. ]));
  6619. $expected = [
  6620. 'form' => [
  6621. 'method' => 'post', 'action' => '/posts/delete/1',
  6622. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6623. ],
  6624. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST']],
  6625. ['input' => ['type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey']],
  6626. 'div' => ['style' => 'display:none;'],
  6627. ['input' => ['type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/']],
  6628. ['input' => ['type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '']],
  6629. ['input' => [
  6630. 'type' => 'hidden', 'name' => '_Token[debug]',
  6631. 'value' => $tokenDebug,
  6632. ]],
  6633. '/div',
  6634. '/form',
  6635. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6636. 'Delete',
  6637. '/a'
  6638. ];
  6639. $this->assertHtml($expected, $result);
  6640. }
  6641. /**
  6642. * Test that postLink adds form tags to view block
  6643. *
  6644. * @return void
  6645. */
  6646. public function testPostLinkFormBuffer()
  6647. {
  6648. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => true]);
  6649. $expected = [
  6650. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6651. 'Delete',
  6652. '/a'
  6653. ];
  6654. $this->assertHtml($expected, $result);
  6655. $result = $this->View->fetch('postLink');
  6656. $expected = [
  6657. 'form' => [
  6658. 'method' => 'post', 'action' => '/posts/delete/1',
  6659. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6660. ],
  6661. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6662. '/form'
  6663. ];
  6664. $this->assertHtml($expected, $result);
  6665. $result = $this->Form->postLink(
  6666. 'Delete',
  6667. '/posts/delete/2',
  6668. ['block' => true, 'method' => 'DELETE']
  6669. );
  6670. $expected = [
  6671. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6672. 'Delete',
  6673. '/a'
  6674. ];
  6675. $this->assertHtml($expected, $result);
  6676. $result = $this->View->fetch('postLink');
  6677. $expected = [
  6678. 'form' => [
  6679. 'method' => 'post', 'action' => '/posts/delete/1',
  6680. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6681. ],
  6682. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6683. '/form',
  6684. [
  6685. 'form' => [
  6686. 'method' => 'post', 'action' => '/posts/delete/2',
  6687. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6688. ],
  6689. ],
  6690. ['input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'DELETE']],
  6691. '/form'
  6692. ];
  6693. $this->assertHtml($expected, $result);
  6694. $result = $this->Form->postLink('Delete', '/posts/delete/1', ['block' => 'foobar']);
  6695. $expected = [
  6696. 'a' => ['href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'],
  6697. 'Delete',
  6698. '/a'
  6699. ];
  6700. $this->assertHtml($expected, $result);
  6701. $result = $this->View->fetch('foobar');
  6702. $expected = [
  6703. 'form' => [
  6704. 'method' => 'post', 'action' => '/posts/delete/1',
  6705. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6706. ],
  6707. 'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
  6708. '/form'
  6709. ];
  6710. $this->assertHtml($expected, $result);
  6711. }
  6712. /**
  6713. * testSubmitButton method
  6714. *
  6715. * @return void
  6716. */
  6717. public function testSubmitButton()
  6718. {
  6719. $result = $this->Form->submit('');
  6720. $expected = [
  6721. 'div' => ['class' => 'submit'],
  6722. 'input' => ['type' => 'submit', 'value' => ''],
  6723. '/div'
  6724. ];
  6725. $this->assertHtml($expected, $result);
  6726. $result = $this->Form->submit('Test Submit');
  6727. $expected = [
  6728. 'div' => ['class' => 'submit'],
  6729. 'input' => ['type' => 'submit', 'value' => 'Test Submit'],
  6730. '/div'
  6731. ];
  6732. $this->assertHtml($expected, $result);
  6733. $result = $this->Form->submit('Next >');
  6734. $expected = [
  6735. 'div' => ['class' => 'submit'],
  6736. 'input' => ['type' => 'submit', 'value' => 'Next &gt;'],
  6737. '/div'
  6738. ];
  6739. $this->assertHtml($expected, $result);
  6740. $result = $this->Form->submit('Next >', ['escape' => false]);
  6741. $expected = [
  6742. 'div' => ['class' => 'submit'],
  6743. 'input' => ['type' => 'submit', 'value' => 'Next >'],
  6744. '/div'
  6745. ];
  6746. $this->assertHtml($expected, $result);
  6747. $result = $this->Form->submit('Reset!', ['type' => 'reset']);
  6748. $expected = [
  6749. 'div' => ['class' => 'submit'],
  6750. 'input' => ['type' => 'reset', 'value' => 'Reset!'],
  6751. '/div'
  6752. ];
  6753. $this->assertHtml($expected, $result);
  6754. }
  6755. /**
  6756. * test image submit types.
  6757. *
  6758. * @return void
  6759. */
  6760. public function testSubmitImage()
  6761. {
  6762. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6763. $expected = [
  6764. 'div' => ['class' => 'submit'],
  6765. 'input' => ['type' => 'image', 'src' => 'http://example.com/cake.power.gif'],
  6766. '/div'
  6767. ];
  6768. $this->assertHtml($expected, $result);
  6769. $result = $this->Form->submit('/relative/cake.power.gif');
  6770. $expected = [
  6771. 'div' => ['class' => 'submit'],
  6772. 'input' => ['type' => 'image', 'src' => 'relative/cake.power.gif'],
  6773. '/div'
  6774. ];
  6775. $this->assertHtml($expected, $result);
  6776. $result = $this->Form->submit('cake.power.gif');
  6777. $expected = [
  6778. 'div' => ['class' => 'submit'],
  6779. 'input' => ['type' => 'image', 'src' => 'img/cake.power.gif'],
  6780. '/div'
  6781. ];
  6782. $this->assertHtml($expected, $result);
  6783. $result = $this->Form->submit('Not.an.image');
  6784. $expected = [
  6785. 'div' => ['class' => 'submit'],
  6786. 'input' => ['type' => 'submit', 'value' => 'Not.an.image'],
  6787. '/div'
  6788. ];
  6789. $this->assertHtml($expected, $result);
  6790. }
  6791. /**
  6792. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6793. * be submitted at a time.
  6794. *
  6795. * @return void
  6796. */
  6797. public function testSubmitUnlockedByDefault()
  6798. {
  6799. $this->Form->request->params['_Token'] = 'secured';
  6800. $this->Form->submit('Go go');
  6801. $this->Form->submit('Save', ['name' => 'save']);
  6802. $result = $this->Form->unlockField();
  6803. $this->assertEquals(['save'], $result, 'Only submits with name attributes should be unlocked.');
  6804. }
  6805. /**
  6806. * Test submit image with timestamps.
  6807. *
  6808. * @return void
  6809. */
  6810. public function testSubmitImageTimestamp()
  6811. {
  6812. Configure::write('Asset.timestamp', 'force');
  6813. $result = $this->Form->submit('cake.power.gif');
  6814. $expected = [
  6815. 'div' => ['class' => 'submit'],
  6816. 'input' => ['type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'],
  6817. '/div'
  6818. ];
  6819. $this->assertHtml($expected, $result);
  6820. }
  6821. /**
  6822. * test that datetime() works with GET style forms.
  6823. *
  6824. * @return void
  6825. */
  6826. public function testDateTimeWithGetForms()
  6827. {
  6828. extract($this->dateRegex);
  6829. $this->Form->create($this->article, ['type' => 'get']);
  6830. $result = $this->Form->datetime('created');
  6831. $this->assertContains('name="created[year]"', $result, 'year name attribute is wrong.');
  6832. $this->assertContains('name="created[month]"', $result, 'month name attribute is wrong.');
  6833. $this->assertContains('name="created[day]"', $result, 'day name attribute is wrong.');
  6834. $this->assertContains('name="created[hour]"', $result, 'hour name attribute is wrong.');
  6835. $this->assertContains('name="created[minute]"', $result, 'min name attribute is wrong.');
  6836. }
  6837. /**
  6838. * testForMagicInputNonExistingNorValidated method
  6839. *
  6840. * @return void
  6841. */
  6842. public function testForMagicInputNonExistingNorValidated()
  6843. {
  6844. $result = $this->Form->create($this->article);
  6845. $this->Form->templates(['inputContainer' => '{{content}}']);
  6846. $result = $this->Form->input('non_existing_nor_validated');
  6847. $expected = [
  6848. 'label' => ['for' => 'non-existing-nor-validated'],
  6849. 'Non Existing Nor Validated',
  6850. '/label',
  6851. 'input' => [
  6852. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6853. 'id' => 'non-existing-nor-validated'
  6854. ]
  6855. ];
  6856. $this->assertHtml($expected, $result);
  6857. $result = $this->Form->input('non_existing_nor_validated', [
  6858. 'val' => 'my value'
  6859. ]);
  6860. $expected = [
  6861. 'label' => ['for' => 'non-existing-nor-validated'],
  6862. 'Non Existing Nor Validated',
  6863. '/label',
  6864. 'input' => [
  6865. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6866. 'value' => 'my value', 'id' => 'non-existing-nor-validated'
  6867. ]
  6868. ];
  6869. $this->assertHtml($expected, $result);
  6870. $this->Form->request->data = ['non_existing_nor_validated' => 'CakePHP magic'];
  6871. $result = $this->Form->input('non_existing_nor_validated');
  6872. $expected = [
  6873. 'label' => ['for' => 'non-existing-nor-validated'],
  6874. 'Non Existing Nor Validated',
  6875. '/label',
  6876. 'input' => [
  6877. 'type' => 'text', 'name' => 'non_existing_nor_validated',
  6878. 'value' => 'CakePHP magic', 'id' => 'non-existing-nor-validated'
  6879. ]
  6880. ];
  6881. $this->assertHtml($expected, $result);
  6882. }
  6883. /**
  6884. * testFormMagicInputLabel method
  6885. *
  6886. * @return void
  6887. */
  6888. public function testFormMagicInputLabel()
  6889. {
  6890. TableRegistry::get('Contacts', [
  6891. 'className' => __NAMESPACE__ . '\ContactsTable'
  6892. ]);
  6893. $this->Form->create([], ['context' => ['table' => 'Contacts']]);
  6894. $this->Form->templates(['inputContainer' => '{{content}}']);
  6895. $result = $this->Form->input('Contacts.name', ['label' => 'My label']);
  6896. $expected = [
  6897. 'label' => ['for' => 'contacts-name'],
  6898. 'My label',
  6899. '/label',
  6900. 'input' => [
  6901. 'type' => 'text', 'name' => 'Contacts[name]',
  6902. 'id' => 'contacts-name', 'maxlength' => '255'
  6903. ]
  6904. ];
  6905. $this->assertHtml($expected, $result);
  6906. $result = $this->Form->input('name', [
  6907. 'label' => ['class' => 'mandatory']
  6908. ]);
  6909. $expected = [
  6910. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6911. 'Name',
  6912. '/label',
  6913. 'input' => [
  6914. 'type' => 'text', 'name' => 'name',
  6915. 'id' => 'name', 'maxlength' => '255'
  6916. ]
  6917. ];
  6918. $this->assertHtml($expected, $result);
  6919. $result = $this->Form->input('name', [
  6920. 'div' => false,
  6921. 'label' => ['class' => 'mandatory', 'text' => 'My label']
  6922. ]);
  6923. $expected = [
  6924. 'label' => ['for' => 'name', 'class' => 'mandatory'],
  6925. 'My label',
  6926. '/label',
  6927. 'input' => [
  6928. 'type' => 'text', 'name' => 'name',
  6929. 'id' => 'name', 'maxlength' => '255'
  6930. ]
  6931. ];
  6932. $this->assertHtml($expected, $result);
  6933. $result = $this->Form->input('Contact.name', [
  6934. 'div' => false, 'id' => 'my_id', 'label' => ['for' => 'my_id']
  6935. ]);
  6936. $expected = [
  6937. 'label' => ['for' => 'my_id'],
  6938. 'Name',
  6939. '/label',
  6940. 'input' => [
  6941. 'type' => 'text', 'name' => 'Contact[name]',
  6942. 'id' => 'my_id', 'maxlength' => '255'
  6943. ]
  6944. ];
  6945. $this->assertHtml($expected, $result);
  6946. $result = $this->Form->input('1.id');
  6947. $expected = ['input' => [
  6948. 'type' => 'hidden', 'name' => '1[id]',
  6949. 'id' => '1-id'
  6950. ]];
  6951. $this->assertHtml($expected, $result);
  6952. $result = $this->Form->input("1.name");
  6953. $expected = [
  6954. 'label' => ['for' => '1-name'],
  6955. 'Name',
  6956. '/label',
  6957. 'input' => [
  6958. 'type' => 'text', 'name' => '1[name]',
  6959. 'id' => '1-name', 'maxlength' => '255'
  6960. ]
  6961. ];
  6962. $this->assertHtml($expected, $result);
  6963. }
  6964. /**
  6965. * testFormEnd method
  6966. *
  6967. * @return void
  6968. */
  6969. public function testFormEnd()
  6970. {
  6971. $this->assertEquals('</form>', $this->Form->end());
  6972. }
  6973. /**
  6974. * Test the generation of fields for a multi record form.
  6975. *
  6976. * @return void
  6977. */
  6978. public function testMultiRecordForm()
  6979. {
  6980. $this->loadFixtures('Articles', 'Comments');
  6981. $articles = TableRegistry::get('Articles');
  6982. $articles->hasMany('Comments');
  6983. $comment = new Entity(['comment' => 'Value']);
  6984. $article = new Article(['comments' => [$comment]]);
  6985. $this->Form->create([$article]);
  6986. $result = $this->Form->input('0.comments.1.comment');
  6987. //@codingStandardsIgnoreStart
  6988. $expected = [
  6989. 'div' => ['class' => 'input textarea'],
  6990. 'label' => ['for' => '0-comments-1-comment'],
  6991. 'Comment',
  6992. '/label',
  6993. 'textarea' => [
  6994. 'name',
  6995. 'id' => '0-comments-1-comment',
  6996. 'rows' => 5
  6997. ],
  6998. '/textarea',
  6999. '/div'
  7000. ];
  7001. //@codingStandardsIgnoreEnd
  7002. $this->assertHtml($expected, $result);
  7003. $result = $this->Form->input('0.comments.0.comment');
  7004. //@codingStandardsIgnoreStart
  7005. $expected = [
  7006. 'div' => ['class' => 'input textarea'],
  7007. 'label' => ['for' => '0-comments-0-comment'],
  7008. 'Comment',
  7009. '/label',
  7010. 'textarea' => [
  7011. 'name',
  7012. 'id' => '0-comments-0-comment',
  7013. 'rows' => 5
  7014. ],
  7015. 'Value',
  7016. '/textarea',
  7017. '/div'
  7018. ];
  7019. //@codingStandardsIgnoreEnd
  7020. $this->assertHtml($expected, $result);
  7021. $comment->errors('comment', ['Not valid']);
  7022. $result = $this->Form->input('0.comments.0.comment');
  7023. //@codingStandardsIgnoreStart
  7024. $expected = [
  7025. 'div' => ['class' => 'input textarea error'],
  7026. 'label' => ['for' => '0-comments-0-comment'],
  7027. 'Comment',
  7028. '/label',
  7029. 'textarea' => [
  7030. 'name',
  7031. 'class' => 'form-error',
  7032. 'id' => '0-comments-0-comment',
  7033. 'rows' => 5
  7034. ],
  7035. 'Value',
  7036. '/textarea',
  7037. ['div' => ['class' => 'error-message']],
  7038. 'Not valid',
  7039. '/div',
  7040. '/div'
  7041. ];
  7042. //@codingStandardsIgnoreEnd
  7043. $this->assertHtml($expected, $result);
  7044. TableRegistry::get('Comments')
  7045. ->validator('default')
  7046. ->allowEmpty('comment', false);
  7047. $result = $this->Form->input('0.comments.1.comment');
  7048. //@codingStandardsIgnoreStart
  7049. $expected = [
  7050. 'div' => ['class' => 'input textarea required'],
  7051. 'label' => ['for' => '0-comments-1-comment'],
  7052. 'Comment',
  7053. '/label',
  7054. 'textarea' => [
  7055. 'name',
  7056. 'required' => 'required',
  7057. 'id' => '0-comments-1-comment',
  7058. 'rows' => 5
  7059. ],
  7060. '/textarea',
  7061. '/div'
  7062. ];
  7063. //@codingStandardsIgnoreEnd
  7064. $this->assertHtml($expected, $result);
  7065. }
  7066. /**
  7067. * test that some html5 inputs + FormHelper::__call() work
  7068. *
  7069. * @return void
  7070. */
  7071. public function testHtml5Inputs()
  7072. {
  7073. $result = $this->Form->email('User.email');
  7074. $expected = [
  7075. 'input' => ['type' => 'email', 'name' => 'User[email]']
  7076. ];
  7077. $this->assertHtml($expected, $result);
  7078. $result = $this->Form->search('User.query');
  7079. $expected = [
  7080. 'input' => ['type' => 'search', 'name' => 'User[query]']
  7081. ];
  7082. $this->assertHtml($expected, $result);
  7083. $result = $this->Form->search('User.query', ['value' => 'test']);
  7084. $expected = [
  7085. 'input' => ['type' => 'search', 'name' => 'User[query]', 'value' => 'test']
  7086. ];
  7087. $this->assertHtml($expected, $result);
  7088. $result = $this->Form->search('User.query', ['type' => 'text', 'value' => 'test']);
  7089. $expected = [
  7090. 'input' => ['type' => 'text', 'name' => 'User[query]', 'value' => 'test']
  7091. ];
  7092. $this->assertHtml($expected, $result);
  7093. }
  7094. /**
  7095. * Test accessing html5 inputs through input().
  7096. *
  7097. * @return void
  7098. */
  7099. public function testHtml5InputWithInput()
  7100. {
  7101. $this->Form->create();
  7102. $this->Form->templates(['inputContainer' => '{{content}}']);
  7103. $result = $this->Form->input('website', [
  7104. 'type' => 'url',
  7105. 'val' => 'http://domain.tld',
  7106. 'label' => false
  7107. ]);
  7108. $expected = [
  7109. 'input' => ['type' => 'url', 'name' => 'website', 'id' => 'website', 'value' => 'http://domain.tld']
  7110. ];
  7111. $this->assertHtml($expected, $result);
  7112. }
  7113. /**
  7114. * Test errors when field name is missing.
  7115. *
  7116. * @expectedException \Cake\Core\Exception\Exception
  7117. * @return void
  7118. */
  7119. public function testHtml5InputException()
  7120. {
  7121. $this->Form->email();
  7122. }
  7123. /**
  7124. * Tests that formhelper sets required attributes.
  7125. *
  7126. * @return void
  7127. */
  7128. public function testRequiredAttribute()
  7129. {
  7130. $this->article['required'] = [
  7131. 'title' => true,
  7132. 'body' => false,
  7133. ];
  7134. $this->Form->create($this->article);
  7135. $result = $this->Form->input('title');
  7136. $expected = [
  7137. 'div' => ['class' => 'input text required'],
  7138. 'label' => ['for' => 'title'],
  7139. 'Title',
  7140. '/label',
  7141. 'input' => [
  7142. 'type' => 'text',
  7143. 'name' => 'title',
  7144. 'id' => 'title',
  7145. 'required' => 'required',
  7146. ],
  7147. '/div'
  7148. ];
  7149. $this->assertHtml($expected, $result);
  7150. $result = $this->Form->input('title', ['required' => false]);
  7151. $this->assertNotContains('required', $result);
  7152. $result = $this->Form->input('body');
  7153. $expected = [
  7154. 'div' => ['class' => 'input text'],
  7155. 'label' => ['for' => 'body'],
  7156. 'Body',
  7157. '/label',
  7158. 'input' => [
  7159. 'type' => 'text',
  7160. 'name' => 'body',
  7161. 'id' => 'body',
  7162. ],
  7163. '/div'
  7164. ];
  7165. $this->assertHtml($expected, $result);
  7166. $result = $this->Form->input('body', ['required' => true]);
  7167. $this->assertContains('required', $result);
  7168. }
  7169. /**
  7170. * Tests that it is possible to put inputs outside of the label
  7171. *
  7172. * @return void
  7173. */
  7174. public function testInputsNotNested()
  7175. {
  7176. $this->Form->templates([
  7177. 'nestingLabel' => '{{hidden}}{{input}}<label{{attrs}}>{{text}}</label>',
  7178. 'formGroup' => '{{input}}{{label}}',
  7179. ]);
  7180. $result = $this->Form->input('foo', ['type' => 'checkbox']);
  7181. $expected = [
  7182. 'div' => ['class' => 'input checkbox'],
  7183. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7184. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7185. 'label' => ['for' => 'foo'],
  7186. 'Foo',
  7187. '/label',
  7188. '/div'
  7189. ];
  7190. $this->assertHtml($expected, $result);
  7191. $result = $this->Form->input('foo', ['type' => 'checkbox', 'label' => false]);
  7192. $expected = [
  7193. 'div' => ['class' => 'input checkbox'],
  7194. ['input' => ['type' => 'hidden', 'name' => 'foo', 'value' => '0']],
  7195. ['input' => ['type' => 'checkbox', 'name' => 'foo', 'id' => 'foo', 'value' => '1']],
  7196. '/div'
  7197. ];
  7198. $this->assertHtml($expected, $result);
  7199. $result = $this->Form->input('confirm', [
  7200. 'type' => 'radio',
  7201. 'options' => ['Y' => 'Yes', 'N' => 'No']
  7202. ]);
  7203. $expected = [
  7204. 'div' => ['class' => 'input radio'],
  7205. ['input' => ['type' => 'hidden', 'name' => 'confirm', 'value' => '']],
  7206. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-y', 'value' => 'Y']],
  7207. ['label' => ['for' => 'confirm-y']],
  7208. 'Yes',
  7209. '/label',
  7210. ['input' => ['type' => 'radio', 'name' => 'confirm', 'id' => 'confirm-n', 'value' => 'N']],
  7211. ['label' => ['for' => 'confirm-n']],
  7212. 'No',
  7213. '/label',
  7214. '<label',
  7215. 'Confirm',
  7216. '/label',
  7217. '/div',
  7218. ];
  7219. $this->assertHtml($expected, $result);
  7220. $result = $this->Form->select('category', ['1', '2'], [
  7221. 'multiple' => 'checkbox',
  7222. 'name' => 'fish',
  7223. ]);
  7224. $expected = [
  7225. 'input' => ['type' => 'hidden', 'name' => 'fish', 'value' => ''],
  7226. ['div' => ['class' => 'checkbox']],
  7227. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0']],
  7228. ['label' => ['for' => 'fish-0']],
  7229. '1',
  7230. '/label',
  7231. '/div',
  7232. ['div' => ['class' => 'checkbox']],
  7233. ['input' => ['type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1']],
  7234. ['label' => ['for' => 'fish-1']],
  7235. '2',
  7236. '/label',
  7237. '/div'
  7238. ];
  7239. $this->assertHtml($expected, $result);
  7240. }
  7241. /**
  7242. * Test that *Container templates are used by input.
  7243. *
  7244. * @return void
  7245. */
  7246. public function testInputContainerTemplates()
  7247. {
  7248. $this->Form->templates([
  7249. 'checkboxContainer' => '<div class="check">{{content}}</div>',
  7250. 'radioContainer' => '<div class="rad">{{content}}</div>',
  7251. 'radioContainerError' => '<div class="rad err">{{content}}</div>',
  7252. 'datetimeContainer' => '<div class="dt">{{content}}</div>',
  7253. ]);
  7254. $this->article['errors'] = [
  7255. 'Article' => ['published' => 'error message']
  7256. ];
  7257. $this->Form->create($this->article);
  7258. $result = $this->Form->input('accept', [
  7259. 'type' => 'checkbox'
  7260. ]);
  7261. $expected = [
  7262. 'div' => ['class' => 'check'],
  7263. ['input' => ['type' => 'hidden', 'name' => 'accept', 'value' => 0]],
  7264. 'label' => ['for' => 'accept'],
  7265. ['input' => ['id' => 'accept', 'type' => 'checkbox', 'name' => 'accept', 'value' => 1]],
  7266. 'Accept',
  7267. '/label',
  7268. '/div'
  7269. ];
  7270. $this->assertHtml($expected, $result);
  7271. $result = $this->Form->input('accept', [
  7272. 'type' => 'radio',
  7273. 'options' => ['Y', 'N']
  7274. ]);
  7275. $this->assertContains('<div class="rad">', $result);
  7276. $result = $this->Form->input('Article.published', [
  7277. 'type' => 'radio',
  7278. 'options' => ['Y', 'N']
  7279. ]);
  7280. $this->assertContains('<div class="rad err">', $result);
  7281. $result = $this->Form->input('Article.created', [
  7282. 'type' => 'datetime',
  7283. ]);
  7284. $this->assertContains('<div class="dt">', $result);
  7285. }
  7286. /**
  7287. * Test that *Container templates are used by input.
  7288. *
  7289. * @return void
  7290. */
  7291. public function testFormGroupTemplates()
  7292. {
  7293. $this->Form->templates([
  7294. 'radioFormGroup' => '<div class="radio">{{label}}{{input}}</div>',
  7295. ]);
  7296. $this->Form->create($this->article);
  7297. $result = $this->Form->input('accept', [
  7298. 'type' => 'radio',
  7299. 'options' => ['Y', 'N']
  7300. ]);
  7301. $this->assertContains('<div class="radio">', $result);
  7302. }
  7303. /**
  7304. * Test resetting templates.
  7305. *
  7306. * @return void
  7307. */
  7308. public function testResetTemplates()
  7309. {
  7310. $this->Form->templates(['input' => '<input/>']);
  7311. $this->assertEquals('<input/>', $this->Form->templater()->get('input'));
  7312. $this->assertNull($this->Form->resetTemplates());
  7313. $this->assertNotEquals('<input/>', $this->Form->templater()->get('input'));
  7314. }
  7315. /**
  7316. * Test the context method.
  7317. *
  7318. * @return void
  7319. */
  7320. public function testContext()
  7321. {
  7322. $result = $this->Form->context();
  7323. $this->assertInstanceOf('Cake\View\Form\ContextInterface', $result);
  7324. $mock = $this->getMock('Cake\View\Form\ContextInterface');
  7325. $this->assertSame($mock, $this->Form->context($mock));
  7326. $this->assertSame($mock, $this->Form->context());
  7327. }
  7328. /**
  7329. * testAutoDomId
  7330. *
  7331. * @return void
  7332. */
  7333. public function testAutoDomId()
  7334. {
  7335. $result = $this->Form->text('field', ['id' => true]);
  7336. $expected = [
  7337. 'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
  7338. ];
  7339. $this->assertHtml($expected, $result);
  7340. // Ensure id => doesn't cause problem when multiple inputs are generated.
  7341. $result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
  7342. $expected = [
  7343. 'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
  7344. ['label' => ['for' => 'field-0']],
  7345. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
  7346. 'option A',
  7347. '/label',
  7348. ['label' => ['for' => 'field-1']],
  7349. ['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
  7350. 'option B',
  7351. '/label',
  7352. ];
  7353. $this->assertHtml($expected, $result);
  7354. $result = $this->Form->select(
  7355. 'multi_field',
  7356. ['first', 'second'],
  7357. ['multiple' => 'checkbox', 'id' => true]
  7358. );
  7359. $expected = [
  7360. 'input' => [
  7361. 'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
  7362. ],
  7363. ['div' => ['class' => 'checkbox']],
  7364. ['label' => ['for' => 'multi-field-0']],
  7365. ['input' => [
  7366. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7367. 'value' => '0', 'id' => 'multi-field-0'
  7368. ]],
  7369. 'first',
  7370. '/label',
  7371. '/div',
  7372. ['div' => ['class' => 'checkbox']],
  7373. ['label' => ['for' => 'multi-field-1']],
  7374. ['input' => [
  7375. 'type' => 'checkbox', 'name' => 'multi_field[]',
  7376. 'value' => '1', 'id' => 'multi-field-1'
  7377. ]],
  7378. 'second',
  7379. '/label',
  7380. '/div',
  7381. ];
  7382. $this->assertHtml($expected, $result);
  7383. }
  7384. }