FormHelperTest.php 241 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  6. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  7. *
  8. * Licensed under The MIT License
  9. * For full copyright and license information, please see the LICENSE.txt
  10. * Redistributions of files must retain the above copyright notice
  11. *
  12. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  13. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  14. * @since CakePHP(tm) v 1.2.0.4206
  15. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  16. */
  17. namespace Cake\Test\TestCase\View\Helper;
  18. use Cake\Controller\Controller;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Plugin;
  22. use Cake\Network\Request;
  23. use Cake\ORM\Entity;
  24. use Cake\ORM\Table;
  25. use Cake\ORM\TableRegistry;
  26. use Cake\Routing\Router;
  27. use Cake\TestSuite\TestCase;
  28. use Cake\Utility\Security;
  29. use Cake\View\Helper\FormHelper;
  30. use Cake\View\Helper\HtmlHelper;
  31. use Cake\View\View;
  32. /**
  33. * Test stub.
  34. */
  35. class Article extends Entity {
  36. }
  37. /**
  38. * ContactTestController class
  39. *
  40. */
  41. class ContactTestController extends Controller {
  42. /**
  43. * uses property
  44. *
  45. * @var mixed null
  46. */
  47. public $uses = null;
  48. }
  49. /**
  50. * Contact class
  51. *
  52. */
  53. class ContactsTable extends Table {
  54. /**
  55. * Default schema
  56. *
  57. * @var array
  58. */
  59. protected $_schema = array(
  60. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  61. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  62. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  66. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  67. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  68. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  69. );
  70. /**
  71. * validate property
  72. *
  73. * @var array
  74. */
  75. public $validate = array(
  76. 'non_existing' => array(),
  77. 'idontexist' => array(),
  78. 'imrequired' => array('_allowEmpty' => false, array('rule' => array('between', 5, 30))),
  79. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  80. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  81. 'imrequiredonboth' => array(
  82. 'required' => array('rule' => 'alphaNumeric'),
  83. ),
  84. 'string_required' => 'notEmpty',
  85. 'imalsorequired' => array('_allowEmpty' => false, array('rule' => 'alphaNumeric')),
  86. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  87. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  88. 'imnotrequired' => array(
  89. '_allowEmpty' => true,
  90. array('rule' => 'alphaNumeric')
  91. ),
  92. 'imalsonotrequired' => array(
  93. '_allowEmpty' => true,
  94. 'alpha' => array('rule' => 'alphaNumeric'),
  95. 'between' => array('rule' => array('between', 5, 30)),
  96. ),
  97. 'imnotrequiredeither' => array(
  98. '_allowEmpty' => true,
  99. array('rule' => array('between', 5, 30))
  100. ),
  101. 'iamrequiredalways' => array(
  102. 'email' => array('rule' => 'email'),
  103. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  104. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  105. ),
  106. 'boolean_field' => array('rule' => 'boolean')
  107. );
  108. /**
  109. * schema method
  110. *
  111. * @return void
  112. */
  113. public function setSchema($schema) {
  114. $this->_schema = $schema;
  115. }
  116. /**
  117. * hasAndBelongsToMany property
  118. *
  119. * @var array
  120. */
  121. public $hasAndBelongsToMany = array(
  122. 'ContactTag' => array(
  123. 'className' => 'Cake\Test\TestCase\View\Helper\ContactTag',
  124. 'with' => 'Cake\Test\TestCase\View\Helper\ContactTagsContact'
  125. )
  126. );
  127. /**
  128. * hasAndBelongsToMany property
  129. *
  130. * @var array
  131. */
  132. public $belongsTo = array(
  133. 'User' => array('className' => 'UserForm'
  134. ));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. */
  140. class ContactTagsContactsTable extends Table {
  141. /**
  142. * Default schema
  143. *
  144. * @var array
  145. */
  146. protected $_schema = array(
  147. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  148. 'contact_tag_id' => array(
  149. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  150. )
  151. );
  152. /**
  153. * schema method
  154. *
  155. * @return void
  156. */
  157. public function setSchema($schema) {
  158. $this->_schema = $schema;
  159. }
  160. }
  161. /**
  162. * ContactNonStandardPk class
  163. *
  164. */
  165. class ContactNonStandardPk extends ContactsTable {
  166. /**
  167. * primaryKey property
  168. *
  169. * @var string
  170. */
  171. public $primaryKey = 'pk';
  172. /**
  173. * schema method
  174. *
  175. * @return void
  176. */
  177. public function schema($field = false) {
  178. $this->_schema = parent::schema();
  179. $this->_schema['pk'] = $this->_schema['id'];
  180. unset($this->_schema['id']);
  181. return $this->_schema;
  182. }
  183. }
  184. /**
  185. * ContactTag class
  186. *
  187. */
  188. class ContactTagsTable extends Table {
  189. /**
  190. * schema definition
  191. *
  192. * @var array
  193. */
  194. protected $_schema = array(
  195. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  196. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  197. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  198. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  199. );
  200. }
  201. /**
  202. * UserForm class
  203. *
  204. */
  205. class UserFormsTable extends Table {
  206. /**
  207. * hasMany property
  208. *
  209. * @var array
  210. */
  211. public $hasMany = array(
  212. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  213. ));
  214. /**
  215. * schema definition
  216. *
  217. * @var array
  218. */
  219. protected $_schema = array(
  220. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  221. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  222. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  223. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  224. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  225. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  226. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  227. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  228. );
  229. }
  230. /**
  231. * OpenidUrl class
  232. *
  233. */
  234. class OpenidUrlsTable extends Table {
  235. /**
  236. * belongsTo property
  237. *
  238. * @var array
  239. */
  240. public $belongsTo = array('UserForm' => array(
  241. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  242. ));
  243. /**
  244. * validate property
  245. *
  246. * @var array
  247. */
  248. public $validate = array('openid_not_registered' => array());
  249. /**
  250. * schema method
  251. *
  252. * @var array
  253. */
  254. protected $_schema = array(
  255. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  256. 'user_form_id' => array(
  257. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  258. ),
  259. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  260. );
  261. /**
  262. * beforeValidate method
  263. *
  264. * @return void
  265. */
  266. public function beforeValidate($options = array()) {
  267. $this->invalidate('openid_not_registered');
  268. return true;
  269. }
  270. }
  271. /**
  272. * ValidateUser class
  273. *
  274. */
  275. class ValidateUsersTable extends Table {
  276. /**
  277. * hasOne property
  278. *
  279. * @var array
  280. */
  281. public $hasOne = array('ValidateProfile' => array(
  282. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  283. ));
  284. /**
  285. * schema method
  286. *
  287. * @var array
  288. */
  289. protected $_schema = array(
  290. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  291. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  292. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  293. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  294. 'cost_decimal' => array('type' => 'decimal', 'null' => false, 'length' => '6,3'),
  295. 'ratio' => array('type' => 'decimal', 'null' => false, 'length' => '10,6'),
  296. 'population' => array('type' => 'decimal', 'null' => false, 'length' => '15,0'),
  297. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  298. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  299. );
  300. /**
  301. * beforeValidate method
  302. *
  303. * @param array $options
  304. * @return void
  305. */
  306. public function beforeValidate($options = array()) {
  307. $this->invalidate('email');
  308. return false;
  309. }
  310. }
  311. /**
  312. * ValidateProfile class
  313. *
  314. */
  315. class ValidateProfilesTable extends Table {
  316. /**
  317. * useTable property
  318. *
  319. * @var boolean
  320. */
  321. public $useTable = false;
  322. /**
  323. * schema property
  324. *
  325. * @var array
  326. */
  327. protected $_schema = array(
  328. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  329. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  330. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  331. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  332. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  333. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  334. );
  335. /**
  336. * hasOne property
  337. *
  338. * @var array
  339. */
  340. public $hasOne = array('ValidateItem' => array(
  341. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  342. ));
  343. /**
  344. * belongsTo property
  345. *
  346. * @var array
  347. */
  348. public $belongsTo = array('ValidateUser' => array(
  349. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  350. ));
  351. /**
  352. * beforeValidate method
  353. *
  354. * @return void
  355. */
  356. public function beforeValidate($options = array()) {
  357. $this->invalidate('full_name');
  358. $this->invalidate('city');
  359. return false;
  360. }
  361. }
  362. /**
  363. * ValidateItem class
  364. *
  365. */
  366. class ValidateItemsTable extends Table {
  367. /**
  368. * schema property
  369. *
  370. * @var array
  371. */
  372. protected $_schema = array(
  373. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  374. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  375. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  376. 'description' => array(
  377. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  378. ),
  379. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  380. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  381. );
  382. /**
  383. * belongsTo property
  384. *
  385. * @var array
  386. */
  387. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  388. /**
  389. * beforeValidate method
  390. *
  391. * @return void
  392. */
  393. public function beforeValidate($options = array()) {
  394. $this->invalidate('description');
  395. return false;
  396. }
  397. }
  398. /**
  399. * TestMail class
  400. *
  401. */
  402. class TestMailsTable extends Table {
  403. }
  404. /**
  405. * FormHelperTest class
  406. *
  407. * @property FormHelper $Form
  408. */
  409. class FormHelperTest extends TestCase {
  410. /**
  411. * Fixtures to be used
  412. *
  413. * @var array
  414. */
  415. public $fixtures = array('core.article');
  416. /**
  417. * Do not load the fixtures by default
  418. *
  419. * @var boolean
  420. */
  421. public $autoFixtures = false;
  422. /**
  423. * setUp method
  424. *
  425. * @return void
  426. */
  427. public function setUp() {
  428. parent::setUp();
  429. Configure::write('Config.language', 'eng');
  430. Configure::write('App.base', '');
  431. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  432. Configure::delete('Asset');
  433. $this->View = new View(null);
  434. $this->Form = new FormHelper($this->View);
  435. $this->Form->Html = new HtmlHelper($this->View);
  436. $this->Form->request = new Request('articles/add');
  437. $this->Form->request->here = '/articles/add';
  438. $this->Form->request['controller'] = 'articles';
  439. $this->Form->request['action'] = 'add';
  440. $this->Form->request->webroot = '';
  441. $this->Form->request->base = '';
  442. $this->dateRegex = array(
  443. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  444. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  445. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  446. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  447. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  448. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  449. );
  450. $this->article = [
  451. 'schema' => [
  452. 'id' => ['type' => 'integer'],
  453. 'author_id' => ['type' => 'integer', 'null' => true],
  454. 'title' => ['type' => 'string', 'null' => true],
  455. 'body' => 'text',
  456. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  457. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  458. ],
  459. 'required' => [
  460. 'author_id' => true,
  461. 'title' => true,
  462. ]
  463. ];
  464. Configure::write('Security.salt', 'foo!');
  465. Router::connect('/:controller', array('action' => 'index'));
  466. Router::connect('/:controller/:action/*');
  467. }
  468. /**
  469. * tearDown method
  470. *
  471. * @return void
  472. */
  473. public function tearDown() {
  474. parent::tearDown();
  475. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  476. }
  477. /**
  478. * Test registering a new widget class and rendering it.
  479. *
  480. * @return void
  481. */
  482. public function testAddWidgetAndRenderWidget() {
  483. $data = [
  484. 'val' => 1
  485. ];
  486. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  487. $this->assertNull($this->Form->addWidget('test', $mock));
  488. $mock->expects($this->once())
  489. ->method('render')
  490. ->with($data)
  491. ->will($this->returnValue('HTML'));
  492. $result = $this->Form->widget('test', $data);
  493. $this->assertEquals('HTML', $result);
  494. }
  495. /**
  496. * Test registering an invalid widget class.
  497. *
  498. * @expectedException \RuntimeException
  499. * @return void
  500. */
  501. public function testAddWidgetInvalid() {
  502. $mock = new \StdClass();
  503. $this->Form->addWidget('test', $mock);
  504. $this->Form->widget('test');
  505. }
  506. /**
  507. * Test adding a new context class.
  508. *
  509. * @return void
  510. */
  511. public function testAddContextProvider() {
  512. $context = 'My data';
  513. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  514. $this->assertInstanceOf('Cake\Network\Request', $request);
  515. $this->assertEquals($context, $data['entity']);
  516. return $this->getMock('Cake\View\Form\ContextInterface');
  517. });
  518. $this->Form->create($context);
  519. }
  520. /**
  521. * Test adding an invalid context class.
  522. *
  523. * @expectedException RuntimeException
  524. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  525. * @return void
  526. */
  527. public function testAddContextProviderInvalid() {
  528. $context = 'My data';
  529. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  530. return new \StdClass();
  531. });
  532. $this->Form->create($context);
  533. }
  534. /**
  535. * Provides context options for create().
  536. *
  537. * @return array
  538. */
  539. public function contextSelectionProvider() {
  540. $entity = new Article();
  541. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  542. $data = [
  543. 'schema' => [
  544. 'title' => ['type' => 'string']
  545. ]
  546. ];
  547. return [
  548. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  549. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  550. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  551. 'none' => [null, 'Cake\View\Form\NullContext'],
  552. 'false' => [false, 'Cake\View\Form\NullContext'],
  553. ];
  554. }
  555. /**
  556. * Test default context selection in create()
  557. *
  558. * @dataProvider contextSelectionProvider
  559. * @return void
  560. */
  561. public function testCreateContextSelectionBuiltIn($data, $class) {
  562. $this->loadFixtures('Article');
  563. $this->Form->create($data);
  564. $this->assertInstanceOf($class, $this->Form->context());
  565. }
  566. /**
  567. * Data provider for type option.
  568. *
  569. * @return array
  570. */
  571. public static function requestTypeProvider() {
  572. return [
  573. // type, method, override
  574. ['post', 'post', 'POST'],
  575. ['put', 'post', 'PUT'],
  576. ['patch', 'post', 'PATCH'],
  577. ['delete', 'post', 'DELETE'],
  578. ];
  579. }
  580. /**
  581. * Test creating file forms.
  582. *
  583. * @return void
  584. */
  585. public function testCreateFile() {
  586. $encoding = strtolower(Configure::read('App.encoding'));
  587. $result = $this->Form->create(false, array('type' => 'file'));
  588. $expected = array(
  589. 'form' => array(
  590. 'method' => 'post', 'action' => '/articles/add',
  591. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  592. ),
  593. 'div' => array('style' => 'display:none;'),
  594. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  595. '/div'
  596. );
  597. $this->assertTags($result, $expected);
  598. }
  599. /**
  600. * Test creating GET forms.
  601. *
  602. * @return void
  603. */
  604. public function testCreateGet() {
  605. $encoding = strtolower(Configure::read('App.encoding'));
  606. $result = $this->Form->create(false, array('type' => 'get'));
  607. $expected = array('form' => array(
  608. 'method' => 'get', 'action' => '/articles/add',
  609. 'accept-charset' => $encoding
  610. ));
  611. $this->assertTags($result, $expected);
  612. }
  613. /**
  614. * test the create() method
  615. *
  616. * @dataProvider requestTypeProvider
  617. * @return void
  618. */
  619. public function testCreateTypeOptions($type, $method, $override) {
  620. $encoding = strtolower(Configure::read('App.encoding'));
  621. $result = $this->Form->create(false, array('type' => $type));
  622. $expected = array(
  623. 'form' => array(
  624. 'method' => $method, 'action' => '/articles/add',
  625. 'accept-charset' => $encoding
  626. ),
  627. 'div' => array('style' => 'display:none;'),
  628. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  629. '/div'
  630. );
  631. $this->assertTags($result, $expected);
  632. }
  633. /**
  634. * Test opening a form for an update operation.
  635. *
  636. * @return void
  637. */
  638. public function testCreateUpdateForm() {
  639. $encoding = strtolower(Configure::read('App.encoding'));
  640. $this->Form->request->here = '/articles/edit/1';
  641. $this->Form->request['action'] = 'edit';
  642. $this->article['defaults']['id'] = 1;
  643. $result = $this->Form->create($this->article);
  644. $expected = array(
  645. 'form' => array(
  646. 'method' => 'post', 'action' => '/articles/edit/1',
  647. 'accept-charset' => $encoding
  648. ),
  649. 'div' => array('style' => 'display:none;'),
  650. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  651. '/div'
  652. );
  653. $this->assertTags($result, $expected);
  654. }
  655. /**
  656. * Test the onsubmit option for create()
  657. *
  658. * @return void
  659. */
  660. public function testCreateOnSubmit() {
  661. $this->Form->request->data = [];
  662. $this->Form->request['controller'] = 'articles';
  663. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  664. $expected = array(
  665. 'form' => array(
  666. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  667. 'accept-charset' => 'utf-8'
  668. ),
  669. 'div' => array('style' => 'display:none;'),
  670. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  671. '/div'
  672. );
  673. $this->assertTags($result, $expected);
  674. $this->Form->request->data = [];
  675. $this->Form->request['controller'] = 'articles';
  676. $result = $this->Form->create($this->article, array(
  677. 'url' => array('action' => 'index', 'param'),
  678. 'default' => false,
  679. 'onsubmit' => 'someFunction();'
  680. ));
  681. $expected = array(
  682. 'form' => array(
  683. 'method' => 'post',
  684. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  685. 'action' => '/articles/index/param',
  686. 'accept-charset' => 'utf-8'
  687. ),
  688. 'div' => array('style' => 'display:none;'),
  689. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  690. '/div'
  691. );
  692. $this->assertTags($result, $expected);
  693. }
  694. /**
  695. * test create() with automatic url generation
  696. *
  697. * @return void
  698. */
  699. public function testCreateAutoUrl() {
  700. $encoding = strtolower(Configure::read('App.encoding'));
  701. $this->Form->request['action'] = 'delete';
  702. $this->Form->request->here = '/articles/delete/10';
  703. $this->Form->request->base = '';
  704. $result = $this->Form->create($this->article);
  705. $expected = array(
  706. 'form' => array(
  707. 'method' => 'post', 'action' => '/articles/delete/10',
  708. 'accept-charset' => $encoding
  709. ),
  710. 'div' => array('style' => 'display:none;'),
  711. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  712. '/div'
  713. );
  714. $this->assertTags($result, $expected);
  715. $this->article['defaults'] = ['id' => 1];
  716. $this->Form->request->here = '/articles/edit/1';
  717. $this->Form->request['action'] = 'delete';
  718. $result = $this->Form->create($this->article, ['action' => 'edit']);
  719. $expected = array(
  720. 'form' => array(
  721. 'method' => 'post',
  722. 'action' => '/articles/edit/1',
  723. 'accept-charset' => $encoding
  724. ),
  725. 'div' => array('style' => 'display:none;'),
  726. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  727. '/div'
  728. );
  729. $this->assertTags($result, $expected);
  730. $this->Form->request['action'] = 'add';
  731. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  732. $expected = array(
  733. 'form' => array(
  734. 'method' => 'post',
  735. 'action' => '/articles/publish/1',
  736. 'accept-charset' => $encoding
  737. ),
  738. 'div' => array('style' => 'display:none;'),
  739. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  740. '/div'
  741. );
  742. $this->assertTags($result, $expected);
  743. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  744. $expected = array(
  745. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  746. 'div' => array('style' => 'display:none;'),
  747. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  748. '/div'
  749. );
  750. $this->assertTags($result, $expected);
  751. $this->Form->request['controller'] = 'pages';
  752. $result = $this->Form->create($this->article, array('action' => 'signup'));
  753. $expected = array(
  754. 'form' => array(
  755. 'method' => 'post', 'action' => '/pages/signup/1',
  756. 'accept-charset' => $encoding
  757. ),
  758. 'div' => array('style' => 'display:none;'),
  759. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  760. '/div'
  761. );
  762. $this->assertTags($result, $expected);
  763. }
  764. /**
  765. * test create() with a custom route
  766. *
  767. * @return void
  768. */
  769. public function testCreateCustomRoute() {
  770. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  771. $encoding = strtolower(Configure::read('App.encoding'));
  772. $this->Form->request['controller'] = 'users';
  773. $result = $this->Form->create(false, array('action' => 'login'));
  774. $expected = array(
  775. 'form' => array(
  776. 'method' => 'post', 'action' => '/login',
  777. 'accept-charset' => $encoding
  778. ),
  779. 'div' => array('style' => 'display:none;'),
  780. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  781. '/div'
  782. );
  783. $this->assertTags($result, $expected);
  784. }
  785. /**
  786. * test that inputDefaults are stored and used.
  787. *
  788. * @return void
  789. */
  790. public function testCreateWithInputDefaults() {
  791. $this->markTestIncomplete('Need to revisit once models work again.');
  792. $this->Form->create('User', array(
  793. 'inputDefaults' => array(
  794. 'div' => false,
  795. 'label' => false,
  796. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  797. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  798. )
  799. ));
  800. $result = $this->Form->input('username');
  801. $expected = array(
  802. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  803. );
  804. $this->assertTags($result, $expected);
  805. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  806. $expected = array(
  807. 'div' => array('class' => 'input text'),
  808. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  809. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  810. '/div'
  811. );
  812. $this->assertTags($result, $expected);
  813. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  814. $expected = array(
  815. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  816. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  817. );
  818. $this->assertTags($result, $expected);
  819. $this->Form->create('User', array(
  820. 'inputDefaults' => array(
  821. 'div' => false,
  822. 'label' => array('class' => 'nice', 'for' => 'changed'),
  823. )
  824. ));
  825. $result = $this->Form->input('username', array('div' => true));
  826. $expected = array(
  827. 'div' => array('class' => 'input text'),
  828. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  829. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  830. '/div'
  831. );
  832. $this->assertTags($result, $expected);
  833. }
  834. /**
  835. * test automatic accept-charset overriding
  836. *
  837. * @return void
  838. */
  839. public function testCreateWithAcceptCharset() {
  840. $result = $this->Form->create($this->article, array(
  841. 'type' => 'post', 'action' => 'index', 'encoding' => 'iso-8859-1'
  842. )
  843. );
  844. $expected = array(
  845. 'form' => array(
  846. 'method' => 'post', 'action' => '/articles',
  847. 'accept-charset' => 'iso-8859-1'
  848. ),
  849. 'div' => array('style' => 'display:none;'),
  850. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  851. '/div'
  852. );
  853. $this->assertTags($result, $expected);
  854. }
  855. /**
  856. * Test base form URL when url param is passed with multiple parameters (&)
  857. *
  858. */
  859. public function testCreateQuerystringrequest() {
  860. $encoding = strtolower(Configure::read('App.encoding'));
  861. $result = $this->Form->create($this->article, array(
  862. 'type' => 'post',
  863. 'escape' => false,
  864. 'url' => array(
  865. 'controller' => 'controller',
  866. 'action' => 'action',
  867. '?' => array('param1' => 'value1', 'param2' => 'value2')
  868. )
  869. ));
  870. $expected = array(
  871. 'form' => array(
  872. 'method' => 'post',
  873. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  874. 'accept-charset' => $encoding
  875. ),
  876. 'div' => array('style' => 'display:none;'),
  877. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  878. '/div'
  879. );
  880. $this->assertTags($result, $expected);
  881. $result = $this->Form->create($this->article, array(
  882. 'type' => 'post',
  883. 'url' => array(
  884. 'controller' => 'controller',
  885. 'action' => 'action',
  886. '?' => array('param1' => 'value1', 'param2' => 'value2')
  887. )
  888. ));
  889. $this->assertTags($result, $expected);
  890. }
  891. /**
  892. * test that create() doesn't cause errors by multiple id's being in the primary key
  893. * as could happen with multiple select or checkboxes.
  894. *
  895. * @return void
  896. */
  897. public function testCreateWithMultipleIdInData() {
  898. $encoding = strtolower(Configure::read('App.encoding'));
  899. $this->Form->request->data['Article']['id'] = array(1, 2);
  900. $result = $this->Form->create($this->article);
  901. $expected = array(
  902. 'form' => array(
  903. 'method' => 'post',
  904. 'action' => '/articles/add',
  905. 'accept-charset' => $encoding
  906. ),
  907. 'div' => array('style' => 'display:none;'),
  908. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  909. '/div'
  910. );
  911. $this->assertTags($result, $expected);
  912. }
  913. /**
  914. * test that create() doesn't add in extra passed params.
  915. *
  916. * @return void
  917. */
  918. public function testCreatePassedArgs() {
  919. $encoding = strtolower(Configure::read('App.encoding'));
  920. $this->Form->request->data['Article']['id'] = 1;
  921. $result = $this->Form->create($this->article, array(
  922. 'type' => 'post',
  923. 'escape' => false,
  924. 'url' => array(
  925. 'action' => 'edit',
  926. 'myparam'
  927. )
  928. ));
  929. $expected = array(
  930. 'form' => array(
  931. 'method' => 'post',
  932. 'action' => '/articles/edit/myparam',
  933. 'accept-charset' => $encoding
  934. ),
  935. 'div' => array('style' => 'display:none;'),
  936. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  937. '/div'
  938. );
  939. $this->assertTags($result, $expected);
  940. }
  941. /**
  942. * test creating a get form, and get form inputs.
  943. *
  944. * @return void
  945. */
  946. public function testGetFormCreate() {
  947. $encoding = strtolower(Configure::read('App.encoding'));
  948. $result = $this->Form->create($this->article, array('type' => 'get'));
  949. $this->assertTags($result, array('form' => array(
  950. 'method' => 'get', 'action' => '/articles/add',
  951. 'accept-charset' => $encoding
  952. )));
  953. $result = $this->Form->text('title');
  954. $this->assertTags($result, array('input' => array(
  955. 'name' => 'title', 'type' => 'text', 'required' => 'required'
  956. )));
  957. $result = $this->Form->password('password');
  958. $this->assertTags($result, array('input' => array(
  959. 'name' => 'password', 'type' => 'password'
  960. )));
  961. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  962. $result = $this->Form->text('user_form');
  963. $this->assertTags($result, array('input' => array(
  964. 'name' => 'user_form', 'type' => 'text'
  965. )));
  966. }
  967. /**
  968. * test get form, and inputs when the model param is false
  969. *
  970. * @return void
  971. */
  972. public function testGetFormWithFalseModel() {
  973. $encoding = strtolower(Configure::read('App.encoding'));
  974. $this->Form->request['controller'] = 'contact_test';
  975. $result = $this->Form->create(false, array(
  976. 'type' => 'get', 'url' => array('controller' => 'contact_test')
  977. ));
  978. $expected = array('form' => array(
  979. 'method' => 'get', 'action' => '/contact_test/add',
  980. 'accept-charset' => $encoding
  981. ));
  982. $this->assertTags($result, $expected);
  983. $result = $this->Form->text('reason');
  984. $expected = array(
  985. 'input' => array('type' => 'text', 'name' => 'reason')
  986. );
  987. $this->assertTags($result, $expected);
  988. }
  989. /**
  990. * testFormCreateWithSecurity method
  991. *
  992. * Test form->create() with security key.
  993. *
  994. * @return void
  995. */
  996. public function testCreateWithSecurity() {
  997. $this->Form->request->params['_csrfToken'] = 'testKey';
  998. $encoding = strtolower(Configure::read('App.encoding'));
  999. $result = $this->Form->create($this->article, [
  1000. 'url' => '/articles/publish',
  1001. ]);
  1002. $expected = array(
  1003. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  1004. 'div' => array('style' => 'display:none;'),
  1005. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1006. array('input' => array(
  1007. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  1008. )),
  1009. '/div'
  1010. );
  1011. $this->assertTags($result, $expected);
  1012. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1013. $expected['form']['id'] = 'MyForm';
  1014. $this->assertTags($result, $expected);
  1015. }
  1016. /**
  1017. * testFormCreateGetNoSecurity method
  1018. *
  1019. * Test form->create() with no security key as its a get form
  1020. *
  1021. * @return void
  1022. */
  1023. public function testCreateEndGetNoSecurity() {
  1024. $this->Form->request->params['_csrfToken'] = 'testKey';
  1025. $encoding = strtolower(Configure::read('App.encoding'));
  1026. $article = new Article();
  1027. $result = $this->Form->create($article, [
  1028. 'type' => 'get',
  1029. 'url' => '/contacts/add'
  1030. ]);
  1031. $this->assertNotContains('testKey', $result);
  1032. $result = $this->Form->end('Save');
  1033. $this->assertNotContains('testKey', $result);
  1034. }
  1035. /**
  1036. * test that create() clears the fields property so it starts fresh
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCreateClearingFields() {
  1041. $this->Form->fields = array('model_id');
  1042. $this->Form->create($this->article);
  1043. $this->assertEquals(array(), $this->Form->fields);
  1044. }
  1045. /**
  1046. * Tests form hash generation with model-less data
  1047. *
  1048. * @return void
  1049. */
  1050. public function testValidateHashNoModel() {
  1051. $this->Form->request->params['_Token'] = 'foo';
  1052. $result = $this->Form->secure(array('anything'));
  1053. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1054. }
  1055. /**
  1056. * Tests that models with identical field names get resolved properly
  1057. *
  1058. * @return void
  1059. */
  1060. public function testDuplicateFieldNameResolution() {
  1061. $this->markTestIncomplete('Need to revisit once models work again.');
  1062. $result = $this->Form->create('ValidateUser');
  1063. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  1064. $result = $this->Form->input('ValidateItem.name');
  1065. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  1066. $result = $this->Form->input('ValidateUser.name');
  1067. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  1068. $this->assertContains('name="ValidateUser[name]"', $result);
  1069. $this->assertContains('type="text"', $result);
  1070. $result = $this->Form->input('ValidateItem.name');
  1071. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  1072. $this->assertContains('name="ValidateItem[name]"', $result);
  1073. $this->assertContains('<textarea', $result);
  1074. $result = $this->Form->input('name');
  1075. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  1076. $this->assertContains('name="ValidateUser[name]"', $result);
  1077. $this->assertContains('type="text"', $result);
  1078. }
  1079. /**
  1080. * Tests that hidden fields generated for checkboxes don't get locked
  1081. *
  1082. * @return void
  1083. */
  1084. public function testNoCheckboxLocking() {
  1085. $this->Form->request->params['_Token'] = 'foo';
  1086. $this->assertSame([], $this->Form->fields);
  1087. $this->Form->checkbox('check', array('value' => '1'));
  1088. $this->assertSame($this->Form->fields, array('check'));
  1089. }
  1090. /**
  1091. * testFormSecurityFields method
  1092. *
  1093. * Test generation of secure form hash generation.
  1094. *
  1095. * @return void
  1096. */
  1097. public function testFormSecurityFields() {
  1098. $this->markTestIncomplete('Need to revisit once models work again.');
  1099. $key = 'testKey';
  1100. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  1101. $this->Form->request->params['_csrfToken'] = $key;
  1102. $result = $this->Form->secure($fields);
  1103. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  1104. $hash .= ':' . 'Model.valid';
  1105. $hash = urlencode($hash);
  1106. $expected = array(
  1107. 'div' => array('style' => 'display:none;'),
  1108. array('input' => array(
  1109. 'type' => 'hidden', 'name' => '_Token[fields]',
  1110. 'value' => $hash
  1111. )),
  1112. array('input' => array(
  1113. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1114. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1115. )),
  1116. '/div'
  1117. );
  1118. $this->assertTags($result, $expected);
  1119. $path = CAKE . 'Test/TestApp/Config/';
  1120. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  1121. $result = $this->Form->secure($fields);
  1122. $expected = array(
  1123. 'div' => array('class' => 'hidden'),
  1124. array('input' => array(
  1125. 'type' => 'hidden', 'name' => '_Token[fields]',
  1126. 'value' => $hash
  1127. )),
  1128. array('input' => array(
  1129. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1130. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1131. )),
  1132. '/div'
  1133. );
  1134. $this->assertTags($result, $expected);
  1135. }
  1136. /**
  1137. * Tests correct generation of number fields for double and float fields
  1138. *
  1139. * @return void
  1140. */
  1141. public function testTextFieldGenerationForFloats() {
  1142. $this->markTestIncomplete('Need to revisit once models work again.');
  1143. $this->article['schema'] = [
  1144. 'foo' => [
  1145. 'type' => 'float',
  1146. 'null' => false,
  1147. 'default' => null,
  1148. 'length' => 10
  1149. ]
  1150. ];
  1151. $this->Form->create('Contact');
  1152. $result = $this->Form->input('foo');
  1153. $expected = array(
  1154. 'div' => array('class' => 'input number'),
  1155. 'label' => array('for' => 'ContactFoo'),
  1156. 'Foo',
  1157. '/label',
  1158. array('input' => array(
  1159. 'type' => 'number',
  1160. 'name' => 'Contact[foo]',
  1161. 'id' => 'ContactFoo',
  1162. 'step' => 'any'
  1163. )),
  1164. '/div'
  1165. );
  1166. $this->assertTags($result, $expected);
  1167. $result = $this->Form->input('foo', array('step' => 0.5));
  1168. $expected = array(
  1169. 'div' => array('class' => 'input number'),
  1170. 'label' => array('for' => 'ContactFoo'),
  1171. 'Foo',
  1172. '/label',
  1173. array('input' => array(
  1174. 'type' => 'number',
  1175. 'name' => 'Contact[foo]',
  1176. 'id' => 'ContactFoo',
  1177. 'step' => '0.5'
  1178. )),
  1179. '/div'
  1180. );
  1181. $this->assertTags($result, $expected);
  1182. }
  1183. /**
  1184. * Tests correct generation of number fields for integer fields
  1185. *
  1186. * @return void
  1187. */
  1188. public function testTextFieldTypeNumberGenerationForIntegers() {
  1189. $this->markTestIncomplete('Need to revisit once models work again.');
  1190. $model->setSchema(array('foo' => array(
  1191. 'type' => 'integer',
  1192. 'null' => false,
  1193. 'default' => null,
  1194. 'length' => null
  1195. )));
  1196. $this->Form->create('Contact');
  1197. $result = $this->Form->input('foo');
  1198. $expected = array(
  1199. 'div' => array('class' => 'input number'),
  1200. 'label' => array('for' => 'ContactFoo'),
  1201. 'Foo',
  1202. '/label',
  1203. array('input' => array(
  1204. 'type' => 'number', 'name' => 'Contact[foo]',
  1205. 'id' => 'ContactFoo'
  1206. )),
  1207. '/div'
  1208. );
  1209. $this->assertTags($result, $expected);
  1210. }
  1211. /**
  1212. * Tests correct generation of file upload fields for binary fields
  1213. *
  1214. * @return void
  1215. */
  1216. public function testFileUploadFieldTypeGenerationForBinaries() {
  1217. $this->markTestIncomplete('Need to revisit once models work again.');
  1218. $model->setSchema(array('foo' => array(
  1219. 'type' => 'binary',
  1220. 'null' => false,
  1221. 'default' => null,
  1222. 'length' => 1024
  1223. )));
  1224. $this->Form->create('Contact');
  1225. $result = $this->Form->input('foo');
  1226. $expected = array(
  1227. 'div' => array('class' => 'input file'),
  1228. 'label' => array('for' => 'ContactFoo'),
  1229. 'Foo',
  1230. '/label',
  1231. array('input' => array(
  1232. 'type' => 'file', 'name' => 'data[Contact][foo]',
  1233. 'id' => 'ContactFoo'
  1234. )),
  1235. '/div'
  1236. );
  1237. $this->assertTags($result, $expected);
  1238. }
  1239. /**
  1240. * testFormSecurityMultipleFields method
  1241. *
  1242. * Test secure() with multiple row form. Ensure hash is correct.
  1243. *
  1244. * @return void
  1245. */
  1246. public function testFormSecurityMultipleFields() {
  1247. $this->Form->request->params['_Token'] = 'foo';
  1248. $fields = array(
  1249. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1250. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1251. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1252. );
  1253. $result = $this->Form->secure($fields);
  1254. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1255. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1256. $expected = array(
  1257. 'div' => array('style' => 'display:none;'),
  1258. array('input' => array(
  1259. 'type' => 'hidden', 'name' => '_Token[fields]',
  1260. 'value' => $hash
  1261. )),
  1262. array('input' => array(
  1263. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1264. 'value' => ''
  1265. )),
  1266. '/div'
  1267. );
  1268. $this->assertTags($result, $expected);
  1269. }
  1270. /**
  1271. * testFormSecurityMultipleSubmitButtons
  1272. *
  1273. * test form submit generation and ensure that _Token is only created on end()
  1274. *
  1275. * @return void
  1276. */
  1277. public function testFormSecurityMultipleSubmitButtons() {
  1278. $this->markTestIncomplete('Need to revisit once models work again.');
  1279. $key = 'testKey';
  1280. $this->Form->request->params['_csrfToken'] = $key;
  1281. $this->Form->create('Addresses');
  1282. $this->Form->input('Address.title');
  1283. $this->Form->input('Address.first_name');
  1284. $result = $this->Form->submit('Save', array('name' => 'save'));
  1285. $expected = array(
  1286. 'div' => array('class' => 'submit'),
  1287. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1288. '/div',
  1289. );
  1290. $this->assertTags($result, $expected);
  1291. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1292. $expected = array(
  1293. 'div' => array('class' => 'submit'),
  1294. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1295. '/div',
  1296. );
  1297. $this->assertTags($result, $expected);
  1298. $result = $this->Form->end(null);
  1299. $expected = array(
  1300. 'div' => array('style' => 'display:none;'),
  1301. array('input' => array(
  1302. 'type' => 'hidden', 'name' => '_Token[fields]',
  1303. 'value' => 'preg:/.+/'
  1304. )),
  1305. array('input' => array(
  1306. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1307. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  1308. )),
  1309. '/div'
  1310. );
  1311. $this->assertTags($result, $expected);
  1312. }
  1313. /**
  1314. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1315. *
  1316. * @return void
  1317. */
  1318. public function testSecurityButtonNestedNamed() {
  1319. $key = 'testKey';
  1320. $this->Form->request->params['_csrfToken'] = $key;
  1321. $this->Form->create('Addresses');
  1322. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1323. $result = $this->Form->unlockField();
  1324. $this->assertEquals(array('Address.button'), $result);
  1325. }
  1326. /**
  1327. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1328. *
  1329. * @return void
  1330. */
  1331. public function testSecuritySubmitNestedNamed() {
  1332. $this->Form->request->params['_Token'] = 'testKey';
  1333. $this->Form->create($this->article);
  1334. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1335. $result = $this->Form->unlockField();
  1336. $this->assertEquals(array('Address.button'), $result);
  1337. }
  1338. /**
  1339. * Test that the correct fields are unlocked for image submits with no names.
  1340. *
  1341. * @return void
  1342. */
  1343. public function testSecuritySubmitImageNoName() {
  1344. $key = 'testKey';
  1345. $this->Form->request->params['_Token'] = 'testKey';
  1346. $this->Form->create(false);
  1347. $result = $this->Form->submit('save.png');
  1348. $expected = array(
  1349. 'div' => array('class' => 'submit'),
  1350. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1351. '/div'
  1352. );
  1353. $this->assertTags($result, $expected);
  1354. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1355. }
  1356. /**
  1357. * Test that the correct fields are unlocked for image submits with names.
  1358. *
  1359. * @return void
  1360. */
  1361. public function testSecuritySubmitImageName() {
  1362. $this->Form->request->params['_Token'] = 'testKey';
  1363. $this->Form->create(null);
  1364. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1365. $expected = array(
  1366. 'div' => array('class' => 'submit'),
  1367. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1368. '/div'
  1369. );
  1370. $this->assertTags($result, $expected);
  1371. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1372. }
  1373. /**
  1374. * testFormSecurityMultipleInputFields method
  1375. *
  1376. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1377. *
  1378. * @return void
  1379. */
  1380. public function testFormSecurityMultipleInputFields() {
  1381. $this->markTestIncomplete('Need to revisit once models work again.');
  1382. $key = 'testKey';
  1383. $this->Form->request->params['_csrfToken'] = $key;
  1384. $this->Form->create('Addresses');
  1385. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1386. $this->Form->input('Addresses.0.title');
  1387. $this->Form->input('Addresses.0.first_name');
  1388. $this->Form->input('Addresses.0.last_name');
  1389. $this->Form->input('Addresses.0.address');
  1390. $this->Form->input('Addresses.0.city');
  1391. $this->Form->input('Addresses.0.phone');
  1392. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1393. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1394. $this->Form->input('Addresses.1.title');
  1395. $this->Form->input('Addresses.1.first_name');
  1396. $this->Form->input('Addresses.1.last_name');
  1397. $this->Form->input('Addresses.1.address');
  1398. $this->Form->input('Addresses.1.city');
  1399. $this->Form->input('Addresses.1.phone');
  1400. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1401. $result = $this->Form->secure($this->Form->fields);
  1402. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1403. $expected = array(
  1404. 'div' => array('style' => 'display:none;'),
  1405. array('input' => array(
  1406. 'type' => 'hidden', 'name' => '_Token[fields]',
  1407. 'value' => $hash
  1408. )),
  1409. array('input' => array(
  1410. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1411. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1412. )),
  1413. '/div'
  1414. );
  1415. $this->assertTags($result, $expected);
  1416. }
  1417. /**
  1418. * Test form security with Model.field.0 style inputs
  1419. *
  1420. * @return void
  1421. */
  1422. public function testFormSecurityArrayFields() {
  1423. $this->markTestIncomplete('Need to revisit once models work again.');
  1424. $key = 'testKey';
  1425. $this->Form->request->params['_csrfToken'] = $key;
  1426. $this->Form->create('Address');
  1427. $this->Form->input('Address.primary.1');
  1428. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1429. $this->Form->input('Address.secondary.1.0');
  1430. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1431. }
  1432. /**
  1433. * testFormSecurityMultipleInputDisabledFields method
  1434. *
  1435. * test secure form generation with multiple records and disabled fields.
  1436. *
  1437. * @return void
  1438. */
  1439. public function testFormSecurityMultipleInputDisabledFields() {
  1440. $this->markTestIncomplete('Need to revisit once models work again.');
  1441. $key = 'testKey';
  1442. $this->Form->request->params['_csrfToken'] = $key;
  1443. $this->Form->request->params['_Token'] = array(
  1444. 'unlockedFields' => array('first_name', 'address')
  1445. );
  1446. $this->Form->create();
  1447. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1448. $this->Form->input('Addresses.0.title');
  1449. $this->Form->input('Addresses.0.first_name');
  1450. $this->Form->input('Addresses.0.last_name');
  1451. $this->Form->input('Addresses.0.address');
  1452. $this->Form->input('Addresses.0.city');
  1453. $this->Form->input('Addresses.0.phone');
  1454. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1455. $this->Form->input('Addresses.1.title');
  1456. $this->Form->input('Addresses.1.first_name');
  1457. $this->Form->input('Addresses.1.last_name');
  1458. $this->Form->input('Addresses.1.address');
  1459. $this->Form->input('Addresses.1.city');
  1460. $this->Form->input('Addresses.1.phone');
  1461. $result = $this->Form->secure($this->Form->fields);
  1462. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1463. $expected = array(
  1464. 'div' => array('style' => 'display:none;'),
  1465. array('input' => array(
  1466. 'type' => 'hidden', 'name' => '_Token[fields]',
  1467. 'value' => $hash
  1468. )),
  1469. array('input' => array(
  1470. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1471. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1472. )),
  1473. '/div'
  1474. );
  1475. $this->assertTags($result, $expected);
  1476. }
  1477. /**
  1478. * testFormSecurityInputDisabledFields method
  1479. *
  1480. * Test single record form with disabled fields.
  1481. *
  1482. * @return void
  1483. */
  1484. public function testFormSecurityInputUnlockedFields() {
  1485. $this->markTestIncomplete('Need to revisit once models work again.');
  1486. $key = 'testKey';
  1487. $this->Form->request->params['_csrfToken'] = $key;
  1488. $this->Form->request['_Token'] = array(
  1489. 'unlockedFields' => array('first_name', 'address')
  1490. );
  1491. $this->Form->create();
  1492. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1493. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1494. $this->Form->input('Addresses.title');
  1495. $this->Form->input('Addresses.first_name');
  1496. $this->Form->input('Addresses.last_name');
  1497. $this->Form->input('Addresses.address');
  1498. $this->Form->input('Addresses.city');
  1499. $this->Form->input('Addresses.phone');
  1500. $result = $this->Form->fields;
  1501. $expected = array(
  1502. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1503. 'Addresses.city', 'Addresses.phone'
  1504. );
  1505. $this->assertEquals($expected, $result);
  1506. $result = $this->Form->secure($expected);
  1507. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1508. $expected = array(
  1509. 'div' => array('style' => 'display:none;'),
  1510. array('input' => array(
  1511. 'type' => 'hidden', 'name' => '_Token[fields]',
  1512. 'value' => $hash
  1513. )),
  1514. array('input' => array(
  1515. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1516. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1517. )),
  1518. '/div'
  1519. );
  1520. $this->assertTags($result, $expected);
  1521. }
  1522. /**
  1523. * test securing inputs with custom name attributes.
  1524. *
  1525. * @return void
  1526. */
  1527. public function testFormSecureWithCustomNameAttribute() {
  1528. $this->Form->request->params['_Token'] = 'testKey';
  1529. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1530. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1531. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1532. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1533. }
  1534. /**
  1535. * testFormSecuredInput method
  1536. *
  1537. * Test generation of entire secure form, assertions made on input() output.
  1538. *
  1539. * @return void
  1540. */
  1541. public function testFormSecuredInput() {
  1542. $this->markTestIncomplete('Need to revisit once models work again.');
  1543. $this->Form->request->params['_csrfToken'] = 'testKey';
  1544. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1545. $encoding = strtolower(Configure::read('App.encoding'));
  1546. $expected = array(
  1547. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1548. 'div' => array('style' => 'display:none;'),
  1549. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1550. array('input' => array(
  1551. 'type' => 'hidden', 'name' => '_csrfToken',
  1552. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1553. )),
  1554. '/div'
  1555. );
  1556. $this->assertTags($result, $expected);
  1557. $result = $this->Form->input('ValidateUser.ratio');
  1558. $expected = array(
  1559. 'div' => array('class'),
  1560. 'label' => array('for'),
  1561. 'Ratio',
  1562. '/label',
  1563. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1564. '/div',
  1565. );
  1566. $this->assertTags($result, $expected);
  1567. $result = $this->Form->input('ValidateUser.population');
  1568. $expected = array(
  1569. 'div' => array('class'),
  1570. 'label' => array('for'),
  1571. 'Population',
  1572. '/label',
  1573. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1574. '/div',
  1575. );
  1576. $this->assertTags($result, $expected);
  1577. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1578. $expected = array(
  1579. 'div' => array('class' => 'input text'),
  1580. 'label' => array('for' => 'UserFormPublished'),
  1581. 'Published',
  1582. '/label',
  1583. array('input' => array(
  1584. 'type' => 'text', 'name' => 'UserForm[published]',
  1585. 'id' => 'UserFormPublished'
  1586. )),
  1587. '/div'
  1588. );
  1589. $this->assertTags($result, $expected);
  1590. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1591. $expected = array(
  1592. 'div' => array('class' => 'input text'),
  1593. 'label' => array('for' => 'UserFormOther'),
  1594. 'Other',
  1595. '/label',
  1596. array('input' => array(
  1597. 'type' => 'text', 'name' => 'UserForm[other]',
  1598. 'id' => 'UserFormOther'
  1599. )),
  1600. '/div'
  1601. );
  1602. $this->assertTags($result, $expected);
  1603. $result = $this->Form->hidden('UserForm.stuff');
  1604. $expected = array(
  1605. 'input' => array(
  1606. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1607. 'id' => 'UserFormStuff'
  1608. ));
  1609. $this->assertTags($result, $expected);
  1610. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1611. $expected = array('input' => array(
  1612. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1613. 'value' => '0', 'id' => 'UserFormHidden'
  1614. ));
  1615. $this->assertTags($result, $expected);
  1616. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1617. $expected = array(
  1618. 'div' => array('class' => 'input checkbox'),
  1619. array('input' => array(
  1620. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1621. 'value' => '0', 'id' => 'UserFormSomething_'
  1622. )),
  1623. array('input' => array(
  1624. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1625. 'value' => '1', 'id' => 'UserFormSomething'
  1626. )),
  1627. 'label' => array('for' => 'UserFormSomething'),
  1628. 'Something',
  1629. '/label',
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->fields;
  1634. $expected = array(
  1635. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1636. 'UserForm.hidden' => '0', 'UserForm.something'
  1637. );
  1638. $this->assertEquals($expected, $result);
  1639. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1640. $result = $this->Form->secure($this->Form->fields);
  1641. $expected = array(
  1642. 'div' => array('style' => 'display:none;'),
  1643. array('input' => array(
  1644. 'type' => 'hidden', 'name' => '_Token[fields]',
  1645. 'value' => $hash
  1646. )),
  1647. array('input' => array(
  1648. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1649. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1650. )),
  1651. '/div'
  1652. );
  1653. $this->assertTags($result, $expected);
  1654. }
  1655. /**
  1656. * Test secured inputs with custom names.
  1657. *
  1658. * @return void
  1659. */
  1660. public function testSecuredInputCustomName() {
  1661. $this->markTestIncomplete('Need to revisit once models work again.');
  1662. $this->Form->request->params['_csrfToken'] = 'testKey';
  1663. $this->assertEquals(array(), $this->Form->fields);
  1664. $this->Form->input('text_input', array(
  1665. 'name' => 'data[Option][General.default_role]',
  1666. ));
  1667. $expected = array('Option.General.default_role');
  1668. $this->assertEquals($expected, $this->Form->fields);
  1669. $this->Form->input('select_box', array(
  1670. 'name' => 'data[Option][General.select_role]',
  1671. 'type' => 'select',
  1672. 'options' => array(1, 2),
  1673. ));
  1674. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1675. $this->assertEquals($expected, $this->Form->fields);
  1676. }
  1677. /**
  1678. * Tests that the correct keys are added to the field hash index
  1679. *
  1680. * @return void
  1681. */
  1682. public function testFormSecuredFileInput() {
  1683. $this->assertEquals(array(), $this->Form->fields);
  1684. $this->Form->file('Attachment.file');
  1685. $expected = array(
  1686. 'Attachment.file.name', 'Attachment.file.type',
  1687. 'Attachment.file.tmp_name', 'Attachment.file.error',
  1688. 'Attachment.file.size'
  1689. );
  1690. $this->assertEquals($expected, $this->Form->fields);
  1691. }
  1692. /**
  1693. * test that multiple selects keys are added to field hash
  1694. *
  1695. * @return void
  1696. */
  1697. public function testFormSecuredMultipleSelect() {
  1698. $this->Form->request->params['_csrfToken'] = 'testKey';
  1699. $this->assertEquals(array(), $this->Form->fields);
  1700. $options = array('1' => 'one', '2' => 'two');
  1701. $this->Form->select('Model.select', $options);
  1702. $expected = array('Model.select');
  1703. $this->assertEquals($expected, $this->Form->fields);
  1704. $this->Form->fields = array();
  1705. $this->Form->select('Model.select', $options, array('multiple' => true));
  1706. $this->assertEquals($expected, $this->Form->fields);
  1707. }
  1708. /**
  1709. * testFormSecuredRadio method
  1710. *
  1711. * @return void
  1712. */
  1713. public function testFormSecuredRadio() {
  1714. $this->Form->request->params['_Token'] = 'testKey';
  1715. $this->assertEquals(array(), $this->Form->fields);
  1716. $options = array('1' => 'option1', '2' => 'option2');
  1717. $this->Form->radio('Test.test', $options);
  1718. $expected = array('Test.test');
  1719. $this->assertEquals($expected, $this->Form->fields);
  1720. }
  1721. /**
  1722. * Test that when disabled is in a list based attribute array it works.
  1723. *
  1724. * @return void
  1725. */
  1726. public function testFormSecuredAndDisabledNotAssoc() {
  1727. $this->Form->request->params['_Token'] = 'testKey';
  1728. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1729. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1730. $this->Form->text('Model.text', array('disabled'));
  1731. $this->Form->textarea('Model.textarea', array('disabled'));
  1732. $this->Form->password('Model.password', array('disabled'));
  1733. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1734. $expected = array(
  1735. 'Model.radio' => ''
  1736. );
  1737. $this->assertEquals($expected, $this->Form->fields);
  1738. }
  1739. /**
  1740. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1741. * hashing.
  1742. *
  1743. * @return void
  1744. */
  1745. public function testFormSecuredAndDisabled() {
  1746. $this->Form->request->params['_Token'] = 'testKey';
  1747. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1748. $this->Form->text('Model.text', array('disabled' => true));
  1749. $this->Form->password('Model.text', array('disabled' => true));
  1750. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1751. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1752. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1753. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1754. $this->Form->month('Model.month', array('disabled' => true));
  1755. $this->Form->day('Model.day', array('disabled' => true));
  1756. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1757. $this->Form->minute('Model.minute', array('disabled' => true));
  1758. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1759. $expected = array(
  1760. 'Model.radio' => ''
  1761. );
  1762. $this->assertEquals($expected, $this->Form->fields);
  1763. }
  1764. /**
  1765. * testDisableSecurityUsingForm method
  1766. *
  1767. * @return void
  1768. */
  1769. public function testDisableSecurityUsingForm() {
  1770. $this->markTestIncomplete('Need to revisit once models work again.');
  1771. $this->Form->request->params['_csrfToken'] = 'testKey';
  1772. $this->Form->request['_Token'] = array(
  1773. 'disabledFields' => array()
  1774. );
  1775. $this->Form->create();
  1776. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1777. $this->Form->input('Addresses.title');
  1778. $this->Form->input('Addresses.first_name', array('secure' => false));
  1779. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1780. $this->Form->input('Addresses.zip', array(
  1781. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1782. ));
  1783. $result = $this->Form->fields;
  1784. $expected = array(
  1785. 'Addresses.id' => '123456', 'Addresses.title',
  1786. );
  1787. $this->assertEquals($expected, $result);
  1788. }
  1789. /**
  1790. * test disableField
  1791. *
  1792. * @return void
  1793. */
  1794. public function testUnlockFieldAddsToList() {
  1795. $this->Form->request['_Token'] = array(
  1796. 'unlockedFields' => array()
  1797. );
  1798. $this->Form->unlockField('Contact.name');
  1799. $this->Form->text('Contact.name');
  1800. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1801. $this->assertEquals(array(), $this->Form->fields);
  1802. }
  1803. /**
  1804. * test unlockField removing from fields array.
  1805. *
  1806. * @return void
  1807. */
  1808. public function testUnlockFieldRemovingFromFields() {
  1809. $this->Form->request['_Token'] = array(
  1810. 'unlockedFields' => array()
  1811. );
  1812. $this->Form->create($this->article);
  1813. $this->Form->hidden('Article.id', array('value' => 1));
  1814. $this->Form->text('Article.title');
  1815. $this->assertEquals(1, $this->Form->fields['Article.id'], 'Hidden input should be secured.');
  1816. $this->assertTrue(in_array('Article.title', $this->Form->fields), 'Field should be secured.');
  1817. $this->Form->unlockField('Article.title');
  1818. $this->Form->unlockField('Article.id');
  1819. $this->assertEquals(array(), $this->Form->fields);
  1820. }
  1821. /**
  1822. * Test validation errors.
  1823. *
  1824. * @return void
  1825. */
  1826. public function testPasswordValidation() {
  1827. $this->markTestIncomplete('Need to revisit once models work again.');
  1828. $Contact->validationErrors['password'] = array('Please provide a password');
  1829. $result = $this->Form->input('Contact.password');
  1830. $expected = array(
  1831. 'div' => array('class' => 'input password error'),
  1832. 'label' => array('for' => 'ContactPassword'),
  1833. 'Password',
  1834. '/label',
  1835. 'input' => array(
  1836. 'type' => 'password', 'name' => 'Contact[password]',
  1837. 'id' => 'ContactPassword', 'class' => 'form-error'
  1838. ),
  1839. array('div' => array('class' => 'error-message')),
  1840. 'Please provide a password',
  1841. '/div',
  1842. '/div'
  1843. );
  1844. $this->assertTags($result, $expected);
  1845. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1846. $expected = array(
  1847. 'div' => array('class' => 'input password error'),
  1848. 'label' => array('for' => 'ContactPassword'),
  1849. 'Password',
  1850. '/label',
  1851. 'input' => array(
  1852. 'type' => 'password', 'name' => 'Contact[password]',
  1853. 'id' => 'ContactPassword', 'class' => 'form-error'
  1854. ),
  1855. '/div'
  1856. );
  1857. $this->assertTags($result, $expected);
  1858. }
  1859. /**
  1860. * Test validation errors, when validation message is an empty string.
  1861. *
  1862. * @return void
  1863. */
  1864. public function testEmptyErrorValidation() {
  1865. $this->markTestIncomplete('Need to revisit once models work again.');
  1866. $this->Form->validationErrors['Contact']['password'] = '';
  1867. $result = $this->Form->input('Contact.password');
  1868. $expected = array(
  1869. 'div' => array('class' => 'input password error'),
  1870. 'label' => array('for' => 'ContactPassword'),
  1871. 'Password',
  1872. '/label',
  1873. 'input' => array(
  1874. 'type' => 'password', 'name' => 'Contact[password]',
  1875. 'id' => 'ContactPassword', 'class' => 'form-error'
  1876. ),
  1877. array('div' => array('class' => 'error-message')),
  1878. array(),
  1879. '/div',
  1880. '/div'
  1881. );
  1882. $this->assertTags($result, $expected);
  1883. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1884. $expected = array(
  1885. 'div' => array('class' => 'input password error'),
  1886. 'label' => array('for' => 'ContactPassword'),
  1887. 'Password',
  1888. '/label',
  1889. 'input' => array(
  1890. 'type' => 'password', 'name' => 'Contact[password]',
  1891. 'id' => 'ContactPassword', 'class' => 'form-error'
  1892. ),
  1893. '/div'
  1894. );
  1895. $this->assertTags($result, $expected);
  1896. }
  1897. /**
  1898. * Test validation errors, when calling input() overriding validation message by an empty string.
  1899. *
  1900. * @return void
  1901. */
  1902. public function testEmptyInputErrorValidation() {
  1903. $this->markTestIncomplete('Need to revisit once models work again.');
  1904. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1905. $result = $this->Form->input('Contact.password', array('error' => ''));
  1906. $expected = array(
  1907. 'div' => array('class' => 'input password error'),
  1908. 'label' => array('for' => 'ContactPassword'),
  1909. 'Password',
  1910. '/label',
  1911. 'input' => array(
  1912. 'type' => 'password', 'name' => 'Contact[password]',
  1913. 'id' => 'ContactPassword', 'class' => 'form-error'
  1914. ),
  1915. array('div' => array('class' => 'error-message')),
  1916. array(),
  1917. '/div',
  1918. '/div'
  1919. );
  1920. $this->assertTags($result, $expected);
  1921. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1922. $expected = array(
  1923. 'div' => array('class' => 'input password error'),
  1924. 'label' => array('for' => 'ContactPassword'),
  1925. 'Password',
  1926. '/label',
  1927. 'input' => array(
  1928. 'type' => 'password', 'name' => 'Contact[password]',
  1929. 'id' => 'ContactPassword', 'class' => 'form-error'
  1930. ),
  1931. '/div'
  1932. );
  1933. $this->assertTags($result, $expected);
  1934. }
  1935. /**
  1936. * testFormValidationAssociated method
  1937. *
  1938. * test display of form errors in conjunction with model::validates.
  1939. *
  1940. * @return void
  1941. */
  1942. public function testFormValidationAssociated() {
  1943. $this->markTestIncomplete('Need to revisit once models work again.');
  1944. $data = array(
  1945. 'UserForm' => array('name' => 'user'),
  1946. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1947. );
  1948. $result = $this->UserForm->OpenidUrl->create($data);
  1949. $this->assertFalse(empty($result));
  1950. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1951. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1952. $encoding = strtolower(Configure::read('App.encoding'));
  1953. $expected = array(
  1954. 'form' => array(
  1955. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1956. 'accept-charset' => $encoding
  1957. ),
  1958. 'div' => array('style' => 'display:none;'),
  1959. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1960. '/div'
  1961. );
  1962. $this->assertTags($result, $expected);
  1963. $result = $this->Form->error(
  1964. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1965. );
  1966. $this->assertEquals('Error, not registered', $result);
  1967. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1968. }
  1969. /**
  1970. * testFormValidationAssociatedFirstLevel method
  1971. *
  1972. * test form error display with associated model.
  1973. *
  1974. * @return void
  1975. */
  1976. public function testFormValidationAssociatedFirstLevel() {
  1977. $this->markTestIncomplete('Need to revisit once models work again.');
  1978. $data = array(
  1979. 'ValidateUser' => array('name' => 'mariano'),
  1980. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1981. );
  1982. $result = $this->ValidateUser->create($data);
  1983. $this->assertFalse(empty($result));
  1984. $this->assertFalse($this->ValidateUser->validates());
  1985. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1986. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1987. $encoding = strtolower(Configure::read('App.encoding'));
  1988. $expected = array(
  1989. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  1990. 'div' => array('style' => 'display:none;'),
  1991. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1992. '/div'
  1993. );
  1994. $this->assertTags($result, $expected);
  1995. $result = $this->Form->error(
  1996. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1997. );
  1998. $this->assertEquals('Invalid email', $result);
  1999. $result = $this->Form->error(
  2000. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  2001. );
  2002. $this->assertEquals('Invalid name', $result);
  2003. $result = $this->Form->error(
  2004. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  2005. );
  2006. $this->assertEquals('Invalid city', $result);
  2007. unset($this->ValidateUser->ValidateProfile);
  2008. unset($this->ValidateUser);
  2009. }
  2010. /**
  2011. * testFormValidationAssociatedSecondLevel method
  2012. *
  2013. * test form error display with associated model.
  2014. *
  2015. * @return void
  2016. */
  2017. public function testFormValidationAssociatedSecondLevel() {
  2018. $this->markTestIncomplete('Need to revisit once models work again.');
  2019. $data = array(
  2020. 'ValidateUser' => array('name' => 'mariano'),
  2021. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  2022. 'ValidateItem' => array('name' => 'Item')
  2023. );
  2024. $result = $this->ValidateUser->create($data);
  2025. $this->assertFalse(empty($result));
  2026. $this->assertFalse($this->ValidateUser->validates());
  2027. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  2028. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  2029. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  2030. $encoding = strtolower(Configure::read('App.encoding'));
  2031. $expected = array(
  2032. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  2033. 'div' => array('style' => 'display:none;'),
  2034. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  2035. '/div'
  2036. );
  2037. $this->assertTags($result, $expected);
  2038. $result = $this->Form->error(
  2039. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  2040. );
  2041. $this->assertEquals('Invalid email', $result);
  2042. $result = $this->Form->error(
  2043. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  2044. );
  2045. $this->assertEquals('Invalid name', $result);
  2046. $result = $this->Form->error(
  2047. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  2048. );
  2049. $result = $this->Form->error(
  2050. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  2051. );
  2052. $this->assertEquals('Invalid description', $result);
  2053. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  2054. unset($this->ValidateUser->ValidateProfile);
  2055. unset($this->ValidateUser);
  2056. }
  2057. /**
  2058. * testFormValidationMultiRecord method
  2059. *
  2060. * test form error display with multiple records.
  2061. *
  2062. * @return void
  2063. */
  2064. public function testFormValidationMultiRecord() {
  2065. $this->markTestIncomplete('Need to revisit once models work again.');
  2066. $Contact->validationErrors[2] = array(
  2067. 'name' => array('The provided value is invalid')
  2068. );
  2069. $result = $this->Form->input('Contact.2.name');
  2070. $expected = array(
  2071. 'div' => array('class' => 'input text error'),
  2072. 'label' => array('for' => 'Contact2Name'),
  2073. 'Name',
  2074. '/label',
  2075. 'input' => array(
  2076. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  2077. 'class' => 'form-error', 'maxlength' => 255
  2078. ),
  2079. array('div' => array('class' => 'error-message')),
  2080. 'The provided value is invalid',
  2081. '/div',
  2082. '/div'
  2083. );
  2084. $this->assertTags($result, $expected);
  2085. }
  2086. /**
  2087. * testMultipleInputValidation method
  2088. *
  2089. * test multiple record form validation error display.
  2090. *
  2091. * @return void
  2092. */
  2093. public function testMultipleInputValidation() {
  2094. $this->markTestIncomplete('Need to revisit once models work again.');
  2095. $Address->validationErrors[0] = array(
  2096. 'title' => array('This field cannot be empty'),
  2097. 'first_name' => array('This field cannot be empty')
  2098. );
  2099. $Address->validationErrors[1] = array(
  2100. 'last_name' => array('You must have a last name')
  2101. );
  2102. $this->Form->create();
  2103. $result = $this->Form->input('Address.0.title');
  2104. $expected = array(
  2105. 'div' => array('class'),
  2106. 'label' => array('for'),
  2107. 'preg:/[^<]+/',
  2108. '/label',
  2109. 'input' => array(
  2110. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  2111. ),
  2112. array('div' => array('class' => 'error-message')),
  2113. 'This field cannot be empty',
  2114. '/div',
  2115. '/div'
  2116. );
  2117. $this->assertTags($result, $expected);
  2118. $result = $this->Form->input('Address.0.first_name');
  2119. $expected = array(
  2120. 'div' => array('class'),
  2121. 'label' => array('for'),
  2122. 'preg:/[^<]+/',
  2123. '/label',
  2124. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  2125. array('div' => array('class' => 'error-message')),
  2126. 'This field cannot be empty',
  2127. '/div',
  2128. '/div'
  2129. );
  2130. $this->assertTags($result, $expected);
  2131. $result = $this->Form->input('Address.0.last_name');
  2132. $expected = array(
  2133. 'div' => array('class'),
  2134. 'label' => array('for'),
  2135. 'preg:/[^<]+/',
  2136. '/label',
  2137. 'input' => array('type' => 'text', 'name', 'id'),
  2138. '/div'
  2139. );
  2140. $this->assertTags($result, $expected);
  2141. $result = $this->Form->input('Address.1.last_name');
  2142. $expected = array(
  2143. 'div' => array('class'),
  2144. 'label' => array('for'),
  2145. 'preg:/[^<]+/',
  2146. '/label',
  2147. 'input' => array(
  2148. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  2149. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  2150. ),
  2151. array('div' => array('class' => 'error-message')),
  2152. 'You must have a last name',
  2153. '/div',
  2154. '/div'
  2155. );
  2156. $this->assertTags($result, $expected);
  2157. }
  2158. /**
  2159. * testInput method
  2160. *
  2161. * Test various incarnations of input().
  2162. *
  2163. * @return void
  2164. */
  2165. public function testInput() {
  2166. $this->markTestIncomplete('Need to revisit once models work again.');
  2167. $result = $this->Form->input('ValidateUser.balance');
  2168. $expected = array(
  2169. 'div' => array('class'),
  2170. 'label' => array('for'),
  2171. 'Balance',
  2172. '/label',
  2173. 'input' => array('name', 'type' => 'number', 'id'),
  2174. '/div',
  2175. );
  2176. $this->assertTags($result, $expected);
  2177. $result = $this->Form->input('ValidateUser.cost_decimal');
  2178. $expected = array(
  2179. 'div' => array('class'),
  2180. 'label' => array('for'),
  2181. 'Cost Decimal',
  2182. '/label',
  2183. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  2184. '/div',
  2185. );
  2186. $this->assertTags($result, $expected);
  2187. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  2188. $expected = array(
  2189. 'div' => array('class' => 'input email'),
  2190. 'label' => array('for' => 'custom'),
  2191. 'Email',
  2192. '/label',
  2193. array('input' => array(
  2194. 'type' => 'email', 'name' => 'Contact[email]',
  2195. 'id' => 'custom', 'maxlength' => 255
  2196. )),
  2197. '/div'
  2198. );
  2199. $this->assertTags($result, $expected);
  2200. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  2201. $expected = array(
  2202. '<div',
  2203. 'label' => array('for' => 'ContactEmail'),
  2204. 'Email',
  2205. '/label',
  2206. array('input' => array(
  2207. 'type' => 'email', 'name' => 'Contact[email]',
  2208. 'id' => 'ContactEmail', 'maxlength' => 255
  2209. )),
  2210. '/div'
  2211. );
  2212. $this->assertTags($result, $expected);
  2213. $result = $this->Form->hidden('Contact.idontexist');
  2214. $expected = array('input' => array(
  2215. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  2216. 'id' => 'ContactIdontexist'
  2217. ));
  2218. $this->assertTags($result, $expected);
  2219. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  2220. $expected = array(
  2221. 'div' => array('class' => 'input text'),
  2222. 'label' => array('for' => 'ContactEmail'),
  2223. 'Email',
  2224. '/label',
  2225. array('input' => array(
  2226. 'type' => 'text', 'name' => 'Contact[email]',
  2227. 'id' => 'ContactEmail'
  2228. )),
  2229. '/div'
  2230. );
  2231. $this->assertTags($result, $expected);
  2232. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  2233. $expected = array(
  2234. 'div' => array('class' => 'input text'),
  2235. 'label' => array('for' => 'Contact5Email'),
  2236. 'Email',
  2237. '/label',
  2238. array('input' => array(
  2239. 'type' => 'text', 'name' => 'Contact[5][email]',
  2240. 'id' => 'Contact5Email'
  2241. )),
  2242. '/div'
  2243. );
  2244. $this->assertTags($result, $expected);
  2245. $result = $this->Form->input('Contact.password');
  2246. $expected = array(
  2247. 'div' => array('class' => 'input password'),
  2248. 'label' => array('for' => 'ContactPassword'),
  2249. 'Password',
  2250. '/label',
  2251. array('input' => array(
  2252. 'type' => 'password', 'name' => 'Contact[password]',
  2253. 'id' => 'ContactPassword'
  2254. )),
  2255. '/div'
  2256. );
  2257. $this->assertTags($result, $expected);
  2258. $result = $this->Form->input('Contact.email', array(
  2259. 'type' => 'file', 'class' => 'textbox'
  2260. ));
  2261. $expected = array(
  2262. 'div' => array('class' => 'input file'),
  2263. 'label' => array('for' => 'ContactEmail'),
  2264. 'Email',
  2265. '/label',
  2266. array('input' => array(
  2267. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2268. 'id' => 'ContactEmail'
  2269. )),
  2270. '/div'
  2271. );
  2272. $this->assertTags($result, $expected);
  2273. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  2274. $result = $this->Form->input('Contact.phone');
  2275. $expected = array(
  2276. 'div' => array('class' => 'input tel'),
  2277. 'label' => array('for' => 'ContactPhone'),
  2278. 'Phone',
  2279. '/label',
  2280. array('input' => array(
  2281. 'type' => 'tel', 'name' => 'Contact[phone]',
  2282. 'value' => 'Hello &amp; World &gt; weird chars',
  2283. 'id' => 'ContactPhone', 'maxlength' => 255
  2284. )),
  2285. '/div'
  2286. );
  2287. $this->assertTags($result, $expected);
  2288. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2289. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2290. $expected = array(
  2291. 'div' => array('class' => 'input text'),
  2292. 'label' => array('for' => 'myId'),
  2293. 'Field',
  2294. '/label',
  2295. 'input' => array(
  2296. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2297. 'value' => 'My value', 'id' => 'myId'
  2298. ),
  2299. '/div'
  2300. );
  2301. $this->assertTags($result, $expected);
  2302. unset($this->Form->request->data);
  2303. $Contact->validationErrors['field'] = array('Badness!');
  2304. $result = $this->Form->input('Contact.field');
  2305. $expected = array(
  2306. 'div' => array('class' => 'input text error'),
  2307. 'label' => array('for' => 'ContactField'),
  2308. 'Field',
  2309. '/label',
  2310. 'input' => array(
  2311. 'type' => 'text', 'name' => 'Contact[field]',
  2312. 'id' => 'ContactField', 'class' => 'form-error'
  2313. ),
  2314. array('div' => array('class' => 'error-message')),
  2315. 'Badness!',
  2316. '/div',
  2317. '/div'
  2318. );
  2319. $this->assertTags($result, $expected);
  2320. $result = $this->Form->input('Contact.field', array(
  2321. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  2322. ));
  2323. $expected = array(
  2324. 'label' => array('for' => 'ContactField'),
  2325. 'Field',
  2326. '/label',
  2327. 'input' => array(
  2328. 'type' => 'text', 'name' => 'Contact[field]',
  2329. 'id' => 'ContactField', 'class' => 'form-error'
  2330. ),
  2331. array('span' => array('class' => 'error-message')),
  2332. 'Badness!',
  2333. '/span'
  2334. );
  2335. $this->assertTags($result, $expected);
  2336. $result = $this->Form->input('Contact.field', array(
  2337. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  2338. ));
  2339. $expected = array(
  2340. 'div' => array('class' => 'input text error'),
  2341. 'label' => array('for' => 'ContactField'),
  2342. 'Field',
  2343. '/label',
  2344. 'input' => array(
  2345. 'type' => 'text', 'name' => 'Contact[field]',
  2346. 'id' => 'ContactField', 'class' => 'form-error'
  2347. ),
  2348. array('div' => array('class' => 'error')),
  2349. 'Badness!',
  2350. '/div'
  2351. );
  2352. $this->assertTags($result, $expected);
  2353. $result = $this->Form->input('Contact.field', array(
  2354. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  2355. ));
  2356. $expected = array(
  2357. 'span' => array('class' => 'input text error'),
  2358. 'label' => array('for' => 'ContactField'),
  2359. 'Field',
  2360. '/label',
  2361. 'input' => array(
  2362. 'type' => 'text', 'name' => 'Contact[field]',
  2363. 'id' => 'ContactField', 'class' => 'form-error'
  2364. ),
  2365. 'Badness!',
  2366. '/span'
  2367. );
  2368. $this->assertTags($result, $expected);
  2369. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  2370. $expected = array(
  2371. 'div' => array('class' => 'input text error'),
  2372. 'label' => array('for' => 'ContactField'),
  2373. 'Field',
  2374. '/label',
  2375. 'input' => array(
  2376. 'type' => 'text', 'name' => 'Contact[field]',
  2377. 'id' => 'ContactField', 'class' => 'form-error'
  2378. ),
  2379. 'A message to you, Rudy',
  2380. array('div' => array('class' => 'error-message')),
  2381. 'Badness!',
  2382. '/div',
  2383. '/div'
  2384. );
  2385. $this->assertTags($result, $expected);
  2386. $this->Form->setEntity(null);
  2387. $this->Form->setEntity('Contact.field');
  2388. $result = $this->Form->input('Contact.field', array(
  2389. 'after' => 'A message to you, Rudy', 'error' => false
  2390. ));
  2391. $expected = array(
  2392. 'div' => array('class' => 'input text'),
  2393. 'label' => array('for' => 'ContactField'),
  2394. 'Field',
  2395. '/label',
  2396. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2397. 'A message to you, Rudy',
  2398. '/div'
  2399. );
  2400. $this->assertTags($result, $expected);
  2401. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  2402. $expected = array(
  2403. 'div' => array('class' => 'input text'),
  2404. 'label' => array('for' => 'ObjectField'),
  2405. 'Field',
  2406. '/label',
  2407. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  2408. 'A message to you, Rudy',
  2409. '/div'
  2410. );
  2411. $this->assertTags($result, $expected);
  2412. $Contact->validationErrors['field'] = array('minLength');
  2413. $result = $this->Form->input('Contact.field', array(
  2414. 'error' => array(
  2415. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2416. 'maxLength' => 'login too large'
  2417. )
  2418. ));
  2419. $expected = array(
  2420. 'div' => array('class' => 'input text error'),
  2421. 'label' => array('for' => 'ContactField'),
  2422. 'Field',
  2423. '/label',
  2424. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2425. array('div' => array('class' => 'error-message')),
  2426. 'Le login doit contenir au moins 2 caractères',
  2427. '/div',
  2428. '/div'
  2429. );
  2430. $this->assertTags($result, $expected);
  2431. $Contact->validationErrors['field'] = array('maxLength');
  2432. $result = $this->Form->input('Contact.field', array(
  2433. 'error' => array(
  2434. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  2435. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2436. 'maxLength' => 'login too large',
  2437. )
  2438. ));
  2439. $expected = array(
  2440. 'div' => array('class' => 'input text error'),
  2441. 'label' => array('for' => 'ContactField'),
  2442. 'Field',
  2443. '/label',
  2444. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2445. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  2446. 'login too large',
  2447. '/span',
  2448. '/div'
  2449. );
  2450. $this->assertTags($result, $expected);
  2451. }
  2452. /**
  2453. * Test that inputs with 0 can be created.
  2454. *
  2455. * @return void
  2456. */
  2457. public function testInputZero() {
  2458. $this->markTestIncomplete('Need to revisit once models work again.');
  2459. $this->Form->create('User');
  2460. $result = $this->Form->input('0');
  2461. $expected = array(
  2462. 'div' => array('class' => 'input text'),
  2463. 'label' => array('for' => 'User0'), '/label',
  2464. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  2465. '/div'
  2466. );
  2467. $this->assertTags($result, $expected);
  2468. }
  2469. /**
  2470. * test input() with checkbox creation
  2471. *
  2472. * @return void
  2473. */
  2474. public function testInputCheckbox() {
  2475. $this->markTestIncomplete('Need to revisit once models work again.');
  2476. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2477. $expected = array(
  2478. 'div' => array('class' => 'input checkbox'),
  2479. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2480. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2481. '/div'
  2482. );
  2483. $this->assertTags($result, $expected);
  2484. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2485. $expected = array(
  2486. 'div' => array('class' => 'input checkbox'),
  2487. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2488. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2489. '/div'
  2490. );
  2491. $this->assertTags($result, $expected);
  2492. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2493. $expected = array(
  2494. 'div' => array('class' => 'input checkbox'),
  2495. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2496. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2497. '/div'
  2498. );
  2499. $this->assertTags($result, $expected);
  2500. $result = $this->Form->input('User.disabled', array(
  2501. 'label' => 'Disabled',
  2502. 'type' => 'checkbox',
  2503. 'data-foo' => 'disabled'
  2504. ));
  2505. $expected = array(
  2506. 'div' => array('class' => 'input checkbox'),
  2507. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2508. array('input' => array(
  2509. 'type' => 'checkbox',
  2510. 'name' => 'data[User][disabled]',
  2511. 'value' => '1',
  2512. 'id' => 'UserDisabled',
  2513. 'data-foo' => 'disabled'
  2514. )),
  2515. 'label' => array('for' => 'UserDisabled'),
  2516. 'Disabled',
  2517. '/label',
  2518. '/div'
  2519. );
  2520. $this->assertTags($result, $expected);
  2521. }
  2522. /**
  2523. * test form->input() with time types.
  2524. *
  2525. */
  2526. public function testInputTime() {
  2527. $this->markTestIncomplete('Need to revisit once models work again.');
  2528. extract($this->dateRegex);
  2529. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2530. $result = explode(':', $result);
  2531. $this->assertRegExp('/option value="23"/', $result[0]);
  2532. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2533. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2534. $result = explode(':', $result);
  2535. $this->assertRegExp('/option value="23"/', $result[0]);
  2536. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2537. $result = $this->Form->input('Model.field', array(
  2538. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2539. ));
  2540. $result = explode(':', $result);
  2541. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2542. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2543. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2544. $result = $this->Form->input('Model.field', array(
  2545. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2546. ));
  2547. $result = explode(':', $result);
  2548. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2549. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2550. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2551. $result = $this->Form->input('prueba', array(
  2552. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2553. 'maxYear' => date('Y') + 1, 'interval' => 15
  2554. ));
  2555. $result = explode(':', $result);
  2556. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2557. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2558. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2559. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2560. $result = $this->Form->input('Random.start_time', array(
  2561. 'type' => 'time',
  2562. 'selected' => '18:15'
  2563. ));
  2564. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2565. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2566. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2567. $result = $this->Form->input('published', array('type' => 'time'));
  2568. $now = strtotime('now');
  2569. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2570. $now = strtotime('2013-03-09 00:42:21');
  2571. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2572. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2573. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2574. }
  2575. /**
  2576. * Test interval + selected near the hour roll over.
  2577. *
  2578. * @return void
  2579. */
  2580. public function testTimeSelectedWithInterval() {
  2581. $this->markTestIncomplete('Need to revisit once models work again.');
  2582. $result = $this->Form->input('Model.start_time', array(
  2583. 'type' => 'time',
  2584. 'interval' => 15,
  2585. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2586. ));
  2587. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2588. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2589. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2590. $result = $this->Form->input('Model.start_time', array(
  2591. 'type' => 'time',
  2592. 'interval' => 15,
  2593. 'selected' => '2012-10-23 15:57:00'
  2594. ));
  2595. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2596. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2597. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2598. $result = $this->Form->input('Model.start_time', array(
  2599. 'timeFormat' => 24,
  2600. 'type' => 'time',
  2601. 'interval' => 15,
  2602. 'selected' => '15:57'
  2603. ));
  2604. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2605. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2606. $result = $this->Form->input('Model.start_time', array(
  2607. 'timeFormat' => 24,
  2608. 'type' => 'time',
  2609. 'interval' => 15,
  2610. 'selected' => '23:57'
  2611. ));
  2612. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2613. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2614. $result = $this->Form->input('Model.created', array(
  2615. 'timeFormat' => 24,
  2616. 'type' => 'datetime',
  2617. 'interval' => 15,
  2618. 'selected' => '2012-09-30 23:56'
  2619. ));
  2620. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2621. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2622. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2623. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2624. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2625. }
  2626. /**
  2627. * Test time with selected values around 12:xx:xx
  2628. *
  2629. * @return void
  2630. */
  2631. public function testTimeSelectedWithIntervalTwelve() {
  2632. $this->markTestIncomplete('Need to revisit once models work again.');
  2633. $result = $this->Form->input('Model.start_time', array(
  2634. 'type' => 'time',
  2635. 'timeFormat' => 12,
  2636. 'interval' => 15,
  2637. 'selected' => '00:00:00'
  2638. ));
  2639. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2640. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2641. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2642. $result = $this->Form->input('Model.start_time', array(
  2643. 'type' => 'time',
  2644. 'timeFormat' => 12,
  2645. 'interval' => 15,
  2646. 'selected' => '12:00:00'
  2647. ));
  2648. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2649. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2650. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2651. $result = $this->Form->input('Model.start_time', array(
  2652. 'type' => 'time',
  2653. 'timeFormat' => 12,
  2654. 'interval' => 15,
  2655. 'selected' => '12:15:00'
  2656. ));
  2657. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2658. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2659. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2660. }
  2661. /**
  2662. * Test interval & timeFormat = 12
  2663. *
  2664. * @return void
  2665. */
  2666. public function testInputTimeWithIntervalAnd12HourFormat() {
  2667. $this->markTestIncomplete('Need to revisit once models work again.');
  2668. $result = $this->Form->input('Model.start_time', array(
  2669. 'type' => 'time',
  2670. 'timeFormat' => 12,
  2671. 'interval' => 5,
  2672. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2673. ));
  2674. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2675. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2676. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2677. $result = $this->Form->input('Model.start_time', array(
  2678. 'type' => 'time',
  2679. 'timeFormat' => '12',
  2680. 'interval' => 5,
  2681. 'selected' => '2013-04-19 16:30:00'
  2682. ));
  2683. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2684. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2685. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2686. $result = $this->Form->input('Model.start_time', array(
  2687. 'type' => 'time',
  2688. 'timeFormat' => '12',
  2689. 'interval' => 10,
  2690. 'selected' => '2013-05-19 00:33:00'
  2691. ));
  2692. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2693. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2694. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2695. $result = $this->Form->input('Model.start_time', array(
  2696. 'type' => 'time',
  2697. 'timeFormat' => '12',
  2698. 'interval' => 10,
  2699. 'selected' => '2013-05-19 13:33:00'
  2700. ));
  2701. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2702. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2703. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2704. $result = $this->Form->input('Model.start_time', array(
  2705. 'type' => 'time',
  2706. 'timeFormat' => '12',
  2707. 'interval' => 10,
  2708. 'selected' => '2013-05-19 01:33:00'
  2709. ));
  2710. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2711. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2712. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2713. }
  2714. /**
  2715. * test form->input() with datetime, date and time types
  2716. *
  2717. * @return void
  2718. */
  2719. public function testInputDatetime() {
  2720. $this->markTestIncomplete('Need to revisit once models work again.');
  2721. extract($this->dateRegex);
  2722. $result = $this->Form->input('prueba', array(
  2723. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2724. 'maxYear' => date('Y') + 1, 'interval' => 15
  2725. ));
  2726. $result = explode(':', $result);
  2727. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2728. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2729. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2730. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2731. //related to ticket #5013
  2732. $result = $this->Form->input('Contact.date', array(
  2733. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2734. ));
  2735. $this->assertRegExp('/class="customClass"/', $result);
  2736. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2737. $result = $this->Form->input('Contact.date', array(
  2738. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2739. ));
  2740. $this->assertRegExp('/id="customIdDay"/', $result);
  2741. $this->assertRegExp('/id="customIdMonth"/', $result);
  2742. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2743. $result = $this->Form->input('Model.field', array(
  2744. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2745. ));
  2746. $this->assertRegExp('/id="customIDDay"/', $result);
  2747. $this->assertRegExp('/id="customIDHour"/', $result);
  2748. $result = explode('</select><select', $result);
  2749. $result = explode(':', $result[1]);
  2750. $this->assertRegExp('/option value="23"/', $result[0]);
  2751. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2752. $result = $this->Form->input('Model.field', array(
  2753. 'type' => 'datetime', 'timeFormat' => 12
  2754. ));
  2755. $result = explode('</select><select', $result);
  2756. $result = explode(':', $result[1]);
  2757. $this->assertRegExp('/option value="12"/', $result[0]);
  2758. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2759. $this->Form->request->data = array('Contact' => array('created' => null));
  2760. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2761. $expected = array(
  2762. 'div' => array('class' => 'input date'),
  2763. 'label' => array('for' => 'ContactCreatedMonth'),
  2764. 'Created',
  2765. '/label',
  2766. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2767. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2768. $monthsRegex,
  2769. '/select', '-',
  2770. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2771. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2772. $daysRegex,
  2773. '/select', '-',
  2774. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2775. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2776. $yearsRegex,
  2777. '/select',
  2778. '/div'
  2779. );
  2780. $this->assertTags($result, $expected);
  2781. $this->Form->request->data = array('Contact' => array('created' => null));
  2782. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2783. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2784. $this->Form->request->data = array('Contact' => array('created' => null));
  2785. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2786. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2787. $result = $this->Form->input('Contact.created', array(
  2788. 'type' => 'date',
  2789. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2790. 'timeFormat' => 'NONE'
  2791. ));
  2792. $this->assertRegExp('/for\="created-month"/', $result);
  2793. }
  2794. /**
  2795. * Test generating checkboxes with disabled elements.
  2796. *
  2797. * @return void
  2798. */
  2799. public function testInputCheckboxWithDisabledElements() {
  2800. $this->markTestIncomplete('Need to revisit once models work again.');
  2801. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2802. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2803. $expected = array(
  2804. array('div' => array('class' => 'input select')),
  2805. array('label' => array('for' => "ContactMultiple")),
  2806. 'Multiple',
  2807. '/label',
  2808. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2809. array('div' => array('class' => 'checkbox')),
  2810. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2811. array('label' => array('for' => "ContactMultiple1")),
  2812. 'One',
  2813. '/label',
  2814. '/div',
  2815. array('div' => array('class' => 'checkbox')),
  2816. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2817. array('label' => array('for' => "ContactMultiple2")),
  2818. 'Two',
  2819. '/label',
  2820. '/div',
  2821. array('div' => array('class' => 'checkbox')),
  2822. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2823. array('label' => array('for' => "ContactMultiple3")),
  2824. 'Three',
  2825. '/label',
  2826. '/div',
  2827. '/div'
  2828. );
  2829. $this->assertTags($result, $expected);
  2830. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2831. $expected = array(
  2832. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2833. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2834. 'label' => array('for' => 'ModelField12'),
  2835. 'half',
  2836. '/label'
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $disabled = array('2', 3);
  2840. $expected = array(
  2841. array('div' => array('class' => 'input select')),
  2842. array('label' => array('for' => "ContactMultiple")),
  2843. 'Multiple',
  2844. '/label',
  2845. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2846. array('div' => array('class' => 'checkbox')),
  2847. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2848. array('label' => array('for' => "ContactMultiple1")),
  2849. 'One',
  2850. '/label',
  2851. '/div',
  2852. array('div' => array('class' => 'checkbox')),
  2853. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2854. array('label' => array('for' => "ContactMultiple2")),
  2855. 'Two',
  2856. '/label',
  2857. '/div',
  2858. array('div' => array('class' => 'checkbox')),
  2859. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2860. array('label' => array('for' => "ContactMultiple3")),
  2861. 'Three',
  2862. '/label',
  2863. '/div',
  2864. '/div'
  2865. );
  2866. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2867. $this->assertTags($result, $expected);
  2868. // make sure 50 does only disable 50, and not 50f5c0cf
  2869. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2870. $disabled = array(50);
  2871. $expected = array(
  2872. array('div' => array('class' => 'input select')),
  2873. array('label' => array('for' => "ContactMultiple")),
  2874. 'Multiple',
  2875. '/label',
  2876. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2877. array('div' => array('class' => 'checkbox')),
  2878. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2879. array('label' => array('for' => "ContactMultiple50")),
  2880. 'Fifty',
  2881. '/label',
  2882. '/div',
  2883. array('div' => array('class' => 'checkbox')),
  2884. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2885. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2886. 'Stringy',
  2887. '/label',
  2888. '/div',
  2889. '/div'
  2890. );
  2891. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2892. $this->assertTags($result, $expected);
  2893. }
  2894. /**
  2895. * test input name with leading integer, ensure attributes are generated correctly.
  2896. *
  2897. * @return void
  2898. */
  2899. public function testInputWithLeadingInteger() {
  2900. $result = $this->Form->text('0.Node.title');
  2901. $expected = array(
  2902. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2903. );
  2904. $this->assertTags($result, $expected);
  2905. }
  2906. /**
  2907. * test form->input() with select type inputs.
  2908. *
  2909. * @return void
  2910. */
  2911. public function testInputSelectType() {
  2912. $this->markTestIncomplete('Need to revisit once models work again.');
  2913. $result = $this->Form->input('email', array(
  2914. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2915. );
  2916. $expected = array(
  2917. 'div' => array('class' => 'input select'),
  2918. 'label' => array('for' => 'email'),
  2919. 'Email',
  2920. '/label',
  2921. array('select' => array('name' => 'email', 'id' => 'email')),
  2922. array('option' => array('value' => '')),
  2923. '/option',
  2924. array('option' => array('value' => 'è')),
  2925. 'Firést',
  2926. '/option',
  2927. array('option' => array('value' => 'é')),
  2928. 'Secoènd',
  2929. '/option',
  2930. '/select',
  2931. '/div'
  2932. );
  2933. $this->assertTags($result, $expected);
  2934. $result = $this->Form->input('email', array(
  2935. 'options' => array('First', 'Second'), 'empty' => true)
  2936. );
  2937. $expected = array(
  2938. 'div' => array('class' => 'input select'),
  2939. 'label' => array('for' => 'email'),
  2940. 'Email',
  2941. '/label',
  2942. array('select' => array('name' => 'email', 'id' => 'email')),
  2943. array('option' => array('value' => '')),
  2944. '/option',
  2945. array('option' => array('value' => '0')),
  2946. 'First',
  2947. '/option',
  2948. array('option' => array('value' => '1')),
  2949. 'Second',
  2950. '/option',
  2951. '/select',
  2952. '/div'
  2953. );
  2954. $this->assertTags($result, $expected);
  2955. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2956. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2957. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2958. $expected = array(
  2959. 'div' => array('class' => 'input select'),
  2960. 'label' => array('for' => 'ModelUserId'),
  2961. 'User',
  2962. '/label',
  2963. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  2964. array('option' => array('value' => '')),
  2965. '/option',
  2966. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2967. 'good',
  2968. '/option',
  2969. array('option' => array('value' => 'other')),
  2970. 'bad',
  2971. '/option',
  2972. '/select',
  2973. '/div'
  2974. );
  2975. $this->assertTags($result, $expected);
  2976. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2977. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2978. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2979. $expected = array(
  2980. 'div' => array('class' => 'input select'),
  2981. 'label' => array('for' => 'ThingUserId'),
  2982. 'User',
  2983. '/label',
  2984. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  2985. array('option' => array('value' => '')),
  2986. 'Some Empty',
  2987. '/option',
  2988. array('option' => array('value' => 'value')),
  2989. 'good',
  2990. '/option',
  2991. array('option' => array('value' => 'other')),
  2992. 'bad',
  2993. '/option',
  2994. '/select',
  2995. '/div'
  2996. );
  2997. $this->assertTags($result, $expected);
  2998. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2999. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  3000. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  3001. $expected = array(
  3002. 'div' => array('class' => 'input select'),
  3003. 'label' => array('for' => 'ThingUserId'),
  3004. 'User',
  3005. '/label',
  3006. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  3007. array('option' => array('value' => '')),
  3008. 'Some Empty',
  3009. '/option',
  3010. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3011. 'good',
  3012. '/option',
  3013. array('option' => array('value' => 'other')),
  3014. 'bad',
  3015. '/option',
  3016. '/select',
  3017. '/div'
  3018. );
  3019. $this->assertTags($result, $expected);
  3020. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3021. $this->Form->request->data = array('User' => array('User' => array('value')));
  3022. $result = $this->Form->input('User.User', array('empty' => true));
  3023. $expected = array(
  3024. 'div' => array('class' => 'input select'),
  3025. 'label' => array('for' => 'UserUser'),
  3026. 'User',
  3027. '/label',
  3028. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  3029. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  3030. array('option' => array('value' => '')),
  3031. '/option',
  3032. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3033. 'good',
  3034. '/option',
  3035. array('option' => array('value' => 'other')),
  3036. 'bad',
  3037. '/option',
  3038. '/select',
  3039. '/div'
  3040. );
  3041. $this->assertTags($result, $expected);
  3042. $this->Form->data = array();
  3043. $result = $this->Form->input('Publisher.id', array(
  3044. 'label' => 'Publisher',
  3045. 'type' => 'select',
  3046. 'multiple' => 'checkbox',
  3047. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  3048. ));
  3049. $expected = array(
  3050. array('div' => array('class' => 'input select')),
  3051. array('label' => array('for' => 'PublisherId')),
  3052. 'Publisher',
  3053. '/label',
  3054. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  3055. array('div' => array('class' => 'checkbox')),
  3056. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  3057. array('label' => array('for' => 'PublisherIdValue1')),
  3058. 'Label 1',
  3059. '/label',
  3060. '/div',
  3061. array('div' => array('class' => 'checkbox')),
  3062. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  3063. array('label' => array('for' => 'PublisherIdValue2')),
  3064. 'Label 2',
  3065. '/label',
  3066. '/div',
  3067. '/div'
  3068. );
  3069. $this->assertTags($result, $expected);
  3070. }
  3071. /**
  3072. * test that input() and a non standard primary key makes a hidden input by default.
  3073. *
  3074. * @return void
  3075. */
  3076. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  3077. $this->markTestIncomplete('Need to revisit once models work again.');
  3078. $this->Form->create('User');
  3079. $this->Form->fieldset = array(
  3080. 'User' => array(
  3081. 'fields' => array(
  3082. 'model_id' => array('type' => 'integer')
  3083. ),
  3084. 'validates' => array(),
  3085. 'key' => 'model_id'
  3086. )
  3087. );
  3088. $result = $this->Form->input('model_id');
  3089. $expected = array(
  3090. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  3091. );
  3092. $this->assertTags($result, $expected);
  3093. }
  3094. /**
  3095. * test that overriding the magic select type widget is possible
  3096. *
  3097. * @return void
  3098. */
  3099. public function testInputOverridingMagicSelectType() {
  3100. $this->markTestIncomplete('Need to revisit once models work again.');
  3101. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3102. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  3103. $expected = array(
  3104. 'div' => array('class' => 'input text'),
  3105. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  3106. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  3107. '/div'
  3108. );
  3109. $this->assertTags($result, $expected);
  3110. //Check that magic types still work for plural/singular vars
  3111. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  3112. $result = $this->Form->input('Model.type');
  3113. $expected = array(
  3114. 'div' => array('class' => 'input select'),
  3115. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  3116. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  3117. array('option' => array('value' => 'value')), 'good', '/option',
  3118. array('option' => array('value' => 'other')), 'bad', '/option',
  3119. '/select',
  3120. '/div'
  3121. );
  3122. $this->assertTags($result, $expected);
  3123. }
  3124. /**
  3125. * Test that inferred types do not override developer input
  3126. *
  3127. * @return void
  3128. */
  3129. public function testInputMagicTypeDoesNotOverride() {
  3130. $this->markTestIncomplete('Need to revisit once models work again.');
  3131. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3132. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  3133. $expected = array(
  3134. 'div' => array('class' => 'input checkbox'),
  3135. array('input' => array(
  3136. 'type' => 'hidden',
  3137. 'name' => 'data[Model][user]',
  3138. 'id' => 'ModelUser_',
  3139. 'value' => 0,
  3140. )),
  3141. array('input' => array(
  3142. 'name' => 'data[Model][user]',
  3143. 'type' => 'checkbox',
  3144. 'id' => 'ModelUser',
  3145. 'value' => 1
  3146. )),
  3147. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  3148. '/div'
  3149. );
  3150. $this->assertTags($result, $expected);
  3151. }
  3152. /**
  3153. * Test that magic input() selects are created for type=number
  3154. *
  3155. * @return void
  3156. */
  3157. public function testInputMagicSelectForTypeNumber() {
  3158. $this->markTestIncomplete('Need to revisit once models work again.');
  3159. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  3160. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  3161. $result = $this->Form->input('ValidateUser.balance');
  3162. $expected = array(
  3163. 'div' => array('class' => 'input select'),
  3164. 'label' => array('for' => 'ValidateUserBalance'),
  3165. 'Balance',
  3166. '/label',
  3167. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  3168. array('option' => array('value' => '0')),
  3169. 'nothing',
  3170. '/option',
  3171. array('option' => array('value' => '1', 'selected' => 'selected')),
  3172. 'some',
  3173. '/option',
  3174. array('option' => array('value' => '100')),
  3175. 'a lot',
  3176. '/option',
  3177. '/select',
  3178. '/div'
  3179. );
  3180. $this->assertTags($result, $expected);
  3181. }
  3182. /**
  3183. * Test that magic input() selects can easily be converted into radio types without error.
  3184. *
  3185. * @return void
  3186. */
  3187. public function testInputMagicSelectChangeToRadio() {
  3188. $this->markTestIncomplete('Need to revisit once models work again.');
  3189. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3190. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  3191. $this->assertRegExp('/input type="radio"/', $result);
  3192. }
  3193. /**
  3194. * fields with the same name as the model should work.
  3195. *
  3196. * @return void
  3197. */
  3198. public function testInputWithMatchingFieldAndModelName() {
  3199. $this->markTestIncomplete('Need to revisit once models work again.');
  3200. $this->Form->create('User');
  3201. $this->Form->fieldset = array(
  3202. 'User' => array(
  3203. 'fields' => array(
  3204. 'User' => array('type' => 'text')
  3205. ),
  3206. 'validates' => array(),
  3207. 'key' => 'id'
  3208. )
  3209. );
  3210. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  3211. $result = $this->Form->input('User', array('type' => 'text'));
  3212. $expected = array(
  3213. 'div' => array('class' => 'input text'),
  3214. 'label' => array('for' => 'UserUser'), 'User', '/label',
  3215. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  3216. '/div'
  3217. );
  3218. $this->assertTags($result, $expected);
  3219. }
  3220. /**
  3221. * testFormInputs method
  3222. *
  3223. * test correct results from form::inputs().
  3224. *
  3225. * @return void
  3226. */
  3227. public function testFormInputs() {
  3228. $this->markTestIncomplete('Need to revisit once models work again.');
  3229. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  3230. $result = $this->Form->inputs('The Legend');
  3231. $expected = array(
  3232. '<fieldset',
  3233. '<legend',
  3234. 'The Legend',
  3235. '/legend',
  3236. '*/fieldset',
  3237. );
  3238. $this->assertTags($result, $expected);
  3239. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3240. $expected = array(
  3241. 'fieldset' => array('class' => 'classy-stuff'),
  3242. '<legend',
  3243. 'Field of Dreams',
  3244. '/legend',
  3245. '*/fieldset'
  3246. );
  3247. $this->assertTags($result, $expected);
  3248. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3249. $this->assertTags($result, $expected);
  3250. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  3251. $this->assertTags($result, $expected);
  3252. $this->Form->create('Contact');
  3253. $this->Form->request['prefix'] = 'admin';
  3254. $this->Form->request['action'] = 'admin_edit';
  3255. $result = $this->Form->inputs();
  3256. $expected = array(
  3257. '<fieldset',
  3258. '<legend',
  3259. 'Edit Contact',
  3260. '/legend',
  3261. '*/fieldset',
  3262. );
  3263. $this->assertTags($result, $expected);
  3264. $this->Form->create('Contact');
  3265. $result = $this->Form->inputs(false);
  3266. $expected = array(
  3267. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3268. array('div' => array('class' => 'input text')),
  3269. '*/div',
  3270. array('div' => array('class' => 'input email')),
  3271. '*/div',
  3272. array('div' => array('class' => 'input tel')),
  3273. '*/div',
  3274. array('div' => array('class' => 'input password')),
  3275. '*/div',
  3276. array('div' => array('class' => 'input date')),
  3277. '*/div',
  3278. array('div' => array('class' => 'input date')),
  3279. '*/div',
  3280. array('div' => array('class' => 'input datetime')),
  3281. '*/div',
  3282. array('div' => array('class' => 'input number')),
  3283. '*/div',
  3284. array('div' => array('class' => 'input select')),
  3285. '*/div',
  3286. );
  3287. $this->assertTags($result, $expected);
  3288. $this->Form->create('Contact');
  3289. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  3290. $expected = array(
  3291. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3292. array('div' => array('class' => 'input text')),
  3293. '*/div',
  3294. array('div' => array('class' => 'input email')),
  3295. '*/div',
  3296. array('div' => array('class' => 'input tel')),
  3297. '*/div',
  3298. array('div' => array('class' => 'input password')),
  3299. '*/div',
  3300. array('div' => array('class' => 'input date')),
  3301. '*/div',
  3302. array('div' => array('class' => 'input date')),
  3303. '*/div',
  3304. array('div' => array('class' => 'input datetime')),
  3305. '*/div',
  3306. array('div' => array('class' => 'input number')),
  3307. '*/div',
  3308. array('div' => array('class' => 'input select')),
  3309. '*/div',
  3310. );
  3311. $this->assertTags($result, $expected);
  3312. $this->Form->create('Contact');
  3313. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  3314. $this->assertTags($result, $expected);
  3315. $this->Form->create('Contact');
  3316. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  3317. $expected = array(
  3318. 'fieldset' => array(),
  3319. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3320. array('div' => array('class' => 'input text')),
  3321. '*/div',
  3322. array('div' => array('class' => 'input email')),
  3323. '*/div',
  3324. array('div' => array('class' => 'input tel')),
  3325. '*/div',
  3326. array('div' => array('class' => 'input password')),
  3327. '*/div',
  3328. array('div' => array('class' => 'input date')),
  3329. '*/div',
  3330. array('div' => array('class' => 'input date')),
  3331. '*/div',
  3332. array('div' => array('class' => 'input datetime')),
  3333. '*/div',
  3334. array('div' => array('class' => 'input number')),
  3335. '*/div',
  3336. array('div' => array('class' => 'input select')),
  3337. '*/div',
  3338. '/fieldset'
  3339. );
  3340. $this->assertTags($result, $expected);
  3341. $this->Form->create('Contact');
  3342. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  3343. $expected = array(
  3344. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3345. array('div' => array('class' => 'input text')),
  3346. '*/div',
  3347. array('div' => array('class' => 'input email')),
  3348. '*/div',
  3349. array('div' => array('class' => 'input tel')),
  3350. '*/div',
  3351. array('div' => array('class' => 'input password')),
  3352. '*/div',
  3353. array('div' => array('class' => 'input date')),
  3354. '*/div',
  3355. array('div' => array('class' => 'input date')),
  3356. '*/div',
  3357. array('div' => array('class' => 'input datetime')),
  3358. '*/div',
  3359. array('div' => array('class' => 'input number')),
  3360. '*/div',
  3361. array('div' => array('class' => 'input select')),
  3362. '*/div',
  3363. );
  3364. $this->assertTags($result, $expected);
  3365. $this->Form->create('Contact');
  3366. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  3367. $this->assertTags($result, $expected);
  3368. $this->Form->create('Contact');
  3369. $result = $this->Form->inputs('Hello');
  3370. $expected = array(
  3371. 'fieldset' => array(),
  3372. 'legend' => array(),
  3373. 'Hello',
  3374. '/legend',
  3375. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3376. array('div' => array('class' => 'input text')),
  3377. '*/div',
  3378. array('div' => array('class' => 'input email')),
  3379. '*/div',
  3380. array('div' => array('class' => 'input tel')),
  3381. '*/div',
  3382. array('div' => array('class' => 'input password')),
  3383. '*/div',
  3384. array('div' => array('class' => 'input date')),
  3385. '*/div',
  3386. array('div' => array('class' => 'input date')),
  3387. '*/div',
  3388. array('div' => array('class' => 'input datetime')),
  3389. '*/div',
  3390. array('div' => array('class' => 'input number')),
  3391. '*/div',
  3392. array('div' => array('class' => 'input select')),
  3393. '*/div',
  3394. '/fieldset'
  3395. );
  3396. $this->assertTags($result, $expected);
  3397. $this->Form->create('Contact');
  3398. $result = $this->Form->inputs(array('legend' => 'Hello'));
  3399. $expected = array(
  3400. 'fieldset' => array(),
  3401. 'legend' => array(),
  3402. 'Hello',
  3403. '/legend',
  3404. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3405. array('div' => array('class' => 'input text')),
  3406. '*/div',
  3407. array('div' => array('class' => 'input email')),
  3408. '*/div',
  3409. array('div' => array('class' => 'input tel')),
  3410. '*/div',
  3411. array('div' => array('class' => 'input password')),
  3412. '*/div',
  3413. array('div' => array('class' => 'input date')),
  3414. '*/div',
  3415. array('div' => array('class' => 'input date')),
  3416. '*/div',
  3417. array('div' => array('class' => 'input datetime')),
  3418. '*/div',
  3419. array('div' => array('class' => 'input number')),
  3420. '*/div',
  3421. array('div' => array('class' => 'input select')),
  3422. '*/div',
  3423. '/fieldset'
  3424. );
  3425. $this->assertTags($result, $expected);
  3426. $this->Form->create('Contact');
  3427. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3428. $this->assertTags($result, $expected);
  3429. $this->Form->end();
  3430. $this->Form->create(false);
  3431. $expected = array(
  3432. 'fieldset' => array(),
  3433. array('div' => array('class' => 'input text')),
  3434. 'label' => array('for' => 'foo'),
  3435. 'Foo',
  3436. '/label',
  3437. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3438. '*/div',
  3439. '/fieldset'
  3440. );
  3441. $result = $this->Form->inputs(
  3442. array('foo' => array('type' => 'text')),
  3443. array(),
  3444. array('legend' => false)
  3445. );
  3446. $this->assertTags($result, $expected);
  3447. }
  3448. /**
  3449. * Tests inputs() works with plugin models
  3450. *
  3451. * @return void
  3452. */
  3453. public function testInputsPluginModel() {
  3454. $this->markTestIncomplete('Need to revisit once models work again.');
  3455. $this->loadFixtures('Post');
  3456. Plugin::load('TestPlugin');
  3457. $this->Form->request['models'] = array(
  3458. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3459. );
  3460. $this->Form->create('TestPlugin.TestPluginPost');
  3461. $result = $this->Form->inputs();
  3462. $this->assertContains('TestPluginPost[id]', $result);
  3463. $this->assertContains('TestPluginPost[author_id]', $result);
  3464. $this->assertContains('TestPluginPost[title]', $result);
  3465. $this->assertEquals('TestPluginPost', $this->Form->model());
  3466. }
  3467. /**
  3468. * testSelectAsCheckbox method
  3469. *
  3470. * test multi-select widget with checkbox formatting.
  3471. *
  3472. * @return void
  3473. */
  3474. public function testSelectAsCheckbox() {
  3475. $result = $this->Form->select(
  3476. 'Model.multi_field',
  3477. array('first', 'second', 'third'),
  3478. array('multiple' => 'checkbox', 'value' => array(0, 1))
  3479. );
  3480. $expected = array(
  3481. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3482. array('div' => array('class' => 'checkbox')),
  3483. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
  3484. array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
  3485. 'first',
  3486. '/label',
  3487. '/div',
  3488. array('div' => array('class' => 'checkbox')),
  3489. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
  3490. array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
  3491. 'second',
  3492. '/label',
  3493. '/div',
  3494. array('div' => array('class' => 'checkbox')),
  3495. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
  3496. array('label' => array('for' => 'model-multi-field-2')),
  3497. 'third',
  3498. '/label',
  3499. '/div',
  3500. );
  3501. $this->assertTags($result, $expected);
  3502. $result = $this->Form->select(
  3503. 'Model.multi_field',
  3504. array('1/2' => 'half'),
  3505. array('multiple' => 'checkbox')
  3506. );
  3507. $expected = array(
  3508. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
  3509. array('div' => array('class' => 'checkbox')),
  3510. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
  3511. array('label' => array('for' => 'model-multi-field-1-2')),
  3512. 'half',
  3513. '/label',
  3514. '/div',
  3515. );
  3516. $this->assertTags($result, $expected);
  3517. }
  3518. /**
  3519. * testLabel method
  3520. *
  3521. * test label generation.
  3522. *
  3523. * @return void
  3524. */
  3525. public function testLabel() {
  3526. $result = $this->Form->label('Person.name');
  3527. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3528. $result = $this->Form->label('Person.name');
  3529. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3530. $result = $this->Form->label('Person.first_name');
  3531. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3532. $result = $this->Form->label('Person.first_name', 'Your first name');
  3533. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3534. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3535. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3536. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3537. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3538. $result = $this->Form->label('Person.first_name', '');
  3539. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3540. $result = $this->Form->label('Person.2.name', '');
  3541. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3542. }
  3543. /**
  3544. * testTextbox method
  3545. *
  3546. * test textbox element generation
  3547. *
  3548. * @return void
  3549. */
  3550. public function testTextbox() {
  3551. $result = $this->Form->text('Model.field');
  3552. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3553. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3554. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3555. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3556. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3557. }
  3558. /**
  3559. * Test that text() hooks up with request data and error fields.
  3560. *
  3561. * @return void
  3562. */
  3563. public function testTextBoxDataAndError() {
  3564. $this->article['errors'] = [
  3565. 'Contact' => ['text' => 'wrong']
  3566. ];
  3567. $this->Form->create($this->article);
  3568. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3569. $result = $this->Form->text('Model.text');
  3570. $expected = [
  3571. 'input' => [
  3572. 'type' => 'text',
  3573. 'name' => 'Model[text]',
  3574. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3575. ]
  3576. ];
  3577. $this->assertTags($result, $expected);
  3578. $this->Form->request->data['Contact']['text'] = 'test';
  3579. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3580. $expected = [
  3581. 'input' => [
  3582. 'type' => 'text',
  3583. 'name' => 'Contact[text]',
  3584. 'value' => 'test',
  3585. 'id' => 'theID',
  3586. 'class' => 'form-error'
  3587. ]
  3588. ];
  3589. $this->assertTags($result, $expected);
  3590. }
  3591. /**
  3592. * testDefaultValue method
  3593. *
  3594. * Test default value setting
  3595. *
  3596. * @return void
  3597. */
  3598. public function testTextDefaultValue() {
  3599. $this->Form->request->data['Model']['field'] = 'test';
  3600. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3601. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3602. $this->assertTags($result, $expected);
  3603. unset($this->Form->request->data['Model']['field']);
  3604. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3605. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3606. $this->assertTags($result, $expected);
  3607. }
  3608. /**
  3609. * testError method
  3610. *
  3611. * Test field error generation
  3612. *
  3613. * @return void
  3614. */
  3615. public function testError() {
  3616. $this->article['errors'] = [
  3617. 'Article' => ['field' => 'email']
  3618. ];
  3619. $this->Form->create($this->article);
  3620. $result = $this->Form->error('Article.field');
  3621. $expected = [
  3622. ['div' => ['class' => 'error-message']],
  3623. 'email',
  3624. '/div',
  3625. ];
  3626. $this->assertTags($result, $expected);
  3627. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>");
  3628. $expected = [
  3629. ['div' => ['class' => 'error-message']],
  3630. '&lt;strong&gt;Badness!&lt;/strong&gt;',
  3631. '/div',
  3632. ];
  3633. $this->assertTags($result, $expected);
  3634. $result = $this->Form->error('Article.field', "<strong>Badness!</strong>", ['escape' => false]);
  3635. $expected = [
  3636. ['div' => ['class' => 'error-message']],
  3637. '<strong', 'Badness!', '/strong',
  3638. '/div',
  3639. ];
  3640. $this->assertTags($result, $expected);
  3641. }
  3642. /**
  3643. * Test error with nested lists.
  3644. *
  3645. * @return void
  3646. */
  3647. public function testErrorMessages() {
  3648. $this->article['errors'] = [
  3649. 'Article' => ['field' => 'email']
  3650. ];
  3651. $this->Form->create($this->article);
  3652. $result = $this->Form->error('Article.field', array(
  3653. 'email' => 'No good!'
  3654. ));
  3655. $expected = array(
  3656. 'div' => array('class' => 'error-message'),
  3657. 'No good!',
  3658. '/div'
  3659. );
  3660. $this->assertTags($result, $expected);
  3661. }
  3662. /**
  3663. * test error() with multiple messages.
  3664. *
  3665. * @return void
  3666. */
  3667. public function testErrorMultipleMessages() {
  3668. $this->article['errors'] = [
  3669. 'field' => ['notEmpty', 'email', 'Something else']
  3670. ];
  3671. $this->Form->create($this->article);
  3672. $result = $this->Form->error('field', array(
  3673. 'notEmpty' => 'Cannot be empty',
  3674. 'email' => 'No good!'
  3675. ));
  3676. $expected = array(
  3677. 'div' => array('class' => 'error-message'),
  3678. 'ul' => array(),
  3679. '<li', 'Cannot be empty', '/li',
  3680. '<li', 'No good!', '/li',
  3681. '<li', 'Something else', '/li',
  3682. '/ul',
  3683. '/div'
  3684. );
  3685. $this->assertTags($result, $expected);
  3686. }
  3687. /**
  3688. * test error options when using form->input();
  3689. *
  3690. * @return void
  3691. */
  3692. public function testInputErrorEscape() {
  3693. $this->markTestIncomplete('Need to revisit once models work again.');
  3694. $this->Form->create('ValidateProfile');
  3695. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3696. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3697. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3698. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3699. $this->assertRegExp('/required<br>/', $result);
  3700. }
  3701. /**
  3702. * testPassword method
  3703. *
  3704. * Test password element generation
  3705. *
  3706. * @return void
  3707. */
  3708. public function testPassword() {
  3709. $this->article['errors'] = [
  3710. 'Contact' => [
  3711. 'passwd' => 1
  3712. ]
  3713. ];
  3714. $this->Form->create($this->article);
  3715. $result = $this->Form->password('Contact.field');
  3716. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3717. $this->Form->request->data['Contact']['passwd'] = 'test';
  3718. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3719. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3720. }
  3721. /**
  3722. * testRadio method
  3723. *
  3724. * Test radio element set generation
  3725. *
  3726. * @return void
  3727. */
  3728. public function testRadio() {
  3729. $result = $this->Form->radio('Model.field', array('option A'));
  3730. $expected = array(
  3731. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3732. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3733. 'label' => array('for' => 'model-field-0'),
  3734. 'option A',
  3735. '/label'
  3736. );
  3737. $this->assertTags($result, $expected);
  3738. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3739. $expected = array(
  3740. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3741. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3742. array('label' => array('for' => 'model-field-0')),
  3743. 'option A',
  3744. '/label',
  3745. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3746. array('label' => array('for' => 'model-field-1')),
  3747. 'option B',
  3748. '/label',
  3749. );
  3750. $this->assertTags($result, $expected);
  3751. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3752. $expected = array(
  3753. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3754. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3755. array('label' => array('for' => 'employee-gender-male')),
  3756. 'Male',
  3757. '/label',
  3758. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3759. array('label' => array('for' => 'employee-gender-female')),
  3760. 'Female',
  3761. '/label',
  3762. );
  3763. $this->assertTags($result, $expected);
  3764. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3765. $expected = array(
  3766. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3767. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3768. array('label' => array('for' => 'model-custom-0')),
  3769. 'option A',
  3770. '/label',
  3771. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3772. array('label' => array('for' => 'model-custom-1')),
  3773. 'option B',
  3774. '/label',
  3775. );
  3776. $this->assertTags($result, $expected);
  3777. }
  3778. /**
  3779. * test generating radio input inside label ala twitter bootstrap
  3780. *
  3781. * @return void
  3782. */
  3783. public function testRadioInputInsideLabel() {
  3784. $this->Form->templates([
  3785. 'label' => '<label{{attrs}}>{{input}}{{text}}</label>',
  3786. 'radioContainer' => '{{label}}'
  3787. ]);
  3788. $result = $this->Form->radio('Model.field', ['option A', 'option B']);
  3789. $expected = [
  3790. ['input' => [
  3791. 'type' => 'hidden',
  3792. 'name' => 'Model[field]',
  3793. 'value' => ''
  3794. ]],
  3795. ['label' => ['for' => 'model-field-0']],
  3796. ['input' => [
  3797. 'type' => 'radio',
  3798. 'name' => 'Model[field]',
  3799. 'value' => '0',
  3800. 'id' => 'model-field-0'
  3801. ]],
  3802. 'option A',
  3803. '/label',
  3804. ['label' => ['for' => 'model-field-1']],
  3805. ['input' => [
  3806. 'type' => 'radio',
  3807. 'name' => 'Model[field]',
  3808. 'value' => '1',
  3809. 'id' => 'model-field-1'
  3810. ]],
  3811. 'option B',
  3812. '/label',
  3813. ];
  3814. $this->assertTags($result, $expected);
  3815. }
  3816. /**
  3817. * test disabling the hidden input for radio buttons
  3818. *
  3819. * @return void
  3820. */
  3821. public function testRadioHiddenInputDisabling() {
  3822. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3823. $expected = array(
  3824. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3825. 'label' => array('for' => 'model-1-field-0'),
  3826. 'option A',
  3827. '/label'
  3828. );
  3829. $this->assertTags($result, $expected);
  3830. }
  3831. /**
  3832. * testDomIdSuffix method
  3833. *
  3834. * @return void
  3835. */
  3836. public function testDomIdSuffix() {
  3837. $this->markTestIncomplete('Need to revisit once models work again.');
  3838. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3839. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3840. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3841. $this->assertEquals('AbcX=FooY=Bar', $result);
  3842. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3843. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3844. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3845. $this->assertEquals('AbcXFooYBar', $result);
  3846. }
  3847. /**
  3848. * testDomIdSuffixCollisionResolvement()
  3849. *
  3850. * @return void
  3851. */
  3852. public function testDomIdSuffixCollisionResolvement() {
  3853. $this->markTestIncomplete('Need to revisit once models work again.');
  3854. $result = $this->Form->domIdSuffix('a>b');
  3855. $this->assertEquals('AB', $result);
  3856. $result = $this->Form->domIdSuffix('a<b');
  3857. $this->assertEquals('AB1', $result);
  3858. $result = $this->Form->domIdSuffix('a\'b');
  3859. $this->assertEquals('AB2', $result);
  3860. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3861. $this->assertEquals('1StringWith1-dollar', $result);
  3862. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3863. $this->assertEquals('1StringWith1-dollar1', $result);
  3864. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3865. $this->assertEquals('1StringWith1-dollar2', $result);
  3866. }
  3867. /**
  3868. * testSelect method
  3869. *
  3870. * Test select element generation.
  3871. *
  3872. * @return void
  3873. */
  3874. public function testSelect() {
  3875. $result = $this->Form->select('Model.field', array());
  3876. $expected = array(
  3877. 'select' => array('name' => 'Model[field]'),
  3878. '/select'
  3879. );
  3880. $this->assertTags($result, $expected);
  3881. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3882. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3883. $expected = array(
  3884. 'select' => array('name' => 'Model[field]'),
  3885. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3886. 'good',
  3887. '/option',
  3888. array('option' => array('value' => 'other')),
  3889. 'bad',
  3890. '/option',
  3891. '/select'
  3892. );
  3893. $this->assertTags($result, $expected);
  3894. $this->Form->request->data = array();
  3895. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3896. $expected = array(
  3897. 'select' => array('name' => 'Model[field]'),
  3898. array('option' => array('value' => 'value')),
  3899. 'good',
  3900. '/option',
  3901. array('option' => array('value' => 'other')),
  3902. 'bad',
  3903. '/option',
  3904. '/select'
  3905. );
  3906. $this->assertTags($result, $expected);
  3907. $result = $this->Form->select(
  3908. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3909. array('empty' => false)
  3910. );
  3911. $expected = array(
  3912. 'select' => array('name' => 'Model[field]'),
  3913. array('option' => array('value' => 'first')),
  3914. 'first &quot;html&quot; &lt;chars&gt;',
  3915. '/option',
  3916. array('option' => array('value' => 'second')),
  3917. 'value',
  3918. '/option',
  3919. '/select'
  3920. );
  3921. $this->assertTags($result, $expected);
  3922. $result = $this->Form->select(
  3923. 'Model.field',
  3924. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3925. array('escape' => false, 'empty' => false)
  3926. );
  3927. $expected = array(
  3928. 'select' => array('name' => 'Model[field]'),
  3929. array('option' => array('value' => 'first')),
  3930. 'first "html" <chars>',
  3931. '/option',
  3932. array('option' => array('value' => 'second')),
  3933. 'value',
  3934. '/option',
  3935. '/select'
  3936. );
  3937. $this->assertTags($result, $expected);
  3938. $options = array(
  3939. array('value' => 'first', 'text' => 'First'),
  3940. array('value' => 'first', 'text' => 'Another First'),
  3941. );
  3942. $result = $this->Form->select(
  3943. 'Model.field',
  3944. $options,
  3945. array('escape' => false, 'empty' => false)
  3946. );
  3947. $expected = array(
  3948. 'select' => array('name' => 'Model[field]'),
  3949. array('option' => array('value' => 'first')),
  3950. 'First',
  3951. '/option',
  3952. array('option' => array('value' => 'first')),
  3953. 'Another First',
  3954. '/option',
  3955. '/select'
  3956. );
  3957. $this->assertTags($result, $expected);
  3958. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3959. $result = $this->Form->select(
  3960. 'Model.contact_id',
  3961. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3962. array('escape' => false, 'empty' => 'pick something')
  3963. );
  3964. $expected = array(
  3965. 'select' => array('name' => 'Model[contact_id]'),
  3966. array('option' => array('value' => '')), 'pick something', '/option',
  3967. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3968. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3969. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3970. '/select'
  3971. );
  3972. $this->assertTags($result, $expected);
  3973. $this->Form->request->data['Model']['field'] = 0;
  3974. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3975. $expected = array(
  3976. 'select' => array('name' => 'Model[field]'),
  3977. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3978. array('option' => array('value' => '1')), 'Yes', '/option',
  3979. '/select'
  3980. );
  3981. $this->assertTags($result, $expected);
  3982. }
  3983. /**
  3984. * test select() with required and disabled attributes.
  3985. *
  3986. * @return void
  3987. */
  3988. public function testSelectRequired() {
  3989. $this->article['required'] = [
  3990. 'user_id' => true
  3991. ];
  3992. $this->Form->create($this->article);
  3993. $result = $this->Form->select('user_id', array('option A'));
  3994. $expected = array(
  3995. 'select' => array(
  3996. 'name' => 'user_id',
  3997. 'required' => 'required'
  3998. ),
  3999. array('option' => array('value' => '0')), 'option A', '/option',
  4000. '/select'
  4001. );
  4002. $this->assertTags($result, $expected);
  4003. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  4004. $expected = array(
  4005. 'select' => array(
  4006. 'name' => 'user_id',
  4007. 'disabled' => 'disabled'
  4008. ),
  4009. array('option' => array('value' => '0')), 'option A', '/option',
  4010. '/select'
  4011. );
  4012. $this->assertTags($result, $expected);
  4013. }
  4014. /**
  4015. * testNestedSelect method
  4016. *
  4017. * test select element generation with optgroups
  4018. *
  4019. * @return void
  4020. */
  4021. public function testNestedSelect() {
  4022. $result = $this->Form->select(
  4023. 'Model.field',
  4024. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4025. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4026. )), array('empty' => false)
  4027. );
  4028. $expected = array(
  4029. 'select' => array('name' => 'Model[field]'),
  4030. array('option' => array('value' => 1)),
  4031. 'One',
  4032. '/option',
  4033. array('option' => array('value' => 2)),
  4034. 'Two',
  4035. '/option',
  4036. array('optgroup' => array('label' => 'Three')),
  4037. array('option' => array('value' => 3)),
  4038. 'Three',
  4039. '/option',
  4040. array('option' => array('value' => 4)),
  4041. 'Four',
  4042. '/option',
  4043. array('option' => array('value' => 5)),
  4044. 'Five',
  4045. '/option',
  4046. '/optgroup',
  4047. '/select'
  4048. );
  4049. $this->assertTags($result, $expected);
  4050. }
  4051. /**
  4052. * testSelectMultiple method
  4053. *
  4054. * test generation of multiple select elements
  4055. *
  4056. * @return void
  4057. */
  4058. public function testSelectMultiple() {
  4059. $options = array('first', 'second', 'third');
  4060. $result = $this->Form->select(
  4061. 'Model.multi_field',
  4062. $options,
  4063. array('multiple' => true)
  4064. );
  4065. $expected = array(
  4066. 'input' => array(
  4067. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4068. ),
  4069. 'select' => array(
  4070. 'name' => 'Model[multi_field][]',
  4071. 'multiple' => 'multiple'
  4072. ),
  4073. array('option' => array('value' => '0')),
  4074. 'first',
  4075. '/option',
  4076. array('option' => array('value' => '1')),
  4077. 'second',
  4078. '/option',
  4079. array('option' => array('value' => '2')),
  4080. 'third',
  4081. '/option',
  4082. '/select'
  4083. );
  4084. $this->assertTags($result, $expected);
  4085. $result = $this->Form->select(
  4086. 'Model.multi_field',
  4087. $options,
  4088. array('multiple' => 'multiple')
  4089. );
  4090. $this->assertTags($result, $expected);
  4091. }
  4092. /**
  4093. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4094. *
  4095. * @return void
  4096. */
  4097. public function testCheckboxZeroValue() {
  4098. $this->markTestIncomplete('Need to revisit once models work again.');
  4099. $result = $this->Form->input('User.get_spam', array(
  4100. 'type' => 'checkbox',
  4101. 'value' => '0',
  4102. 'hiddenField' => '1',
  4103. ));
  4104. $expected = array(
  4105. 'div' => array('class' => 'input checkbox'),
  4106. array('input' => array(
  4107. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  4108. 'value' => '1', 'id' => 'UserGetSpam_'
  4109. )),
  4110. array('input' => array(
  4111. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  4112. 'value' => '0', 'id' => 'UserGetSpam'
  4113. )),
  4114. 'label' => array('for' => 'UserGetSpam'),
  4115. 'Get Spam',
  4116. '/label',
  4117. '/div'
  4118. );
  4119. $this->assertTags($result, $expected);
  4120. }
  4121. /**
  4122. * test generation of habtm select boxes.
  4123. *
  4124. * @return void
  4125. */
  4126. public function testHabtmSelectBox() {
  4127. $this->markTestIncomplete('Need to revisit once models work again.');
  4128. $this->View->viewVars['contactTags'] = array(
  4129. 1 => 'blue',
  4130. 2 => 'red',
  4131. 3 => 'green'
  4132. );
  4133. $this->Form->request->data = array(
  4134. 'Contact' => array(),
  4135. 'ContactTag' => array(
  4136. array(
  4137. 'id' => '1',
  4138. 'name' => 'blue'
  4139. ),
  4140. array(
  4141. 'id' => 3,
  4142. 'name' => 'green'
  4143. )
  4144. )
  4145. );
  4146. $this->Form->create('Contact');
  4147. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4148. $expected = array(
  4149. 'input' => array(
  4150. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4151. ),
  4152. 'select' => array(
  4153. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4154. 'multiple' => 'multiple'
  4155. ),
  4156. array('option' => array('value' => '1', 'selected' => 'selected')),
  4157. 'blue',
  4158. '/option',
  4159. array('option' => array('value' => '2')),
  4160. 'red',
  4161. '/option',
  4162. array('option' => array('value' => '3', 'selected' => 'selected')),
  4163. 'green',
  4164. '/option',
  4165. '/select'
  4166. );
  4167. $this->assertTags($result, $expected);
  4168. // make sure only 50 is selected, and not 50f5c0cf
  4169. $this->View->viewVars['contactTags'] = array(
  4170. '1' => 'blue',
  4171. '50f5c0cf' => 'red',
  4172. '50' => 'green'
  4173. );
  4174. $this->Form->request->data = array(
  4175. 'Contact' => array(),
  4176. 'ContactTag' => array(
  4177. array(
  4178. 'id' => 1,
  4179. 'name' => 'blue'
  4180. ),
  4181. array(
  4182. 'id' => 50,
  4183. 'name' => 'green'
  4184. )
  4185. )
  4186. );
  4187. $this->Form->create('Contact');
  4188. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4189. $expected = array(
  4190. 'input' => array(
  4191. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4192. ),
  4193. 'select' => array(
  4194. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4195. 'multiple' => 'multiple'
  4196. ),
  4197. array('option' => array('value' => '1', 'selected' => 'selected')),
  4198. 'blue',
  4199. '/option',
  4200. array('option' => array('value' => '50f5c0cf')),
  4201. 'red',
  4202. '/option',
  4203. array('option' => array('value' => '50', 'selected' => 'selected')),
  4204. 'green',
  4205. '/option',
  4206. '/select'
  4207. );
  4208. $this->assertTags($result, $expected);
  4209. }
  4210. /**
  4211. * test generation of multi select elements in checkbox format
  4212. *
  4213. * @return void
  4214. */
  4215. public function testSelectMultipleCheckboxes() {
  4216. $result = $this->Form->select(
  4217. 'Model.multi_field',
  4218. array('first', 'second', 'third'),
  4219. array('multiple' => 'checkbox')
  4220. );
  4221. $expected = array(
  4222. 'input' => array(
  4223. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4224. ),
  4225. array('div' => array('class' => 'checkbox')),
  4226. array('input' => array(
  4227. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4228. 'value' => '0', 'id' => 'model-multi-field-0'
  4229. )),
  4230. array('label' => array('for' => 'model-multi-field-0')),
  4231. 'first',
  4232. '/label',
  4233. '/div',
  4234. array('div' => array('class' => 'checkbox')),
  4235. array('input' => array(
  4236. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4237. 'value' => '1', 'id' => 'model-multi-field-1'
  4238. )),
  4239. array('label' => array('for' => 'model-multi-field-1')),
  4240. 'second',
  4241. '/label',
  4242. '/div',
  4243. array('div' => array('class' => 'checkbox')),
  4244. array('input' => array(
  4245. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4246. 'value' => '2', 'id' => 'model-multi-field-2'
  4247. )),
  4248. array('label' => array('for' => 'model-multi-field-2')),
  4249. 'third',
  4250. '/label',
  4251. '/div'
  4252. );
  4253. $this->assertTags($result, $expected);
  4254. $result = $this->Form->select(
  4255. 'Model.multi_field',
  4256. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4257. array('multiple' => 'checkbox')
  4258. );
  4259. $expected = array(
  4260. 'input' => array(
  4261. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4262. ),
  4263. array('div' => array('class' => 'checkbox')),
  4264. array('input' => array(
  4265. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4266. 'value' => 'a+', 'id' => 'model-multi-field-a+'
  4267. )),
  4268. array('label' => array('for' => 'model-multi-field-a+')),
  4269. 'first',
  4270. '/label',
  4271. '/div',
  4272. array('div' => array('class' => 'checkbox')),
  4273. array('input' => array(
  4274. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4275. 'value' => 'a++', 'id' => 'model-multi-field-a++'
  4276. )),
  4277. array('label' => array('for' => 'model-multi-field-a++')),
  4278. 'second',
  4279. '/label',
  4280. '/div',
  4281. array('div' => array('class' => 'checkbox')),
  4282. array('input' => array(
  4283. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4284. 'value' => 'a+++', 'id' => 'model-multi-field-a+++'
  4285. )),
  4286. array('label' => array('for' => 'model-multi-field-a+++')),
  4287. 'third',
  4288. '/label',
  4289. '/div'
  4290. );
  4291. $this->assertTags($result, $expected);
  4292. $result = $this->Form->select(
  4293. 'Model.multi_field',
  4294. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4295. array('multiple' => 'checkbox')
  4296. );
  4297. $expected = array(
  4298. 'input' => array(
  4299. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4300. ),
  4301. array('div' => array('class' => 'checkbox')),
  4302. array('input' => array(
  4303. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4304. 'value' => 'a&gt;b', 'id' => 'model-multi-field-a-b'
  4305. )),
  4306. array('label' => array('for' => 'model-multi-field-a-b')),
  4307. 'first',
  4308. '/label',
  4309. '/div',
  4310. array('div' => array('class' => 'checkbox')),
  4311. array('input' => array(
  4312. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4313. 'value' => 'a&lt;b', 'id' => 'model-multi-field-a-b1'
  4314. )),
  4315. array('label' => array('for' => 'model-multi-field-a-b1')),
  4316. 'second',
  4317. '/label',
  4318. '/div',
  4319. array('div' => array('class' => 'checkbox')),
  4320. array('input' => array(
  4321. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4322. 'value' => 'a&quot;b', 'id' => 'model-multi-field-a-b2'
  4323. )),
  4324. array('label' => array('for' => 'model-multi-field-a-b2')),
  4325. 'third',
  4326. '/label',
  4327. '/div'
  4328. );
  4329. $this->assertTags($result, $expected);
  4330. }
  4331. /**
  4332. * Ensure that multiCheckbox reads from the request data.
  4333. *
  4334. * @return void
  4335. */
  4336. public function testSelectMultipleCheckboxRequestData() {
  4337. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4338. $result = $this->Form->select(
  4339. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4340. );
  4341. $expected = array(
  4342. 'input' => array(
  4343. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => ''
  4344. ),
  4345. array('div' => array('class' => 'checkbox')),
  4346. array('input' => array(
  4347. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4348. 'value' => '1', 'id' => 'model-tags-1', 'checked' => 'checked'
  4349. )),
  4350. array('label' => array('for' => 'model-tags-1', 'class' => 'selected')),
  4351. 'first',
  4352. '/label',
  4353. '/div',
  4354. array('div' => array('class' => 'checkbox')),
  4355. array('input' => array(
  4356. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4357. 'value' => 'Array', 'id' => 'model-tags-array'
  4358. )),
  4359. array('label' => array('for' => 'model-tags-array')),
  4360. 'Array',
  4361. '/label',
  4362. '/div'
  4363. );
  4364. $this->assertTags($result, $expected);
  4365. }
  4366. /**
  4367. * Checks the security hash array generated for multiple-input checkbox elements
  4368. *
  4369. * @return void
  4370. */
  4371. public function testSelectMultipleCheckboxSecurity() {
  4372. $this->Form->request->params['_Token'] = 'testKey';
  4373. $this->assertEquals(array(), $this->Form->fields);
  4374. $result = $this->Form->select(
  4375. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4376. array('multiple' => 'checkbox')
  4377. );
  4378. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4379. $result = $this->Form->secure($this->Form->fields);
  4380. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4381. $this->assertRegExp('/"' . $key . '"/', $result);
  4382. }
  4383. /**
  4384. * Multiple select elements should always be secured as they always participate
  4385. * in the POST data.
  4386. *
  4387. * @return void
  4388. */
  4389. public function testSelectMultipleSecureWithNoOptions() {
  4390. $this->assertEquals(array(), $this->Form->fields);
  4391. $this->Form->select(
  4392. 'Model.select',
  4393. array(),
  4394. array('multiple' => true)
  4395. );
  4396. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4397. }
  4398. /**
  4399. * When a select box has no options it should not be added to the fields list
  4400. * as it always fail post validation.
  4401. *
  4402. * @return void
  4403. */
  4404. public function testSelectNoSecureWithNoOptions() {
  4405. $this->Form->request->params['_Token'] = 'testkey';
  4406. $this->assertEquals([], $this->Form->fields);
  4407. $this->Form->select(
  4408. 'Model.select',
  4409. []
  4410. );
  4411. $this->assertEquals([], $this->Form->fields);
  4412. $this->Form->select(
  4413. 'Model.user_id',
  4414. [],
  4415. ['empty' => true]
  4416. );
  4417. $this->assertEquals(array('Model.user_id'), $this->Form->fields);
  4418. }
  4419. /**
  4420. * testInputMultipleCheckboxes method
  4421. *
  4422. * test input() resulting in multi select elements being generated.
  4423. *
  4424. * @return void
  4425. */
  4426. public function testInputMultipleCheckboxes() {
  4427. $this->markTestIncomplete('Need to revisit once models work again.');
  4428. $result = $this->Form->input('Model.multi_field', array(
  4429. 'options' => array('first', 'second', 'third'),
  4430. 'multiple' => 'checkbox'
  4431. ));
  4432. $expected = array(
  4433. array('div' => array('class' => 'input select')),
  4434. array('label' => array('for' => 'ModelMultiField')),
  4435. 'Multi Field',
  4436. '/label',
  4437. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4438. array('div' => array('class' => 'checkbox')),
  4439. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4440. array('label' => array('for' => 'ModelMultiField0')),
  4441. 'first',
  4442. '/label',
  4443. '/div',
  4444. array('div' => array('class' => 'checkbox')),
  4445. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4446. array('label' => array('for' => 'ModelMultiField1')),
  4447. 'second',
  4448. '/label',
  4449. '/div',
  4450. array('div' => array('class' => 'checkbox')),
  4451. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4452. array('label' => array('for' => 'ModelMultiField2')),
  4453. 'third',
  4454. '/label',
  4455. '/div',
  4456. '/div'
  4457. );
  4458. $this->assertTags($result, $expected);
  4459. $result = $this->Form->input('Model.multi_field', array(
  4460. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4461. 'multiple' => 'checkbox'
  4462. ));
  4463. $expected = array(
  4464. array('div' => array('class' => 'input select')),
  4465. array('label' => array('for' => 'ModelMultiField')),
  4466. 'Multi Field',
  4467. '/label',
  4468. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4469. array('div' => array('class' => 'checkbox')),
  4470. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4471. array('label' => array('for' => 'ModelMultiFieldA')),
  4472. 'first',
  4473. '/label',
  4474. '/div',
  4475. array('div' => array('class' => 'checkbox')),
  4476. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4477. array('label' => array('for' => 'ModelMultiFieldB')),
  4478. 'second',
  4479. '/label',
  4480. '/div',
  4481. array('div' => array('class' => 'checkbox')),
  4482. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4483. array('label' => array('for' => 'ModelMultiFieldC')),
  4484. 'third',
  4485. '/label',
  4486. '/div',
  4487. '/div'
  4488. );
  4489. $this->assertTags($result, $expected);
  4490. $result = $this->Form->input('Model.multi_field', array(
  4491. 'options' => array('1' => 'first'),
  4492. 'multiple' => 'checkbox',
  4493. 'label' => false,
  4494. 'div' => false
  4495. ));
  4496. $expected = array(
  4497. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4498. array('div' => array('class' => 'checkbox')),
  4499. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4500. array('label' => array('for' => 'ModelMultiField1')),
  4501. 'first',
  4502. '/label',
  4503. '/div'
  4504. );
  4505. $this->assertTags($result, $expected);
  4506. $result = $this->Form->input('Model.multi_field', array(
  4507. 'options' => array('2' => 'second'),
  4508. 'multiple' => 'checkbox',
  4509. 'label' => false,
  4510. 'div' => false
  4511. ));
  4512. $expected = array(
  4513. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4514. array('div' => array('class' => 'checkbox')),
  4515. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4516. array('label' => array('for' => 'ModelMultiField2')),
  4517. 'second',
  4518. '/label',
  4519. '/div'
  4520. );
  4521. $this->assertTags($result, $expected);
  4522. }
  4523. /**
  4524. * testSelectHiddenFieldOmission method
  4525. *
  4526. * test that select() with 'hiddenField' => false omits the hidden field
  4527. *
  4528. * @return void
  4529. */
  4530. public function testSelectHiddenFieldOmission() {
  4531. $result = $this->Form->select('Model.multi_field',
  4532. array('first', 'second'),
  4533. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4534. );
  4535. $this->assertNotContains('type="hidden"', $result);
  4536. }
  4537. /**
  4538. * test that select() with multiple = checkbox works with overriding name attribute.
  4539. *
  4540. * @return void
  4541. */
  4542. public function testSelectCheckboxMultipleOverrideName() {
  4543. $result = $this->Form->select('category', ['1', '2'], [
  4544. 'multiple' => 'checkbox',
  4545. 'name' => 'fish',
  4546. ]);
  4547. $expected = array(
  4548. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
  4549. array('div' => array('class' => 'checkbox')),
  4550. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish-0')),
  4551. array('label' => array('for' => 'fish-0')), '1', '/label',
  4552. '/div',
  4553. array('div' => array('class' => 'checkbox')),
  4554. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish-1')),
  4555. array('label' => array('for' => 'fish-1')), '2', '/label',
  4556. '/div'
  4557. );
  4558. $this->assertTags($result, $expected);
  4559. $result = $this->Form->multiCheckbox('category', ['1', '2'], [
  4560. 'name' => 'fish',
  4561. ]);
  4562. $this->assertTags($result, $expected);
  4563. }
  4564. /**
  4565. * testCheckbox method
  4566. *
  4567. * Test generation of checkboxes
  4568. *
  4569. * @return void
  4570. */
  4571. public function testCheckbox() {
  4572. $result = $this->Form->checkbox('Model.field');
  4573. $expected = array(
  4574. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4575. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4576. );
  4577. $this->assertTags($result, $expected);
  4578. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4579. $expected = array(
  4580. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4581. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4582. );
  4583. $this->assertTags($result, $expected);
  4584. }
  4585. /**
  4586. * testCheckboxDefaultValue method
  4587. *
  4588. * Test default value setting on checkbox() method
  4589. *
  4590. * @return void
  4591. */
  4592. public function testCheckboxDefaultValue() {
  4593. $this->Form->request->data['Model']['field'] = false;
  4594. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4595. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4596. unset($this->Form->request->data['Model']['field']);
  4597. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  4598. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4599. $this->Form->request->data['Model']['field'] = true;
  4600. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4601. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'checked' => 'checked')));
  4602. unset($this->Form->request->data['Model']['field']);
  4603. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  4604. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1')));
  4605. }
  4606. /**
  4607. * Test checkbox being checked or having errors.
  4608. *
  4609. * @return void
  4610. */
  4611. public function testCheckboxCheckedAndError() {
  4612. $this->article['errors'] = [
  4613. 'published' => true
  4614. ];
  4615. $this->Form->request->data['published'] = 'myvalue';
  4616. $this->Form->create($this->article);
  4617. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4618. $expected = array(
  4619. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4620. array('input' => array(
  4621. 'type' => 'checkbox',
  4622. 'name' => 'published',
  4623. 'value' => 'myvalue',
  4624. 'id' => 'theID',
  4625. 'checked' => 'checked',
  4626. 'class' => 'form-error'
  4627. ))
  4628. );
  4629. $this->assertTags($result, $expected);
  4630. $this->Form->request->data['published'] = '';
  4631. $result = $this->Form->checkbox('published');
  4632. $expected = array(
  4633. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4634. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4635. );
  4636. $this->assertTags($result, $expected);
  4637. }
  4638. /**
  4639. * test checkbox() with a custom name attribute
  4640. *
  4641. * @return void
  4642. */
  4643. public function testCheckboxCustomNameAttribute() {
  4644. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4645. $expected = array(
  4646. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4647. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4648. );
  4649. $this->assertTags($result, $expected);
  4650. }
  4651. /**
  4652. * Test that the hidden input for checkboxes can be omitted or set to a
  4653. * specific value.
  4654. *
  4655. * @return void
  4656. */
  4657. public function testCheckboxHiddenField() {
  4658. $result = $this->Form->checkbox('UserForm.something', array(
  4659. 'hiddenField' => false
  4660. ));
  4661. $expected = array(
  4662. 'input' => array(
  4663. 'type' => 'checkbox',
  4664. 'name' => 'UserForm[something]',
  4665. 'value' => '1'
  4666. ),
  4667. );
  4668. $this->assertTags($result, $expected);
  4669. $result = $this->Form->checkbox('UserForm.something', array(
  4670. 'value' => 'Y',
  4671. 'hiddenField' => 'N',
  4672. ));
  4673. $expected = array(
  4674. array('input' => array(
  4675. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4676. 'value' => 'N'
  4677. )),
  4678. array('input' => array(
  4679. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4680. 'value' => 'Y'
  4681. )),
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. }
  4685. /**
  4686. * testDateTime method
  4687. *
  4688. * Test generation of date/time select elements
  4689. *
  4690. * @return void
  4691. */
  4692. public function testDateTime() {
  4693. extract($this->dateRegex);
  4694. $result = $this->Form->dateTime('Contact.date', array('empty' => false));
  4695. $now = strtotime('now');
  4696. $expected = array(
  4697. array('select' => array('name' => 'Contact[date][month]')),
  4698. $monthsRegex,
  4699. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4700. date('F', $now),
  4701. '/option',
  4702. '*/select',
  4703. array('select' => array('name' => 'Contact[date][day]')),
  4704. $daysRegex,
  4705. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4706. date('j', $now),
  4707. '/option',
  4708. '*/select',
  4709. array('select' => array('name' => 'Contact[date][year]')),
  4710. $yearsRegex,
  4711. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4712. date('Y', $now),
  4713. '/option',
  4714. '*/select',
  4715. array('select' => array('name' => 'Contact[date][hour]')),
  4716. $hoursRegex,
  4717. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4718. date('g', $now),
  4719. '/option',
  4720. '*/select',
  4721. array('select' => array('name' => 'Contact[date][minute]')),
  4722. $minutesRegex,
  4723. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4724. date('i', $now),
  4725. '/option',
  4726. '*/select',
  4727. array('select' => array('name' => 'Contact[date][meridian]')),
  4728. $meridianRegex,
  4729. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4730. date('a', $now),
  4731. '/option',
  4732. '*/select'
  4733. );
  4734. $this->assertTags($result, $expected);
  4735. }
  4736. /**
  4737. * Test empty defaulting to true for datetime.
  4738. *
  4739. * @return void
  4740. */
  4741. public function testDatetimeEmpty() {
  4742. extract($this->dateRegex);
  4743. $now = strtotime('now');
  4744. $result = $this->Form->dateTime('Contact.date', array(
  4745. 'empty' => true,
  4746. ));
  4747. $expected = array(
  4748. array('select' => array('name' => 'Contact[date][month]')),
  4749. $monthsRegex,
  4750. array('option' => array('value' => '')),
  4751. '/option',
  4752. '*/select',
  4753. array('select' => array('name' => 'Contact[date][day]')),
  4754. $daysRegex,
  4755. array('option' => array('value' => '')),
  4756. '/option',
  4757. '*/select',
  4758. array('select' => array('name' => 'Contact[date][year]')),
  4759. $yearsRegex,
  4760. array('option' => array('value' => '')),
  4761. '/option',
  4762. '*/select',
  4763. array('select' => array('name' => 'Contact[date][hour]')),
  4764. $hoursRegex,
  4765. array('option' => array('value' => '')),
  4766. '/option',
  4767. '*/select',
  4768. array('select' => array('name' => 'Contact[date][minute]')),
  4769. $minutesRegex,
  4770. array('option' => array('value' => '')),
  4771. '/option',
  4772. '*/select',
  4773. array('select' => array('name' => 'Contact[date][meridian]')),
  4774. $meridianRegex,
  4775. array('option' => array('value' => '')),
  4776. '/option',
  4777. '*/select'
  4778. );
  4779. $this->assertTags($result, $expected);
  4780. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4781. }
  4782. /**
  4783. * Test datetime with interval option.
  4784. *
  4785. * @return void
  4786. */
  4787. public function testDatetimeMinuteInterval() {
  4788. extract($this->dateRegex);
  4789. $now = strtotime('now');
  4790. $result = $this->Form->dateTime('Contact.date', array(
  4791. 'interval' => 5,
  4792. 'value' => ''
  4793. ));
  4794. $expected = array(
  4795. array('select' => array('name' => 'Contact[date][month]')),
  4796. $monthsRegex,
  4797. array('option' => array('selected' => 'selected', 'value' => '')),
  4798. '/option',
  4799. '*/select',
  4800. array('select' => array('name' => 'Contact[date][day]')),
  4801. $daysRegex,
  4802. array('option' => array('selected' => 'selected', 'value' => '')),
  4803. '/option',
  4804. '*/select',
  4805. array('select' => array('name' => 'Contact[date][year]')),
  4806. $yearsRegex,
  4807. array('option' => array('selected' => 'selected', 'value' => '')),
  4808. '/option',
  4809. '*/select',
  4810. array('select' => array('name' => 'Contact[date][hour]')),
  4811. $hoursRegex,
  4812. array('option' => array('selected' => 'selected', 'value' => '')),
  4813. '/option',
  4814. '*/select',
  4815. array('select' => array('name' => 'Contact[date][minute]')),
  4816. $minutesRegex,
  4817. array('option' => array('selected' => 'selected', 'value' => '')),
  4818. '/option',
  4819. array('option' => array('value' => '00')),
  4820. '00',
  4821. '/option',
  4822. array('option' => array('value' => '05')),
  4823. '05',
  4824. '/option',
  4825. array('option' => array('value' => '10')),
  4826. '10',
  4827. '/option',
  4828. '*/select',
  4829. );
  4830. $this->assertTags($result, $expected);
  4831. }
  4832. /**
  4833. * Test dateTime with rounding
  4834. *
  4835. * @return void
  4836. */
  4837. public function testDateTimeRounding() {
  4838. $this->Form->request->data['Contact'] = array(
  4839. 'date' => array(
  4840. 'day' => '13',
  4841. 'month' => '12',
  4842. 'year' => '2010',
  4843. 'hour' => '04',
  4844. 'minute' => '19',
  4845. 'meridian' => 'AM'
  4846. )
  4847. );
  4848. $result = $this->Form->dateTime('Contact.date', array('interval' => 15));
  4849. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4850. $result = $this->Form->dateTime('Contact.date', array('interval' => 15, 'round' => 'up'));
  4851. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  4852. $result = $this->Form->dateTime('Contact.date', array('interval' => 5, 'round' => 'down'));
  4853. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  4854. }
  4855. /**
  4856. * test that datetime() and default values work.
  4857. *
  4858. * @return void
  4859. */
  4860. public function testDatetimeWithDefault() {
  4861. $this->markTestIncomplete('Need to revisit soon.');
  4862. $result = $this->Form->dateTime('Contact.updated', array('value' => '2009-06-01 11:15:30'));
  4863. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4864. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4865. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4866. $result = $this->Form->dateTime('Contact.updated', array(
  4867. 'default' => '2009-06-01 11:15:30'
  4868. ));
  4869. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4870. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4871. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4872. }
  4873. /**
  4874. * testDateTime all zeros
  4875. *
  4876. * @return void
  4877. */
  4878. public function testDateTimeAllZeros() {
  4879. $result = $this->Form->dateTime('Contact.date', array(
  4880. 'timeFormat' => false,
  4881. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  4882. 'value' => '0000-00-00'
  4883. ));
  4884. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  4885. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  4886. }
  4887. /**
  4888. * testDateTimeEmptyAsArray
  4889. *
  4890. * @return void
  4891. */
  4892. public function testDateTimeEmptyAsArray() {
  4893. $result = $this->Form->dateTime('Contact.date', array(
  4894. 'empty' => array(
  4895. 'day' => 'DAY',
  4896. 'month' => 'MONTH',
  4897. 'year' => 'YEAR',
  4898. 'hour' => 'HOUR',
  4899. 'minute' => 'MINUTE',
  4900. 'meridian' => false
  4901. )
  4902. ));
  4903. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4904. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4905. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4906. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4907. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4908. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4909. $result = $this->Form->dateTime('Contact.date', array(
  4910. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4911. ));
  4912. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4913. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4914. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4915. }
  4916. /**
  4917. * testFormDateTimeMulti method
  4918. *
  4919. * test multiple datetime element generation
  4920. *
  4921. * @return void
  4922. */
  4923. public function testFormDateTimeMulti() {
  4924. extract($this->dateRegex);
  4925. $result = $this->Form->dateTime('Contact.1.updated');
  4926. $this->assertContains('Contact[1][updated][month]', $result);
  4927. $this->assertContains('Contact[1][updated][day]', $result);
  4928. $this->assertContains('Contact[1][updated][year]', $result);
  4929. $this->assertContains('Contact[1][updated][hour]', $result);
  4930. $this->assertContains('Contact[1][updated][minute]', $result);
  4931. $this->assertContains('Contact[1][updated][meridian]', $result);
  4932. }
  4933. /**
  4934. * When changing the date format, the label should always focus the first select box when
  4935. * clicked.
  4936. *
  4937. * @return void
  4938. */
  4939. public function testDateTimeLabelIdMatchesFirstInput() {
  4940. $this->markTestIncomplete('Need to revisit once models work again.');
  4941. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4942. $this->assertContains('label for="ModelDateMonth"', $result);
  4943. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4944. $this->assertContains('label for="ModelDateDay"', $result);
  4945. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4946. $this->assertContains('label for="ModelDateYear"', $result);
  4947. }
  4948. /**
  4949. * testMonth method
  4950. *
  4951. * @return void
  4952. */
  4953. public function testMonth() {
  4954. $result = $this->Form->month('Model.field', ['value' => '']);
  4955. $expected = array(
  4956. array('select' => array('name' => 'Model[field][month]')),
  4957. array('option' => array('value' => '', 'selected' => 'selected')),
  4958. '/option',
  4959. array('option' => array('value' => '01')),
  4960. date('F', strtotime('2008-01-01 00:00:00')),
  4961. '/option',
  4962. array('option' => array('value' => '02')),
  4963. date('F', strtotime('2008-02-01 00:00:00')),
  4964. '/option',
  4965. '*/select',
  4966. );
  4967. $this->assertTags($result, $expected);
  4968. $result = $this->Form->month('Model.field', ['empty' => true, 'value' => '']);
  4969. $expected = array(
  4970. array('select' => array('name' => 'Model[field][month]')),
  4971. array('option' => array('selected' => 'selected', 'value' => '')),
  4972. '/option',
  4973. array('option' => array('value' => '01')),
  4974. date('F', strtotime('2008-01-01 00:00:00')),
  4975. '/option',
  4976. array('option' => array('value' => '02')),
  4977. date('F', strtotime('2008-02-01 00:00:00')),
  4978. '/option',
  4979. '*/select',
  4980. );
  4981. $this->assertTags($result, $expected);
  4982. $result = $this->Form->month('Model.field', ['value' => '', 'monthNames' => false]);
  4983. $expected = array(
  4984. array('select' => array('name' => 'Model[field][month]')),
  4985. array('option' => array('selected' => 'selected', 'value' => '')),
  4986. '/option',
  4987. array('option' => array('value' => '01')),
  4988. '1',
  4989. '/option',
  4990. array('option' => array('value' => '02')),
  4991. '2',
  4992. '/option',
  4993. '*/select',
  4994. );
  4995. $this->assertTags($result, $expected);
  4996. $monthNames = [
  4997. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4998. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec'
  4999. ];
  5000. $result = $this->Form->month('Model.field', array('value' => '1', 'monthNames' => $monthNames));
  5001. $expected = array(
  5002. array('select' => array('name' => 'Model[field][month]')),
  5003. array('option' => array('value' => '')),
  5004. '/option',
  5005. array('option' => array('value' => '01', 'selected' => 'selected')),
  5006. 'Jan',
  5007. '/option',
  5008. array('option' => array('value' => '02')),
  5009. 'Feb',
  5010. '/option',
  5011. '*/select',
  5012. );
  5013. $this->assertTags($result, $expected);
  5014. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5015. $result = $this->Form->month('Project.release');
  5016. $expected = array(
  5017. array('select' => array('name' => 'Project[release][month]')),
  5018. array('option' => array('value' => '')),
  5019. '/option',
  5020. array('option' => array('value' => '01')),
  5021. 'January',
  5022. '/option',
  5023. array('option' => array('value' => '02', 'selected' => 'selected')),
  5024. 'February',
  5025. '/option',
  5026. '*/select',
  5027. );
  5028. $this->assertTags($result, $expected);
  5029. }
  5030. /**
  5031. * testDay method
  5032. *
  5033. * @return void
  5034. */
  5035. public function testDay() {
  5036. extract($this->dateRegex);
  5037. $result = $this->Form->day('Model.field', array('value' => false));
  5038. $expected = array(
  5039. array('select' => array('name' => 'Model[field][day]')),
  5040. array('option' => array('selected' => 'selected', 'value' => '')),
  5041. '/option',
  5042. array('option' => array('value' => '01')),
  5043. '1',
  5044. '/option',
  5045. array('option' => array('value' => '02')),
  5046. '2',
  5047. '/option',
  5048. $daysRegex,
  5049. '/select',
  5050. );
  5051. $this->assertTags($result, $expected);
  5052. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5053. $result = $this->Form->day('Model.field');
  5054. $expected = array(
  5055. array('select' => array('name' => 'Model[field][day]')),
  5056. array('option' => array('value' => '')),
  5057. '/option',
  5058. array('option' => array('value' => '01')),
  5059. '1',
  5060. '/option',
  5061. array('option' => array('value' => '02')),
  5062. '2',
  5063. '/option',
  5064. $daysRegex,
  5065. array('option' => array('value' => '10', 'selected' => 'selected')),
  5066. '10',
  5067. '/option',
  5068. $daysRegex,
  5069. '/select',
  5070. );
  5071. $this->assertTags($result, $expected);
  5072. $this->Form->request->data['Model']['field'] = '';
  5073. $result = $this->Form->day('Model.field', array('value' => '10'));
  5074. $expected = array(
  5075. array('select' => array('name' => 'Model[field][day]')),
  5076. array('option' => array('value' => '')),
  5077. '/option',
  5078. array('option' => array('value' => '01')),
  5079. '1',
  5080. '/option',
  5081. array('option' => array('value' => '02')),
  5082. '2',
  5083. '/option',
  5084. $daysRegex,
  5085. array('option' => array('value' => '10', 'selected' => 'selected')),
  5086. '10',
  5087. '/option',
  5088. $daysRegex,
  5089. '/select',
  5090. );
  5091. $this->assertTags($result, $expected);
  5092. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5093. $result = $this->Form->day('Project.release');
  5094. $expected = array(
  5095. array('select' => array('name' => 'Project[release][day]')),
  5096. array('option' => array('value' => '')),
  5097. '/option',
  5098. array('option' => array('value' => '01')),
  5099. '1',
  5100. '/option',
  5101. array('option' => array('value' => '02')),
  5102. '2',
  5103. '/option',
  5104. $daysRegex,
  5105. array('option' => array('value' => '10', 'selected' => 'selected')),
  5106. '10',
  5107. '/option',
  5108. $daysRegex,
  5109. '/select',
  5110. );
  5111. $this->assertTags($result, $expected);
  5112. }
  5113. /**
  5114. * testMinute method
  5115. *
  5116. * @return void
  5117. */
  5118. public function testMinute() {
  5119. $this->markTestIncomplete('Need to revisit once models work again.');
  5120. extract($this->dateRegex);
  5121. $result = $this->Form->minute('Model.field');
  5122. $expected = array(
  5123. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5124. array('option' => array('value' => '')),
  5125. '/option',
  5126. array('option' => array('value' => '00')),
  5127. '00',
  5128. '/option',
  5129. array('option' => array('value' => '01')),
  5130. '01',
  5131. '/option',
  5132. array('option' => array('value' => '02')),
  5133. '02',
  5134. '/option',
  5135. $minutesRegex,
  5136. '/select',
  5137. );
  5138. $this->assertTags($result, $expected);
  5139. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5140. $result = $this->Form->minute('Model.field');
  5141. $expected = array(
  5142. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5143. array('option' => array('value' => '')),
  5144. '/option',
  5145. array('option' => array('value' => '00')),
  5146. '00',
  5147. '/option',
  5148. array('option' => array('value' => '01')),
  5149. '01',
  5150. '/option',
  5151. array('option' => array('value' => '02')),
  5152. '02',
  5153. '/option',
  5154. $minutesRegex,
  5155. array('option' => array('value' => '12', 'selected' => 'selected')),
  5156. '12',
  5157. '/option',
  5158. $minutesRegex,
  5159. '/select',
  5160. );
  5161. $this->assertTags($result, $expected);
  5162. $this->Form->request->data['Model']['field'] = '';
  5163. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5164. $expected = array(
  5165. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5166. array('option' => array('value' => '')),
  5167. '/option',
  5168. array('option' => array('value' => '00')),
  5169. '00',
  5170. '/option',
  5171. array('option' => array('value' => '05')),
  5172. '05',
  5173. '/option',
  5174. array('option' => array('value' => '10')),
  5175. '10',
  5176. '/option',
  5177. $minutesRegex,
  5178. '/select',
  5179. );
  5180. $this->assertTags($result, $expected);
  5181. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5182. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5183. $expected = array(
  5184. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5185. array('option' => array('value' => '')),
  5186. '/option',
  5187. array('option' => array('value' => '00')),
  5188. '00',
  5189. '/option',
  5190. array('option' => array('value' => '05')),
  5191. '05',
  5192. '/option',
  5193. array('option' => array('value' => '10', 'selected' => 'selected')),
  5194. '10',
  5195. '/option',
  5196. $minutesRegex,
  5197. '/select',
  5198. );
  5199. $this->assertTags($result, $expected);
  5200. }
  5201. /**
  5202. * testHour method
  5203. *
  5204. * @return void
  5205. */
  5206. public function testHour() {
  5207. $this->markTestIncomplete('Need to revisit once models work again.');
  5208. extract($this->dateRegex);
  5209. $result = $this->Form->hour('Model.field', false);
  5210. $expected = array(
  5211. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5212. array('option' => array('value' => '')),
  5213. '/option',
  5214. array('option' => array('value' => '01')),
  5215. '1',
  5216. '/option',
  5217. array('option' => array('value' => '02')),
  5218. '2',
  5219. '/option',
  5220. $hoursRegex,
  5221. '/select',
  5222. );
  5223. $this->assertTags($result, $expected);
  5224. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5225. $result = $this->Form->hour('Model.field', false);
  5226. $expected = array(
  5227. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5228. array('option' => array('value' => '')),
  5229. '/option',
  5230. array('option' => array('value' => '01')),
  5231. '1',
  5232. '/option',
  5233. array('option' => array('value' => '02')),
  5234. '2',
  5235. '/option',
  5236. $hoursRegex,
  5237. array('option' => array('value' => '12', 'selected' => 'selected')),
  5238. '12',
  5239. '/option',
  5240. '/select',
  5241. );
  5242. $this->assertTags($result, $expected);
  5243. $this->Form->request->data['Model']['field'] = '';
  5244. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5245. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5246. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5247. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5248. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5249. $result = $this->Form->hour('Model.field', true);
  5250. $expected = array(
  5251. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5252. array('option' => array('value' => '')),
  5253. '/option',
  5254. array('option' => array('value' => '00', 'selected' => 'selected')),
  5255. '0',
  5256. '/option',
  5257. array('option' => array('value' => '01')),
  5258. '1',
  5259. '/option',
  5260. array('option' => array('value' => '02')),
  5261. '2',
  5262. '/option',
  5263. $hoursRegex,
  5264. '/select',
  5265. );
  5266. $this->assertTags($result, $expected);
  5267. unset($this->Form->request->data['Model']['field']);
  5268. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5269. $thisHour = date('H');
  5270. $optValue = date('G');
  5271. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5272. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5273. $result = $this->Form->hour('Model.field', true);
  5274. $expected = array(
  5275. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5276. array('option' => array('value' => '')),
  5277. '/option',
  5278. array('option' => array('value' => '00')),
  5279. '0',
  5280. '/option',
  5281. array('option' => array('value' => '01', 'selected' => 'selected')),
  5282. '1',
  5283. '/option',
  5284. array('option' => array('value' => '02')),
  5285. '2',
  5286. '/option',
  5287. $hoursRegex,
  5288. '/select',
  5289. );
  5290. $this->assertTags($result, $expected);
  5291. }
  5292. /**
  5293. * testYear method
  5294. *
  5295. * @return void
  5296. */
  5297. public function testYear() {
  5298. $result = $this->Form->year('Model.field', ['minYear' => 2006, 'maxYear' => 2007]);
  5299. $expected = array(
  5300. array('select' => array('name' => 'Model[field][year]')),
  5301. array('option' => array('value' => '')),
  5302. '/option',
  5303. array('option' => array('value' => '2007')),
  5304. '2007',
  5305. '/option',
  5306. array('option' => array('value' => '2006')),
  5307. '2006',
  5308. '/option',
  5309. '/select',
  5310. );
  5311. $this->assertTags($result, $expected);
  5312. $result = $this->Form->year('Model.field', [
  5313. 'minYear' => 2006,
  5314. 'maxYear' => 2007,
  5315. 'orderYear' => 'asc'
  5316. ]);
  5317. $expected = array(
  5318. array('select' => array('name' => 'Model[field][year]')),
  5319. array('option' => array('value' => '')),
  5320. '/option',
  5321. array('option' => array('value' => '2006')),
  5322. '2006',
  5323. '/option',
  5324. array('option' => array('value' => '2007')),
  5325. '2007',
  5326. '/option',
  5327. '/select',
  5328. );
  5329. $this->assertTags($result, $expected);
  5330. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5331. $result = $this->Form->year('Contact.published', [
  5332. 'empty' => false,
  5333. 'maxYear' => 2006,
  5334. 'minYear' => 2007,
  5335. ]);
  5336. $expected = array(
  5337. array('select' => array('name' => 'Contact[published][year]')),
  5338. array('option' => array('value' => '2007')),
  5339. '2007',
  5340. '/option',
  5341. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5342. '2006',
  5343. '/option',
  5344. '/select',
  5345. );
  5346. $this->assertTags($result, $expected);
  5347. $this->Form->request->data['Contact']['published'] = '';
  5348. $result = $this->Form->year('Contact.published', [
  5349. 'minYear' => 2006,
  5350. 'maxYear' => 2007,
  5351. 'value' => 2007
  5352. ]);
  5353. $expected = array(
  5354. array('select' => array('name' => 'Contact[published][year]')),
  5355. array('option' => array('value' => '')),
  5356. '/option',
  5357. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5358. '2007',
  5359. '/option',
  5360. array('option' => array('value' => '2006')),
  5361. '2006',
  5362. '/option',
  5363. '/select',
  5364. );
  5365. $this->assertTags($result, $expected);
  5366. }
  5367. /**
  5368. * testYearAutoExpandRange method
  5369. *
  5370. * @return void
  5371. */
  5372. public function testYearAutoExpandRange() {
  5373. $this->markTestIncomplete('Need to revisit once models work again.');
  5374. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  5375. $result = $this->Form->year('User.birthday');
  5376. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5377. $result = $matches[1];
  5378. $expected = range(date('Y') + 20, 1930);
  5379. $this->assertEquals($expected, $result);
  5380. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5381. $result = $this->Form->year('Project.release');
  5382. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5383. $result = $matches[1];
  5384. $expected = range(2050, date('Y') - 20);
  5385. $this->assertEquals($expected, $result);
  5386. $this->Form->request->data['Project']['release'] = '1881-10-10';
  5387. $result = $this->Form->year('Project.release', 1890, 1900);
  5388. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  5389. $result = $matches[1];
  5390. $expected = range(1900, 1881);
  5391. $this->assertEquals($expected, $result);
  5392. }
  5393. /**
  5394. * testInputDate method
  5395. *
  5396. * Test various inputs with type date and different dateFormat values.
  5397. * Failing to provide a dateFormat key should not error.
  5398. * It should simply not pre-select any value then.
  5399. *
  5400. * @return void
  5401. */
  5402. public function testInputDate() {
  5403. $this->markTestIncomplete('Need to revisit once models work again.');
  5404. $this->Form->request->data = array(
  5405. 'User' => array(
  5406. 'month_year' => array('month' => date('m')),
  5407. 'just_year' => array('month' => date('m')),
  5408. 'just_month' => array('year' => date('Y')),
  5409. 'just_day' => array('month' => date('m')),
  5410. )
  5411. );
  5412. $this->Form->create('User');
  5413. $result = $this->Form->input('month_year',
  5414. array(
  5415. 'label' => false,
  5416. 'div' => false,
  5417. 'type' => 'date',
  5418. 'dateFormat' => 'MY',
  5419. 'minYear' => 2006,
  5420. 'maxYear' => 2008
  5421. )
  5422. );
  5423. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  5424. $this->assertNotContains('value="2008" selected="selected"', $result);
  5425. $result = $this->Form->input('just_year',
  5426. array(
  5427. 'type' => 'date',
  5428. 'label' => false,
  5429. 'dateFormat' => 'Y',
  5430. 'minYear' => date('Y'),
  5431. 'maxYear' => date('Y', strtotime('+20 years'))
  5432. )
  5433. );
  5434. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  5435. $result = $this->Form->input('just_month',
  5436. array(
  5437. 'type' => 'date',
  5438. 'label' => false,
  5439. 'dateFormat' => 'M',
  5440. 'empty' => false,
  5441. )
  5442. );
  5443. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  5444. $result = $this->Form->input('just_day',
  5445. array(
  5446. 'type' => 'date',
  5447. 'label' => false,
  5448. 'dateFormat' => 'D',
  5449. 'empty' => false,
  5450. )
  5451. );
  5452. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  5453. }
  5454. /**
  5455. * testInputDateMaxYear method
  5456. *
  5457. * Let's say we want to only allow users born from 2006 to 2008 to register
  5458. * This being the first singup page, we still don't have any data
  5459. *
  5460. * @return void
  5461. */
  5462. public function testInputDateMaxYear() {
  5463. $this->markTestIncomplete('Need to revisit once models work again.');
  5464. $this->Form->request->data = array();
  5465. $this->Form->create('User');
  5466. $result = $this->Form->input('birthday',
  5467. array(
  5468. 'label' => false,
  5469. 'div' => false,
  5470. 'type' => 'date',
  5471. 'dateFormat' => 'DMY',
  5472. 'minYear' => 2006,
  5473. 'maxYear' => 2008
  5474. )
  5475. );
  5476. $this->assertContains('value="2008" selected="selected"', $result);
  5477. }
  5478. /**
  5479. * testTextArea method
  5480. *
  5481. * @return void
  5482. */
  5483. public function testTextArea() {
  5484. $this->Form->request->data = array('field' => 'some test data');
  5485. $result = $this->Form->textarea('field');
  5486. $expected = array(
  5487. 'textarea' => array('name' => 'field'),
  5488. 'some test data',
  5489. '/textarea',
  5490. );
  5491. $this->assertTags($result, $expected);
  5492. $result = $this->Form->textarea('user.bio');
  5493. $expected = array(
  5494. 'textarea' => array('name' => 'user[bio]'),
  5495. '/textarea',
  5496. );
  5497. $this->assertTags($result, $expected);
  5498. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  5499. $result = $this->Form->textarea('field');
  5500. $expected = array(
  5501. 'textarea' => array('name' => 'field'),
  5502. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5503. '/textarea',
  5504. );
  5505. $this->assertTags($result, $expected);
  5506. $this->Form->request->data = [
  5507. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  5508. ];
  5509. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  5510. $expected = array(
  5511. 'textarea' => array('name' => 'Model[field]'),
  5512. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5513. '/textarea',
  5514. );
  5515. $this->assertTags($result, $expected);
  5516. $result = $this->Form->textarea('0.OtherModel.field');
  5517. $expected = array(
  5518. 'textarea' => array('name' => '0[OtherModel][field]'),
  5519. '/textarea'
  5520. );
  5521. $this->assertTags($result, $expected);
  5522. }
  5523. /**
  5524. * testTextAreaWithStupidCharacters method
  5525. *
  5526. * test text area with non-ascii characters
  5527. *
  5528. * @return void
  5529. */
  5530. public function testTextAreaWithStupidCharacters() {
  5531. $result = $this->Form->textarea('Post.content', [
  5532. 'value' => "GREAT®",
  5533. 'rows' => '15',
  5534. 'cols' => '75'
  5535. ]);
  5536. $expected = [
  5537. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  5538. 'GREAT®',
  5539. '/textarea',
  5540. ];
  5541. $this->assertTags($result, $expected);
  5542. }
  5543. /**
  5544. * testHiddenField method
  5545. *
  5546. * @return void
  5547. */
  5548. public function testHiddenField() {
  5549. $this->article['errors'] = [
  5550. 'field' => true
  5551. ];
  5552. $this->Form->request->data['field'] = 'test';
  5553. $this->Form->create($this->article);
  5554. $result = $this->Form->hidden('field', array('id' => 'theID'));
  5555. $this->assertTags($result, array(
  5556. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  5557. );
  5558. $result = $this->Form->hidden('field', ['value' => 'my value']);
  5559. $expected = [
  5560. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  5561. ];
  5562. $this->assertTags($result, $expected);
  5563. }
  5564. /**
  5565. * testFileUploadField method
  5566. *
  5567. * @return void
  5568. */
  5569. public function testFileUploadField() {
  5570. $expected = ['input' => ['type' => 'file', 'name' => 'Model[upload]']];
  5571. $result = $this->Form->file('Model.upload');
  5572. $this->assertTags($result, $expected);
  5573. $this->Form->request->data['Model']['upload'] = [
  5574. 'name' => '', 'type' => '', 'tmp_name' => '',
  5575. 'error' => 4, 'size' => 0
  5576. ];
  5577. $result = $this->Form->file('Model.upload');
  5578. $this->assertTags($result, $expected);
  5579. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5580. $result = $this->Form->file('Model.upload');
  5581. $this->assertTags($result, $expected);
  5582. }
  5583. /**
  5584. * test File upload input on a model not used in create();
  5585. *
  5586. * @return void
  5587. */
  5588. public function testFileUploadOnOtherModel() {
  5589. $this->Form->create($this->article, array('type' => 'file'));
  5590. $result = $this->Form->file('ValidateProfile.city');
  5591. $expected = array(
  5592. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]')
  5593. );
  5594. $this->assertTags($result, $expected);
  5595. }
  5596. /**
  5597. * testButton method
  5598. *
  5599. * @return void
  5600. */
  5601. public function testButton() {
  5602. $result = $this->Form->button('Hi');
  5603. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5604. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5605. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5606. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5607. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5608. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5609. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5610. $result = $this->Form->button('No type', array('type' => false));
  5611. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5612. $result = $this->Form->button('Upload Text', array(
  5613. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  5614. 'escape' => false
  5615. ));
  5616. $this->assertNotRegExp('/\&039/', $result);
  5617. }
  5618. /**
  5619. * Test that button() makes unlocked fields by default.
  5620. *
  5621. * @return void
  5622. */
  5623. public function testButtonUnlockedByDefault() {
  5624. $this->Form->request->params['_csrfToken'] = 'secured';
  5625. $this->Form->button('Save', array('name' => 'save'));
  5626. $this->Form->button('Clear');
  5627. $result = $this->Form->unlockField();
  5628. $this->assertEquals(array('save'), $result);
  5629. }
  5630. /**
  5631. * testPostButton method
  5632. *
  5633. * @return void
  5634. */
  5635. public function testPostButton() {
  5636. $result = $this->Form->postButton('Hi', '/controller/action');
  5637. $this->assertTags($result, array(
  5638. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5639. 'div' => array('style' => 'display:none;'),
  5640. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5641. '/div',
  5642. 'button' => array('type' => 'submit'),
  5643. 'Hi',
  5644. '/button',
  5645. '/form'
  5646. ));
  5647. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5648. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  5649. }
  5650. /**
  5651. * Test using postButton with N dimensional data.
  5652. *
  5653. * @return void
  5654. */
  5655. public function testPostButtonNestedData() {
  5656. $data = array(
  5657. 'one' => array(
  5658. 'two' => array(
  5659. 3, 4, 5
  5660. )
  5661. )
  5662. );
  5663. $result = $this->Form->postButton('Send', '/', array('data' => $data));
  5664. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5665. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5666. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5667. }
  5668. /**
  5669. * Test that postButton adds _Token fields.
  5670. *
  5671. * @return void
  5672. */
  5673. public function testSecurePostButton() {
  5674. $this->Form->request->params['_csrfToken'] = 'testkey';
  5675. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  5676. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5677. $expected = array(
  5678. 'form' => array(
  5679. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5680. ),
  5681. array('div' => array('style' => 'display:none;')),
  5682. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5683. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5684. '/div',
  5685. 'button' => array('type' => 'submit'),
  5686. 'Delete',
  5687. '/button',
  5688. array('div' => array('style' => 'display:none;')),
  5689. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5690. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5691. '/div',
  5692. '/form',
  5693. );
  5694. $this->assertTags($result, $expected);
  5695. }
  5696. /**
  5697. * testPostLink method
  5698. *
  5699. * @return void
  5700. */
  5701. public function testPostLink() {
  5702. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5703. $this->assertTags($result, array(
  5704. 'form' => array(
  5705. 'method' => 'post', 'action' => '/posts/delete/1',
  5706. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5707. ),
  5708. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5709. '/form',
  5710. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5711. 'Delete',
  5712. '/a'
  5713. ));
  5714. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5715. $this->assertTags($result, array(
  5716. 'form' => array(
  5717. 'method' => 'post', 'action' => '/posts/delete/1',
  5718. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5719. ),
  5720. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5721. '/form',
  5722. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5723. 'Delete',
  5724. '/a'
  5725. ));
  5726. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5727. $this->assertTags($result, array(
  5728. 'form' => array(
  5729. 'method' => 'post', 'action' => '/posts/delete/1',
  5730. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5731. ),
  5732. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5733. '/form',
  5734. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5735. 'Delete',
  5736. '/a'
  5737. ));
  5738. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  5739. $this->assertTags($result, array(
  5740. 'form' => array(
  5741. 'method' => 'post', 'action' => '/posts/delete/1',
  5742. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5743. ),
  5744. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5745. '/form',
  5746. '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;/'),
  5747. 'Delete',
  5748. '/a'
  5749. ));
  5750. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5751. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  5752. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5753. $this->assertTags($result, array(
  5754. 'form' => array(
  5755. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5756. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5757. ),
  5758. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5759. '/form',
  5760. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5761. 'Delete',
  5762. '/a'
  5763. ));
  5764. $result = $this->Form->postLink(
  5765. '',
  5766. array('controller' => 'items', 'action' => 'delete', 10),
  5767. array('class' => 'btn btn-danger', 'escape' => false),
  5768. 'Confirm thing'
  5769. );
  5770. $this->assertTags($result, array(
  5771. 'form' => array(
  5772. 'method' => 'post', 'action' => '/items/delete/10',
  5773. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5774. ),
  5775. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5776. '/form',
  5777. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5778. '/a'
  5779. ));
  5780. }
  5781. /**
  5782. * Test using postLink with N dimensional data.
  5783. *
  5784. * @return void
  5785. */
  5786. public function testPostLinkNestedData() {
  5787. $data = array(
  5788. 'one' => array(
  5789. 'two' => array(
  5790. 3, 4, 5
  5791. )
  5792. )
  5793. );
  5794. $result = $this->Form->postLink('Send', '/', array('data' => $data));
  5795. $this->assertContains('<input type="hidden" name="one[two][0]" value="3"', $result);
  5796. $this->assertContains('<input type="hidden" name="one[two][1]" value="4"', $result);
  5797. $this->assertContains('<input type="hidden" name="one[two][2]" value="5"', $result);
  5798. }
  5799. /**
  5800. * test creating postLinks after a GET form.
  5801. *
  5802. * @return void
  5803. */
  5804. public function testPostLinkAfterGetForm() {
  5805. $this->Form->request->params['_csrfToken'] = 'testkey';
  5806. $this->Form->request->params['_Token'] = 'val';
  5807. $this->Form->create($this->article, array('type' => 'get'));
  5808. $this->Form->end();
  5809. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5810. $this->assertTags($result, array(
  5811. 'form' => array(
  5812. 'method' => 'post', 'action' => '/posts/delete/1',
  5813. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5814. ),
  5815. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5816. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  5817. 'div' => array('style' => 'display:none;'),
  5818. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  5819. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  5820. '/div',
  5821. '/form',
  5822. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5823. 'Delete',
  5824. '/a'
  5825. ));
  5826. }
  5827. /**
  5828. * Test that postLink adds form tags to view block
  5829. *
  5830. * @return void
  5831. */
  5832. public function testPostLinkFormBuffer() {
  5833. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  5834. $this->assertTags($result, array(
  5835. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5836. 'Delete',
  5837. '/a'
  5838. ));
  5839. $result = $this->View->fetch('postLink');
  5840. $this->assertTags($result, array(
  5841. 'form' => array(
  5842. 'method' => 'post', 'action' => '/posts/delete/1',
  5843. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5844. ),
  5845. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5846. '/form'
  5847. ));
  5848. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  5849. array('inline' => false, 'method' => 'DELETE')
  5850. );
  5851. $this->assertTags($result, array(
  5852. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5853. 'Delete',
  5854. '/a'
  5855. ));
  5856. $result = $this->View->fetch('postLink');
  5857. $this->assertTags($result, array(
  5858. 'form' => array(
  5859. 'method' => 'post', 'action' => '/posts/delete/1',
  5860. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5861. ),
  5862. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5863. '/form',
  5864. array(
  5865. 'form' => array(
  5866. 'method' => 'post', 'action' => '/posts/delete/2',
  5867. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5868. ),
  5869. ),
  5870. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  5871. '/form'
  5872. ));
  5873. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  5874. $this->assertTags($result, array(
  5875. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5876. 'Delete',
  5877. '/a'
  5878. ));
  5879. $result = $this->View->fetch('foobar');
  5880. $this->assertTags($result, array(
  5881. 'form' => array(
  5882. 'method' => 'post', 'action' => '/posts/delete/1',
  5883. 'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5884. ),
  5885. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5886. '/form'
  5887. ));
  5888. }
  5889. /**
  5890. * testSubmitButton method
  5891. *
  5892. * @return void
  5893. */
  5894. public function testSubmitButton() {
  5895. $result = $this->Form->submit('');
  5896. $expected = array(
  5897. 'div' => array('class' => 'submit'),
  5898. 'input' => array('type' => 'submit', 'value' => ''),
  5899. '/div'
  5900. );
  5901. $this->assertTags($result, $expected);
  5902. $result = $this->Form->submit('Test Submit');
  5903. $expected = array(
  5904. 'div' => array('class' => 'submit'),
  5905. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5906. '/div'
  5907. );
  5908. $this->assertTags($result, $expected);
  5909. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5910. $expected = array(
  5911. 'span' => array('class' => 'submit'),
  5912. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5913. '/span'
  5914. );
  5915. $this->assertTags($result, $expected);
  5916. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5917. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5918. $this->assertTags($result, $expected);
  5919. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5920. $expected = array(
  5921. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5922. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5923. '/div'
  5924. );
  5925. $this->assertTags($result, $expected);
  5926. $result = $this->Form->submit('Next >');
  5927. $expected = array(
  5928. 'div' => array('class' => 'submit'),
  5929. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5930. '/div'
  5931. );
  5932. $this->assertTags($result, $expected);
  5933. $result = $this->Form->submit('Next >', array('escape' => false));
  5934. $expected = array(
  5935. 'div' => array('class' => 'submit'),
  5936. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5937. '/div'
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5941. $expected = array(
  5942. 'div' => array('class' => 'submit'),
  5943. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5944. '/div'
  5945. );
  5946. $this->assertTags($result, $expected);
  5947. $before = '--before--';
  5948. $after = '--after--';
  5949. $result = $this->Form->submit('Test', array('before' => $before));
  5950. $expected = array(
  5951. 'div' => array('class' => 'submit'),
  5952. '--before--',
  5953. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5954. '/div'
  5955. );
  5956. $this->assertTags($result, $expected);
  5957. $result = $this->Form->submit('Test', array('after' => $after));
  5958. $expected = array(
  5959. 'div' => array('class' => 'submit'),
  5960. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5961. '--after--',
  5962. '/div'
  5963. );
  5964. $this->assertTags($result, $expected);
  5965. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5966. $expected = array(
  5967. 'div' => array('class' => 'submit'),
  5968. '--before--',
  5969. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5970. '--after--',
  5971. '/div'
  5972. );
  5973. $this->assertTags($result, $expected);
  5974. }
  5975. /**
  5976. * test image submit types.
  5977. *
  5978. * @return void
  5979. */
  5980. public function testSubmitImage() {
  5981. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5982. $expected = array(
  5983. 'div' => array('class' => 'submit'),
  5984. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5985. '/div'
  5986. );
  5987. $this->assertTags($result, $expected);
  5988. $result = $this->Form->submit('/relative/cake.power.gif');
  5989. $expected = array(
  5990. 'div' => array('class' => 'submit'),
  5991. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5992. '/div'
  5993. );
  5994. $this->assertTags($result, $expected);
  5995. $result = $this->Form->submit('cake.power.gif');
  5996. $expected = array(
  5997. 'div' => array('class' => 'submit'),
  5998. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5999. '/div'
  6000. );
  6001. $this->assertTags($result, $expected);
  6002. $result = $this->Form->submit('Not.an.image');
  6003. $expected = array(
  6004. 'div' => array('class' => 'submit'),
  6005. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6006. '/div'
  6007. );
  6008. $this->assertTags($result, $expected);
  6009. $after = '--after--';
  6010. $before = '--before--';
  6011. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6012. $expected = array(
  6013. 'div' => array('class' => 'submit'),
  6014. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6015. '--after--',
  6016. '/div'
  6017. );
  6018. $this->assertTags($result, $expected);
  6019. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6020. $expected = array(
  6021. 'div' => array('class' => 'submit'),
  6022. '--before--',
  6023. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6024. '/div'
  6025. );
  6026. $this->assertTags($result, $expected);
  6027. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6028. $expected = array(
  6029. 'div' => array('class' => 'submit'),
  6030. '--before--',
  6031. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6032. '--after--',
  6033. '/div'
  6034. );
  6035. $this->assertTags($result, $expected);
  6036. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6037. $expected = array(
  6038. 'div' => array('class' => 'submit'),
  6039. '--before--',
  6040. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6041. '--after--',
  6042. '/div'
  6043. );
  6044. $this->assertTags($result, $expected);
  6045. }
  6046. /**
  6047. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6048. * be submitted at a time.
  6049. *
  6050. * @return void
  6051. */
  6052. public function testSubmitUnlockedByDefault() {
  6053. $this->Form->request->params['_Token'] = 'secured';
  6054. $this->Form->submit('Go go');
  6055. $this->Form->submit('Save', array('name' => 'save'));
  6056. $result = $this->Form->unlockField();
  6057. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6058. }
  6059. /**
  6060. * Test submit image with timestamps.
  6061. *
  6062. * @return void
  6063. */
  6064. public function testSubmitImageTimestamp() {
  6065. Configure::write('Asset.timestamp', 'force');
  6066. $result = $this->Form->submit('cake.power.gif');
  6067. $expected = array(
  6068. 'div' => array('class' => 'submit'),
  6069. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6070. '/div'
  6071. );
  6072. $this->assertTags($result, $expected);
  6073. }
  6074. /**
  6075. * test that datetime() works with GET style forms.
  6076. *
  6077. * @return void
  6078. */
  6079. public function testDateTimeWithGetForms() {
  6080. $this->markTestIncomplete('Need to revisit once models work again.');
  6081. extract($this->dateRegex);
  6082. $this->Form->create('Contact', array('type' => 'get'));
  6083. $result = $this->Form->datetime('created');
  6084. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6085. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6086. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6087. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6088. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6089. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6090. }
  6091. /**
  6092. * testEditFormWithData method
  6093. *
  6094. * test auto populating form elements from submitted data.
  6095. *
  6096. * @return void
  6097. */
  6098. public function testEditFormWithData() {
  6099. $this->markTestIncomplete('Need to revisit once models work again.');
  6100. $this->Form->request->data = array('Person' => array(
  6101. 'id' => 1,
  6102. 'first_name' => 'Nate',
  6103. 'last_name' => 'Abele',
  6104. 'email' => 'nate@example.com'
  6105. ));
  6106. $this->Form->request->addParams(array(
  6107. 'models' => array('Person'),
  6108. 'controller' => 'people',
  6109. 'action' => 'add'
  6110. ));
  6111. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6112. $this->Form->create();
  6113. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6114. $expected = array(
  6115. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6116. 'select' => array(
  6117. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6118. ),
  6119. array('option' => array('value' => 1)), 'Nate', '/option',
  6120. array('option' => array('value' => 2)), 'Garrett', '/option',
  6121. array('option' => array('value' => 3)), 'Larry', '/option',
  6122. '/select'
  6123. );
  6124. $this->assertTags($result, $expected);
  6125. }
  6126. /**
  6127. * Test that required fields are created for various types of validation.
  6128. *
  6129. * @return void
  6130. */
  6131. public function testFormInputRequiredDetection() {
  6132. $this->markTestIncomplete('Need to revisit once models work again.');
  6133. $this->Form->create('Contact');
  6134. $result = $this->Form->input('Contact.non_existing');
  6135. $expected = array(
  6136. 'div' => array('class' => 'input text'),
  6137. 'label' => array('for' => 'ContactNonExisting'),
  6138. 'Non Existing',
  6139. '/label',
  6140. 'input' => array(
  6141. 'type' => 'text', 'name' => 'Contact[non_existing]',
  6142. 'id' => 'ContactNonExisting'
  6143. ),
  6144. '/div'
  6145. );
  6146. $this->assertTags($result, $expected);
  6147. $result = $this->Form->input('Contact.imrequired');
  6148. $expected = array(
  6149. 'div' => array('class' => 'input text required'),
  6150. 'label' => array('for' => 'ContactImrequired'),
  6151. 'Imrequired',
  6152. '/label',
  6153. 'input' => array(
  6154. 'type' => 'text', 'name' => 'Contact[imrequired]',
  6155. 'id' => 'ContactImrequired',
  6156. 'required' => 'required'
  6157. ),
  6158. '/div'
  6159. );
  6160. $this->assertTags($result, $expected);
  6161. $result = $this->Form->input('Contact.imalsorequired');
  6162. $expected = array(
  6163. 'div' => array('class' => 'input text required'),
  6164. 'label' => array('for' => 'ContactImalsorequired'),
  6165. 'Imalsorequired',
  6166. '/label',
  6167. 'input' => array(
  6168. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  6169. 'id' => 'ContactImalsorequired',
  6170. 'required' => 'required'
  6171. ),
  6172. '/div'
  6173. );
  6174. $this->assertTags($result, $expected);
  6175. $result = $this->Form->input('Contact.imrequiredtoo');
  6176. $expected = array(
  6177. 'div' => array('class' => 'input text required'),
  6178. 'label' => array('for' => 'ContactImrequiredtoo'),
  6179. 'Imrequiredtoo',
  6180. '/label',
  6181. 'input' => array(
  6182. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  6183. 'id' => 'ContactImrequiredtoo',
  6184. 'required' => 'required'
  6185. ),
  6186. '/div'
  6187. );
  6188. $this->assertTags($result, $expected);
  6189. $result = $this->Form->input('Contact.required_one');
  6190. $expected = array(
  6191. 'div' => array('class' => 'input text required'),
  6192. 'label' => array('for' => 'ContactRequiredOne'),
  6193. 'Required One',
  6194. '/label',
  6195. 'input' => array(
  6196. 'type' => 'text', 'name' => 'Contact[required_one]',
  6197. 'id' => 'ContactRequiredOne',
  6198. 'required' => 'required'
  6199. ),
  6200. '/div'
  6201. );
  6202. $this->assertTags($result, $expected);
  6203. $result = $this->Form->input('Contact.string_required');
  6204. $expected = array(
  6205. 'div' => array('class' => 'input text required'),
  6206. 'label' => array('for' => 'ContactStringRequired'),
  6207. 'String Required',
  6208. '/label',
  6209. 'input' => array(
  6210. 'type' => 'text', 'name' => 'Contact[string_required]',
  6211. 'id' => 'ContactStringRequired',
  6212. 'required' => 'required'
  6213. ),
  6214. '/div'
  6215. );
  6216. $this->assertTags($result, $expected);
  6217. $result = $this->Form->input('Contact.imnotrequired');
  6218. $expected = array(
  6219. 'div' => array('class' => 'input text'),
  6220. 'label' => array('for' => 'ContactImnotrequired'),
  6221. 'Imnotrequired',
  6222. '/label',
  6223. 'input' => array(
  6224. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  6225. 'id' => 'ContactImnotrequired'
  6226. ),
  6227. '/div'
  6228. );
  6229. $this->assertTags($result, $expected);
  6230. $result = $this->Form->input('Contact.imalsonotrequired');
  6231. $expected = array(
  6232. 'div' => array('class' => 'input text'),
  6233. 'label' => array('for' => 'ContactImalsonotrequired'),
  6234. 'Imalsonotrequired',
  6235. '/label',
  6236. 'input' => array(
  6237. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  6238. 'id' => 'ContactImalsonotrequired'
  6239. ),
  6240. '/div'
  6241. );
  6242. $this->assertTags($result, $expected);
  6243. $result = $this->Form->input('Contact.imalsonotrequired2');
  6244. $expected = array(
  6245. 'div' => array('class' => 'input text'),
  6246. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6247. 'Imalsonotrequired2',
  6248. '/label',
  6249. 'input' => array(
  6250. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  6251. 'id' => 'ContactImalsonotrequired2'
  6252. ),
  6253. '/div'
  6254. );
  6255. $this->assertTags($result, $expected);
  6256. $result = $this->Form->input('Contact.imnotrequiredeither');
  6257. $expected = array(
  6258. 'div' => array('class' => 'input text'),
  6259. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6260. 'Imnotrequiredeither',
  6261. '/label',
  6262. 'input' => array(
  6263. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  6264. 'id' => 'ContactImnotrequiredeither'
  6265. ),
  6266. '/div'
  6267. );
  6268. $this->assertTags($result, $expected);
  6269. $result = $this->Form->input('Contact.iamrequiredalways');
  6270. $expected = array(
  6271. 'div' => array('class' => 'input text required'),
  6272. 'label' => array('for' => 'ContactIamrequiredalways'),
  6273. 'Iamrequiredalways',
  6274. '/label',
  6275. 'input' => array(
  6276. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  6277. 'id' => 'ContactIamrequiredalways',
  6278. 'required' => 'required'
  6279. ),
  6280. '/div'
  6281. );
  6282. $this->assertTags($result, $expected);
  6283. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6284. $expected = array(
  6285. 'div' => array('class' => 'input checkbox required'),
  6286. array('input' => array(
  6287. 'type' => 'hidden',
  6288. 'name' => 'Contact[boolean_field]',
  6289. 'id' => 'ContactBooleanField_',
  6290. 'value' => '0'
  6291. )),
  6292. array('input' => array(
  6293. 'type' => 'checkbox',
  6294. 'name' => 'Contact[boolean_field]',
  6295. 'value' => '1',
  6296. 'id' => 'ContactBooleanField'
  6297. )),
  6298. 'label' => array('for' => 'ContactBooleanField'),
  6299. 'Boolean Field',
  6300. '/label',
  6301. '/div'
  6302. );
  6303. $this->assertTags($result, $expected);
  6304. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  6305. $expected = array(
  6306. 'div' => array('class' => 'input checkbox required'),
  6307. array('input' => array(
  6308. 'type' => 'hidden',
  6309. 'name' => 'Contact[boolean_field]',
  6310. 'id' => 'ContactBooleanField_',
  6311. 'value' => '0'
  6312. )),
  6313. array('input' => array(
  6314. 'type' => 'checkbox',
  6315. 'name' => 'Contact[boolean_field]',
  6316. 'value' => '1',
  6317. 'id' => 'ContactBooleanField',
  6318. 'required' => 'required'
  6319. )),
  6320. 'label' => array('for' => 'ContactBooleanField'),
  6321. 'Boolean Field',
  6322. '/label',
  6323. '/div'
  6324. );
  6325. $this->assertTags($result, $expected);
  6326. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  6327. $expected = array(
  6328. 'div' => array('class' => 'input file required'),
  6329. 'label' => array('for' => 'ContactIamrequiredalways'),
  6330. 'Iamrequiredalways',
  6331. '/label',
  6332. 'input' => array(
  6333. 'type' => 'file',
  6334. 'name' => 'data[Contact][iamrequiredalways]',
  6335. 'id' => 'ContactIamrequiredalways',
  6336. 'required' => 'required'
  6337. ),
  6338. '/div'
  6339. );
  6340. $this->assertTags($result, $expected);
  6341. }
  6342. /**
  6343. * Test that required fields are created when only using ModelValidator::add().
  6344. *
  6345. * @return void
  6346. */
  6347. public function testFormInputRequiredDetectionModelValidator() {
  6348. $this->markTestIncomplete('Need to revisit once models work again.');
  6349. $this->Form->create('ContactTag');
  6350. $result = $this->Form->input('ContactTag.iwillberequired');
  6351. $expected = array(
  6352. 'div' => array('class' => 'input text required'),
  6353. 'label' => array('for' => 'ContactTagIwillberequired'),
  6354. 'Iwillberequired',
  6355. '/label',
  6356. 'input' => array(
  6357. 'name' => 'data[ContactTag][iwillberequired]',
  6358. 'type' => 'text',
  6359. 'id' => 'ContactTagIwillberequired',
  6360. 'required' => 'required'
  6361. ),
  6362. '/div'
  6363. );
  6364. $this->assertTags($result, $expected);
  6365. }
  6366. /**
  6367. * testFormMagicInput method
  6368. *
  6369. * @return void
  6370. */
  6371. public function testFormMagicInput() {
  6372. $this->markTestIncomplete('Need to revisit once models work again.');
  6373. $encoding = strtolower(Configure::read('App.encoding'));
  6374. $result = $this->Form->create('Contact');
  6375. $expected = array(
  6376. 'form' => array(
  6377. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6378. 'accept-charset' => $encoding
  6379. ),
  6380. 'div' => array('style' => 'display:none;'),
  6381. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6382. '/div'
  6383. );
  6384. $this->assertTags($result, $expected);
  6385. $result = $this->Form->input('name');
  6386. $expected = array(
  6387. 'div' => array('class' => 'input text'),
  6388. 'label' => array('for' => 'ContactName'),
  6389. 'Name',
  6390. '/label',
  6391. 'input' => array(
  6392. 'type' => 'text', 'name' => 'Contact[name]',
  6393. 'id' => 'ContactName', 'maxlength' => '255'
  6394. ),
  6395. '/div'
  6396. );
  6397. $this->assertTags($result, $expected);
  6398. $result = $this->Form->input('non_existing_field_in_contact_model');
  6399. $expected = array(
  6400. 'div' => array('class' => 'input text'),
  6401. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6402. 'Non Existing Field In Contact Model',
  6403. '/label',
  6404. 'input' => array(
  6405. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  6406. 'id' => 'ContactNonExistingFieldInContactModel'
  6407. ),
  6408. '/div'
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. $result = $this->Form->input('Address.street');
  6412. $expected = array(
  6413. 'div' => array('class' => 'input text'),
  6414. 'label' => array('for' => 'AddressStreet'),
  6415. 'Street',
  6416. '/label',
  6417. 'input' => array(
  6418. 'type' => 'text', 'name' => 'Address[street]',
  6419. 'id' => 'AddressStreet'
  6420. ),
  6421. '/div'
  6422. );
  6423. $this->assertTags($result, $expected);
  6424. $result = $this->Form->input('Address.non_existing_field_in_model');
  6425. $expected = array(
  6426. 'div' => array('class' => 'input text'),
  6427. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6428. 'Non Existing Field In Model',
  6429. '/label',
  6430. 'input' => array(
  6431. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  6432. 'id' => 'AddressNonExistingFieldInModel'
  6433. ),
  6434. '/div'
  6435. );
  6436. $this->assertTags($result, $expected);
  6437. $result = $this->Form->input('name', array('div' => false));
  6438. $expected = array(
  6439. 'label' => array('for' => 'ContactName'),
  6440. 'Name',
  6441. '/label',
  6442. 'input' => array(
  6443. 'type' => 'text', 'name' => 'Contact[name]',
  6444. 'id' => 'ContactName', 'maxlength' => '255'
  6445. )
  6446. );
  6447. $this->assertTags($result, $expected);
  6448. extract($this->dateRegex);
  6449. $now = strtotime('now');
  6450. $result = $this->Form->input('Contact.published', array('div' => false));
  6451. $expected = array(
  6452. 'label' => array('for' => 'ContactPublishedMonth'),
  6453. 'Published',
  6454. '/label',
  6455. array('select' => array(
  6456. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  6457. )),
  6458. $monthsRegex,
  6459. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6460. date('F', $now),
  6461. '/option',
  6462. '*/select',
  6463. '-',
  6464. array('select' => array(
  6465. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  6466. )),
  6467. $daysRegex,
  6468. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6469. date('j', $now),
  6470. '/option',
  6471. '*/select',
  6472. '-',
  6473. array('select' => array(
  6474. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  6475. )),
  6476. $yearsRegex,
  6477. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6478. date('Y', $now),
  6479. '*/select'
  6480. );
  6481. $this->assertTags($result, $expected);
  6482. $result = $this->Form->input('Contact.updated', array('div' => false));
  6483. $expected = array(
  6484. 'label' => array('for' => 'ContactUpdatedMonth'),
  6485. 'Updated',
  6486. '/label',
  6487. array('select' => array(
  6488. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  6489. )),
  6490. $monthsRegex,
  6491. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6492. date('F', $now),
  6493. '/option',
  6494. '*/select',
  6495. '-',
  6496. array('select' => array(
  6497. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  6498. )),
  6499. $daysRegex,
  6500. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6501. date('j', $now),
  6502. '/option',
  6503. '*/select',
  6504. '-',
  6505. array('select' => array(
  6506. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  6507. )),
  6508. $yearsRegex,
  6509. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6510. date('Y', $now),
  6511. '*/select'
  6512. );
  6513. $this->assertTags($result, $expected);
  6514. $result = $this->Form->input('UserForm.stuff');
  6515. $expected = array(
  6516. 'div' => array('class' => 'input text'),
  6517. 'label' => array('for' => 'UserFormStuff'),
  6518. 'Stuff',
  6519. '/label',
  6520. 'input' => array(
  6521. 'type' => 'text', 'name' => 'UserForm[stuff]',
  6522. 'id' => 'UserFormStuff', 'maxlength' => 10
  6523. ),
  6524. '/div'
  6525. );
  6526. $this->assertTags($result, $expected);
  6527. }
  6528. /**
  6529. * testForMagicInputNonExistingNorValidated method
  6530. *
  6531. * @return void
  6532. */
  6533. public function testForMagicInputNonExistingNorValidated() {
  6534. $this->markTestIncomplete('Need to revisit once models work again.');
  6535. $encoding = strtolower(Configure::read('App.encoding'));
  6536. $result = $this->Form->create('Contact');
  6537. $expected = array(
  6538. 'form' => array(
  6539. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6540. 'accept-charset' => $encoding
  6541. ),
  6542. 'div' => array('style' => 'display:none;'),
  6543. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6544. '/div'
  6545. );
  6546. $this->assertTags($result, $expected);
  6547. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6548. $expected = array(
  6549. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6550. 'Non Existing Nor Validated',
  6551. '/label',
  6552. 'input' => array(
  6553. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6554. 'id' => 'ContactNonExistingNorValidated'
  6555. )
  6556. );
  6557. $this->assertTags($result, $expected);
  6558. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6559. 'div' => false, 'value' => 'my value'
  6560. ));
  6561. $expected = array(
  6562. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6563. 'Non Existing Nor Validated',
  6564. '/label',
  6565. 'input' => array(
  6566. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6567. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6568. )
  6569. );
  6570. $this->assertTags($result, $expected);
  6571. $this->Form->request->data = array(
  6572. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6573. ));
  6574. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6575. $expected = array(
  6576. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6577. 'Non Existing Nor Validated',
  6578. '/label',
  6579. 'input' => array(
  6580. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  6581. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6582. )
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. }
  6586. /**
  6587. * testFormMagicInputLabel method
  6588. *
  6589. * @return void
  6590. */
  6591. public function testFormMagicInputLabel() {
  6592. $this->markTestIncomplete('Need to revisit once models work again.');
  6593. $encoding = strtolower(Configure::read('App.encoding'));
  6594. $result = $this->Form->create('Contact');
  6595. $expected = array(
  6596. 'form' => array(
  6597. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6598. 'accept-charset' => $encoding
  6599. ),
  6600. 'div' => array('style' => 'display:none;'),
  6601. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6602. '/div'
  6603. );
  6604. $this->assertTags($result, $expected);
  6605. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6606. $this->assertTags($result, array('input' => array(
  6607. 'name' => 'Contact[name]', 'type' => 'text',
  6608. 'id' => 'ContactName', 'maxlength' => '255')
  6609. ));
  6610. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6611. $expected = array(
  6612. 'label' => array('for' => 'ContactName'),
  6613. 'My label',
  6614. '/label',
  6615. 'input' => array(
  6616. 'type' => 'text', 'name' => 'Contact[name]',
  6617. 'id' => 'ContactName', 'maxlength' => '255'
  6618. )
  6619. );
  6620. $this->assertTags($result, $expected);
  6621. $result = $this->Form->input('Contact.name', array(
  6622. 'div' => false, 'label' => array('class' => 'mandatory')
  6623. ));
  6624. $expected = array(
  6625. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6626. 'Name',
  6627. '/label',
  6628. 'input' => array(
  6629. 'type' => 'text', 'name' => 'Contact[name]',
  6630. 'id' => 'ContactName', 'maxlength' => '255'
  6631. )
  6632. );
  6633. $this->assertTags($result, $expected);
  6634. $result = $this->Form->input('Contact.name', array(
  6635. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6636. ));
  6637. $expected = array(
  6638. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6639. 'My label',
  6640. '/label',
  6641. 'input' => array(
  6642. 'type' => 'text', 'name' => 'Contact[name]',
  6643. 'id' => 'ContactName', 'maxlength' => '255'
  6644. )
  6645. );
  6646. $this->assertTags($result, $expected);
  6647. $result = $this->Form->input('Contact.name', array(
  6648. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6649. ));
  6650. $expected = array(
  6651. 'label' => array('for' => 'my_id'),
  6652. 'Name',
  6653. '/label',
  6654. 'input' => array(
  6655. 'type' => 'text', 'name' => 'Contact[name]',
  6656. 'id' => 'my_id', 'maxlength' => '255'
  6657. )
  6658. );
  6659. $this->assertTags($result, $expected);
  6660. $result = $this->Form->input('1.id');
  6661. $this->assertTags($result, array('input' => array(
  6662. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6663. 'id' => 'Contact1Id'
  6664. )));
  6665. $result = $this->Form->input("1.name");
  6666. $expected = array(
  6667. 'div' => array('class' => 'input text'),
  6668. 'label' => array('for' => 'Contact1Name'),
  6669. 'Name',
  6670. '/label',
  6671. 'input' => array(
  6672. 'type' => 'text', 'name' => 'Contact[1][name]',
  6673. 'id' => 'Contact1Name', 'maxlength' => '255'
  6674. ),
  6675. '/div'
  6676. );
  6677. $this->assertTags($result, $expected);
  6678. $result = $this->Form->input('Contact.1.id');
  6679. $this->assertTags($result, array(
  6680. 'input' => array(
  6681. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  6682. 'id' => 'Contact1Id'
  6683. )
  6684. ));
  6685. $result = $this->Form->input("Model.1.name");
  6686. $expected = array(
  6687. 'div' => array('class' => 'input text'),
  6688. 'label' => array('for' => 'Model1Name'),
  6689. 'Name',
  6690. '/label',
  6691. 'input' => array(
  6692. 'type' => 'text', 'name' => 'Model[1][name]',
  6693. 'id' => 'Model1Name'
  6694. ),
  6695. '/div'
  6696. );
  6697. $this->assertTags($result, $expected);
  6698. }
  6699. /**
  6700. * testFormEnd method
  6701. *
  6702. * @return void
  6703. */
  6704. public function testFormEnd() {
  6705. $this->assertEquals('</form>', $this->Form->end());
  6706. }
  6707. /**
  6708. * testMultipleFormWithIdFields method
  6709. *
  6710. * @return void
  6711. */
  6712. public function testMultipleFormWithIdFields() {
  6713. $this->markTestIncomplete('Need to revisit once models work again.');
  6714. $this->Form->create('UserForm');
  6715. $result = $this->Form->input('id');
  6716. $this->assertTags($result, array('input' => array(
  6717. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  6718. )));
  6719. $result = $this->Form->input('ValidateItem.id');
  6720. $this->assertTags($result, array('input' => array(
  6721. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  6722. 'id' => 'ValidateItemId'
  6723. )));
  6724. $result = $this->Form->input('ValidateUser.id');
  6725. $this->assertTags($result, array('input' => array(
  6726. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  6727. 'id' => 'ValidateUserId'
  6728. )));
  6729. }
  6730. /**
  6731. * testDbLessModel method
  6732. *
  6733. * @return void
  6734. */
  6735. public function testDbLessModel() {
  6736. $this->markTestIncomplete('Need to revisit once models work again.');
  6737. $this->Form->create('TestMail');
  6738. $result = $this->Form->input('name');
  6739. $expected = array(
  6740. 'div' => array('class' => 'input text'),
  6741. 'label' => array('for' => 'TestMailName'),
  6742. 'Name',
  6743. '/label',
  6744. 'input' => array(
  6745. 'name' => 'TestMail[name]', 'type' => 'text',
  6746. 'id' => 'TestMailName'
  6747. ),
  6748. '/div'
  6749. );
  6750. $this->assertTags($result, $expected);
  6751. $this->Form->create('TestMail');
  6752. $result = $this->Form->input('name');
  6753. $expected = array(
  6754. 'div' => array('class' => 'input text'),
  6755. 'label' => array('for' => 'TestMailName'),
  6756. 'Name',
  6757. '/label',
  6758. 'input' => array(
  6759. 'name' => 'TestMail[name]', 'type' => 'text',
  6760. 'id' => 'TestMailName'
  6761. ),
  6762. '/div'
  6763. );
  6764. $this->assertTags($result, $expected);
  6765. }
  6766. /**
  6767. * testBrokenness method
  6768. *
  6769. * @return void
  6770. */
  6771. public function testBrokenness() {
  6772. $this->markTestIncomplete('Need to revisit once models work again.');
  6773. /*
  6774. * #4 This test has two parents and four children. By default (as of r7117) both
  6775. * parents are show but the first parent is missing a child. This is the inconsistency
  6776. * in the default behaviour - one parent has all children, the other does not - dependent
  6777. * on the data values.
  6778. */
  6779. $result = $this->Form->select('Model.field', array(
  6780. 'Fred' => array(
  6781. 'freds_son_1' => 'Fred',
  6782. 'freds_son_2' => 'Freddie'
  6783. ),
  6784. 'Bert' => array(
  6785. 'berts_son_1' => 'Albert',
  6786. 'berts_son_2' => 'Bertie')
  6787. ),
  6788. array('showParents' => true, 'empty' => false)
  6789. );
  6790. $expected = array(
  6791. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6792. array('optgroup' => array('label' => 'Fred')),
  6793. array('option' => array('value' => 'freds_son_1')),
  6794. 'Fred',
  6795. '/option',
  6796. array('option' => array('value' => 'freds_son_2')),
  6797. 'Freddie',
  6798. '/option',
  6799. '/optgroup',
  6800. array('optgroup' => array('label' => 'Bert')),
  6801. array('option' => array('value' => 'berts_son_1')),
  6802. 'Albert',
  6803. '/option',
  6804. array('option' => array('value' => 'berts_son_2')),
  6805. 'Bertie',
  6806. '/option',
  6807. '/optgroup',
  6808. '/select'
  6809. );
  6810. $this->assertTags($result, $expected);
  6811. /*
  6812. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6813. * changed, so we should expect the same select list data, just with a different name
  6814. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6815. * This is where data corruption can occur, because when a select value is missing from
  6816. * a list a form will substitute the first value in the list - without the user knowing.
  6817. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6818. * affect the availability of 3 => 'Three' as a valid option.
  6819. */
  6820. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6821. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6822. ));
  6823. $result = $this->Form->select(
  6824. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6825. );
  6826. $expected = array(
  6827. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  6828. array('option' => array('value' => 1)),
  6829. 'One',
  6830. '/option',
  6831. array('option' => array('value' => 2)),
  6832. 'Two',
  6833. '/option',
  6834. array('optgroup' => array('label' => 'Three')),
  6835. array('option' => array('value' => 3)),
  6836. 'Three',
  6837. '/option',
  6838. array('option' => array('value' => 4)),
  6839. 'Four',
  6840. '/option',
  6841. array('option' => array('value' => 5)),
  6842. 'Five',
  6843. '/option',
  6844. '/optgroup',
  6845. '/select'
  6846. );
  6847. $this->assertTags($result, $expected);
  6848. }
  6849. /**
  6850. * Test the generation of fields for a multi record form.
  6851. *
  6852. * @return void
  6853. */
  6854. public function testMultiRecordForm() {
  6855. $this->markTestIncomplete('Need to revisit once models work again.');
  6856. $this->Form->create('ValidateProfile');
  6857. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6858. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6859. $expected = array(
  6860. 'div' => array('class' => 'input textarea'),
  6861. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6862. 'Name',
  6863. '/label',
  6864. 'textarea' => array(
  6865. 'id' => 'ValidateProfile1ValidateItem2Name',
  6866. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  6867. 'cols' => 30,
  6868. 'rows' => 6
  6869. ),
  6870. 'Value',
  6871. '/textarea',
  6872. '/div'
  6873. );
  6874. $this->assertTags($result, $expected);
  6875. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  6876. $expected = array(
  6877. 'div' => array('class' => 'input date'),
  6878. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6879. 'Created',
  6880. '/label',
  6881. array('select' => array(
  6882. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  6883. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6884. )
  6885. ),
  6886. array('option' => array('value' => '')), '/option',
  6887. $this->dateRegex['monthsRegex'],
  6888. '/select', '-',
  6889. array('select' => array(
  6890. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  6891. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6892. )
  6893. ),
  6894. array('option' => array('value' => '')), '/option',
  6895. $this->dateRegex['daysRegex'],
  6896. '/select', '-',
  6897. array('select' => array(
  6898. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  6899. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6900. )
  6901. ),
  6902. array('option' => array('value' => '')), '/option',
  6903. $this->dateRegex['yearsRegex'],
  6904. '/select',
  6905. '/div'
  6906. );
  6907. $this->assertTags($result, $expected);
  6908. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6909. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6910. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6911. $expected = array(
  6912. 'div' => array('class' => 'input select error'),
  6913. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6914. 'Profile',
  6915. '/label',
  6916. 'select' => array(
  6917. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  6918. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6919. 'class' => 'form-error'
  6920. ),
  6921. '/select',
  6922. array('div' => array('class' => 'error-message')),
  6923. 'Error',
  6924. '/div',
  6925. '/div'
  6926. );
  6927. $this->assertTags($result, $expected);
  6928. }
  6929. /**
  6930. * test the correct display of multi-record form validation errors.
  6931. *
  6932. * @return void
  6933. */
  6934. public function testMultiRecordFormValidationErrors() {
  6935. $this->markTestIncomplete('Need to revisit once models work again.');
  6936. $this->Form->create('ValidateProfile');
  6937. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6938. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6939. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6940. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6941. $result = $this->Form->error('ValidateProfile.2.city');
  6942. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6943. $result = $this->Form->error('2.city');
  6944. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6945. }
  6946. /**
  6947. * test the correct display of multi-record form validation errors.
  6948. *
  6949. * @return void
  6950. */
  6951. public function testSaveManyRecordFormValidationErrors() {
  6952. $this->markTestIncomplete('Need to revisit once models work again.');
  6953. $this->Form->create('ValidateUser');
  6954. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  6955. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  6956. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6957. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  6958. $result = $this->Form->error('ValidateUser.0.city');
  6959. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6960. }
  6961. /**
  6962. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  6963. *
  6964. * @return void
  6965. */
  6966. public function testInputTemplate() {
  6967. $this->markTestIncomplete('Need to revisit once models work again.');
  6968. $result = $this->Form->input('Contact.email', array(
  6969. 'type' => 'text', 'format' => array('input')
  6970. ));
  6971. $expected = array(
  6972. 'div' => array('class' => 'input text'),
  6973. 'input' => array(
  6974. 'type' => 'text', 'name' => 'Contact[email]',
  6975. 'id' => 'ContactEmail'
  6976. ),
  6977. '/div'
  6978. );
  6979. $this->assertTags($result, $expected);
  6980. $result = $this->Form->input('Contact.email', array(
  6981. 'type' => 'text', 'format' => array('input', 'label'),
  6982. 'label' => '<em>Email (required)</em>'
  6983. ));
  6984. $expected = array(
  6985. 'div' => array('class' => 'input text'),
  6986. array('input' => array(
  6987. 'type' => 'text', 'name' => 'Contact[email]',
  6988. 'id' => 'ContactEmail'
  6989. )),
  6990. 'label' => array('for' => 'ContactEmail'),
  6991. 'em' => array(),
  6992. 'Email (required)',
  6993. '/em',
  6994. '/label',
  6995. '/div'
  6996. );
  6997. $this->assertTags($result, $expected);
  6998. $result = $this->Form->input('Contact.email', array(
  6999. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7000. 'between' => '<div>Something in the middle</div>',
  7001. 'after' => '<span>Some text at the end</span>'
  7002. ));
  7003. $expected = array(
  7004. 'div' => array('class' => 'input text'),
  7005. array('input' => array(
  7006. 'type' => 'text', 'name' => 'Contact[email]',
  7007. 'id' => 'ContactEmail'
  7008. )),
  7009. array('div' => array()),
  7010. 'Something in the middle',
  7011. '/div',
  7012. 'label' => array('for' => 'ContactEmail'),
  7013. 'Email',
  7014. '/label',
  7015. 'span' => array(),
  7016. 'Some text at the end',
  7017. '/span',
  7018. '/div'
  7019. );
  7020. $this->assertTags($result, $expected);
  7021. $result = $this->Form->input('Contact.method', array(
  7022. 'type' => 'radio',
  7023. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7024. 'between' => 'I am between',
  7025. ));
  7026. $expected = array(
  7027. 'div' => array('class' => 'input radio'),
  7028. 'fieldset' => array(),
  7029. 'legend' => array(),
  7030. 'Method',
  7031. '/legend',
  7032. 'I am between',
  7033. 'input' => array(
  7034. 'type' => 'hidden', 'name' => 'Contact[method]',
  7035. 'value' => '', 'id' => 'ContactMethod_'
  7036. ),
  7037. array('input' => array(
  7038. 'type' => 'radio', 'name' => 'Contact[method]',
  7039. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7040. )),
  7041. array('label' => array('for' => 'ContactMethodEmail')),
  7042. 'Email',
  7043. '/label',
  7044. array('input' => array(
  7045. 'type' => 'radio', 'name' => 'Contact[method]',
  7046. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7047. )),
  7048. array('label' => array('for' => 'ContactMethodPigeon')),
  7049. 'Pigeon',
  7050. '/label',
  7051. '/fieldset',
  7052. '/div',
  7053. );
  7054. $this->assertTags($result, $expected);
  7055. }
  7056. /**
  7057. * test that some html5 inputs + FormHelper::__call() work
  7058. *
  7059. * @return void
  7060. */
  7061. public function testHtml5Inputs() {
  7062. $result = $this->Form->email('User.email');
  7063. $expected = array(
  7064. 'input' => array('type' => 'email', 'name' => 'User[email]')
  7065. );
  7066. $this->assertTags($result, $expected);
  7067. $result = $this->Form->search('User.query');
  7068. $expected = array(
  7069. 'input' => array('type' => 'search', 'name' => 'User[query]')
  7070. );
  7071. $this->assertTags($result, $expected);
  7072. $result = $this->Form->search('User.query', array('value' => 'test'));
  7073. $expected = array(
  7074. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  7075. );
  7076. $this->assertTags($result, $expected);
  7077. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7078. $expected = array(
  7079. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  7080. );
  7081. $this->assertTags($result, $expected);
  7082. }
  7083. /**
  7084. * Test accessing html5 inputs through input().
  7085. *
  7086. * @return void
  7087. */
  7088. public function testHtml5InputWithInput() {
  7089. $this->markTestIncomplete('Need to revisit once models work again.');
  7090. $result = $this->Form->input('User.website', array(
  7091. 'type' => 'url',
  7092. 'value' => 'http://domain.tld',
  7093. 'div' => false,
  7094. 'label' => false));
  7095. $expected = array(
  7096. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7097. );
  7098. $this->assertTags($result, $expected);
  7099. }
  7100. /**
  7101. * Test errors when field name is missing.
  7102. *
  7103. * @expectedException \Cake\Error\Exception
  7104. * @return void
  7105. */
  7106. public function testHtml5InputException() {
  7107. $this->Form->email();
  7108. }
  7109. /**
  7110. * Tests that a model can be loaded from the model names passed in the request object
  7111. *
  7112. * @return void
  7113. */
  7114. public function testIntrospectModelFromRequest() {
  7115. $this->markTestIncomplete('Need to revisit once models work again.');
  7116. $this->loadFixtures('Post');
  7117. App::build(array(
  7118. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  7119. ));
  7120. Plugin::load('TestPlugin');
  7121. $this->Form->request['models'] = array(
  7122. 'TestPluginPost' => array(
  7123. 'plugin' => 'TestPlugin',
  7124. 'className' => 'TestPluginPost'
  7125. )
  7126. );
  7127. $this->Form->create('TestPluginPost');
  7128. Plugin::unload();
  7129. }
  7130. /**
  7131. * Tests that it is possible to set the validation errors directly in the helper for a field
  7132. *
  7133. * @return void
  7134. */
  7135. public function testCustomValidationErrors() {
  7136. $this->markTestIncomplete('Need to revisit once models work again.');
  7137. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7138. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7139. $this->assertEquals('Badness!', $result);
  7140. }
  7141. /**
  7142. * Tests that the 'on' key validates as expected on create
  7143. *
  7144. * @return void
  7145. */
  7146. public function testRequiredOnCreate() {
  7147. $this->markTestIncomplete('Need to revisit once models work again.');
  7148. $this->Form->create('Contact');
  7149. $result = $this->Form->input('Contact.imrequiredonupdate');
  7150. $expected = array(
  7151. 'div' => array('class' => 'input text'),
  7152. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7153. 'Imrequiredonupdate',
  7154. '/label',
  7155. 'input' => array(
  7156. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7157. 'id' => 'ContactImrequiredonupdate'
  7158. ),
  7159. '/div'
  7160. );
  7161. $this->assertTags($result, $expected);
  7162. $result = $this->Form->input('Contact.imrequiredoncreate');
  7163. $expected = array(
  7164. 'div' => array('class' => 'input text required'),
  7165. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7166. 'Imrequiredoncreate',
  7167. '/label',
  7168. 'input' => array(
  7169. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7170. 'id' => 'ContactImrequiredoncreate',
  7171. 'required' => 'required'
  7172. ),
  7173. '/div'
  7174. );
  7175. $this->assertTags($result, $expected);
  7176. $result = $this->Form->input('Contact.imrequiredonboth');
  7177. $expected = array(
  7178. 'div' => array('class' => 'input text required'),
  7179. 'label' => array('for' => 'ContactImrequiredonboth'),
  7180. 'Imrequiredonboth',
  7181. '/label',
  7182. 'input' => array(
  7183. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7184. 'id' => 'ContactImrequiredonboth',
  7185. 'required' => 'required'
  7186. ),
  7187. '/div'
  7188. );
  7189. $this->assertTags($result, $expected);
  7190. $this->Form->inputDefaults(array('required' => false));
  7191. $result = $this->Form->input('Contact.imrequired');
  7192. $expected = array(
  7193. 'div' => array('class' => 'input text'),
  7194. 'label' => array('for' => 'ContactImrequired'),
  7195. 'Imrequired',
  7196. '/label',
  7197. 'input' => array(
  7198. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7199. 'id' => 'ContactImrequired'
  7200. ),
  7201. '/div'
  7202. );
  7203. $this->assertTags($result, $expected);
  7204. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7205. $this->assertTags($result, $expected);
  7206. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  7207. $expected = array(
  7208. 'div' => array('class' => 'input text required'),
  7209. 'label' => array('for' => 'ContactImrequired'),
  7210. 'Imrequired',
  7211. '/label',
  7212. 'input' => array(
  7213. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7214. 'id' => 'ContactImrequired'
  7215. ),
  7216. '/div'
  7217. );
  7218. $this->assertTags($result, $expected);
  7219. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  7220. $this->assertTags($result, $expected);
  7221. }
  7222. /**
  7223. * Tests that the 'on' key validates as expected on update
  7224. *
  7225. * @return void
  7226. */
  7227. public function testRequiredOnUpdate() {
  7228. $this->markTestIncomplete('Need to revisit once models work again.');
  7229. $this->Form->request->data['Contact']['id'] = 1;
  7230. $this->Form->create('Contact');
  7231. $result = $this->Form->input('Contact.imrequiredonupdate');
  7232. $expected = array(
  7233. 'div' => array('class' => 'input text required'),
  7234. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7235. 'Imrequiredonupdate',
  7236. '/label',
  7237. 'input' => array(
  7238. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7239. 'id' => 'ContactImrequiredonupdate',
  7240. 'required' => 'required'
  7241. ),
  7242. '/div'
  7243. );
  7244. $this->assertTags($result, $expected);
  7245. $result = $this->Form->input('Contact.imrequiredoncreate');
  7246. $expected = array(
  7247. 'div' => array('class' => 'input text'),
  7248. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7249. 'Imrequiredoncreate',
  7250. '/label',
  7251. 'input' => array(
  7252. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7253. 'id' => 'ContactImrequiredoncreate'
  7254. ),
  7255. '/div'
  7256. );
  7257. $this->assertTags($result, $expected);
  7258. $result = $this->Form->input('Contact.imrequiredonboth');
  7259. $expected = array(
  7260. 'div' => array('class' => 'input text required'),
  7261. 'label' => array('for' => 'ContactImrequiredonboth'),
  7262. 'Imrequiredonboth',
  7263. '/label',
  7264. 'input' => array(
  7265. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7266. 'id' => 'ContactImrequiredonboth',
  7267. 'required' => 'required'
  7268. ),
  7269. '/div'
  7270. );
  7271. $this->assertTags($result, $expected);
  7272. $result = $this->Form->input('Contact.imrequired');
  7273. $expected = array(
  7274. 'div' => array('class' => 'input text required'),
  7275. 'label' => array('for' => 'ContactImrequired'),
  7276. 'Imrequired',
  7277. '/label',
  7278. 'input' => array(
  7279. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7280. 'id' => 'ContactImrequired',
  7281. 'required' => 'required'
  7282. ),
  7283. '/div'
  7284. );
  7285. $this->assertTags($result, $expected);
  7286. }
  7287. /**
  7288. * Test inputDefaults setter and getter
  7289. *
  7290. * @return void
  7291. */
  7292. public function testInputDefaults() {
  7293. $this->markTestIncomplete('Need to revisit once models work again.');
  7294. $this->Form->create('Contact');
  7295. $this->Form->inputDefaults(array(
  7296. 'label' => false,
  7297. 'div' => array(
  7298. 'style' => 'color: #000;'
  7299. )
  7300. ));
  7301. $result = $this->Form->input('Contact.field1');
  7302. $expected = array(
  7303. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7304. 'input' => array(
  7305. 'type' => 'text', 'name' => 'Contact[field1]',
  7306. 'id' => 'ContactField1'
  7307. ),
  7308. '/div'
  7309. );
  7310. $this->assertTags($result, $expected);
  7311. $this->Form->inputDefaults(array(
  7312. 'div' => false,
  7313. 'label' => 'Label',
  7314. ));
  7315. $result = $this->Form->input('Contact.field1');
  7316. $expected = array(
  7317. 'label' => array('for' => 'ContactField1'),
  7318. 'Label',
  7319. '/label',
  7320. 'input' => array(
  7321. 'type' => 'text', 'name' => 'Contact[field1]',
  7322. 'id' => 'ContactField1'
  7323. ),
  7324. );
  7325. $this->assertTags($result, $expected);
  7326. $this->Form->inputDefaults(array(
  7327. 'label' => false,
  7328. ), true);
  7329. $result = $this->Form->input('Contact.field1');
  7330. $expected = array(
  7331. 'input' => array(
  7332. 'type' => 'text', 'name' => 'Contact[field1]',
  7333. 'id' => 'ContactField1'
  7334. ),
  7335. );
  7336. $this->assertTags($result, $expected);
  7337. $result = $this->Form->inputDefaults();
  7338. $expected = array(
  7339. 'div' => false,
  7340. 'label' => false,
  7341. );
  7342. $this->assertEquals($expected, $result);
  7343. }
  7344. }