FormHelperTest.php 172 KB

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