FormHelperTest.php 263 KB

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