FormHelperTest.php 188 KB

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