FormHelperTest.php 273 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799
  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. * @return void
  304. */
  305. public function beforeValidate($options = array()) {
  306. $this->invalidate('email');
  307. return false;
  308. }
  309. }
  310. /**
  311. * ValidateProfile class
  312. *
  313. */
  314. class ValidateProfilesTable extends Table {
  315. /**
  316. * useTable property
  317. *
  318. * @var boolean
  319. */
  320. public $useTable = false;
  321. /**
  322. * schema property
  323. *
  324. * @var array
  325. */
  326. protected $_schema = array(
  327. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  328. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  329. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  330. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  331. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  332. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  333. );
  334. /**
  335. * hasOne property
  336. *
  337. * @var array
  338. */
  339. public $hasOne = array('ValidateItem' => array(
  340. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  341. ));
  342. /**
  343. * belongsTo property
  344. *
  345. * @var array
  346. */
  347. public $belongsTo = array('ValidateUser' => array(
  348. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  349. ));
  350. /**
  351. * beforeValidate method
  352. *
  353. * @return void
  354. */
  355. public function beforeValidate($options = array()) {
  356. $this->invalidate('full_name');
  357. $this->invalidate('city');
  358. return false;
  359. }
  360. }
  361. /**
  362. * ValidateItem class
  363. *
  364. */
  365. class ValidateItemsTable extends Table {
  366. /**
  367. * schema property
  368. *
  369. * @var array
  370. */
  371. protected $_schema = array(
  372. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  373. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  374. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  375. 'description' => array(
  376. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  377. ),
  378. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  379. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  380. );
  381. /**
  382. * belongsTo property
  383. *
  384. * @var array
  385. */
  386. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  387. /**
  388. * beforeValidate method
  389. *
  390. * @return void
  391. */
  392. public function beforeValidate($options = array()) {
  393. $this->invalidate('description');
  394. return false;
  395. }
  396. }
  397. /**
  398. * TestMail class
  399. *
  400. */
  401. class TestMailsTable extends Table {
  402. }
  403. /**
  404. * FormHelperTest class
  405. *
  406. * @property FormHelper $Form
  407. */
  408. class FormHelperTest extends TestCase {
  409. /**
  410. * Fixtures to be used
  411. *
  412. * @var array
  413. */
  414. public $fixtures = array('core.article');
  415. /**
  416. * Do not load the fixtures by default
  417. *
  418. * @var boolean
  419. */
  420. public $autoFixtures = false;
  421. /**
  422. * setUp method
  423. *
  424. * @return void
  425. */
  426. public function setUp() {
  427. parent::setUp();
  428. Configure::write('Config.language', 'eng');
  429. Configure::write('App.base', '');
  430. Configure::write('App.namespace', 'Cake\Test\TestCase\View\Helper');
  431. Configure::delete('Asset');
  432. $this->View = new View(null);
  433. $this->Form = new FormHelper($this->View);
  434. $this->Form->Html = new HtmlHelper($this->View);
  435. $this->Form->request = new Request('articles/add');
  436. $this->Form->request->here = '/articles/add';
  437. $this->Form->request['controller'] = 'articles';
  438. $this->Form->request['action'] = 'add';
  439. $this->Form->request->webroot = '';
  440. $this->Form->request->base = '';
  441. $this->dateRegex = array(
  442. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  443. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  444. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  445. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  446. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  447. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  448. );
  449. $this->article = [
  450. 'schema' => [
  451. 'id' => ['type' => 'integer'],
  452. 'author_id' => ['type' => 'integer', 'null' => true],
  453. 'title' => ['type' => 'string', 'null' => true],
  454. 'body' => 'text',
  455. 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
  456. '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
  457. ],
  458. 'required' => [
  459. 'author_id' => true,
  460. 'title' => true,
  461. ]
  462. ];
  463. Configure::write('Security.salt', 'foo!');
  464. Router::connect('/:controller', array('action' => 'index'));
  465. Router::connect('/:controller/:action/*');
  466. }
  467. /**
  468. * tearDown method
  469. *
  470. * @return void
  471. */
  472. public function tearDown() {
  473. parent::tearDown();
  474. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  475. }
  476. /**
  477. * Test registering a new widget class and rendering it.
  478. *
  479. * @return void
  480. */
  481. public function testAddWidgetAndRenderWidget() {
  482. $data = [
  483. 'val' => 1
  484. ];
  485. $mock = $this->getMock('Cake\View\Widget\WidgetInterface');
  486. $this->assertNull($this->Form->addWidget('test', $mock));
  487. $mock->expects($this->once())
  488. ->method('render')
  489. ->with($data)
  490. ->will($this->returnValue('HTML'));
  491. $result = $this->Form->widget('test', $data);
  492. $this->assertEquals('HTML', $result);
  493. }
  494. /**
  495. * Test registering an invalid widget class.
  496. *
  497. * @expectedException \RuntimeException
  498. * @return void
  499. */
  500. public function testAddWidgetInvalid() {
  501. $mock = new \StdClass();
  502. $this->Form->addWidget('test', $mock);
  503. $this->Form->widget('test');
  504. }
  505. /**
  506. * Test adding a new context class.
  507. *
  508. * @return void
  509. */
  510. public function testAddContextProvider() {
  511. $context = 'My data';
  512. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  513. $this->assertInstanceOf('Cake\Network\Request', $request);
  514. $this->assertEquals($context, $data['entity']);
  515. return $this->getMock('Cake\View\Form\ContextInterface');
  516. });
  517. $this->Form->create($context);
  518. }
  519. /**
  520. * Test adding an invalid context class.
  521. *
  522. * @expectedException RuntimeException
  523. * @expectedExceptionMessage Context objects must implement Cake\View\Form\ContextInterface
  524. * @return void
  525. */
  526. public function testAddContextProviderInvalid() {
  527. $context = 'My data';
  528. $this->Form->addContextProvider('test', function ($request, $data) use ($context) {
  529. return new \StdClass();
  530. });
  531. $this->Form->create($context);
  532. }
  533. /**
  534. * Provides context options for create().
  535. *
  536. * @return array
  537. */
  538. public function contextSelectionProvider() {
  539. $entity = new Article();
  540. $collection = $this->getMock('Cake\Collection\Collection', ['extract'], [[$entity]]);
  541. $data = [
  542. 'schema' => [
  543. 'title' => ['type' => 'string']
  544. ]
  545. ];
  546. return [
  547. 'entity' => [$entity, 'Cake\View\Form\EntityContext'],
  548. 'collection' => [$collection, 'Cake\View\Form\EntityContext'],
  549. 'array' => [$data, 'Cake\View\Form\ArrayContext'],
  550. 'none' => [null, 'Cake\View\Form\NullContext'],
  551. 'false' => [false, 'Cake\View\Form\NullContext'],
  552. ];
  553. }
  554. /**
  555. * Test default context selection in create()
  556. *
  557. * @dataProvider contextSelectionProvider
  558. * @return void
  559. */
  560. public function testCreateContextSelectionBuiltIn($data, $class) {
  561. $this->loadFixtures('Article');
  562. $this->Form->create($data);
  563. $this->assertInstanceOf($class, $this->Form->context());
  564. }
  565. /**
  566. * Data provider for type option.
  567. *
  568. * @return array
  569. */
  570. public static function requestTypeProvider() {
  571. return [
  572. // type, method, override
  573. ['post', 'post', 'POST'],
  574. ['put', 'post', 'PUT'],
  575. ['patch', 'post', 'PATCH'],
  576. ['delete', 'post', 'DELETE'],
  577. ];
  578. }
  579. /**
  580. * Test creating file forms.
  581. *
  582. * @return void
  583. */
  584. public function testCreateFile() {
  585. $encoding = strtolower(Configure::read('App.encoding'));
  586. $result = $this->Form->create(false, array('type' => 'file'));
  587. $expected = array(
  588. 'form' => array(
  589. 'method' => 'post', 'action' => '/articles/add',
  590. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  591. ),
  592. 'div' => array('style' => 'display:none;'),
  593. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  594. '/div'
  595. );
  596. $this->assertTags($result, $expected);
  597. }
  598. /**
  599. * Test creating GET forms.
  600. *
  601. * @return void
  602. */
  603. public function testCreateGet() {
  604. $encoding = strtolower(Configure::read('App.encoding'));
  605. $result = $this->Form->create(false, array('type' => 'get'));
  606. $expected = array('form' => array(
  607. 'method' => 'get', 'action' => '/articles/add',
  608. 'accept-charset' => $encoding
  609. ));
  610. $this->assertTags($result, $expected);
  611. }
  612. /**
  613. * test the create() method
  614. *
  615. * @dataProvider requestTypeProvider
  616. * @return void
  617. */
  618. public function testCreateTypeOptions($type, $method, $override) {
  619. $encoding = strtolower(Configure::read('App.encoding'));
  620. $result = $this->Form->create(false, array('type' => $type));
  621. $expected = array(
  622. 'form' => array(
  623. 'method' => $method, 'action' => '/articles/add',
  624. 'accept-charset' => $encoding
  625. ),
  626. 'div' => array('style' => 'display:none;'),
  627. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => $override),
  628. '/div'
  629. );
  630. $this->assertTags($result, $expected);
  631. }
  632. /**
  633. * Test opening a form for an update operation.
  634. *
  635. * @return void
  636. */
  637. public function testCreateUpdateForm() {
  638. $encoding = strtolower(Configure::read('App.encoding'));
  639. $this->Form->request->here = '/articles/edit/1';
  640. $this->Form->request['action'] = 'edit';
  641. $this->article['defaults']['id'] = 1;
  642. $result = $this->Form->create($this->article);
  643. $expected = array(
  644. 'form' => array(
  645. 'method' => 'post', 'action' => '/articles/edit/1',
  646. 'accept-charset' => $encoding
  647. ),
  648. 'div' => array('style' => 'display:none;'),
  649. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  650. '/div'
  651. );
  652. $this->assertTags($result, $expected);
  653. }
  654. /**
  655. * Test the onsubmit option for create()
  656. *
  657. * @return void
  658. */
  659. public function testCreateOnSubmit() {
  660. $this->Form->request->data = [];
  661. $this->Form->request['controller'] = 'articles';
  662. $result = $this->Form->create($this->article, ['url' => ['action' => 'index', 'param'], 'default' => false]);
  663. $expected = array(
  664. 'form' => array(
  665. 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/articles/index/param',
  666. 'accept-charset' => 'utf-8'
  667. ),
  668. 'div' => array('style' => 'display:none;'),
  669. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  670. '/div'
  671. );
  672. $this->assertTags($result, $expected);
  673. $this->Form->request->data = [];
  674. $this->Form->request['controller'] = 'articles';
  675. $result = $this->Form->create($this->article, array(
  676. 'url' => array('action' => 'index', 'param'),
  677. 'default' => false,
  678. 'onsubmit' => 'someFunction();'
  679. ));
  680. $expected = array(
  681. 'form' => array(
  682. 'method' => 'post',
  683. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  684. 'action' => '/articles/index/param',
  685. 'accept-charset' => 'utf-8'
  686. ),
  687. 'div' => array('style' => 'display:none;'),
  688. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  689. '/div'
  690. );
  691. $this->assertTags($result, $expected);
  692. }
  693. /**
  694. * test create() with automatic url generation
  695. *
  696. * @return void
  697. */
  698. public function testCreateAutoUrl() {
  699. $encoding = strtolower(Configure::read('App.encoding'));
  700. $this->Form->request['action'] = 'delete';
  701. $this->Form->request->here = '/articles/delete/10';
  702. $this->Form->request->base = '';
  703. $result = $this->Form->create($this->article);
  704. $expected = array(
  705. 'form' => array(
  706. 'method' => 'post', 'action' => '/articles/delete/10',
  707. 'accept-charset' => $encoding
  708. ),
  709. 'div' => array('style' => 'display:none;'),
  710. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  711. '/div'
  712. );
  713. $this->assertTags($result, $expected);
  714. $this->article['defaults'] = ['id' => 1];
  715. $this->Form->request->here = '/articles/edit/1';
  716. $this->Form->request['action'] = 'delete';
  717. $result = $this->Form->create($this->article, ['action' => 'edit']);
  718. $expected = array(
  719. 'form' => array(
  720. 'method' => 'post',
  721. 'action' => '/articles/edit/1',
  722. 'accept-charset' => $encoding
  723. ),
  724. 'div' => array('style' => 'display:none;'),
  725. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  726. '/div'
  727. );
  728. $this->assertTags($result, $expected);
  729. $this->Form->request['action'] = 'add';
  730. $result = $this->Form->create($this->article, ['url' => ['action' => 'publish']]);
  731. $expected = array(
  732. 'form' => array(
  733. 'method' => 'post',
  734. 'action' => '/articles/publish/1',
  735. 'accept-charset' => $encoding
  736. ),
  737. 'div' => array('style' => 'display:none;'),
  738. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  739. '/div'
  740. );
  741. $this->assertTags($result, $expected);
  742. $result = $this->Form->create($this->article, array('url' => '/articles/publish'));
  743. $expected = array(
  744. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  745. 'div' => array('style' => 'display:none;'),
  746. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  747. '/div'
  748. );
  749. $this->assertTags($result, $expected);
  750. $this->Form->request['controller'] = 'pages';
  751. $result = $this->Form->create($this->article, array('action' => 'signup'));
  752. $expected = array(
  753. 'form' => array(
  754. 'method' => 'post', 'action' => '/pages/signup/1',
  755. 'accept-charset' => $encoding
  756. ),
  757. 'div' => array('style' => 'display:none;'),
  758. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  759. '/div'
  760. );
  761. $this->assertTags($result, $expected);
  762. }
  763. /**
  764. * test create() with a custom route
  765. *
  766. * @return void
  767. */
  768. public function testCreateCustomRoute() {
  769. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  770. $encoding = strtolower(Configure::read('App.encoding'));
  771. $this->Form->request['controller'] = 'users';
  772. $result = $this->Form->create(false, array('action' => 'login'));
  773. $expected = array(
  774. 'form' => array(
  775. 'method' => 'post', 'action' => '/login',
  776. 'accept-charset' => $encoding
  777. ),
  778. 'div' => array('style' => 'display:none;'),
  779. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  780. '/div'
  781. );
  782. $this->assertTags($result, $expected);
  783. }
  784. /**
  785. * test that inputDefaults are stored and used.
  786. *
  787. * @return void
  788. */
  789. public function testCreateWithInputDefaults() {
  790. $this->markTestIncomplete('Need to revisit once models work again.');
  791. $this->Form->create('User', array(
  792. 'inputDefaults' => array(
  793. 'div' => false,
  794. 'label' => false,
  795. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  796. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  797. )
  798. ));
  799. $result = $this->Form->input('username');
  800. $expected = array(
  801. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername')
  802. );
  803. $this->assertTags($result, $expected);
  804. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  805. $expected = array(
  806. 'div' => array('class' => 'input text'),
  807. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  808. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  809. '/div'
  810. );
  811. $this->assertTags($result, $expected);
  812. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  813. $expected = array(
  814. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  815. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  816. );
  817. $this->assertTags($result, $expected);
  818. $this->Form->create('User', array(
  819. 'inputDefaults' => array(
  820. 'div' => false,
  821. 'label' => array('class' => 'nice', 'for' => 'changed'),
  822. )
  823. ));
  824. $result = $this->Form->input('username', array('div' => true));
  825. $expected = array(
  826. 'div' => array('class' => 'input text'),
  827. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  828. 'input' => array('type' => 'text', 'name' => 'User[username]', 'id' => 'UserUsername'),
  829. '/div'
  830. );
  831. $this->assertTags($result, $expected);
  832. }
  833. /**
  834. * test automatic accept-charset overriding
  835. *
  836. * @return void
  837. */
  838. public function testCreateWithAcceptCharset() {
  839. $this->markTestIncomplete('Need to revisit once models work again.');
  840. $result = $this->Form->create('UserForm', array(
  841. 'type' => 'post', 'action' => 'login', 'encoding' => 'iso-8859-1'
  842. )
  843. );
  844. $expected = array(
  845. 'form' => array(
  846. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  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. $this->markTestIncomplete('Need to revisit once models work again.');
  861. $encoding = strtolower(Configure::read('App.encoding'));
  862. $result = $this->Form->create('Contact', array(
  863. 'type' => 'post',
  864. 'escape' => false,
  865. 'url' => array(
  866. 'controller' => 'controller',
  867. 'action' => 'action',
  868. '?' => array('param1' => 'value1', 'param2' => 'value2')
  869. )
  870. ));
  871. $expected = array(
  872. 'form' => array(
  873. 'id' => 'ContactAddForm',
  874. 'method' => 'post',
  875. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  876. 'accept-charset' => $encoding
  877. ),
  878. 'div' => array('style' => 'display:none;'),
  879. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  880. '/div'
  881. );
  882. $this->assertTags($result, $expected);
  883. $result = $this->Form->create('Contact', array(
  884. 'type' => 'post',
  885. 'url' => array(
  886. 'controller' => 'controller',
  887. 'action' => 'action',
  888. '?' => array('param1' => 'value1', 'param2' => 'value2')
  889. )
  890. ));
  891. $expected = array(
  892. 'form' => array(
  893. 'id' => 'ContactAddForm',
  894. 'method' => 'post',
  895. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  896. 'accept-charset' => $encoding
  897. ),
  898. 'div' => array('style' => 'display:none;'),
  899. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  900. '/div'
  901. );
  902. $this->assertTags($result, $expected);
  903. }
  904. /**
  905. * test that create() doesn't cause errors by multiple id's being in the primary key
  906. * as could happen with multiple select or checkboxes.
  907. *
  908. * @return void
  909. */
  910. public function testCreateWithMultipleIdInData() {
  911. $this->markTestIncomplete('Need to revisit once models work again.');
  912. $encoding = strtolower(Configure::read('App.encoding'));
  913. $this->Form->request->data['Contact']['id'] = array(1, 2);
  914. $result = $this->Form->create('Contact');
  915. $expected = array(
  916. 'form' => array(
  917. 'id' => 'ContactAddForm',
  918. 'method' => 'post',
  919. 'action' => '/contacts/add',
  920. 'accept-charset' => $encoding
  921. ),
  922. 'div' => array('style' => 'display:none;'),
  923. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  924. '/div'
  925. );
  926. $this->assertTags($result, $expected);
  927. }
  928. /**
  929. * test that create() doesn't add in extra passed params.
  930. *
  931. * @return void
  932. */
  933. public function testCreatePassedArgs() {
  934. $this->markTestIncomplete('Need to revisit once models work again.');
  935. $encoding = strtolower(Configure::read('App.encoding'));
  936. $this->Form->request->data['Contact']['id'] = 1;
  937. $result = $this->Form->create('Contact', array(
  938. 'type' => 'post',
  939. 'escape' => false,
  940. 'url' => array(
  941. 'action' => 'edit',
  942. 'myparam'
  943. )
  944. ));
  945. $expected = array(
  946. 'form' => array(
  947. 'id' => 'ContactAddForm',
  948. 'method' => 'post',
  949. 'action' => '/contacts/edit/myparam',
  950. 'accept-charset' => $encoding
  951. ),
  952. 'div' => array('style' => 'display:none;'),
  953. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  954. '/div'
  955. );
  956. $this->assertTags($result, $expected);
  957. }
  958. /**
  959. * test creating a get form, and get form inputs.
  960. *
  961. * @return void
  962. */
  963. public function testGetFormCreate() {
  964. $this->markTestIncomplete('Need to revisit once models work again.');
  965. $encoding = strtolower(Configure::read('App.encoding'));
  966. $result = $this->Form->create('Contact', array('type' => 'get'));
  967. $this->assertTags($result, array('form' => array(
  968. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  969. 'accept-charset' => $encoding
  970. )));
  971. $result = $this->Form->text('Contact.name');
  972. $this->assertTags($result, array('input' => array(
  973. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  974. )));
  975. $result = $this->Form->password('password');
  976. $this->assertTags($result, array('input' => array(
  977. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  978. )));
  979. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  980. $result = $this->Form->text('user_form');
  981. $this->assertTags($result, array('input' => array(
  982. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  983. )));
  984. }
  985. /**
  986. * test get form, and inputs when the model param is false
  987. *
  988. * @return void
  989. */
  990. public function testGetFormWithFalseModel() {
  991. $this->markTestIncomplete('Need to revisit once models work again.');
  992. $encoding = strtolower(Configure::read('App.encoding'));
  993. $this->Form->request['controller'] = 'contact_test';
  994. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  995. $expected = array('form' => array(
  996. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  997. 'accept-charset' => $encoding
  998. ));
  999. $this->assertTags($result, $expected);
  1000. $result = $this->Form->text('reason');
  1001. $expected = array(
  1002. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  1003. );
  1004. $this->assertTags($result, $expected);
  1005. }
  1006. /**
  1007. * testFormCreateWithSecurity method
  1008. *
  1009. * Test form->create() with security key.
  1010. *
  1011. * @return void
  1012. */
  1013. public function testCreateWithSecurity() {
  1014. $this->Form->request->params['_csrfToken'] = 'testKey';
  1015. $encoding = strtolower(Configure::read('App.encoding'));
  1016. $result = $this->Form->create($this->article, [
  1017. 'url' => '/articles/publish',
  1018. ]);
  1019. $expected = array(
  1020. 'form' => array('method' => 'post', 'action' => '/articles/publish', 'accept-charset' => $encoding),
  1021. 'div' => array('style' => 'display:none;'),
  1022. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1023. array('input' => array(
  1024. 'type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testKey'
  1025. )),
  1026. '/div'
  1027. );
  1028. $this->assertTags($result, $expected);
  1029. $result = $this->Form->create($this->article, ['url' => '/articles/publish', 'id' => 'MyForm']);
  1030. $expected['form']['id'] = 'MyForm';
  1031. $this->assertTags($result, $expected);
  1032. }
  1033. /**
  1034. * testFormCreateGetNoSecurity method
  1035. *
  1036. * Test form->create() with no security key as its a get form
  1037. *
  1038. * @return void
  1039. */
  1040. public function testCreateEndGetNoSecurity() {
  1041. $this->Form->request->params['_csrfToken'] = 'testKey';
  1042. $encoding = strtolower(Configure::read('App.encoding'));
  1043. $article = new Article();
  1044. $result = $this->Form->create($article, [
  1045. 'type' => 'get',
  1046. 'url' => '/contacts/add'
  1047. ]);
  1048. $this->assertNotContains('testKey', $result);
  1049. $result = $this->Form->end('Save');
  1050. $this->assertNotContains('testKey', $result);
  1051. }
  1052. /**
  1053. * test that create() clears the fields property so it starts fresh
  1054. *
  1055. * @return void
  1056. */
  1057. public function testCreateClearingFields() {
  1058. $this->Form->fields = array('model_id');
  1059. $this->Form->create($this->article);
  1060. $this->assertEquals(array(), $this->Form->fields);
  1061. }
  1062. /**
  1063. * Tests form hash generation with model-less data
  1064. *
  1065. * @return void
  1066. */
  1067. public function testValidateHashNoModel() {
  1068. $this->markTestIncomplete('Need to revisit once models work again.');
  1069. $this->Form->request->params['_csrfToken'] = 'foo';
  1070. $result = $this->Form->secure(array('anything'));
  1071. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  1072. }
  1073. /**
  1074. * Tests that models with identical field names get resolved properly
  1075. *
  1076. * @return void
  1077. */
  1078. public function testDuplicateFieldNameResolution() {
  1079. $this->markTestIncomplete('Need to revisit once models work again.');
  1080. $result = $this->Form->create('ValidateUser');
  1081. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  1082. $result = $this->Form->input('ValidateItem.name');
  1083. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  1084. $result = $this->Form->input('ValidateUser.name');
  1085. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  1086. $this->assertContains('name="ValidateUser[name]"', $result);
  1087. $this->assertContains('type="text"', $result);
  1088. $result = $this->Form->input('ValidateItem.name');
  1089. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  1090. $this->assertContains('name="ValidateItem[name]"', $result);
  1091. $this->assertContains('<textarea', $result);
  1092. $result = $this->Form->input('name');
  1093. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  1094. $this->assertContains('name="ValidateUser[name]"', $result);
  1095. $this->assertContains('type="text"', $result);
  1096. }
  1097. /**
  1098. * Tests that hidden fields generated for checkboxes don't get locked
  1099. *
  1100. * @return void
  1101. */
  1102. public function testNoCheckboxLocking() {
  1103. $this->Form->request->params['_Token'] = 'foo';
  1104. $this->assertSame([], $this->Form->fields);
  1105. $this->Form->checkbox('check', array('value' => '1'));
  1106. $this->assertSame($this->Form->fields, array('check'));
  1107. }
  1108. /**
  1109. * testFormSecurityFields method
  1110. *
  1111. * Test generation of secure form hash generation.
  1112. *
  1113. * @return void
  1114. */
  1115. public function testFormSecurityFields() {
  1116. $this->markTestIncomplete('Need to revisit once models work again.');
  1117. $key = 'testKey';
  1118. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  1119. $this->Form->request->params['_csrfToken'] = $key;
  1120. $result = $this->Form->secure($fields);
  1121. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  1122. $hash .= ':' . 'Model.valid';
  1123. $hash = urlencode($hash);
  1124. $expected = array(
  1125. 'div' => array('style' => 'display:none;'),
  1126. array('input' => array(
  1127. 'type' => 'hidden', 'name' => '_Token[fields]',
  1128. 'value' => $hash
  1129. )),
  1130. array('input' => array(
  1131. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1132. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1133. )),
  1134. '/div'
  1135. );
  1136. $this->assertTags($result, $expected);
  1137. $path = CAKE . 'Test/TestApp/Config/';
  1138. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  1139. $result = $this->Form->secure($fields);
  1140. $expected = array(
  1141. 'div' => array('class' => 'hidden'),
  1142. array('input' => array(
  1143. 'type' => 'hidden', 'name' => '_Token[fields]',
  1144. 'value' => $hash
  1145. )),
  1146. array('input' => array(
  1147. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1148. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1149. )),
  1150. '/div'
  1151. );
  1152. $this->assertTags($result, $expected);
  1153. }
  1154. /**
  1155. * Tests correct generation of number fields for double and float fields
  1156. *
  1157. * @return void
  1158. */
  1159. public function testTextFieldGenerationForFloats() {
  1160. $this->markTestIncomplete('Need to revisit once models work again.');
  1161. $this->article['schema'] = [
  1162. 'foo' => [
  1163. 'type' => 'float',
  1164. 'null' => false,
  1165. 'default' => null,
  1166. 'length' => 10
  1167. ]
  1168. ];
  1169. $this->Form->create('Contact');
  1170. $result = $this->Form->input('foo');
  1171. $expected = array(
  1172. 'div' => array('class' => 'input number'),
  1173. 'label' => array('for' => 'ContactFoo'),
  1174. 'Foo',
  1175. '/label',
  1176. array('input' => array(
  1177. 'type' => 'number',
  1178. 'name' => 'Contact[foo]',
  1179. 'id' => 'ContactFoo',
  1180. 'step' => 'any'
  1181. )),
  1182. '/div'
  1183. );
  1184. $this->assertTags($result, $expected);
  1185. $result = $this->Form->input('foo', array('step' => 0.5));
  1186. $expected = array(
  1187. 'div' => array('class' => 'input number'),
  1188. 'label' => array('for' => 'ContactFoo'),
  1189. 'Foo',
  1190. '/label',
  1191. array('input' => array(
  1192. 'type' => 'number',
  1193. 'name' => 'Contact[foo]',
  1194. 'id' => 'ContactFoo',
  1195. 'step' => '0.5'
  1196. )),
  1197. '/div'
  1198. );
  1199. $this->assertTags($result, $expected);
  1200. }
  1201. /**
  1202. * Tests correct generation of number fields for integer fields
  1203. *
  1204. * @return void
  1205. */
  1206. public function testTextFieldTypeNumberGenerationForIntegers() {
  1207. $this->markTestIncomplete('Need to revisit once models work again.');
  1208. $model->setSchema(array('foo' => array(
  1209. 'type' => 'integer',
  1210. 'null' => false,
  1211. 'default' => null,
  1212. 'length' => null
  1213. )));
  1214. $this->Form->create('Contact');
  1215. $result = $this->Form->input('foo');
  1216. $expected = array(
  1217. 'div' => array('class' => 'input number'),
  1218. 'label' => array('for' => 'ContactFoo'),
  1219. 'Foo',
  1220. '/label',
  1221. array('input' => array(
  1222. 'type' => 'number', 'name' => 'Contact[foo]',
  1223. 'id' => 'ContactFoo'
  1224. )),
  1225. '/div'
  1226. );
  1227. $this->assertTags($result, $expected);
  1228. }
  1229. /**
  1230. * Tests correct generation of file upload fields for binary fields
  1231. *
  1232. * @return void
  1233. */
  1234. public function testFileUploadFieldTypeGenerationForBinaries() {
  1235. $this->markTestIncomplete('Need to revisit once models work again.');
  1236. $model->setSchema(array('foo' => array(
  1237. 'type' => 'binary',
  1238. 'null' => false,
  1239. 'default' => null,
  1240. 'length' => 1024
  1241. )));
  1242. $this->Form->create('Contact');
  1243. $result = $this->Form->input('foo');
  1244. $expected = array(
  1245. 'div' => array('class' => 'input file'),
  1246. 'label' => array('for' => 'ContactFoo'),
  1247. 'Foo',
  1248. '/label',
  1249. array('input' => array(
  1250. 'type' => 'file', 'name' => 'data[Contact][foo]',
  1251. 'id' => 'ContactFoo'
  1252. )),
  1253. '/div'
  1254. );
  1255. $this->assertTags($result, $expected);
  1256. }
  1257. /**
  1258. * testFormSecurityMultipleFields method
  1259. *
  1260. * Test secure() with multiple row form. Ensure hash is correct.
  1261. *
  1262. * @return void
  1263. */
  1264. public function testFormSecurityMultipleFields() {
  1265. $this->markTestIncomplete('Need to revisit once models work again.');
  1266. $key = 'testKey';
  1267. $fields = array(
  1268. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  1269. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  1270. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  1271. );
  1272. $this->Form->request->params['_csrfToken'] = $key;
  1273. $result = $this->Form->secure($fields);
  1274. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  1275. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  1276. $expected = array(
  1277. 'div' => array('style' => 'display:none;'),
  1278. array('input' => array(
  1279. 'type' => 'hidden', 'name' => '_Token[fields]',
  1280. 'value' => $hash
  1281. )),
  1282. array('input' => array(
  1283. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1284. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1285. )),
  1286. '/div'
  1287. );
  1288. $this->assertTags($result, $expected);
  1289. }
  1290. /**
  1291. * testFormSecurityMultipleSubmitButtons
  1292. *
  1293. * test form submit generation and ensure that _Token is only created on end()
  1294. *
  1295. * @return void
  1296. */
  1297. public function testFormSecurityMultipleSubmitButtons() {
  1298. $this->markTestIncomplete('Need to revisit once models work again.');
  1299. $key = 'testKey';
  1300. $this->Form->request->params['_csrfToken'] = $key;
  1301. $this->Form->create('Addresses');
  1302. $this->Form->input('Address.title');
  1303. $this->Form->input('Address.first_name');
  1304. $result = $this->Form->submit('Save', array('name' => 'save'));
  1305. $expected = array(
  1306. 'div' => array('class' => 'submit'),
  1307. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  1308. '/div',
  1309. );
  1310. $this->assertTags($result, $expected);
  1311. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  1312. $expected = array(
  1313. 'div' => array('class' => 'submit'),
  1314. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  1315. '/div',
  1316. );
  1317. $this->assertTags($result, $expected);
  1318. $result = $this->Form->end(null);
  1319. $expected = array(
  1320. 'div' => array('style' => 'display:none;'),
  1321. array('input' => array(
  1322. 'type' => 'hidden', 'name' => '_Token[fields]',
  1323. 'value' => 'preg:/.+/'
  1324. )),
  1325. array('input' => array(
  1326. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1327. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  1328. )),
  1329. '/div'
  1330. );
  1331. $this->assertTags($result, $expected);
  1332. }
  1333. /**
  1334. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  1335. *
  1336. * @return void
  1337. */
  1338. public function testSecurityButtonNestedNamed() {
  1339. $this->markTestIncomplete('Need to revisit once models work again.');
  1340. $key = 'testKey';
  1341. $this->Form->request->params['_csrfToken'] = $key;
  1342. $this->Form->create('Addresses');
  1343. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1344. $result = $this->Form->unlockField();
  1345. $this->assertEquals(array('Address.button'), $result);
  1346. }
  1347. /**
  1348. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  1349. *
  1350. * @return void
  1351. */
  1352. public function testSecuritySubmitNestedNamed() {
  1353. $this->markTestIncomplete('Need to revisit once models work again.');
  1354. $key = 'testKey';
  1355. $this->Form->request->params['_csrfToken'] = $key;
  1356. $this->Form->create('Addresses');
  1357. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  1358. $result = $this->Form->unlockField();
  1359. $this->assertEquals(array('Address.button'), $result);
  1360. }
  1361. /**
  1362. * Test that the correct fields are unlocked for image submits with no names.
  1363. *
  1364. * @return void
  1365. */
  1366. public function testSecuritySubmitImageNoName() {
  1367. $this->markTestIncomplete('Need to revisit once models work again.');
  1368. $key = 'testKey';
  1369. $this->Form->request->params['_csrfToken'] = $key;
  1370. $this->Form->create('User');
  1371. $result = $this->Form->submit('save.png');
  1372. $expected = array(
  1373. 'div' => array('class' => 'submit'),
  1374. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  1375. '/div'
  1376. );
  1377. $this->assertTags($result, $expected);
  1378. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  1379. }
  1380. /**
  1381. * Test that the correct fields are unlocked for image submits with names.
  1382. *
  1383. * @return void
  1384. */
  1385. public function testSecuritySubmitImageName() {
  1386. $this->markTestIncomplete('Need to revisit once models work again.');
  1387. $key = 'testKey';
  1388. $this->Form->request->params['_csrfToken'] = $key;
  1389. $this->Form->create('User');
  1390. $result = $this->Form->submit('save.png', array('name' => 'test'));
  1391. $expected = array(
  1392. 'div' => array('class' => 'submit'),
  1393. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  1394. '/div'
  1395. );
  1396. $this->assertTags($result, $expected);
  1397. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  1398. }
  1399. /**
  1400. * testFormSecurityMultipleInputFields method
  1401. *
  1402. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  1403. *
  1404. * @return void
  1405. */
  1406. public function testFormSecurityMultipleInputFields() {
  1407. $this->markTestIncomplete('Need to revisit once models work again.');
  1408. $key = 'testKey';
  1409. $this->Form->request->params['_csrfToken'] = $key;
  1410. $this->Form->create('Addresses');
  1411. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1412. $this->Form->input('Addresses.0.title');
  1413. $this->Form->input('Addresses.0.first_name');
  1414. $this->Form->input('Addresses.0.last_name');
  1415. $this->Form->input('Addresses.0.address');
  1416. $this->Form->input('Addresses.0.city');
  1417. $this->Form->input('Addresses.0.phone');
  1418. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  1419. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1420. $this->Form->input('Addresses.1.title');
  1421. $this->Form->input('Addresses.1.first_name');
  1422. $this->Form->input('Addresses.1.last_name');
  1423. $this->Form->input('Addresses.1.address');
  1424. $this->Form->input('Addresses.1.city');
  1425. $this->Form->input('Addresses.1.phone');
  1426. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  1427. $result = $this->Form->secure($this->Form->fields);
  1428. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  1429. $expected = array(
  1430. 'div' => array('style' => 'display:none;'),
  1431. array('input' => array(
  1432. 'type' => 'hidden', 'name' => '_Token[fields]',
  1433. 'value' => $hash
  1434. )),
  1435. array('input' => array(
  1436. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1437. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1438. )),
  1439. '/div'
  1440. );
  1441. $this->assertTags($result, $expected);
  1442. }
  1443. /**
  1444. * Test form security with Model.field.0 style inputs
  1445. *
  1446. * @return void
  1447. */
  1448. public function testFormSecurityArrayFields() {
  1449. $this->markTestIncomplete('Need to revisit once models work again.');
  1450. $key = 'testKey';
  1451. $this->Form->request->params['_csrfToken'] = $key;
  1452. $this->Form->create('Address');
  1453. $this->Form->input('Address.primary.1');
  1454. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  1455. $this->Form->input('Address.secondary.1.0');
  1456. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  1457. }
  1458. /**
  1459. * testFormSecurityMultipleInputDisabledFields method
  1460. *
  1461. * test secure form generation with multiple records and disabled fields.
  1462. *
  1463. * @return void
  1464. */
  1465. public function testFormSecurityMultipleInputDisabledFields() {
  1466. $this->markTestIncomplete('Need to revisit once models work again.');
  1467. $key = 'testKey';
  1468. $this->Form->request->params['_csrfToken'] = $key;
  1469. $this->Form->request->params['_Token'] = array(
  1470. 'unlockedFields' => array('first_name', 'address')
  1471. );
  1472. $this->Form->create();
  1473. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1474. $this->Form->input('Addresses.0.title');
  1475. $this->Form->input('Addresses.0.first_name');
  1476. $this->Form->input('Addresses.0.last_name');
  1477. $this->Form->input('Addresses.0.address');
  1478. $this->Form->input('Addresses.0.city');
  1479. $this->Form->input('Addresses.0.phone');
  1480. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1481. $this->Form->input('Addresses.1.title');
  1482. $this->Form->input('Addresses.1.first_name');
  1483. $this->Form->input('Addresses.1.last_name');
  1484. $this->Form->input('Addresses.1.address');
  1485. $this->Form->input('Addresses.1.city');
  1486. $this->Form->input('Addresses.1.phone');
  1487. $result = $this->Form->secure($this->Form->fields);
  1488. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1489. $expected = array(
  1490. 'div' => array('style' => 'display:none;'),
  1491. array('input' => array(
  1492. 'type' => 'hidden', 'name' => '_Token[fields]',
  1493. 'value' => $hash
  1494. )),
  1495. array('input' => array(
  1496. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1497. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1498. )),
  1499. '/div'
  1500. );
  1501. $this->assertTags($result, $expected);
  1502. }
  1503. /**
  1504. * testFormSecurityInputDisabledFields method
  1505. *
  1506. * Test single record form with disabled fields.
  1507. *
  1508. * @return void
  1509. */
  1510. public function testFormSecurityInputUnlockedFields() {
  1511. $this->markTestIncomplete('Need to revisit once models work again.');
  1512. $key = 'testKey';
  1513. $this->Form->request->params['_csrfToken'] = $key;
  1514. $this->Form->request['_Token'] = array(
  1515. 'unlockedFields' => array('first_name', 'address')
  1516. );
  1517. $this->Form->create();
  1518. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1519. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1520. $this->Form->input('Addresses.title');
  1521. $this->Form->input('Addresses.first_name');
  1522. $this->Form->input('Addresses.last_name');
  1523. $this->Form->input('Addresses.address');
  1524. $this->Form->input('Addresses.city');
  1525. $this->Form->input('Addresses.phone');
  1526. $result = $this->Form->fields;
  1527. $expected = array(
  1528. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1529. 'Addresses.city', 'Addresses.phone'
  1530. );
  1531. $this->assertEquals($expected, $result);
  1532. $result = $this->Form->secure($expected);
  1533. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1534. $expected = array(
  1535. 'div' => array('style' => 'display:none;'),
  1536. array('input' => array(
  1537. 'type' => 'hidden', 'name' => '_Token[fields]',
  1538. 'value' => $hash
  1539. )),
  1540. array('input' => array(
  1541. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1542. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1543. )),
  1544. '/div'
  1545. );
  1546. $this->assertTags($result, $expected);
  1547. }
  1548. /**
  1549. * test securing inputs with custom name attributes.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormSecureWithCustomNameAttribute() {
  1554. $this->markTestIncomplete('Need to revisit once models work again.');
  1555. $this->Form->request->params['_csrfToken'] = 'testKey';
  1556. $this->Form->text('UserForm.published', array('name' => 'User[custom]'));
  1557. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1558. $this->Form->text('UserForm.published', array('name' => 'User[custom][another][value]'));
  1559. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1560. }
  1561. /**
  1562. * testFormSecuredInput method
  1563. *
  1564. * Test generation of entire secure form, assertions made on input() output.
  1565. *
  1566. * @return void
  1567. */
  1568. public function testFormSecuredInput() {
  1569. $this->markTestIncomplete('Need to revisit once models work again.');
  1570. $this->Form->request->params['_csrfToken'] = 'testKey';
  1571. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1572. $encoding = strtolower(Configure::read('App.encoding'));
  1573. $expected = array(
  1574. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1575. 'div' => array('style' => 'display:none;'),
  1576. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1577. array('input' => array(
  1578. 'type' => 'hidden', 'name' => '_csrfToken',
  1579. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1580. )),
  1581. '/div'
  1582. );
  1583. $this->assertTags($result, $expected);
  1584. $result = $this->Form->input('ValidateUser.ratio');
  1585. $expected = array(
  1586. 'div' => array('class'),
  1587. 'label' => array('for'),
  1588. 'Ratio',
  1589. '/label',
  1590. 'input' => array('name', 'type' => 'number', 'step' => '0.000001', 'id'),
  1591. '/div',
  1592. );
  1593. $this->assertTags($result, $expected);
  1594. $result = $this->Form->input('ValidateUser.population');
  1595. $expected = array(
  1596. 'div' => array('class'),
  1597. 'label' => array('for'),
  1598. 'Population',
  1599. '/label',
  1600. 'input' => array('name', 'type' => 'number', 'step' => '1', 'id'),
  1601. '/div',
  1602. );
  1603. $this->assertTags($result, $expected);
  1604. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1605. $expected = array(
  1606. 'div' => array('class' => 'input text'),
  1607. 'label' => array('for' => 'UserFormPublished'),
  1608. 'Published',
  1609. '/label',
  1610. array('input' => array(
  1611. 'type' => 'text', 'name' => 'UserForm[published]',
  1612. 'id' => 'UserFormPublished'
  1613. )),
  1614. '/div'
  1615. );
  1616. $this->assertTags($result, $expected);
  1617. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1618. $expected = array(
  1619. 'div' => array('class' => 'input text'),
  1620. 'label' => array('for' => 'UserFormOther'),
  1621. 'Other',
  1622. '/label',
  1623. array('input' => array(
  1624. 'type' => 'text', 'name' => 'UserForm[other]',
  1625. 'id' => 'UserFormOther'
  1626. )),
  1627. '/div'
  1628. );
  1629. $this->assertTags($result, $expected);
  1630. $result = $this->Form->hidden('UserForm.stuff');
  1631. $expected = array(
  1632. 'input' => array(
  1633. 'type' => 'hidden', 'name' => 'UserForm[stuff]',
  1634. 'id' => 'UserFormStuff'
  1635. ));
  1636. $this->assertTags($result, $expected);
  1637. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1638. $expected = array('input' => array(
  1639. 'type' => 'hidden', 'name' => 'UserForm[hidden]',
  1640. 'value' => '0', 'id' => 'UserFormHidden'
  1641. ));
  1642. $this->assertTags($result, $expected);
  1643. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1644. $expected = array(
  1645. 'div' => array('class' => 'input checkbox'),
  1646. array('input' => array(
  1647. 'type' => 'hidden', 'name' => 'UserForm[something]',
  1648. 'value' => '0', 'id' => 'UserFormSomething_'
  1649. )),
  1650. array('input' => array(
  1651. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  1652. 'value' => '1', 'id' => 'UserFormSomething'
  1653. )),
  1654. 'label' => array('for' => 'UserFormSomething'),
  1655. 'Something',
  1656. '/label',
  1657. '/div'
  1658. );
  1659. $this->assertTags($result, $expected);
  1660. $result = $this->Form->fields;
  1661. $expected = array(
  1662. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1663. 'UserForm.hidden' => '0', 'UserForm.something'
  1664. );
  1665. $this->assertEquals($expected, $result);
  1666. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1667. $result = $this->Form->secure($this->Form->fields);
  1668. $expected = array(
  1669. 'div' => array('style' => 'display:none;'),
  1670. array('input' => array(
  1671. 'type' => 'hidden', 'name' => '_Token[fields]',
  1672. 'value' => $hash
  1673. )),
  1674. array('input' => array(
  1675. 'type' => 'hidden', 'name' => '_Token[unlocked]',
  1676. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1677. )),
  1678. '/div'
  1679. );
  1680. $this->assertTags($result, $expected);
  1681. }
  1682. /**
  1683. * Test secured inputs with custom names.
  1684. *
  1685. * @return void
  1686. */
  1687. public function testSecuredInputCustomName() {
  1688. $this->markTestIncomplete('Need to revisit once models work again.');
  1689. $this->Form->request->params['_csrfToken'] = 'testKey';
  1690. $this->assertEquals(array(), $this->Form->fields);
  1691. $this->Form->input('text_input', array(
  1692. 'name' => 'data[Option][General.default_role]',
  1693. ));
  1694. $expected = array('Option.General.default_role');
  1695. $this->assertEquals($expected, $this->Form->fields);
  1696. $this->Form->input('select_box', array(
  1697. 'name' => 'data[Option][General.select_role]',
  1698. 'type' => 'select',
  1699. 'options' => array(1, 2),
  1700. ));
  1701. $expected = array('Option.General.default_role', 'Option.General.select_role');
  1702. $this->assertEquals($expected, $this->Form->fields);
  1703. }
  1704. /**
  1705. * Tests that the correct keys are added to the field hash index
  1706. *
  1707. * @return void
  1708. */
  1709. public function testFormSecuredFileInput() {
  1710. $this->markTestIncomplete('Need to revisit once models work again.');
  1711. $this->Form->request->params['_csrfToken'] = 'testKey';
  1712. $this->assertEquals(array(), $this->Form->fields);
  1713. $this->Form->file('Attachment.file');
  1714. $expected = array(
  1715. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1716. 'Attachment.file.error', 'Attachment.file.size'
  1717. );
  1718. $this->assertEquals($expected, $this->Form->fields);
  1719. }
  1720. /**
  1721. * test that multiple selects keys are added to field hash
  1722. *
  1723. * @return void
  1724. */
  1725. public function testFormSecuredMultipleSelect() {
  1726. $this->markTestIncomplete('Need to revisit once models work again.');
  1727. $this->Form->request->params['_csrfToken'] = 'testKey';
  1728. $this->assertEquals(array(), $this->Form->fields);
  1729. $options = array('1' => 'one', '2' => 'two');
  1730. $this->Form->select('Model.select', $options);
  1731. $expected = array('Model.select');
  1732. $this->assertEquals($expected, $this->Form->fields);
  1733. $this->Form->fields = array();
  1734. $this->Form->select('Model.select', $options, array('multiple' => true));
  1735. $this->assertEquals($expected, $this->Form->fields);
  1736. }
  1737. /**
  1738. * testFormSecuredRadio method
  1739. *
  1740. * @return void
  1741. */
  1742. public function testFormSecuredRadio() {
  1743. $this->markTestIncomplete('Need to revisit once models work again.');
  1744. $this->Form->request->params['_csrfToken'] = 'testKey';
  1745. $this->assertEquals(array(), $this->Form->fields);
  1746. $options = array('1' => 'option1', '2' => 'option2');
  1747. $this->Form->radio('Test.test', $options);
  1748. $expected = array('Test.test');
  1749. $this->assertEquals($expected, $this->Form->fields);
  1750. }
  1751. /**
  1752. * Test that when disabled is in a list based attribute array it works.
  1753. *
  1754. * @return void
  1755. */
  1756. public function testFormSecuredAndDisabledNotAssoc() {
  1757. $this->markTestIncomplete('Need to revisit once models work again.');
  1758. $this->Form->request->params['_csrfToken'] = 'testKey';
  1759. $this->Form->select('Model.select', array(1, 2), array('disabled'));
  1760. $this->Form->checkbox('Model.checkbox', array('disabled'));
  1761. $this->Form->text('Model.text', array('disabled'));
  1762. $this->Form->textarea('Model.textarea', array('disabled'));
  1763. $this->Form->password('Model.password', array('disabled'));
  1764. $this->Form->radio('Model.radio', array(1, 2), array('disabled'));
  1765. $expected = array(
  1766. 'Model.radio' => ''
  1767. );
  1768. $this->assertEquals($expected, $this->Form->fields);
  1769. }
  1770. /**
  1771. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1772. * hashing.
  1773. *
  1774. * @return void
  1775. */
  1776. public function testFormSecuredAndDisabled() {
  1777. $this->markTestIncomplete('Need to revisit once models work again.');
  1778. $this->Form->request->params['_csrfToken'] = 'testKey';
  1779. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1780. $this->Form->text('Model.text', array('disabled' => true));
  1781. $this->Form->password('Model.text', array('disabled' => true));
  1782. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1783. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1784. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1785. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1786. $this->Form->month('Model.month', array('disabled' => true));
  1787. $this->Form->day('Model.day', array('disabled' => true));
  1788. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1789. $this->Form->minute('Model.minute', array('disabled' => true));
  1790. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1791. $expected = array(
  1792. 'Model.radio' => ''
  1793. );
  1794. $this->assertEquals($expected, $this->Form->fields);
  1795. }
  1796. /**
  1797. * testDisableSecurityUsingForm method
  1798. *
  1799. * @return void
  1800. */
  1801. public function testDisableSecurityUsingForm() {
  1802. $this->markTestIncomplete('Need to revisit once models work again.');
  1803. $this->Form->request->params['_csrfToken'] = 'testKey';
  1804. $this->Form->request['_Token'] = array(
  1805. 'disabledFields' => array()
  1806. );
  1807. $this->Form->create();
  1808. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1809. $this->Form->input('Addresses.title');
  1810. $this->Form->input('Addresses.first_name', array('secure' => false));
  1811. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1812. $this->Form->input('Addresses.zip', array(
  1813. 'type' => 'select', 'options' => array(1, 2), 'secure' => false
  1814. ));
  1815. $result = $this->Form->fields;
  1816. $expected = array(
  1817. 'Addresses.id' => '123456', 'Addresses.title',
  1818. );
  1819. $this->assertEquals($expected, $result);
  1820. }
  1821. /**
  1822. * test disableField
  1823. *
  1824. * @return void
  1825. */
  1826. public function testUnlockFieldAddsToList() {
  1827. $this->markTestIncomplete('Need to revisit once models work again.');
  1828. $this->Form->request->params['_csrfToken'] = 'testKey';
  1829. $this->Form->request['_Token'] = array(
  1830. 'unlockedFields' => array()
  1831. );
  1832. $this->Form->create('Contact');
  1833. $this->Form->unlockField('Contact.name');
  1834. $this->Form->text('Contact.name');
  1835. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1836. $this->assertEquals(array(), $this->Form->fields);
  1837. }
  1838. /**
  1839. * test unlockField removing from fields array.
  1840. *
  1841. * @return void
  1842. */
  1843. public function testUnlockFieldRemovingFromFields() {
  1844. $this->markTestIncomplete('Need to revisit once models work again.');
  1845. $this->Form->request->params['_csrfToken'] = 'testKey';
  1846. $this->Form->request['_Token'] = array(
  1847. 'unlockedFields' => array()
  1848. );
  1849. $this->Form->create('Contact');
  1850. $this->Form->hidden('Contact.id', array('value' => 1));
  1851. $this->Form->text('Contact.name');
  1852. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1853. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1854. $this->Form->unlockField('Contact.name');
  1855. $this->Form->unlockField('Contact.id');
  1856. $this->assertEquals(array(), $this->Form->fields);
  1857. }
  1858. /**
  1859. * testTagIsInvalid method
  1860. *
  1861. * @return void
  1862. */
  1863. public function testTagIsInvalid() {
  1864. $this->markTestIncomplete('Need to revisit once models work again.');
  1865. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1866. $this->Form->setEntity('Contact.0.email');
  1867. $result = $this->Form->tagIsInvalid();
  1868. $this->assertEquals($expected, $result);
  1869. $this->Form->setEntity('Contact.1.email');
  1870. $result = $this->Form->tagIsInvalid();
  1871. $this->assertFalse($result);
  1872. $this->Form->setEntity('Contact.0.name');
  1873. $result = $this->Form->tagIsInvalid();
  1874. $this->assertFalse($result);
  1875. }
  1876. /**
  1877. * Test tagIsInvalid with validation errors from a saveMany
  1878. *
  1879. * @return void
  1880. */
  1881. public function testTagIsInvalidSaveMany() {
  1882. $this->markTestIncomplete('Need to revisit once models work again.');
  1883. $Contact->validationErrors[0]['email'] = $expected = array('Please provide an email');
  1884. $this->Form->create('Contact');
  1885. $this->Form->setEntity('0.email');
  1886. $result = $this->Form->tagIsInvalid();
  1887. $this->assertEquals($expected, $result);
  1888. $this->Form->setEntity('0.Contact.email');
  1889. $result = $this->Form->tagIsInvalid();
  1890. $this->assertEquals($expected, $result);
  1891. }
  1892. /**
  1893. * Test validation errors.
  1894. *
  1895. * @return void
  1896. */
  1897. public function testPasswordValidation() {
  1898. $this->markTestIncomplete('Need to revisit once models work again.');
  1899. $Contact->validationErrors['password'] = array('Please provide a password');
  1900. $result = $this->Form->input('Contact.password');
  1901. $expected = array(
  1902. 'div' => array('class' => 'input password error'),
  1903. 'label' => array('for' => 'ContactPassword'),
  1904. 'Password',
  1905. '/label',
  1906. 'input' => array(
  1907. 'type' => 'password', 'name' => 'Contact[password]',
  1908. 'id' => 'ContactPassword', 'class' => 'form-error'
  1909. ),
  1910. array('div' => array('class' => 'error-message')),
  1911. 'Please provide a password',
  1912. '/div',
  1913. '/div'
  1914. );
  1915. $this->assertTags($result, $expected);
  1916. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1917. $expected = array(
  1918. 'div' => array('class' => 'input password error'),
  1919. 'label' => array('for' => 'ContactPassword'),
  1920. 'Password',
  1921. '/label',
  1922. 'input' => array(
  1923. 'type' => 'password', 'name' => 'Contact[password]',
  1924. 'id' => 'ContactPassword', 'class' => 'form-error'
  1925. ),
  1926. '/div'
  1927. );
  1928. $this->assertTags($result, $expected);
  1929. }
  1930. /**
  1931. * Test validation errors, when validation message is an empty string.
  1932. *
  1933. * @return void
  1934. */
  1935. public function testEmptyErrorValidation() {
  1936. $this->markTestIncomplete('Need to revisit once models work again.');
  1937. $this->Form->validationErrors['Contact']['password'] = '';
  1938. $result = $this->Form->input('Contact.password');
  1939. $expected = array(
  1940. 'div' => array('class' => 'input password error'),
  1941. 'label' => array('for' => 'ContactPassword'),
  1942. 'Password',
  1943. '/label',
  1944. 'input' => array(
  1945. 'type' => 'password', 'name' => 'Contact[password]',
  1946. 'id' => 'ContactPassword', 'class' => 'form-error'
  1947. ),
  1948. array('div' => array('class' => 'error-message')),
  1949. array(),
  1950. '/div',
  1951. '/div'
  1952. );
  1953. $this->assertTags($result, $expected);
  1954. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1955. $expected = array(
  1956. 'div' => array('class' => 'input password error'),
  1957. 'label' => array('for' => 'ContactPassword'),
  1958. 'Password',
  1959. '/label',
  1960. 'input' => array(
  1961. 'type' => 'password', 'name' => 'Contact[password]',
  1962. 'id' => 'ContactPassword', 'class' => 'form-error'
  1963. ),
  1964. '/div'
  1965. );
  1966. $this->assertTags($result, $expected);
  1967. }
  1968. /**
  1969. * Test validation errors, when calling input() overriding validation message by an empty string.
  1970. *
  1971. * @return void
  1972. */
  1973. public function testEmptyInputErrorValidation() {
  1974. $this->markTestIncomplete('Need to revisit once models work again.');
  1975. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1976. $result = $this->Form->input('Contact.password', array('error' => ''));
  1977. $expected = array(
  1978. 'div' => array('class' => 'input password error'),
  1979. 'label' => array('for' => 'ContactPassword'),
  1980. 'Password',
  1981. '/label',
  1982. 'input' => array(
  1983. 'type' => 'password', 'name' => 'Contact[password]',
  1984. 'id' => 'ContactPassword', 'class' => 'form-error'
  1985. ),
  1986. array('div' => array('class' => 'error-message')),
  1987. array(),
  1988. '/div',
  1989. '/div'
  1990. );
  1991. $this->assertTags($result, $expected);
  1992. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1993. $expected = array(
  1994. 'div' => array('class' => 'input password error'),
  1995. 'label' => array('for' => 'ContactPassword'),
  1996. 'Password',
  1997. '/label',
  1998. 'input' => array(
  1999. 'type' => 'password', 'name' => 'Contact[password]',
  2000. 'id' => 'ContactPassword', 'class' => 'form-error'
  2001. ),
  2002. '/div'
  2003. );
  2004. $this->assertTags($result, $expected);
  2005. }
  2006. /**
  2007. * testFormValidationAssociated method
  2008. *
  2009. * test display of form errors in conjunction with model::validates.
  2010. *
  2011. * @return void
  2012. */
  2013. public function testFormValidationAssociated() {
  2014. $this->markTestIncomplete('Need to revisit once models work again.');
  2015. $data = array(
  2016. 'UserForm' => array('name' => 'user'),
  2017. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  2018. );
  2019. $result = $this->UserForm->OpenidUrl->create($data);
  2020. $this->assertFalse(empty($result));
  2021. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  2022. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  2023. $encoding = strtolower(Configure::read('App.encoding'));
  2024. $expected = array(
  2025. 'form' => array(
  2026. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  2027. 'accept-charset' => $encoding
  2028. ),
  2029. 'div' => array('style' => 'display:none;'),
  2030. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  2031. '/div'
  2032. );
  2033. $this->assertTags($result, $expected);
  2034. $result = $this->Form->error(
  2035. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  2036. );
  2037. $this->assertEquals('Error, not registered', $result);
  2038. unset($this->UserForm->OpenidUrl, $this->UserForm);
  2039. }
  2040. /**
  2041. * testFormValidationAssociatedFirstLevel method
  2042. *
  2043. * test form error display with associated model.
  2044. *
  2045. * @return void
  2046. */
  2047. public function testFormValidationAssociatedFirstLevel() {
  2048. $this->markTestIncomplete('Need to revisit once models work again.');
  2049. $data = array(
  2050. 'ValidateUser' => array('name' => 'mariano'),
  2051. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  2052. );
  2053. $result = $this->ValidateUser->create($data);
  2054. $this->assertFalse(empty($result));
  2055. $this->assertFalse($this->ValidateUser->validates());
  2056. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  2057. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  2058. $encoding = strtolower(Configure::read('App.encoding'));
  2059. $expected = array(
  2060. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  2061. 'div' => array('style' => 'display:none;'),
  2062. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  2063. '/div'
  2064. );
  2065. $this->assertTags($result, $expected);
  2066. $result = $this->Form->error(
  2067. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  2068. );
  2069. $this->assertEquals('Invalid email', $result);
  2070. $result = $this->Form->error(
  2071. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  2072. );
  2073. $this->assertEquals('Invalid name', $result);
  2074. $result = $this->Form->error(
  2075. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  2076. );
  2077. $this->assertEquals('Invalid city', $result);
  2078. unset($this->ValidateUser->ValidateProfile);
  2079. unset($this->ValidateUser);
  2080. }
  2081. /**
  2082. * testFormValidationAssociatedSecondLevel method
  2083. *
  2084. * test form error display with associated model.
  2085. *
  2086. * @return void
  2087. */
  2088. public function testFormValidationAssociatedSecondLevel() {
  2089. $this->markTestIncomplete('Need to revisit once models work again.');
  2090. $data = array(
  2091. 'ValidateUser' => array('name' => 'mariano'),
  2092. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  2093. 'ValidateItem' => array('name' => 'Item')
  2094. );
  2095. $result = $this->ValidateUser->create($data);
  2096. $this->assertFalse(empty($result));
  2097. $this->assertFalse($this->ValidateUser->validates());
  2098. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  2099. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  2100. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  2101. $encoding = strtolower(Configure::read('App.encoding'));
  2102. $expected = array(
  2103. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id', 'accept-charset' => $encoding),
  2104. 'div' => array('style' => 'display:none;'),
  2105. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  2106. '/div'
  2107. );
  2108. $this->assertTags($result, $expected);
  2109. $result = $this->Form->error(
  2110. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  2111. );
  2112. $this->assertEquals('Invalid email', $result);
  2113. $result = $this->Form->error(
  2114. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  2115. );
  2116. $this->assertEquals('Invalid name', $result);
  2117. $result = $this->Form->error(
  2118. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  2119. );
  2120. $result = $this->Form->error(
  2121. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  2122. );
  2123. $this->assertEquals('Invalid description', $result);
  2124. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  2125. unset($this->ValidateUser->ValidateProfile);
  2126. unset($this->ValidateUser);
  2127. }
  2128. /**
  2129. * testFormValidationMultiRecord method
  2130. *
  2131. * test form error display with multiple records.
  2132. *
  2133. * @return void
  2134. */
  2135. public function testFormValidationMultiRecord() {
  2136. $this->markTestIncomplete('Need to revisit once models work again.');
  2137. $Contact->validationErrors[2] = array(
  2138. 'name' => array('The provided value is invalid')
  2139. );
  2140. $result = $this->Form->input('Contact.2.name');
  2141. $expected = array(
  2142. 'div' => array('class' => 'input text error'),
  2143. 'label' => array('for' => 'Contact2Name'),
  2144. 'Name',
  2145. '/label',
  2146. 'input' => array(
  2147. 'type' => 'text', 'name' => 'Contact[2][name]', 'id' => 'Contact2Name',
  2148. 'class' => 'form-error', 'maxlength' => 255
  2149. ),
  2150. array('div' => array('class' => 'error-message')),
  2151. 'The provided value is invalid',
  2152. '/div',
  2153. '/div'
  2154. );
  2155. $this->assertTags($result, $expected);
  2156. }
  2157. /**
  2158. * testMultipleInputValidation method
  2159. *
  2160. * test multiple record form validation error display.
  2161. *
  2162. * @return void
  2163. */
  2164. public function testMultipleInputValidation() {
  2165. $this->markTestIncomplete('Need to revisit once models work again.');
  2166. $Address->validationErrors[0] = array(
  2167. 'title' => array('This field cannot be empty'),
  2168. 'first_name' => array('This field cannot be empty')
  2169. );
  2170. $Address->validationErrors[1] = array(
  2171. 'last_name' => array('You must have a last name')
  2172. );
  2173. $this->Form->create();
  2174. $result = $this->Form->input('Address.0.title');
  2175. $expected = array(
  2176. 'div' => array('class'),
  2177. 'label' => array('for'),
  2178. 'preg:/[^<]+/',
  2179. '/label',
  2180. 'input' => array(
  2181. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  2182. ),
  2183. array('div' => array('class' => 'error-message')),
  2184. 'This field cannot be empty',
  2185. '/div',
  2186. '/div'
  2187. );
  2188. $this->assertTags($result, $expected);
  2189. $result = $this->Form->input('Address.0.first_name');
  2190. $expected = array(
  2191. 'div' => array('class'),
  2192. 'label' => array('for'),
  2193. 'preg:/[^<]+/',
  2194. '/label',
  2195. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  2196. array('div' => array('class' => 'error-message')),
  2197. 'This field cannot be empty',
  2198. '/div',
  2199. '/div'
  2200. );
  2201. $this->assertTags($result, $expected);
  2202. $result = $this->Form->input('Address.0.last_name');
  2203. $expected = array(
  2204. 'div' => array('class'),
  2205. 'label' => array('for'),
  2206. 'preg:/[^<]+/',
  2207. '/label',
  2208. 'input' => array('type' => 'text', 'name', 'id'),
  2209. '/div'
  2210. );
  2211. $this->assertTags($result, $expected);
  2212. $result = $this->Form->input('Address.1.last_name');
  2213. $expected = array(
  2214. 'div' => array('class'),
  2215. 'label' => array('for'),
  2216. 'preg:/[^<]+/',
  2217. '/label',
  2218. 'input' => array(
  2219. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  2220. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  2221. ),
  2222. array('div' => array('class' => 'error-message')),
  2223. 'You must have a last name',
  2224. '/div',
  2225. '/div'
  2226. );
  2227. $this->assertTags($result, $expected);
  2228. }
  2229. /**
  2230. * testInput method
  2231. *
  2232. * Test various incarnations of input().
  2233. *
  2234. * @return void
  2235. */
  2236. public function testInput() {
  2237. $this->markTestIncomplete('Need to revisit once models work again.');
  2238. $result = $this->Form->input('ValidateUser.balance');
  2239. $expected = array(
  2240. 'div' => array('class'),
  2241. 'label' => array('for'),
  2242. 'Balance',
  2243. '/label',
  2244. 'input' => array('name', 'type' => 'number', 'id'),
  2245. '/div',
  2246. );
  2247. $this->assertTags($result, $expected);
  2248. $result = $this->Form->input('ValidateUser.cost_decimal');
  2249. $expected = array(
  2250. 'div' => array('class'),
  2251. 'label' => array('for'),
  2252. 'Cost Decimal',
  2253. '/label',
  2254. 'input' => array('name', 'type' => 'number', 'step' => '0.001', 'id'),
  2255. '/div',
  2256. );
  2257. $this->assertTags($result, $expected);
  2258. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  2259. $expected = array(
  2260. 'div' => array('class' => 'input email'),
  2261. 'label' => array('for' => 'custom'),
  2262. 'Email',
  2263. '/label',
  2264. array('input' => array(
  2265. 'type' => 'email', 'name' => 'Contact[email]',
  2266. 'id' => 'custom', 'maxlength' => 255
  2267. )),
  2268. '/div'
  2269. );
  2270. $this->assertTags($result, $expected);
  2271. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  2272. $expected = array(
  2273. '<div',
  2274. 'label' => array('for' => 'ContactEmail'),
  2275. 'Email',
  2276. '/label',
  2277. array('input' => array(
  2278. 'type' => 'email', 'name' => 'Contact[email]',
  2279. 'id' => 'ContactEmail', 'maxlength' => 255
  2280. )),
  2281. '/div'
  2282. );
  2283. $this->assertTags($result, $expected);
  2284. $result = $this->Form->hidden('Contact.idontexist');
  2285. $expected = array('input' => array(
  2286. 'type' => 'hidden', 'name' => 'Contact[idontexist]',
  2287. 'id' => 'ContactIdontexist'
  2288. ));
  2289. $this->assertTags($result, $expected);
  2290. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  2291. $expected = array(
  2292. 'div' => array('class' => 'input text'),
  2293. 'label' => array('for' => 'ContactEmail'),
  2294. 'Email',
  2295. '/label',
  2296. array('input' => array(
  2297. 'type' => 'text', 'name' => 'Contact[email]',
  2298. 'id' => 'ContactEmail'
  2299. )),
  2300. '/div'
  2301. );
  2302. $this->assertTags($result, $expected);
  2303. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  2304. $expected = array(
  2305. 'div' => array('class' => 'input text'),
  2306. 'label' => array('for' => 'Contact5Email'),
  2307. 'Email',
  2308. '/label',
  2309. array('input' => array(
  2310. 'type' => 'text', 'name' => 'Contact[5][email]',
  2311. 'id' => 'Contact5Email'
  2312. )),
  2313. '/div'
  2314. );
  2315. $this->assertTags($result, $expected);
  2316. $result = $this->Form->input('Contact.password');
  2317. $expected = array(
  2318. 'div' => array('class' => 'input password'),
  2319. 'label' => array('for' => 'ContactPassword'),
  2320. 'Password',
  2321. '/label',
  2322. array('input' => array(
  2323. 'type' => 'password', 'name' => 'Contact[password]',
  2324. 'id' => 'ContactPassword'
  2325. )),
  2326. '/div'
  2327. );
  2328. $this->assertTags($result, $expected);
  2329. $result = $this->Form->input('Contact.email', array(
  2330. 'type' => 'file', 'class' => 'textbox'
  2331. ));
  2332. $expected = array(
  2333. 'div' => array('class' => 'input file'),
  2334. 'label' => array('for' => 'ContactEmail'),
  2335. 'Email',
  2336. '/label',
  2337. array('input' => array(
  2338. 'type' => 'file', 'name' => 'Contact[email]', 'class' => 'textbox',
  2339. 'id' => 'ContactEmail'
  2340. )),
  2341. '/div'
  2342. );
  2343. $this->assertTags($result, $expected);
  2344. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  2345. $result = $this->Form->input('Contact.phone');
  2346. $expected = array(
  2347. 'div' => array('class' => 'input tel'),
  2348. 'label' => array('for' => 'ContactPhone'),
  2349. 'Phone',
  2350. '/label',
  2351. array('input' => array(
  2352. 'type' => 'tel', 'name' => 'Contact[phone]',
  2353. 'value' => 'Hello &amp; World &gt; weird chars',
  2354. 'id' => 'ContactPhone', 'maxlength' => 255
  2355. )),
  2356. '/div'
  2357. );
  2358. $this->assertTags($result, $expected);
  2359. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2360. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  2361. $expected = array(
  2362. 'div' => array('class' => 'input text'),
  2363. 'label' => array('for' => 'myId'),
  2364. 'Field',
  2365. '/label',
  2366. 'input' => array(
  2367. 'type' => 'text', 'name' => 'Model[0][OtherModel][field]',
  2368. 'value' => 'My value', 'id' => 'myId'
  2369. ),
  2370. '/div'
  2371. );
  2372. $this->assertTags($result, $expected);
  2373. unset($this->Form->request->data);
  2374. $Contact->validationErrors['field'] = array('Badness!');
  2375. $result = $this->Form->input('Contact.field');
  2376. $expected = array(
  2377. 'div' => array('class' => 'input text error'),
  2378. 'label' => array('for' => 'ContactField'),
  2379. 'Field',
  2380. '/label',
  2381. 'input' => array(
  2382. 'type' => 'text', 'name' => 'Contact[field]',
  2383. 'id' => 'ContactField', 'class' => 'form-error'
  2384. ),
  2385. array('div' => array('class' => 'error-message')),
  2386. 'Badness!',
  2387. '/div',
  2388. '/div'
  2389. );
  2390. $this->assertTags($result, $expected);
  2391. $result = $this->Form->input('Contact.field', array(
  2392. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  2393. ));
  2394. $expected = array(
  2395. 'label' => array('for' => 'ContactField'),
  2396. 'Field',
  2397. '/label',
  2398. 'input' => array(
  2399. 'type' => 'text', 'name' => 'Contact[field]',
  2400. 'id' => 'ContactField', 'class' => 'form-error'
  2401. ),
  2402. array('span' => array('class' => 'error-message')),
  2403. 'Badness!',
  2404. '/span'
  2405. );
  2406. $this->assertTags($result, $expected);
  2407. $result = $this->Form->input('Contact.field', array(
  2408. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  2409. ));
  2410. $expected = array(
  2411. 'div' => array('class' => 'input text error'),
  2412. 'label' => array('for' => 'ContactField'),
  2413. 'Field',
  2414. '/label',
  2415. 'input' => array(
  2416. 'type' => 'text', 'name' => 'Contact[field]',
  2417. 'id' => 'ContactField', 'class' => 'form-error'
  2418. ),
  2419. array('div' => array('class' => 'error')),
  2420. 'Badness!',
  2421. '/div'
  2422. );
  2423. $this->assertTags($result, $expected);
  2424. $result = $this->Form->input('Contact.field', array(
  2425. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  2426. ));
  2427. $expected = array(
  2428. 'span' => array('class' => 'input text error'),
  2429. 'label' => array('for' => 'ContactField'),
  2430. 'Field',
  2431. '/label',
  2432. 'input' => array(
  2433. 'type' => 'text', 'name' => 'Contact[field]',
  2434. 'id' => 'ContactField', 'class' => 'form-error'
  2435. ),
  2436. 'Badness!',
  2437. '/span'
  2438. );
  2439. $this->assertTags($result, $expected);
  2440. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  2441. $expected = array(
  2442. 'div' => array('class' => 'input text error'),
  2443. 'label' => array('for' => 'ContactField'),
  2444. 'Field',
  2445. '/label',
  2446. 'input' => array(
  2447. 'type' => 'text', 'name' => 'Contact[field]',
  2448. 'id' => 'ContactField', 'class' => 'form-error'
  2449. ),
  2450. 'A message to you, Rudy',
  2451. array('div' => array('class' => 'error-message')),
  2452. 'Badness!',
  2453. '/div',
  2454. '/div'
  2455. );
  2456. $this->assertTags($result, $expected);
  2457. $this->Form->setEntity(null);
  2458. $this->Form->setEntity('Contact.field');
  2459. $result = $this->Form->input('Contact.field', array(
  2460. 'after' => 'A message to you, Rudy', 'error' => false
  2461. ));
  2462. $expected = array(
  2463. 'div' => array('class' => 'input text'),
  2464. 'label' => array('for' => 'ContactField'),
  2465. 'Field',
  2466. '/label',
  2467. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2468. 'A message to you, Rudy',
  2469. '/div'
  2470. );
  2471. $this->assertTags($result, $expected);
  2472. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  2473. $expected = array(
  2474. 'div' => array('class' => 'input text'),
  2475. 'label' => array('for' => 'ObjectField'),
  2476. 'Field',
  2477. '/label',
  2478. 'input' => array('type' => 'text', 'name' => 'Object[field]', 'id' => 'ObjectField'),
  2479. 'A message to you, Rudy',
  2480. '/div'
  2481. );
  2482. $this->assertTags($result, $expected);
  2483. $Contact->validationErrors['field'] = array('minLength');
  2484. $result = $this->Form->input('Contact.field', array(
  2485. 'error' => array(
  2486. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2487. 'maxLength' => 'login too large'
  2488. )
  2489. ));
  2490. $expected = array(
  2491. 'div' => array('class' => 'input text error'),
  2492. 'label' => array('for' => 'ContactField'),
  2493. 'Field',
  2494. '/label',
  2495. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2496. array('div' => array('class' => 'error-message')),
  2497. 'Le login doit contenir au moins 2 caractères',
  2498. '/div',
  2499. '/div'
  2500. );
  2501. $this->assertTags($result, $expected);
  2502. $Contact->validationErrors['field'] = array('maxLength');
  2503. $result = $this->Form->input('Contact.field', array(
  2504. 'error' => array(
  2505. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  2506. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  2507. 'maxLength' => 'login too large',
  2508. )
  2509. ));
  2510. $expected = array(
  2511. 'div' => array('class' => 'input text error'),
  2512. 'label' => array('for' => 'ContactField'),
  2513. 'Field',
  2514. '/label',
  2515. 'input' => array('type' => 'text', 'name' => 'Contact[field]', 'id' => 'ContactField', 'class' => 'form-error'),
  2516. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  2517. 'login too large',
  2518. '/span',
  2519. '/div'
  2520. );
  2521. $this->assertTags($result, $expected);
  2522. }
  2523. /**
  2524. * Test that inputs with 0 can be created.
  2525. *
  2526. * @return void
  2527. */
  2528. public function testInputZero() {
  2529. $this->markTestIncomplete('Need to revisit once models work again.');
  2530. $this->Form->create('User');
  2531. $result = $this->Form->input('0');
  2532. $expected = array(
  2533. 'div' => array('class' => 'input text'),
  2534. 'label' => array('for' => 'User0'), '/label',
  2535. 'input' => array('type' => 'text', 'name' => 'User[0]', 'id' => 'User0'),
  2536. '/div'
  2537. );
  2538. $this->assertTags($result, $expected);
  2539. }
  2540. /**
  2541. * test input() with checkbox creation
  2542. *
  2543. * @return void
  2544. */
  2545. public function testInputCheckbox() {
  2546. $this->markTestIncomplete('Need to revisit once models work again.');
  2547. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  2548. $expected = array(
  2549. 'div' => array('class' => 'input checkbox'),
  2550. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2551. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2552. '/div'
  2553. );
  2554. $this->assertTags($result, $expected);
  2555. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  2556. $expected = array(
  2557. 'div' => array('class' => 'input checkbox'),
  2558. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2559. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2560. '/div'
  2561. );
  2562. $this->assertTags($result, $expected);
  2563. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  2564. $expected = array(
  2565. 'div' => array('class' => 'input checkbox'),
  2566. 'input' => array('type' => 'hidden', 'name' => 'User[active]', 'value' => '0', 'id' => 'UserActive_'),
  2567. array('input' => array('type' => 'checkbox', 'name' => 'User[active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  2568. '/div'
  2569. );
  2570. $this->assertTags($result, $expected);
  2571. $result = $this->Form->input('User.disabled', array(
  2572. 'label' => 'Disabled',
  2573. 'type' => 'checkbox',
  2574. 'data-foo' => 'disabled'
  2575. ));
  2576. $expected = array(
  2577. 'div' => array('class' => 'input checkbox'),
  2578. 'input' => array('type' => 'hidden', 'name' => 'data[User][disabled]', 'value' => '0', 'id' => 'UserDisabled_'),
  2579. array('input' => array(
  2580. 'type' => 'checkbox',
  2581. 'name' => 'data[User][disabled]',
  2582. 'value' => '1',
  2583. 'id' => 'UserDisabled',
  2584. 'data-foo' => 'disabled'
  2585. )),
  2586. 'label' => array('for' => 'UserDisabled'),
  2587. 'Disabled',
  2588. '/label',
  2589. '/div'
  2590. );
  2591. $this->assertTags($result, $expected);
  2592. }
  2593. /**
  2594. * test form->input() with time types.
  2595. *
  2596. */
  2597. public function testInputTime() {
  2598. $this->markTestIncomplete('Need to revisit once models work again.');
  2599. extract($this->dateRegex);
  2600. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2601. $result = explode(':', $result);
  2602. $this->assertRegExp('/option value="23"/', $result[0]);
  2603. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2604. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  2605. $result = explode(':', $result);
  2606. $this->assertRegExp('/option value="23"/', $result[0]);
  2607. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2608. $result = $this->Form->input('Model.field', array(
  2609. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  2610. ));
  2611. $result = explode(':', $result);
  2612. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2613. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2614. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2615. $result = $this->Form->input('Model.field', array(
  2616. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2617. ));
  2618. $result = explode(':', $result);
  2619. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2620. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2621. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2622. $result = $this->Form->input('prueba', array(
  2623. 'type' => 'time', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2624. 'maxYear' => date('Y') + 1, 'interval' => 15
  2625. ));
  2626. $result = explode(':', $result);
  2627. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2628. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2629. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2630. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2631. $result = $this->Form->input('Random.start_time', array(
  2632. 'type' => 'time',
  2633. 'selected' => '18:15'
  2634. ));
  2635. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2636. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2637. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2638. $result = $this->Form->input('published', array('type' => 'time'));
  2639. $now = strtotime('now');
  2640. $this->assertContains('<option value="' . date('h', $now) . '" selected="selected">' . date('g', $now) . '</option>', $result);
  2641. $now = strtotime('2013-03-09 00:42:21');
  2642. $result = $this->Form->input('published', array('type' => 'time', 'selected' => $now));
  2643. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2644. $this->assertContains('<option value="42" selected="selected">42</option>', $result);
  2645. }
  2646. /**
  2647. * Test interval + selected near the hour roll over.
  2648. *
  2649. * @return void
  2650. */
  2651. public function testTimeSelectedWithInterval() {
  2652. $this->markTestIncomplete('Need to revisit once models work again.');
  2653. $result = $this->Form->input('Model.start_time', array(
  2654. 'type' => 'time',
  2655. 'interval' => 15,
  2656. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2657. ));
  2658. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2659. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2660. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2661. $result = $this->Form->input('Model.start_time', array(
  2662. 'type' => 'time',
  2663. 'interval' => 15,
  2664. 'selected' => '2012-10-23 15:57:00'
  2665. ));
  2666. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2667. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2668. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2669. $result = $this->Form->input('Model.start_time', array(
  2670. 'timeFormat' => 24,
  2671. 'type' => 'time',
  2672. 'interval' => 15,
  2673. 'selected' => '15:57'
  2674. ));
  2675. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2676. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2677. $result = $this->Form->input('Model.start_time', array(
  2678. 'timeFormat' => 24,
  2679. 'type' => 'time',
  2680. 'interval' => 15,
  2681. 'selected' => '23:57'
  2682. ));
  2683. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2684. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2685. $result = $this->Form->input('Model.created', array(
  2686. 'timeFormat' => 24,
  2687. 'type' => 'datetime',
  2688. 'interval' => 15,
  2689. 'selected' => '2012-09-30 23:56'
  2690. ));
  2691. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2692. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2693. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2694. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2695. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2696. }
  2697. /**
  2698. * Test time with selected values around 12:xx:xx
  2699. *
  2700. * @return void
  2701. */
  2702. public function testTimeSelectedWithIntervalTwelve() {
  2703. $this->markTestIncomplete('Need to revisit once models work again.');
  2704. $result = $this->Form->input('Model.start_time', array(
  2705. 'type' => 'time',
  2706. 'timeFormat' => 12,
  2707. 'interval' => 15,
  2708. 'selected' => '00:00:00'
  2709. ));
  2710. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2711. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2712. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2713. $result = $this->Form->input('Model.start_time', array(
  2714. 'type' => 'time',
  2715. 'timeFormat' => 12,
  2716. 'interval' => 15,
  2717. 'selected' => '12:00:00'
  2718. ));
  2719. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2720. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2721. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2722. $result = $this->Form->input('Model.start_time', array(
  2723. 'type' => 'time',
  2724. 'timeFormat' => 12,
  2725. 'interval' => 15,
  2726. 'selected' => '12:15:00'
  2727. ));
  2728. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2729. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2730. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2731. }
  2732. /**
  2733. * Test interval & timeFormat = 12
  2734. *
  2735. * @return void
  2736. */
  2737. public function testInputTimeWithIntervalAnd12HourFormat() {
  2738. $this->markTestIncomplete('Need to revisit once models work again.');
  2739. $result = $this->Form->input('Model.start_time', array(
  2740. 'type' => 'time',
  2741. 'timeFormat' => 12,
  2742. 'interval' => 5,
  2743. 'selected' => array('hour' => '4', 'min' => '30', 'meridian' => 'pm')
  2744. ));
  2745. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2746. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2747. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2748. $result = $this->Form->input('Model.start_time', array(
  2749. 'type' => 'time',
  2750. 'timeFormat' => '12',
  2751. 'interval' => 5,
  2752. 'selected' => '2013-04-19 16:30:00'
  2753. ));
  2754. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2755. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2756. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2757. $result = $this->Form->input('Model.start_time', array(
  2758. 'type' => 'time',
  2759. 'timeFormat' => '12',
  2760. 'interval' => 10,
  2761. 'selected' => '2013-05-19 00:33:00'
  2762. ));
  2763. $this->assertContains('<option value="12" selected="selected">12</option>', $result);
  2764. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2765. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2766. $result = $this->Form->input('Model.start_time', array(
  2767. 'type' => 'time',
  2768. 'timeFormat' => '12',
  2769. 'interval' => 10,
  2770. 'selected' => '2013-05-19 13:33:00'
  2771. ));
  2772. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2773. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2774. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2775. $result = $this->Form->input('Model.start_time', array(
  2776. 'type' => 'time',
  2777. 'timeFormat' => '12',
  2778. 'interval' => 10,
  2779. 'selected' => '2013-05-19 01:33:00'
  2780. ));
  2781. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2782. $this->assertContains('<option value="30" selected="selected">30</option>', $result);
  2783. $this->assertContains('<option value="am" selected="selected">am</option>', $result);
  2784. }
  2785. /**
  2786. * test form->input() with datetime, date and time types
  2787. *
  2788. * @return void
  2789. */
  2790. public function testInputDatetime() {
  2791. $this->markTestIncomplete('Need to revisit once models work again.');
  2792. extract($this->dateRegex);
  2793. $result = $this->Form->input('prueba', array(
  2794. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2795. 'maxYear' => date('Y') + 1, 'interval' => 15
  2796. ));
  2797. $result = explode(':', $result);
  2798. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2799. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2800. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2801. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2802. //related to ticket #5013
  2803. $result = $this->Form->input('Contact.date', array(
  2804. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2805. ));
  2806. $this->assertRegExp('/class="customClass"/', $result);
  2807. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2808. $result = $this->Form->input('Contact.date', array(
  2809. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2810. ));
  2811. $this->assertRegExp('/id="customIdDay"/', $result);
  2812. $this->assertRegExp('/id="customIdMonth"/', $result);
  2813. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2814. $result = $this->Form->input('Model.field', array(
  2815. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2816. ));
  2817. $this->assertRegExp('/id="customIDDay"/', $result);
  2818. $this->assertRegExp('/id="customIDHour"/', $result);
  2819. $result = explode('</select><select', $result);
  2820. $result = explode(':', $result[1]);
  2821. $this->assertRegExp('/option value="23"/', $result[0]);
  2822. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2823. $result = $this->Form->input('Model.field', array(
  2824. 'type' => 'datetime', 'timeFormat' => 12
  2825. ));
  2826. $result = explode('</select><select', $result);
  2827. $result = explode(':', $result[1]);
  2828. $this->assertRegExp('/option value="12"/', $result[0]);
  2829. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2830. $this->Form->request->data = array('Contact' => array('created' => null));
  2831. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2832. $expected = array(
  2833. 'div' => array('class' => 'input date'),
  2834. 'label' => array('for' => 'ContactCreatedMonth'),
  2835. 'Created',
  2836. '/label',
  2837. array('select' => array('name' => 'Contact[created][month]', 'id' => 'ContactCreatedMonth')),
  2838. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2839. $monthsRegex,
  2840. '/select', '-',
  2841. array('select' => array('name' => 'Contact[created][day]', 'id' => 'ContactCreatedDay')),
  2842. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2843. $daysRegex,
  2844. '/select', '-',
  2845. array('select' => array('name' => 'Contact[created][year]', 'id' => 'ContactCreatedYear')),
  2846. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2847. $yearsRegex,
  2848. '/select',
  2849. '/div'
  2850. );
  2851. $this->assertTags($result, $expected);
  2852. $this->Form->request->data = array('Contact' => array('created' => null));
  2853. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2854. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2855. $this->Form->request->data = array('Contact' => array('created' => null));
  2856. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2857. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2858. $result = $this->Form->input('Contact.created', array(
  2859. 'type' => 'date',
  2860. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2861. 'timeFormat' => 'NONE'
  2862. ));
  2863. $this->assertRegExp('/for\="created-month"/', $result);
  2864. }
  2865. /**
  2866. * Test generating checkboxes with disabled elements.
  2867. *
  2868. * @return void
  2869. */
  2870. public function testInputCheckboxWithDisabledElements() {
  2871. $this->markTestIncomplete('Need to revisit once models work again.');
  2872. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2873. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2874. $expected = array(
  2875. array('div' => array('class' => 'input select')),
  2876. array('label' => array('for' => "ContactMultiple")),
  2877. 'Multiple',
  2878. '/label',
  2879. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2880. array('div' => array('class' => 'checkbox')),
  2881. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2882. array('label' => array('for' => "ContactMultiple1")),
  2883. 'One',
  2884. '/label',
  2885. '/div',
  2886. array('div' => array('class' => 'checkbox')),
  2887. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2888. array('label' => array('for' => "ContactMultiple2")),
  2889. 'Two',
  2890. '/label',
  2891. '/div',
  2892. array('div' => array('class' => 'checkbox')),
  2893. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2894. array('label' => array('for' => "ContactMultiple3")),
  2895. 'Three',
  2896. '/label',
  2897. '/div',
  2898. '/div'
  2899. );
  2900. $this->assertTags($result, $expected);
  2901. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2902. $expected = array(
  2903. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '', 'id' => 'ModelField_'),
  2904. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2905. 'label' => array('for' => 'ModelField12'),
  2906. 'half',
  2907. '/label'
  2908. );
  2909. $this->assertTags($result, $expected);
  2910. $disabled = array('2', 3);
  2911. $expected = array(
  2912. array('div' => array('class' => 'input select')),
  2913. array('label' => array('for' => "ContactMultiple")),
  2914. 'Multiple',
  2915. '/label',
  2916. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2917. array('div' => array('class' => 'checkbox')),
  2918. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2919. array('label' => array('for' => "ContactMultiple1")),
  2920. 'One',
  2921. '/label',
  2922. '/div',
  2923. array('div' => array('class' => 'checkbox')),
  2924. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2925. array('label' => array('for' => "ContactMultiple2")),
  2926. 'Two',
  2927. '/label',
  2928. '/div',
  2929. array('div' => array('class' => 'checkbox')),
  2930. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2931. array('label' => array('for' => "ContactMultiple3")),
  2932. 'Three',
  2933. '/label',
  2934. '/div',
  2935. '/div'
  2936. );
  2937. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2938. $this->assertTags($result, $expected);
  2939. // make sure 50 does only disable 50, and not 50f5c0cf
  2940. $options = array('50' => 'Fifty', '50f5c0cf' => 'Stringy');
  2941. $disabled = array(50);
  2942. $expected = array(
  2943. array('div' => array('class' => 'input select')),
  2944. array('label' => array('for' => "ContactMultiple")),
  2945. 'Multiple',
  2946. '/label',
  2947. array('input' => array('type' => 'hidden', 'name' => "Contact[multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2948. array('div' => array('class' => 'checkbox')),
  2949. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => 50, 'disabled' => 'disabled', 'id' => "ContactMultiple50")),
  2950. array('label' => array('for' => "ContactMultiple50")),
  2951. 'Fifty',
  2952. '/label',
  2953. '/div',
  2954. array('div' => array('class' => 'checkbox')),
  2955. array('input' => array('type' => 'checkbox', 'name' => "Contact[multiple][]", 'value' => '50f5c0cf', 'id' => "ContactMultiple50f5c0cf")),
  2956. array('label' => array('for' => "ContactMultiple50f5c0cf")),
  2957. 'Stringy',
  2958. '/label',
  2959. '/div',
  2960. '/div'
  2961. );
  2962. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2963. $this->assertTags($result, $expected);
  2964. }
  2965. /**
  2966. * test input name with leading integer, ensure attributes are generated correctly.
  2967. *
  2968. * @return void
  2969. */
  2970. public function testInputWithLeadingInteger() {
  2971. $result = $this->Form->text('0.Node.title');
  2972. $expected = array(
  2973. 'input' => array('name' => '0[Node][title]', 'type' => 'text')
  2974. );
  2975. $this->assertTags($result, $expected);
  2976. }
  2977. /**
  2978. * test form->input() with select type inputs.
  2979. *
  2980. * @return void
  2981. */
  2982. public function testInputSelectType() {
  2983. $this->markTestIncomplete('Need to revisit once models work again.');
  2984. $result = $this->Form->input('email', array(
  2985. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2986. );
  2987. $expected = array(
  2988. 'div' => array('class' => 'input select'),
  2989. 'label' => array('for' => 'email'),
  2990. 'Email',
  2991. '/label',
  2992. array('select' => array('name' => 'email', 'id' => 'email')),
  2993. array('option' => array('value' => '')),
  2994. '/option',
  2995. array('option' => array('value' => 'è')),
  2996. 'Firést',
  2997. '/option',
  2998. array('option' => array('value' => 'é')),
  2999. 'Secoènd',
  3000. '/option',
  3001. '/select',
  3002. '/div'
  3003. );
  3004. $this->assertTags($result, $expected);
  3005. $result = $this->Form->input('email', array(
  3006. 'options' => array('First', 'Second'), 'empty' => true)
  3007. );
  3008. $expected = array(
  3009. 'div' => array('class' => 'input select'),
  3010. 'label' => array('for' => 'email'),
  3011. 'Email',
  3012. '/label',
  3013. array('select' => array('name' => 'email', 'id' => 'email')),
  3014. array('option' => array('value' => '')),
  3015. '/option',
  3016. array('option' => array('value' => '0')),
  3017. 'First',
  3018. '/option',
  3019. array('option' => array('value' => '1')),
  3020. 'Second',
  3021. '/option',
  3022. '/select',
  3023. '/div'
  3024. );
  3025. $this->assertTags($result, $expected);
  3026. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3027. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  3028. $result = $this->Form->input('Model.user_id', array('empty' => true));
  3029. $expected = array(
  3030. 'div' => array('class' => 'input select'),
  3031. 'label' => array('for' => 'ModelUserId'),
  3032. 'User',
  3033. '/label',
  3034. 'select' => array('name' => 'Model[user_id]', 'id' => 'ModelUserId'),
  3035. array('option' => array('value' => '')),
  3036. '/option',
  3037. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3038. 'good',
  3039. '/option',
  3040. array('option' => array('value' => 'other')),
  3041. 'bad',
  3042. '/option',
  3043. '/select',
  3044. '/div'
  3045. );
  3046. $this->assertTags($result, $expected);
  3047. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3048. $this->Form->request->data = array('Thing' => array('user_id' => null));
  3049. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  3050. $expected = array(
  3051. 'div' => array('class' => 'input select'),
  3052. 'label' => array('for' => 'ThingUserId'),
  3053. 'User',
  3054. '/label',
  3055. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  3056. array('option' => array('value' => '')),
  3057. 'Some Empty',
  3058. '/option',
  3059. array('option' => array('value' => 'value')),
  3060. 'good',
  3061. '/option',
  3062. array('option' => array('value' => 'other')),
  3063. 'bad',
  3064. '/option',
  3065. '/select',
  3066. '/div'
  3067. );
  3068. $this->assertTags($result, $expected);
  3069. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3070. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  3071. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  3072. $expected = array(
  3073. 'div' => array('class' => 'input select'),
  3074. 'label' => array('for' => 'ThingUserId'),
  3075. 'User',
  3076. '/label',
  3077. 'select' => array('name' => 'Thing[user_id]', 'id' => 'ThingUserId'),
  3078. array('option' => array('value' => '')),
  3079. 'Some Empty',
  3080. '/option',
  3081. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3082. 'good',
  3083. '/option',
  3084. array('option' => array('value' => 'other')),
  3085. 'bad',
  3086. '/option',
  3087. '/select',
  3088. '/div'
  3089. );
  3090. $this->assertTags($result, $expected);
  3091. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3092. $this->Form->request->data = array('User' => array('User' => array('value')));
  3093. $result = $this->Form->input('User.User', array('empty' => true));
  3094. $expected = array(
  3095. 'div' => array('class' => 'input select'),
  3096. 'label' => array('for' => 'UserUser'),
  3097. 'User',
  3098. '/label',
  3099. 'input' => array('type' => 'hidden', 'name' => 'User[User]', 'value' => '', 'id' => 'UserUser_'),
  3100. 'select' => array('name' => 'User[User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  3101. array('option' => array('value' => '')),
  3102. '/option',
  3103. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3104. 'good',
  3105. '/option',
  3106. array('option' => array('value' => 'other')),
  3107. 'bad',
  3108. '/option',
  3109. '/select',
  3110. '/div'
  3111. );
  3112. $this->assertTags($result, $expected);
  3113. $this->Form->data = array();
  3114. $result = $this->Form->input('Publisher.id', array(
  3115. 'label' => 'Publisher',
  3116. 'type' => 'select',
  3117. 'multiple' => 'checkbox',
  3118. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  3119. ));
  3120. $expected = array(
  3121. array('div' => array('class' => 'input select')),
  3122. array('label' => array('for' => 'PublisherId')),
  3123. 'Publisher',
  3124. '/label',
  3125. 'input' => array('type' => 'hidden', 'name' => 'Publisher[id]', 'value' => '', 'id' => 'PublisherId'),
  3126. array('div' => array('class' => 'checkbox')),
  3127. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  3128. array('label' => array('for' => 'PublisherIdValue1')),
  3129. 'Label 1',
  3130. '/label',
  3131. '/div',
  3132. array('div' => array('class' => 'checkbox')),
  3133. array('input' => array('type' => 'checkbox', 'name' => 'Publisher[id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  3134. array('label' => array('for' => 'PublisherIdValue2')),
  3135. 'Label 2',
  3136. '/label',
  3137. '/div',
  3138. '/div'
  3139. );
  3140. $this->assertTags($result, $expected);
  3141. }
  3142. /**
  3143. * test that input() and a non standard primary key makes a hidden input by default.
  3144. *
  3145. * @return void
  3146. */
  3147. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  3148. $this->markTestIncomplete('Need to revisit once models work again.');
  3149. $this->Form->create('User');
  3150. $this->Form->fieldset = array(
  3151. 'User' => array(
  3152. 'fields' => array(
  3153. 'model_id' => array('type' => 'integer')
  3154. ),
  3155. 'validates' => array(),
  3156. 'key' => 'model_id'
  3157. )
  3158. );
  3159. $result = $this->Form->input('model_id');
  3160. $expected = array(
  3161. 'input' => array('type' => 'hidden', 'name' => 'User[model_id]', 'id' => 'UserModelId'),
  3162. );
  3163. $this->assertTags($result, $expected);
  3164. }
  3165. /**
  3166. * test that overriding the magic select type widget is possible
  3167. *
  3168. * @return void
  3169. */
  3170. public function testInputOverridingMagicSelectType() {
  3171. $this->markTestIncomplete('Need to revisit once models work again.');
  3172. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3173. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  3174. $expected = array(
  3175. 'div' => array('class' => 'input text'),
  3176. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  3177. 'input' => array('name' => 'Model[user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  3178. '/div'
  3179. );
  3180. $this->assertTags($result, $expected);
  3181. //Check that magic types still work for plural/singular vars
  3182. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  3183. $result = $this->Form->input('Model.type');
  3184. $expected = array(
  3185. 'div' => array('class' => 'input select'),
  3186. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  3187. 'select' => array('name' => 'Model[type]', 'id' => 'ModelType'),
  3188. array('option' => array('value' => 'value')), 'good', '/option',
  3189. array('option' => array('value' => 'other')), 'bad', '/option',
  3190. '/select',
  3191. '/div'
  3192. );
  3193. $this->assertTags($result, $expected);
  3194. }
  3195. /**
  3196. * Test that inferred types do not override developer input
  3197. *
  3198. * @return void
  3199. */
  3200. public function testInputMagicTypeDoesNotOverride() {
  3201. $this->markTestIncomplete('Need to revisit once models work again.');
  3202. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3203. $result = $this->Form->input('Model.user', array('type' => 'checkbox'));
  3204. $expected = array(
  3205. 'div' => array('class' => 'input checkbox'),
  3206. array('input' => array(
  3207. 'type' => 'hidden',
  3208. 'name' => 'data[Model][user]',
  3209. 'id' => 'ModelUser_',
  3210. 'value' => 0,
  3211. )),
  3212. array('input' => array(
  3213. 'name' => 'data[Model][user]',
  3214. 'type' => 'checkbox',
  3215. 'id' => 'ModelUser',
  3216. 'value' => 1
  3217. )),
  3218. 'label' => array('for' => 'ModelUser'), 'User', '/label',
  3219. '/div'
  3220. );
  3221. $this->assertTags($result, $expected);
  3222. }
  3223. /**
  3224. * Test that magic input() selects are created for type=number
  3225. *
  3226. * @return void
  3227. */
  3228. public function testInputMagicSelectForTypeNumber() {
  3229. $this->markTestIncomplete('Need to revisit once models work again.');
  3230. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  3231. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  3232. $result = $this->Form->input('ValidateUser.balance');
  3233. $expected = array(
  3234. 'div' => array('class' => 'input select'),
  3235. 'label' => array('for' => 'ValidateUserBalance'),
  3236. 'Balance',
  3237. '/label',
  3238. 'select' => array('name' => 'ValidateUser[balance]', 'id' => 'ValidateUserBalance'),
  3239. array('option' => array('value' => '0')),
  3240. 'nothing',
  3241. '/option',
  3242. array('option' => array('value' => '1', 'selected' => 'selected')),
  3243. 'some',
  3244. '/option',
  3245. array('option' => array('value' => '100')),
  3246. 'a lot',
  3247. '/option',
  3248. '/select',
  3249. '/div'
  3250. );
  3251. $this->assertTags($result, $expected);
  3252. }
  3253. /**
  3254. * Test that magic input() selects can easily be converted into radio types without error.
  3255. *
  3256. * @return void
  3257. */
  3258. public function testInputMagicSelectChangeToRadio() {
  3259. $this->markTestIncomplete('Need to revisit once models work again.');
  3260. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  3261. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  3262. $this->assertRegExp('/input type="radio"/', $result);
  3263. }
  3264. /**
  3265. * fields with the same name as the model should work.
  3266. *
  3267. * @return void
  3268. */
  3269. public function testInputWithMatchingFieldAndModelName() {
  3270. $this->markTestIncomplete('Need to revisit once models work again.');
  3271. $this->Form->create('User');
  3272. $this->Form->fieldset = array(
  3273. 'User' => array(
  3274. 'fields' => array(
  3275. 'User' => array('type' => 'text')
  3276. ),
  3277. 'validates' => array(),
  3278. 'key' => 'id'
  3279. )
  3280. );
  3281. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  3282. $result = $this->Form->input('User', array('type' => 'text'));
  3283. $expected = array(
  3284. 'div' => array('class' => 'input text'),
  3285. 'label' => array('for' => 'UserUser'), 'User', '/label',
  3286. 'input' => array('name' => 'User[User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  3287. '/div'
  3288. );
  3289. $this->assertTags($result, $expected);
  3290. }
  3291. /**
  3292. * testFormInputs method
  3293. *
  3294. * test correct results from form::inputs().
  3295. *
  3296. * @return void
  3297. */
  3298. public function testFormInputs() {
  3299. $this->markTestIncomplete('Need to revisit once models work again.');
  3300. $this->Form->create('Cake\Test\TestCase\View\Helper\Contact');
  3301. $result = $this->Form->inputs('The Legend');
  3302. $expected = array(
  3303. '<fieldset',
  3304. '<legend',
  3305. 'The Legend',
  3306. '/legend',
  3307. '*/fieldset',
  3308. );
  3309. $this->assertTags($result, $expected);
  3310. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3311. $expected = array(
  3312. 'fieldset' => array('class' => 'classy-stuff'),
  3313. '<legend',
  3314. 'Field of Dreams',
  3315. '/legend',
  3316. '*/fieldset'
  3317. );
  3318. $this->assertTags($result, $expected);
  3319. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  3320. $this->assertTags($result, $expected);
  3321. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  3322. $this->assertTags($result, $expected);
  3323. $this->Form->create('Contact');
  3324. $this->Form->request['prefix'] = 'admin';
  3325. $this->Form->request['action'] = 'admin_edit';
  3326. $result = $this->Form->inputs();
  3327. $expected = array(
  3328. '<fieldset',
  3329. '<legend',
  3330. 'Edit Contact',
  3331. '/legend',
  3332. '*/fieldset',
  3333. );
  3334. $this->assertTags($result, $expected);
  3335. $this->Form->create('Contact');
  3336. $result = $this->Form->inputs(false);
  3337. $expected = array(
  3338. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3339. array('div' => array('class' => 'input text')),
  3340. '*/div',
  3341. array('div' => array('class' => 'input email')),
  3342. '*/div',
  3343. array('div' => array('class' => 'input tel')),
  3344. '*/div',
  3345. array('div' => array('class' => 'input password')),
  3346. '*/div',
  3347. array('div' => array('class' => 'input date')),
  3348. '*/div',
  3349. array('div' => array('class' => 'input date')),
  3350. '*/div',
  3351. array('div' => array('class' => 'input datetime')),
  3352. '*/div',
  3353. array('div' => array('class' => 'input number')),
  3354. '*/div',
  3355. array('div' => array('class' => 'input select')),
  3356. '*/div',
  3357. );
  3358. $this->assertTags($result, $expected);
  3359. $this->Form->create('Contact');
  3360. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  3361. $expected = array(
  3362. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3363. array('div' => array('class' => 'input text')),
  3364. '*/div',
  3365. array('div' => array('class' => 'input email')),
  3366. '*/div',
  3367. array('div' => array('class' => 'input tel')),
  3368. '*/div',
  3369. array('div' => array('class' => 'input password')),
  3370. '*/div',
  3371. array('div' => array('class' => 'input date')),
  3372. '*/div',
  3373. array('div' => array('class' => 'input date')),
  3374. '*/div',
  3375. array('div' => array('class' => 'input datetime')),
  3376. '*/div',
  3377. array('div' => array('class' => 'input number')),
  3378. '*/div',
  3379. array('div' => array('class' => 'input select')),
  3380. '*/div',
  3381. );
  3382. $this->assertTags($result, $expected);
  3383. $this->Form->create('Contact');
  3384. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  3385. $this->assertTags($result, $expected);
  3386. $this->Form->create('Contact');
  3387. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  3388. $expected = array(
  3389. 'fieldset' => array(),
  3390. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3391. array('div' => array('class' => 'input text')),
  3392. '*/div',
  3393. array('div' => array('class' => 'input email')),
  3394. '*/div',
  3395. array('div' => array('class' => 'input tel')),
  3396. '*/div',
  3397. array('div' => array('class' => 'input password')),
  3398. '*/div',
  3399. array('div' => array('class' => 'input date')),
  3400. '*/div',
  3401. array('div' => array('class' => 'input date')),
  3402. '*/div',
  3403. array('div' => array('class' => 'input datetime')),
  3404. '*/div',
  3405. array('div' => array('class' => 'input number')),
  3406. '*/div',
  3407. array('div' => array('class' => 'input select')),
  3408. '*/div',
  3409. '/fieldset'
  3410. );
  3411. $this->assertTags($result, $expected);
  3412. $this->Form->create('Contact');
  3413. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  3414. $expected = array(
  3415. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3416. array('div' => array('class' => 'input text')),
  3417. '*/div',
  3418. array('div' => array('class' => 'input email')),
  3419. '*/div',
  3420. array('div' => array('class' => 'input tel')),
  3421. '*/div',
  3422. array('div' => array('class' => 'input password')),
  3423. '*/div',
  3424. array('div' => array('class' => 'input date')),
  3425. '*/div',
  3426. array('div' => array('class' => 'input date')),
  3427. '*/div',
  3428. array('div' => array('class' => 'input datetime')),
  3429. '*/div',
  3430. array('div' => array('class' => 'input number')),
  3431. '*/div',
  3432. array('div' => array('class' => 'input select')),
  3433. '*/div',
  3434. );
  3435. $this->assertTags($result, $expected);
  3436. $this->Form->create('Contact');
  3437. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  3438. $this->assertTags($result, $expected);
  3439. $this->Form->create('Contact');
  3440. $result = $this->Form->inputs('Hello');
  3441. $expected = array(
  3442. 'fieldset' => array(),
  3443. 'legend' => array(),
  3444. 'Hello',
  3445. '/legend',
  3446. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3447. array('div' => array('class' => 'input text')),
  3448. '*/div',
  3449. array('div' => array('class' => 'input email')),
  3450. '*/div',
  3451. array('div' => array('class' => 'input tel')),
  3452. '*/div',
  3453. array('div' => array('class' => 'input password')),
  3454. '*/div',
  3455. array('div' => array('class' => 'input date')),
  3456. '*/div',
  3457. array('div' => array('class' => 'input date')),
  3458. '*/div',
  3459. array('div' => array('class' => 'input datetime')),
  3460. '*/div',
  3461. array('div' => array('class' => 'input number')),
  3462. '*/div',
  3463. array('div' => array('class' => 'input select')),
  3464. '*/div',
  3465. '/fieldset'
  3466. );
  3467. $this->assertTags($result, $expected);
  3468. $this->Form->create('Contact');
  3469. $result = $this->Form->inputs(array('legend' => 'Hello'));
  3470. $expected = array(
  3471. 'fieldset' => array(),
  3472. 'legend' => array(),
  3473. 'Hello',
  3474. '/legend',
  3475. 'input' => array('type' => 'hidden', 'name' => 'Contact[id]', 'id' => 'ContactId'),
  3476. array('div' => array('class' => 'input text')),
  3477. '*/div',
  3478. array('div' => array('class' => 'input email')),
  3479. '*/div',
  3480. array('div' => array('class' => 'input tel')),
  3481. '*/div',
  3482. array('div' => array('class' => 'input password')),
  3483. '*/div',
  3484. array('div' => array('class' => 'input date')),
  3485. '*/div',
  3486. array('div' => array('class' => 'input date')),
  3487. '*/div',
  3488. array('div' => array('class' => 'input datetime')),
  3489. '*/div',
  3490. array('div' => array('class' => 'input number')),
  3491. '*/div',
  3492. array('div' => array('class' => 'input select')),
  3493. '*/div',
  3494. '/fieldset'
  3495. );
  3496. $this->assertTags($result, $expected);
  3497. $this->Form->create('Contact');
  3498. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  3499. $this->assertTags($result, $expected);
  3500. $this->Form->end();
  3501. $this->Form->create(false);
  3502. $expected = array(
  3503. 'fieldset' => array(),
  3504. array('div' => array('class' => 'input text')),
  3505. 'label' => array('for' => 'foo'),
  3506. 'Foo',
  3507. '/label',
  3508. 'input' => array('type' => 'text', 'name' => 'foo', 'id' => 'foo'),
  3509. '*/div',
  3510. '/fieldset'
  3511. );
  3512. $result = $this->Form->inputs(
  3513. array('foo' => array('type' => 'text')),
  3514. array(),
  3515. array('legend' => false)
  3516. );
  3517. $this->assertTags($result, $expected);
  3518. }
  3519. /**
  3520. * Tests inputs() works with plugin models
  3521. *
  3522. * @return void
  3523. */
  3524. public function testInputsPluginModel() {
  3525. $this->markTestIncomplete('Need to revisit once models work again.');
  3526. $this->loadFixtures('Post');
  3527. Plugin::load('TestPlugin');
  3528. $this->Form->request['models'] = array(
  3529. 'TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost')
  3530. );
  3531. $this->Form->create('TestPlugin.TestPluginPost');
  3532. $result = $this->Form->inputs();
  3533. $this->assertContains('TestPluginPost[id]', $result);
  3534. $this->assertContains('TestPluginPost[author_id]', $result);
  3535. $this->assertContains('TestPluginPost[title]', $result);
  3536. $this->assertEquals('TestPluginPost', $this->Form->model());
  3537. }
  3538. /**
  3539. * testSelectAsCheckbox method
  3540. *
  3541. * test multi-select widget with checkbox formatting.
  3542. *
  3543. * @return void
  3544. */
  3545. public function testSelectAsCheckbox() {
  3546. $this->markTestIncomplete('Need to revisit once models work again.');
  3547. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  3548. $expected = array(
  3549. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3550. array('div' => array('class' => 'checkbox')),
  3551. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  3552. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  3553. 'first',
  3554. '/label',
  3555. '/div',
  3556. array('div' => array('class' => 'checkbox')),
  3557. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  3558. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  3559. 'second',
  3560. '/label',
  3561. '/div',
  3562. array('div' => array('class' => 'checkbox')),
  3563. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3564. array('label' => array('for' => 'ModelMultiField2')),
  3565. 'third',
  3566. '/label',
  3567. '/div',
  3568. );
  3569. $this->assertTags($result, $expected);
  3570. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  3571. $expected = array(
  3572. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3573. array('div' => array('class' => 'checkbox')),
  3574. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/22')),
  3575. array('label' => array('for' => 'ModelMultiField1/22')),
  3576. 'half',
  3577. '/label',
  3578. '/div',
  3579. );
  3580. $this->assertTags($result, $expected);
  3581. }
  3582. /**
  3583. * testLabel method
  3584. *
  3585. * test label generation.
  3586. *
  3587. * @return void
  3588. */
  3589. public function testLabel() {
  3590. $result = $this->Form->label('Person.name');
  3591. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3592. $result = $this->Form->label('Person.name');
  3593. $this->assertTags($result, array('label' => array('for' => 'person-name'), 'Name', '/label'));
  3594. $result = $this->Form->label('Person.first_name');
  3595. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'First Name', '/label'));
  3596. $result = $this->Form->label('Person.first_name', 'Your first name');
  3597. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), 'Your first name', '/label'));
  3598. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  3599. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class'), 'Your first name', '/label'));
  3600. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  3601. $this->assertTags($result, array('label' => array('for' => 'person-first-name', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  3602. $result = $this->Form->label('Person.first_name', '');
  3603. $this->assertTags($result, array('label' => array('for' => 'person-first-name'), '/label'));
  3604. $result = $this->Form->label('Person.2.name', '');
  3605. $this->assertTags($result, array('label' => array('for' => 'person-2-name'), '/label'));
  3606. }
  3607. /**
  3608. * testTextbox method
  3609. *
  3610. * test textbox element generation
  3611. *
  3612. * @return void
  3613. */
  3614. public function testTextbox() {
  3615. $result = $this->Form->text('Model.field');
  3616. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]')));
  3617. $result = $this->Form->text('Model.field', array('type' => 'password'));
  3618. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Model[field]')));
  3619. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  3620. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'Model[field]', 'id' => 'theID')));
  3621. }
  3622. /**
  3623. * Test that text() hooks up with request data and error fields.
  3624. *
  3625. * @return void
  3626. */
  3627. public function testTextBoxDataAndError() {
  3628. $this->article['errors'] = [
  3629. 'Contact' => ['text' => 'wrong']
  3630. ];
  3631. $this->Form->create($this->article);
  3632. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  3633. $result = $this->Form->text('Model.text');
  3634. $expected = [
  3635. 'input' => [
  3636. 'type' => 'text',
  3637. 'name' => 'Model[text]',
  3638. 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values',
  3639. ]
  3640. ];
  3641. $this->assertTags($result, $expected);
  3642. $this->Form->request->data['Contact']['text'] = 'test';
  3643. $result = $this->Form->text('Contact.text', ['id' => 'theID']);
  3644. $expected = [
  3645. 'input' => [
  3646. 'type' => 'text',
  3647. 'name' => 'Contact[text]',
  3648. 'value' => 'test',
  3649. 'id' => 'theID',
  3650. 'class' => 'form-error'
  3651. ]
  3652. ];
  3653. $this->assertTags($result, $expected);
  3654. }
  3655. /**
  3656. * testDefaultValue method
  3657. *
  3658. * Test default value setting
  3659. *
  3660. * @return void
  3661. */
  3662. public function testTextDefaultValue() {
  3663. $this->Form->request->data['Model']['field'] = 'test';
  3664. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3665. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'test']];
  3666. $this->assertTags($result, $expected);
  3667. unset($this->Form->request->data['Model']['field']);
  3668. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  3669. $expected = ['input' => ['type' => 'text', 'name' => 'Model[field]', 'value' => 'default value']];
  3670. $this->assertTags($result, $expected);
  3671. }
  3672. /**
  3673. * testCheckboxDefaultValue method
  3674. *
  3675. * Test default value setting on checkbox() method
  3676. *
  3677. * @return void
  3678. */
  3679. public function testCheckboxDefaultValue() {
  3680. $this->markTestIncomplete('Need to revisit once models work again.');
  3681. $this->Form->request->data['Model']['field'] = false;
  3682. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3683. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3684. unset($this->Form->request->data['Model']['field']);
  3685. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  3686. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3687. $this->Form->request->data['Model']['field'] = true;
  3688. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3689. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  3690. unset($this->Form->request->data['Model']['field']);
  3691. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  3692. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1', 'id' => 'ModelField')));
  3693. }
  3694. /**
  3695. * testError method
  3696. *
  3697. * Test field error generation
  3698. *
  3699. * @return void
  3700. */
  3701. public function testError() {
  3702. $this->markTestIncomplete('Need to revisit once models work again.');
  3703. $Contact->validationErrors['field'] = array(1);
  3704. $result = $this->Form->error('Contact.field');
  3705. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  3706. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3707. $this->assertEquals('Error in field Field', $result);
  3708. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3709. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  3710. $this->assertEquals('This field contains invalid input', $result);
  3711. $Contact->validationErrors['field'] = array("This field contains invalid input");
  3712. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  3713. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  3714. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  3715. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  3716. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  3717. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3718. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  3719. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  3720. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  3721. $this->assertEquals('<strong>Badness!</strong>', $result);
  3722. $Contact->validationErrors['field'] = array("email");
  3723. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  3724. $expected = array(
  3725. 'div' => array('class' => 'field-error'),
  3726. 'No good!',
  3727. '/div'
  3728. );
  3729. $this->assertTags($result, $expected);
  3730. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  3731. $result = $this->Form->error('Contact.field', array(
  3732. 'notEmpty' => 'Cannot be empty',
  3733. 'email' => 'No good!'
  3734. ));
  3735. $expected = array(
  3736. 'div' => array('class' => 'error-message'),
  3737. 'ul' => array(),
  3738. '<li', 'Cannot be empty', '/li',
  3739. '<li', 'No good!', '/li',
  3740. '<li', 'Something else', '/li',
  3741. '/ul',
  3742. '/div'
  3743. );
  3744. $this->assertTags($result, $expected);
  3745. // Testing error messages list options
  3746. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  3747. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  3748. $expected = array(
  3749. 'div' => array('class' => 'error-message'),
  3750. 'ol' => array(),
  3751. '<li', 'notEmpty', '/li',
  3752. '<li', 'email', '/li',
  3753. '/ol',
  3754. '/div'
  3755. );
  3756. $this->assertTags($result, $expected);
  3757. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  3758. $expected = array(
  3759. 'div' => array('class' => 'error-message'),
  3760. 'ol' => array(),
  3761. '<li', 'notEmpty', '/li',
  3762. '<li', 'email', '/li',
  3763. '/ol',
  3764. '/div'
  3765. );
  3766. $this->assertTags($result, $expected);
  3767. $result = $this->Form->error('Contact.field', null, array(
  3768. 'listOptions' => array(
  3769. 'class' => 'ul-class',
  3770. 'itemOptions' => array(
  3771. 'class' => 'li-class'
  3772. )
  3773. )
  3774. ));
  3775. $expected = array(
  3776. 'div' => array('class' => 'error-message'),
  3777. 'ul' => array('class' => 'ul-class'),
  3778. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  3779. array('li' => array('class' => 'li-class')), 'email', '/li',
  3780. '/ul',
  3781. '/div'
  3782. );
  3783. $this->assertTags($result, $expected);
  3784. }
  3785. /**
  3786. * test error options when using form->input();
  3787. *
  3788. * @return void
  3789. */
  3790. public function testInputErrorEscape() {
  3791. $this->markTestIncomplete('Need to revisit once models work again.');
  3792. $this->Form->create('ValidateProfile');
  3793. $ValidateProfile->validationErrors['city'] = array('required<br>');
  3794. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => true))));
  3795. $this->assertRegExp('/required&lt;br&gt;/', $result);
  3796. $result = $this->Form->input('city', array('error' => array('attributes' => array('escape' => false))));
  3797. $this->assertRegExp('/required<br>/', $result);
  3798. }
  3799. /**
  3800. * testPassword method
  3801. *
  3802. * Test password element generation
  3803. *
  3804. * @return void
  3805. */
  3806. public function testPassword() {
  3807. $this->article['errors'] = [
  3808. 'Contact' => [
  3809. 'passwd' => 1
  3810. ]
  3811. ];
  3812. $this->Form->create($this->article);
  3813. $result = $this->Form->password('Contact.field');
  3814. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[field]')));
  3815. $this->Form->request->data['Contact']['passwd'] = 'test';
  3816. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  3817. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'Contact[passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  3818. }
  3819. /**
  3820. * testRadio method
  3821. *
  3822. * Test radio element set generation
  3823. *
  3824. * @return void
  3825. */
  3826. public function testRadio() {
  3827. $result = $this->Form->radio('Model.field', array('option A'));
  3828. $expected = array(
  3829. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3830. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3831. 'label' => array('for' => 'model-field-0'),
  3832. 'option A',
  3833. '/label'
  3834. );
  3835. $this->assertTags($result, $expected);
  3836. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  3837. $expected = array(
  3838. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => ''),
  3839. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '0', 'id' => 'model-field-0')),
  3840. array('label' => array('for' => 'model-field-0')),
  3841. 'option A',
  3842. '/label',
  3843. array('input' => array('type' => 'radio', 'name' => 'Model[field]', 'value' => '1', 'id' => 'model-field-1')),
  3844. array('label' => array('for' => 'model-field-1')),
  3845. 'option B',
  3846. '/label',
  3847. );
  3848. $this->assertTags($result, $expected);
  3849. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3850. $expected = array(
  3851. 'input' => array('type' => 'hidden', 'name' => 'Employee[gender]', 'value' => ''),
  3852. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'male', 'id' => 'employee-gender-male')),
  3853. array('label' => array('for' => 'employee-gender-male')),
  3854. 'Male',
  3855. '/label',
  3856. array('input' => array('type' => 'radio', 'name' => 'Employee[gender]', 'value' => 'female', 'id' => 'employee-gender-female')),
  3857. array('label' => array('for' => 'employee-gender-female')),
  3858. 'Female',
  3859. '/label',
  3860. );
  3861. $this->assertTags($result, $expected);
  3862. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'Model[custom]'));
  3863. $expected = array(
  3864. array('input' => array('type' => 'hidden', 'name' => 'Model[custom]', 'value' => '')),
  3865. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '0', 'id' => 'model-custom-0')),
  3866. array('label' => array('for' => 'model-custom-0')),
  3867. 'option A',
  3868. '/label',
  3869. array('input' => array('type' => 'radio', 'name' => 'Model[custom]', 'value' => '1', 'id' => 'model-custom-1')),
  3870. array('label' => array('for' => 'model-custom-1')),
  3871. 'option B',
  3872. '/label',
  3873. );
  3874. $this->assertTags($result, $expected);
  3875. }
  3876. /**
  3877. * test disabling the hidden input for radio buttons
  3878. *
  3879. * @return void
  3880. */
  3881. public function testRadioHiddenInputDisabling() {
  3882. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3883. $expected = array(
  3884. 'input' => array('type' => 'radio', 'name' => 'Model[1][field]', 'value' => '0', 'id' => 'model-1-field-0'),
  3885. 'label' => array('for' => 'model-1-field-0'),
  3886. 'option A',
  3887. '/label'
  3888. );
  3889. $this->assertTags($result, $expected);
  3890. }
  3891. /**
  3892. * testDomIdSuffix method
  3893. *
  3894. * @return void
  3895. */
  3896. public function testDomIdSuffix() {
  3897. $this->markTestIncomplete('Need to revisit once models work again.');
  3898. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs');
  3899. $this->assertEquals('1StringWith1$-dollarSigns', $result);
  3900. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>');
  3901. $this->assertEquals('AbcX=FooY=Bar', $result);
  3902. $result = $this->Form->domIdSuffix('1 string with 1$-dollar signs', 'xhtml');
  3903. $this->assertEquals('1StringWith1-dollarSigns', $result);
  3904. $result = $this->Form->domIdSuffix('<abc x="foo" y=\'bar\'>', 'xhtml');
  3905. $this->assertEquals('AbcXFooYBar', $result);
  3906. }
  3907. /**
  3908. * testDomIdSuffixCollisionResolvement()
  3909. *
  3910. * @return void
  3911. */
  3912. public function testDomIdSuffixCollisionResolvement() {
  3913. $this->markTestIncomplete('Need to revisit once models work again.');
  3914. $result = $this->Form->domIdSuffix('a>b');
  3915. $this->assertEquals('AB', $result);
  3916. $result = $this->Form->domIdSuffix('a<b');
  3917. $this->assertEquals('AB1', $result);
  3918. $result = $this->Form->domIdSuffix('a\'b');
  3919. $this->assertEquals('AB2', $result);
  3920. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3921. $this->assertEquals('1StringWith1-dollar', $result);
  3922. $result = $this->Form->domIdSuffix('1 string with 1€-dollar', 'xhtml');
  3923. $this->assertEquals('1StringWith1-dollar1', $result);
  3924. $result = $this->Form->domIdSuffix('1 string with 1$-dollar', 'xhtml');
  3925. $this->assertEquals('1StringWith1-dollar2', $result);
  3926. }
  3927. /**
  3928. * testSelect method
  3929. *
  3930. * Test select element generation.
  3931. *
  3932. * @return void
  3933. */
  3934. public function testSelect() {
  3935. $result = $this->Form->select('Model.field', array());
  3936. $expected = array(
  3937. 'select' => array('name' => 'Model[field]'),
  3938. array('option' => array('value' => '')),
  3939. '/option',
  3940. '/select'
  3941. );
  3942. $this->assertTags($result, $expected);
  3943. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3944. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3945. $expected = array(
  3946. 'select' => array('name' => 'Model[field]'),
  3947. array('option' => array('value' => '')),
  3948. '/option',
  3949. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3950. 'good',
  3951. '/option',
  3952. array('option' => array('value' => 'other')),
  3953. 'bad',
  3954. '/option',
  3955. '/select'
  3956. );
  3957. $this->assertTags($result, $expected);
  3958. $this->Form->request->data = array();
  3959. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3960. $expected = array(
  3961. 'select' => array('name' => 'Model[field]'),
  3962. array('option' => array('value' => '')),
  3963. '/option',
  3964. array('option' => array('value' => 'value')),
  3965. 'good',
  3966. '/option',
  3967. array('option' => array('value' => 'other')),
  3968. 'bad',
  3969. '/option',
  3970. '/select'
  3971. );
  3972. $this->assertTags($result, $expected);
  3973. $result = $this->Form->select(
  3974. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3975. array('empty' => false)
  3976. );
  3977. $expected = array(
  3978. 'select' => array('name' => 'Model[field]'),
  3979. array('option' => array('value' => 'first')),
  3980. 'first &quot;html&quot; &lt;chars&gt;',
  3981. '/option',
  3982. array('option' => array('value' => 'second')),
  3983. 'value',
  3984. '/option',
  3985. '/select'
  3986. );
  3987. $this->assertTags($result, $expected);
  3988. $result = $this->Form->select(
  3989. 'Model.field',
  3990. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3991. array('escape' => false, 'empty' => false)
  3992. );
  3993. $expected = array(
  3994. 'select' => array('name' => 'Model[field]'),
  3995. array('option' => array('value' => 'first')),
  3996. 'first "html" <chars>',
  3997. '/option',
  3998. array('option' => array('value' => 'second')),
  3999. 'value',
  4000. '/option',
  4001. '/select'
  4002. );
  4003. $this->assertTags($result, $expected);
  4004. $options = array(
  4005. array('value' => 'first', 'text' => 'First'),
  4006. array('value' => 'first', 'text' => 'Another First'),
  4007. );
  4008. $result = $this->Form->select(
  4009. 'Model.field',
  4010. $options,
  4011. array('escape' => false, 'empty' => false)
  4012. );
  4013. $expected = array(
  4014. 'select' => array('name' => 'Model[field]'),
  4015. array('option' => array('value' => 'first')),
  4016. 'First',
  4017. '/option',
  4018. array('option' => array('value' => 'first')),
  4019. 'Another First',
  4020. '/option',
  4021. '/select'
  4022. );
  4023. $this->assertTags($result, $expected);
  4024. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  4025. $result = $this->Form->select(
  4026. 'Model.contact_id',
  4027. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  4028. array('escape' => false, 'empty' => 'pick something')
  4029. );
  4030. $expected = array(
  4031. 'select' => array('name' => 'Model[contact_id]'),
  4032. array('option' => array('value' => '')), 'pick something', '/option',
  4033. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  4034. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  4035. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  4036. '/select'
  4037. );
  4038. $this->assertTags($result, $expected);
  4039. $this->Form->request->data['Model']['field'] = 0;
  4040. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  4041. $expected = array(
  4042. 'select' => array('name' => 'Model[field]'),
  4043. array('option' => array('value' => '')), '/option',
  4044. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  4045. array('option' => array('value' => '1')), 'Yes', '/option',
  4046. '/select'
  4047. );
  4048. $this->assertTags($result, $expected);
  4049. }
  4050. /**
  4051. * test select() with required and disabled attributes.
  4052. *
  4053. * @return void
  4054. */
  4055. public function testSelectRequired() {
  4056. $this->article['required'] = [
  4057. 'user_id' => true
  4058. ];
  4059. $this->Form->create($this->article);
  4060. $result = $this->Form->select('user_id', array('option A'));
  4061. $expected = array(
  4062. 'select' => array(
  4063. 'name' => 'user_id',
  4064. 'required' => 'required'
  4065. ),
  4066. array('option' => array('value' => '')), '/option',
  4067. array('option' => array('value' => '0')), 'option A', '/option',
  4068. '/select'
  4069. );
  4070. $this->assertTags($result, $expected);
  4071. $result = $this->Form->select('user_id', array('option A'), array('disabled' => true));
  4072. $expected = array(
  4073. 'select' => array(
  4074. 'name' => 'user_id',
  4075. 'disabled' => 'disabled'
  4076. ),
  4077. array('option' => array('value' => '')), '/option',
  4078. array('option' => array('value' => '0')), 'option A', '/option',
  4079. '/select'
  4080. );
  4081. $this->assertTags($result, $expected);
  4082. }
  4083. /**
  4084. * testNestedSelect method
  4085. *
  4086. * test select element generation with optgroups
  4087. *
  4088. * @return void
  4089. */
  4090. public function testNestedSelect() {
  4091. $result = $this->Form->select(
  4092. 'Model.field',
  4093. array(1 => 'One', 2 => 'Two', 'Three' => array(
  4094. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  4095. )), array('empty' => false)
  4096. );
  4097. $expected = array(
  4098. 'select' => array('name' => 'Model[field]'),
  4099. array('option' => array('value' => 1)),
  4100. 'One',
  4101. '/option',
  4102. array('option' => array('value' => 2)),
  4103. 'Two',
  4104. '/option',
  4105. array('optgroup' => array('label' => 'Three')),
  4106. array('option' => array('value' => 3)),
  4107. 'Three',
  4108. '/option',
  4109. array('option' => array('value' => 4)),
  4110. 'Four',
  4111. '/option',
  4112. array('option' => array('value' => 5)),
  4113. 'Five',
  4114. '/option',
  4115. '/optgroup',
  4116. '/select'
  4117. );
  4118. $this->assertTags($result, $expected);
  4119. }
  4120. /**
  4121. * testSelectMultiple method
  4122. *
  4123. * test generation of multiple select elements
  4124. *
  4125. * @return void
  4126. */
  4127. public function testSelectMultiple() {
  4128. $options = array('first', 'second', 'third');
  4129. $result = $this->Form->select(
  4130. 'Model.multi_field',
  4131. $options,
  4132. array('multiple' => true)
  4133. );
  4134. $expected = array(
  4135. 'input' => array(
  4136. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''
  4137. ),
  4138. 'select' => array(
  4139. 'name' => 'Model[multi_field][]',
  4140. 'multiple' => 'multiple'
  4141. ),
  4142. array('option' => array('value' => '0')),
  4143. 'first',
  4144. '/option',
  4145. array('option' => array('value' => '1')),
  4146. 'second',
  4147. '/option',
  4148. array('option' => array('value' => '2')),
  4149. 'third',
  4150. '/option',
  4151. '/select'
  4152. );
  4153. $this->assertTags($result, $expected);
  4154. $result = $this->Form->select(
  4155. 'Model.multi_field',
  4156. $options,
  4157. array('multiple' => 'multiple')
  4158. );
  4159. $this->assertTags($result, $expected);
  4160. }
  4161. /**
  4162. * Test that a checkbox can have 0 for the value and 1 for the hidden input.
  4163. *
  4164. * @return void
  4165. */
  4166. public function testCheckboxZeroValue() {
  4167. $this->markTestIncomplete('Need to revisit once models work again.');
  4168. $result = $this->Form->input('User.get_spam', array(
  4169. 'type' => 'checkbox',
  4170. 'value' => '0',
  4171. 'hiddenField' => '1',
  4172. ));
  4173. $expected = array(
  4174. 'div' => array('class' => 'input checkbox'),
  4175. array('input' => array(
  4176. 'type' => 'hidden', 'name' => 'data[User][get_spam]',
  4177. 'value' => '1', 'id' => 'UserGetSpam_'
  4178. )),
  4179. array('input' => array(
  4180. 'type' => 'checkbox', 'name' => 'data[User][get_spam]',
  4181. 'value' => '0', 'id' => 'UserGetSpam'
  4182. )),
  4183. 'label' => array('for' => 'UserGetSpam'),
  4184. 'Get Spam',
  4185. '/label',
  4186. '/div'
  4187. );
  4188. $this->assertTags($result, $expected);
  4189. }
  4190. /**
  4191. * test generation of habtm select boxes.
  4192. *
  4193. * @return void
  4194. */
  4195. public function testHabtmSelectBox() {
  4196. $this->markTestIncomplete('Need to revisit once models work again.');
  4197. $this->View->viewVars['contactTags'] = array(
  4198. 1 => 'blue',
  4199. 2 => 'red',
  4200. 3 => 'green'
  4201. );
  4202. $this->Form->request->data = array(
  4203. 'Contact' => array(),
  4204. 'ContactTag' => array(
  4205. array(
  4206. 'id' => '1',
  4207. 'name' => 'blue'
  4208. ),
  4209. array(
  4210. 'id' => 3,
  4211. 'name' => 'green'
  4212. )
  4213. )
  4214. );
  4215. $this->Form->create('Contact');
  4216. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4217. $expected = array(
  4218. 'input' => array(
  4219. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4220. ),
  4221. 'select' => array(
  4222. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4223. 'multiple' => 'multiple'
  4224. ),
  4225. array('option' => array('value' => '1', 'selected' => 'selected')),
  4226. 'blue',
  4227. '/option',
  4228. array('option' => array('value' => '2')),
  4229. 'red',
  4230. '/option',
  4231. array('option' => array('value' => '3', 'selected' => 'selected')),
  4232. 'green',
  4233. '/option',
  4234. '/select'
  4235. );
  4236. $this->assertTags($result, $expected);
  4237. // make sure only 50 is selected, and not 50f5c0cf
  4238. $this->View->viewVars['contactTags'] = array(
  4239. '1' => 'blue',
  4240. '50f5c0cf' => 'red',
  4241. '50' => 'green'
  4242. );
  4243. $this->Form->request->data = array(
  4244. 'Contact' => array(),
  4245. 'ContactTag' => array(
  4246. array(
  4247. 'id' => 1,
  4248. 'name' => 'blue'
  4249. ),
  4250. array(
  4251. 'id' => 50,
  4252. 'name' => 'green'
  4253. )
  4254. )
  4255. );
  4256. $this->Form->create('Contact');
  4257. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  4258. $expected = array(
  4259. 'input' => array(
  4260. 'type' => 'hidden', 'name' => 'ContactTag[ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  4261. ),
  4262. 'select' => array(
  4263. 'name' => 'ContactTag[ContactTag][]', 'id' => 'ContactTagContactTag',
  4264. 'multiple' => 'multiple'
  4265. ),
  4266. array('option' => array('value' => '1', 'selected' => 'selected')),
  4267. 'blue',
  4268. '/option',
  4269. array('option' => array('value' => '50f5c0cf')),
  4270. 'red',
  4271. '/option',
  4272. array('option' => array('value' => '50', 'selected' => 'selected')),
  4273. 'green',
  4274. '/option',
  4275. '/select'
  4276. );
  4277. $this->assertTags($result, $expected);
  4278. }
  4279. /**
  4280. * test generation of multi select elements in checkbox format
  4281. *
  4282. * @return void
  4283. */
  4284. public function testSelectMultipleCheckboxes() {
  4285. $this->markTestIncomplete('Need to revisit once models work again.');
  4286. $result = $this->Form->select(
  4287. 'Model.multi_field',
  4288. array('first', 'second', 'third'),
  4289. array('multiple' => 'checkbox')
  4290. );
  4291. $expected = array(
  4292. 'input' => array(
  4293. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4294. ),
  4295. array('div' => array('class' => 'checkbox')),
  4296. array('input' => array(
  4297. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4298. 'value' => '0', 'id' => 'ModelMultiField0'
  4299. )),
  4300. array('label' => array('for' => 'ModelMultiField0')),
  4301. 'first',
  4302. '/label',
  4303. '/div',
  4304. array('div' => array('class' => 'checkbox')),
  4305. array('input' => array(
  4306. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4307. 'value' => '1', 'id' => 'ModelMultiField1'
  4308. )),
  4309. array('label' => array('for' => 'ModelMultiField1')),
  4310. 'second',
  4311. '/label',
  4312. '/div',
  4313. array('div' => array('class' => 'checkbox')),
  4314. array('input' => array(
  4315. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4316. 'value' => '2', 'id' => 'ModelMultiField2'
  4317. )),
  4318. array('label' => array('for' => 'ModelMultiField2')),
  4319. 'third',
  4320. '/label',
  4321. '/div'
  4322. );
  4323. $this->assertTags($result, $expected);
  4324. $result = $this->Form->select(
  4325. 'Model.multi_field',
  4326. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4327. array('multiple' => 'checkbox')
  4328. );
  4329. $expected = array(
  4330. 'input' => array(
  4331. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4332. ),
  4333. array('div' => array('class' => 'checkbox')),
  4334. array('input' => array(
  4335. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4336. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  4337. )),
  4338. array('label' => array('for' => 'ModelMultiFieldA')),
  4339. 'first',
  4340. '/label',
  4341. '/div',
  4342. array('div' => array('class' => 'checkbox')),
  4343. array('input' => array(
  4344. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4345. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  4346. )),
  4347. array('label' => array('for' => 'ModelMultiFieldB')),
  4348. 'second',
  4349. '/label',
  4350. '/div',
  4351. array('div' => array('class' => 'checkbox')),
  4352. array('input' => array(
  4353. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4354. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  4355. )),
  4356. array('label' => array('for' => 'ModelMultiFieldC')),
  4357. 'third',
  4358. '/label',
  4359. '/div'
  4360. );
  4361. $this->assertTags($result, $expected);
  4362. $result = $this->Form->select(
  4363. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  4364. );
  4365. $expected = array(
  4366. 'input' => array(
  4367. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4368. ),
  4369. array('div' => array('class' => 'checkbox')),
  4370. array('input' => array(
  4371. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4372. 'value' => '1', 'id' => 'ModelMultiField1'
  4373. )),
  4374. array('label' => array('for' => 'ModelMultiField1')),
  4375. 'first',
  4376. '/label',
  4377. '/div'
  4378. );
  4379. $this->assertTags($result, $expected);
  4380. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  4381. $result = $this->Form->select(
  4382. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  4383. );
  4384. $expected = array(
  4385. 'input' => array(
  4386. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4387. ),
  4388. array('div' => array('class' => 'checkbox')),
  4389. array('input' => array(
  4390. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4391. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  4392. )),
  4393. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  4394. 'first',
  4395. '/label',
  4396. '/div',
  4397. array('div' => array('class' => 'checkbox')),
  4398. array('input' => array(
  4399. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4400. 'value' => 'Array', 'id' => 'ModelTagsArray'
  4401. )),
  4402. array('label' => array('for' => 'ModelTagsArray')),
  4403. 'Array',
  4404. '/label',
  4405. '/div'
  4406. );
  4407. $this->assertTags($result, $expected);
  4408. $result = $this->Form->select(
  4409. 'Model.multi_field',
  4410. array('a+' => 'first', 'a++' => 'second', 'a+++' => 'third'),
  4411. array('multiple' => 'checkbox')
  4412. );
  4413. $expected = array(
  4414. 'input' => array(
  4415. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4416. ),
  4417. array('div' => array('class' => 'checkbox')),
  4418. array('input' => array(
  4419. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4420. 'value' => 'a+', 'id' => 'ModelMultiFieldA+'
  4421. )),
  4422. array('label' => array('for' => 'ModelMultiFieldA+')),
  4423. 'first',
  4424. '/label',
  4425. '/div',
  4426. array('div' => array('class' => 'checkbox')),
  4427. array('input' => array(
  4428. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4429. 'value' => 'a++', 'id' => 'ModelMultiFieldA++'
  4430. )),
  4431. array('label' => array('for' => 'ModelMultiFieldA++')),
  4432. 'second',
  4433. '/label',
  4434. '/div',
  4435. array('div' => array('class' => 'checkbox')),
  4436. array('input' => array(
  4437. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4438. 'value' => 'a+++', 'id' => 'ModelMultiFieldA+++'
  4439. )),
  4440. array('label' => array('for' => 'ModelMultiFieldA+++')),
  4441. 'third',
  4442. '/label',
  4443. '/div'
  4444. );
  4445. $this->assertTags($result, $expected);
  4446. $result = $this->Form->select(
  4447. 'Model.multi_field',
  4448. array('a>b' => 'first', 'a<b' => 'second', 'a"b' => 'third'),
  4449. array('multiple' => 'checkbox')
  4450. );
  4451. $expected = array(
  4452. 'input' => array(
  4453. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  4454. ),
  4455. array('div' => array('class' => 'checkbox')),
  4456. array('input' => array(
  4457. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4458. 'value' => 'a&gt;b', 'id' => 'ModelMultiFieldAB2'
  4459. )),
  4460. array('label' => array('for' => 'ModelMultiFieldAB2')),
  4461. 'first',
  4462. '/label',
  4463. '/div',
  4464. array('div' => array('class' => 'checkbox')),
  4465. array('input' => array(
  4466. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4467. 'value' => 'a&lt;b', 'id' => 'ModelMultiFieldAB1'
  4468. )),
  4469. array('label' => array('for' => 'ModelMultiFieldAB1')),
  4470. 'second',
  4471. '/label',
  4472. '/div',
  4473. array('div' => array('class' => 'checkbox')),
  4474. array('input' => array(
  4475. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4476. 'value' => 'a&quot;b', 'id' => 'ModelMultiFieldAB'
  4477. )),
  4478. array('label' => array('for' => 'ModelMultiFieldAB')),
  4479. 'third',
  4480. '/label',
  4481. '/div'
  4482. );
  4483. $this->assertTags($result, $expected);
  4484. }
  4485. /**
  4486. * test multiple checkboxes with div styles.
  4487. *
  4488. * @return void
  4489. */
  4490. public function testSelectMultipleCheckboxDiv() {
  4491. $this->markTestIncomplete('Need to revisit once models work again.');
  4492. $result = $this->Form->select(
  4493. 'Model.tags',
  4494. array('first', 'second'),
  4495. array('multiple' => 'checkbox', 'class' => 'my-class')
  4496. );
  4497. $expected = array(
  4498. 'input' => array(
  4499. 'type' => 'hidden', 'name' => 'Model[tags]', 'value' => '', 'id' => 'ModelTags'
  4500. ),
  4501. array('div' => array('class' => 'my-class')),
  4502. array('input' => array(
  4503. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4504. 'value' => '0', 'id' => 'ModelTags0'
  4505. )),
  4506. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4507. '/div',
  4508. array('div' => array('class' => 'my-class')),
  4509. array('input' => array(
  4510. 'type' => 'checkbox', 'name' => 'Model[tags][]',
  4511. 'value' => '1', 'id' => 'ModelTags1'
  4512. )),
  4513. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4514. '/div'
  4515. );
  4516. $this->assertTags($result, $expected);
  4517. $result = $this->Form->input('Model.tags', array(
  4518. 'options' => array('first', 'second'),
  4519. 'multiple' => 'checkbox',
  4520. 'class' => 'my-class',
  4521. 'div' => false,
  4522. 'label' => false
  4523. ));
  4524. $this->assertTags($result, $expected);
  4525. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4526. $result = $this->Form->input('Contact.tags', array(
  4527. 'options' => array('one'),
  4528. 'multiple' => 'checkbox',
  4529. 'label' => false,
  4530. 'div' => false
  4531. ));
  4532. $expected = array(
  4533. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4534. array('div' => array('class' => 'checkbox form-error')),
  4535. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4536. array('label' => array('for' => 'ContactTags0')),
  4537. 'one',
  4538. '/label',
  4539. '/div'
  4540. );
  4541. $this->assertTags($result, $expected);
  4542. $result = $this->Form->input('Contact.tags', array(
  4543. 'options' => array('one'),
  4544. 'multiple' => 'checkbox',
  4545. 'class' => 'mycheckbox',
  4546. 'label' => false,
  4547. 'div' => false
  4548. ));
  4549. $expected = array(
  4550. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'Contact[tags]', 'value' => '', 'id' => 'ContactTags'),
  4551. array('div' => array('class' => 'mycheckbox form-error')),
  4552. array('input' => array('type' => 'checkbox', 'name' => 'Contact[tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4553. array('label' => array('for' => 'ContactTags0')),
  4554. 'one',
  4555. '/label',
  4556. '/div'
  4557. );
  4558. $this->assertTags($result, $expected);
  4559. }
  4560. /**
  4561. * Checks the security hash array generated for multiple-input checkbox elements
  4562. *
  4563. * @return void
  4564. */
  4565. public function testSelectMultipleCheckboxSecurity() {
  4566. $this->markTestIncomplete('Need to revisit once models work again.');
  4567. $this->Form->request->params['_csrfToken'] = 'testKey';
  4568. $this->assertEquals(array(), $this->Form->fields);
  4569. $result = $this->Form->select(
  4570. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4571. array('multiple' => 'checkbox')
  4572. );
  4573. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4574. $result = $this->Form->secure($this->Form->fields);
  4575. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4576. $this->assertRegExp('/"' . $key . '"/', $result);
  4577. }
  4578. /**
  4579. * Multiple select elements should always be secured as they always participate
  4580. * in the POST data.
  4581. *
  4582. * @return void
  4583. */
  4584. public function testSelectMultipleSecureWithNoOptions() {
  4585. $this->Form->request->params['_Token'] = 'testkey';
  4586. $this->assertEquals(array(), $this->Form->fields);
  4587. $this->Form->select(
  4588. 'Model.select',
  4589. array(),
  4590. array('multiple' => true)
  4591. );
  4592. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4593. }
  4594. /**
  4595. * When a select box has no options it should not be added to the fields list
  4596. * as it always fail post validation.
  4597. *
  4598. * @return void
  4599. */
  4600. public function testSelectNoSecureWithNoOptions() {
  4601. $this->markTestIncomplete('Need to revisit once models work again.');
  4602. $this->Form->request->params['_csrfToken'] = 'testkey';
  4603. $this->assertEquals(array(), $this->Form->fields);
  4604. $this->Form->select(
  4605. 'Model.select',
  4606. array()
  4607. );
  4608. $this->assertEquals(array(), $this->Form->fields);
  4609. $this->Form->select(
  4610. 'Model.select',
  4611. array(),
  4612. array('empty' => true)
  4613. );
  4614. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4615. }
  4616. /**
  4617. * testInputMultipleCheckboxes method
  4618. *
  4619. * test input() resulting in multi select elements being generated.
  4620. *
  4621. * @return void
  4622. */
  4623. public function testInputMultipleCheckboxes() {
  4624. $this->markTestIncomplete('Need to revisit once models work again.');
  4625. $result = $this->Form->input('Model.multi_field', array(
  4626. 'options' => array('first', 'second', 'third'),
  4627. 'multiple' => 'checkbox'
  4628. ));
  4629. $expected = array(
  4630. array('div' => array('class' => 'input select')),
  4631. array('label' => array('for' => 'ModelMultiField')),
  4632. 'Multi Field',
  4633. '/label',
  4634. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4635. array('div' => array('class' => 'checkbox')),
  4636. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4637. array('label' => array('for' => 'ModelMultiField0')),
  4638. 'first',
  4639. '/label',
  4640. '/div',
  4641. array('div' => array('class' => 'checkbox')),
  4642. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4643. array('label' => array('for' => 'ModelMultiField1')),
  4644. 'second',
  4645. '/label',
  4646. '/div',
  4647. array('div' => array('class' => 'checkbox')),
  4648. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4649. array('label' => array('for' => 'ModelMultiField2')),
  4650. 'third',
  4651. '/label',
  4652. '/div',
  4653. '/div'
  4654. );
  4655. $this->assertTags($result, $expected);
  4656. $result = $this->Form->input('Model.multi_field', array(
  4657. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4658. 'multiple' => 'checkbox'
  4659. ));
  4660. $expected = array(
  4661. array('div' => array('class' => 'input select')),
  4662. array('label' => array('for' => 'ModelMultiField')),
  4663. 'Multi Field',
  4664. '/label',
  4665. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4666. array('div' => array('class' => 'checkbox')),
  4667. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4668. array('label' => array('for' => 'ModelMultiFieldA')),
  4669. 'first',
  4670. '/label',
  4671. '/div',
  4672. array('div' => array('class' => 'checkbox')),
  4673. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4674. array('label' => array('for' => 'ModelMultiFieldB')),
  4675. 'second',
  4676. '/label',
  4677. '/div',
  4678. array('div' => array('class' => 'checkbox')),
  4679. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4680. array('label' => array('for' => 'ModelMultiFieldC')),
  4681. 'third',
  4682. '/label',
  4683. '/div',
  4684. '/div'
  4685. );
  4686. $this->assertTags($result, $expected);
  4687. $result = $this->Form->input('Model.multi_field', array(
  4688. 'options' => array('1' => 'first'),
  4689. 'multiple' => 'checkbox',
  4690. 'label' => false,
  4691. 'div' => false
  4692. ));
  4693. $expected = array(
  4694. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4695. array('div' => array('class' => 'checkbox')),
  4696. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4697. array('label' => array('for' => 'ModelMultiField1')),
  4698. 'first',
  4699. '/label',
  4700. '/div'
  4701. );
  4702. $this->assertTags($result, $expected);
  4703. $result = $this->Form->input('Model.multi_field', array(
  4704. 'options' => array('2' => 'second'),
  4705. 'multiple' => 'checkbox',
  4706. 'label' => false,
  4707. 'div' => false
  4708. ));
  4709. $expected = array(
  4710. 'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4711. array('div' => array('class' => 'checkbox')),
  4712. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4713. array('label' => array('for' => 'ModelMultiField2')),
  4714. 'second',
  4715. '/label',
  4716. '/div'
  4717. );
  4718. $this->assertTags($result, $expected);
  4719. }
  4720. /**
  4721. * testSelectHiddenFieldOmission method
  4722. *
  4723. * test that select() with 'hiddenField' => false omits the hidden field
  4724. *
  4725. * @return void
  4726. */
  4727. public function testSelectHiddenFieldOmission() {
  4728. $this->markTestIncomplete('Need to revisit once models work again.');
  4729. $result = $this->Form->select('Model.multi_field',
  4730. array('first', 'second'),
  4731. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4732. );
  4733. $expected = array(
  4734. array('div' => array('class' => 'checkbox')),
  4735. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4736. array('label' => array('for' => 'ModelMultiField0')),
  4737. 'first',
  4738. '/label',
  4739. '/div',
  4740. array('div' => array('class' => 'checkbox')),
  4741. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4742. array('label' => array('for' => 'ModelMultiField1')),
  4743. 'second',
  4744. '/label',
  4745. '/div'
  4746. );
  4747. $this->assertTags($result, $expected);
  4748. $result = $this->Form->input('Model.multi_field', array(
  4749. 'options' => array('first', 'second'),
  4750. 'multiple' => 'checkbox',
  4751. 'hiddenField' => false
  4752. ));
  4753. $expected = array(
  4754. array('div' => array('class' => 'input select')),
  4755. array('label' => array('for' => 'ModelMultiField')),
  4756. 'Multi Field',
  4757. '/label',
  4758. array('div' => array('class' => 'checkbox')),
  4759. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4760. array('label' => array('for' => 'ModelMultiField0')),
  4761. 'first',
  4762. '/label',
  4763. '/div',
  4764. array('div' => array('class' => 'checkbox')),
  4765. array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4766. array('label' => array('for' => 'ModelMultiField1')),
  4767. 'second',
  4768. '/label',
  4769. '/div',
  4770. '/div'
  4771. );
  4772. $this->assertTags($result, $expected);
  4773. }
  4774. /**
  4775. * test that select() with multiple = checkbox works with overriding name attribute.
  4776. *
  4777. * @return void
  4778. */
  4779. public function testSelectCheckboxMultipleOverrideName() {
  4780. $this->markTestIncomplete('Need to revisit once models work again.');
  4781. $result = $this->Form->input('category', array(
  4782. 'type' => 'select',
  4783. 'multiple' => 'checkbox',
  4784. 'name' => 'fish',
  4785. 'options' => array('1', '2'),
  4786. 'div' => false,
  4787. 'label' => false,
  4788. ));
  4789. $expected = array(
  4790. 'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'),
  4791. array('div' => array('class' => 'checkbox')),
  4792. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'Category0')),
  4793. array('label' => array('for' => 'Category0')), '1', '/label',
  4794. '/div',
  4795. array('div' => array('class' => 'checkbox')),
  4796. array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'Category1')),
  4797. array('label' => array('for' => 'Category1')), '2', '/label',
  4798. '/div'
  4799. );
  4800. $this->assertTags($result, $expected);
  4801. }
  4802. /**
  4803. * Test that 'id' overrides all the checkbox id's as well.
  4804. *
  4805. * @return void
  4806. */
  4807. public function testSelectCheckboxMultipleId() {
  4808. $this->markTestIncomplete('Need to revisit once models work again.');
  4809. $result = $this->Form->select(
  4810. 'Model.multi_field',
  4811. array('first', 'second', 'third'),
  4812. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4813. );
  4814. $expected = array(
  4815. 'input' => array(
  4816. 'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'CustomId'
  4817. ),
  4818. array('div' => array('class' => 'checkbox')),
  4819. array('input' => array(
  4820. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4821. 'value' => '0', 'id' => 'CustomId0'
  4822. )),
  4823. array('label' => array('for' => 'CustomId0')),
  4824. 'first',
  4825. '/label',
  4826. '/div',
  4827. array('div' => array('class' => 'checkbox')),
  4828. array('input' => array(
  4829. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4830. 'value' => '1', 'id' => 'CustomId1'
  4831. )),
  4832. array('label' => array('for' => 'CustomId1')),
  4833. 'second',
  4834. '/label',
  4835. '/div',
  4836. array('div' => array('class' => 'checkbox')),
  4837. array('input' => array(
  4838. 'type' => 'checkbox', 'name' => 'Model[multi_field][]',
  4839. 'value' => '2', 'id' => 'CustomId2'
  4840. )),
  4841. array('label' => array('for' => 'CustomId2')),
  4842. 'third',
  4843. '/label',
  4844. '/div'
  4845. );
  4846. $this->assertTags($result, $expected);
  4847. }
  4848. /**
  4849. * testCheckbox method
  4850. *
  4851. * Test generation of checkboxes
  4852. *
  4853. * @return void
  4854. */
  4855. public function testCheckbox() {
  4856. $result = $this->Form->checkbox('Model.field');
  4857. $expected = array(
  4858. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4859. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => '1'))
  4860. );
  4861. $this->assertTags($result, $expected);
  4862. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4863. $expected = array(
  4864. 'input' => array('type' => 'hidden', 'name' => 'Model[field]', 'value' => '0'),
  4865. array('input' => array('type' => 'checkbox', 'name' => 'Model[field]', 'value' => 'myvalue', 'id' => 'theID'))
  4866. );
  4867. $this->assertTags($result, $expected);
  4868. }
  4869. /**
  4870. * Test checkbox being checked or having errors.
  4871. *
  4872. * @return void
  4873. */
  4874. public function testCheckboxCheckedAndError() {
  4875. $this->article['errors'] = [
  4876. 'published' => true
  4877. ];
  4878. $this->Form->request->data['published'] = 'myvalue';
  4879. $this->Form->create($this->article);
  4880. $result = $this->Form->checkbox('published', array('id' => 'theID', 'value' => 'myvalue'));
  4881. $expected = array(
  4882. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4883. array('input' => array(
  4884. 'type' => 'checkbox',
  4885. 'name' => 'published',
  4886. 'value' => 'myvalue',
  4887. 'id' => 'theID',
  4888. 'checked' => 'checked',
  4889. 'class' => 'form-error'
  4890. ))
  4891. );
  4892. $this->assertTags($result, $expected);
  4893. $this->Form->request->data['published'] = '';
  4894. $result = $this->Form->checkbox('published');
  4895. $expected = array(
  4896. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'published', 'value' => '0'),
  4897. array('input' => array('type' => 'checkbox', 'name' => 'published', 'value' => '1', 'class' => 'form-error'))
  4898. );
  4899. $this->assertTags($result, $expected);
  4900. }
  4901. /**
  4902. * test checkbox() with a custom name attribute
  4903. *
  4904. * @return void
  4905. */
  4906. public function testCheckboxCustomNameAttribute() {
  4907. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4908. $expected = array(
  4909. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0'),
  4910. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1'))
  4911. );
  4912. $this->assertTags($result, $expected);
  4913. }
  4914. /**
  4915. * Test that the hidden input for checkboxes can be omitted or set to a
  4916. * specific value.
  4917. *
  4918. * @return void
  4919. */
  4920. public function testCheckboxHiddenField() {
  4921. $result = $this->Form->checkbox('UserForm.something', array(
  4922. 'hiddenField' => false
  4923. ));
  4924. $expected = array(
  4925. 'input' => array(
  4926. 'type' => 'checkbox',
  4927. 'name' => 'UserForm[something]',
  4928. 'value' => '1'
  4929. ),
  4930. );
  4931. $this->assertTags($result, $expected);
  4932. $result = $this->Form->checkbox('UserForm.something', array(
  4933. 'value' => 'Y',
  4934. 'hiddenField' => 'N',
  4935. ));
  4936. $expected = array(
  4937. array('input' => array(
  4938. 'type' => 'hidden', 'name' => 'UserForm[something]',
  4939. 'value' => 'N'
  4940. )),
  4941. array('input' => array(
  4942. 'type' => 'checkbox', 'name' => 'UserForm[something]',
  4943. 'value' => 'Y'
  4944. )),
  4945. );
  4946. $this->assertTags($result, $expected);
  4947. }
  4948. /**
  4949. * testDateTime method
  4950. *
  4951. * Test generation of date/time select elements
  4952. *
  4953. * @return void
  4954. */
  4955. public function testDateTime() {
  4956. $this->markTestIncomplete('Need to revisit once models work again.');
  4957. extract($this->dateRegex);
  4958. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4959. $now = strtotime('now');
  4960. $expected = array(
  4961. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  4962. $daysRegex,
  4963. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4964. date('j', $now),
  4965. '/option',
  4966. '*/select',
  4967. '-',
  4968. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  4969. $monthsRegex,
  4970. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4971. date('F', $now),
  4972. '/option',
  4973. '*/select',
  4974. '-',
  4975. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  4976. $yearsRegex,
  4977. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4978. date('Y', $now),
  4979. '/option',
  4980. '*/select',
  4981. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  4982. $hoursRegex,
  4983. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4984. date('g', $now),
  4985. '/option',
  4986. '*/select',
  4987. ':',
  4988. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  4989. $minutesRegex,
  4990. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4991. date('i', $now),
  4992. '/option',
  4993. '*/select',
  4994. ' ',
  4995. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  4996. $meridianRegex,
  4997. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4998. date('a', $now),
  4999. '/option',
  5000. '*/select'
  5001. );
  5002. $this->assertTags($result, $expected);
  5003. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5004. $expected = array(
  5005. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5006. $daysRegex,
  5007. array('option' => array('value' => '')),
  5008. '/option',
  5009. '*/select',
  5010. '-',
  5011. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5012. $monthsRegex,
  5013. array('option' => array('value' => '')),
  5014. '/option',
  5015. '*/select',
  5016. '-',
  5017. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5018. $yearsRegex,
  5019. array('option' => array('value' => '')),
  5020. '/option',
  5021. '*/select',
  5022. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5023. $hoursRegex,
  5024. array('option' => array('value' => '')),
  5025. '/option',
  5026. '*/select',
  5027. ':',
  5028. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5029. $minutesRegex,
  5030. array('option' => array('value' => '')),
  5031. '/option',
  5032. '*/select',
  5033. ' ',
  5034. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5035. $meridianRegex,
  5036. array('option' => array('value' => '')),
  5037. '/option',
  5038. '*/select'
  5039. );
  5040. $this->assertTags($result, $expected);
  5041. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5042. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  5043. $this->assertTags($result, $expected);
  5044. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5045. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  5046. $this->assertTags($result, $expected);
  5047. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5048. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  5049. $expected = array(
  5050. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5051. $daysRegex,
  5052. array('option' => array('value' => '')),
  5053. '/option',
  5054. '*/select',
  5055. '-',
  5056. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5057. $monthsRegex,
  5058. array('option' => array('value' => '')),
  5059. '/option',
  5060. '*/select',
  5061. '-',
  5062. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5063. $yearsRegex,
  5064. array('option' => array('value' => '')),
  5065. '/option',
  5066. '*/select',
  5067. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5068. $hoursRegex,
  5069. array('option' => array('value' => '')),
  5070. '/option',
  5071. '*/select',
  5072. ':',
  5073. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5074. $minutesRegex,
  5075. array('option' => array('value' => '')),
  5076. '/option',
  5077. array('option' => array('value' => '00')),
  5078. '00',
  5079. '/option',
  5080. array('option' => array('value' => '05')),
  5081. '05',
  5082. '/option',
  5083. array('option' => array('value' => '10')),
  5084. '10',
  5085. '/option',
  5086. '*/select',
  5087. ' ',
  5088. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5089. $meridianRegex,
  5090. array('option' => array('value' => '')),
  5091. '/option',
  5092. '*/select'
  5093. );
  5094. $this->assertTags($result, $expected);
  5095. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5096. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5097. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  5098. $this->Form->request->data['Contact']['data'] = null;
  5099. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  5100. $expected = array(
  5101. array('select' => array('name' => 'Contact[date][day]', 'id' => 'ContactDateDay')),
  5102. $daysRegex,
  5103. array('option' => array('value' => '')),
  5104. '/option',
  5105. '*/select',
  5106. '-',
  5107. array('select' => array('name' => 'Contact[date][month]', 'id' => 'ContactDateMonth')),
  5108. $monthsRegex,
  5109. array('option' => array('value' => '')),
  5110. '/option',
  5111. '*/select',
  5112. '-',
  5113. array('select' => array('name' => 'Contact[date][year]', 'id' => 'ContactDateYear')),
  5114. $yearsRegex,
  5115. array('option' => array('value' => '')),
  5116. '/option',
  5117. '*/select',
  5118. array('select' => array('name' => 'Contact[date][hour]', 'id' => 'ContactDateHour')),
  5119. $hoursRegex,
  5120. array('option' => array('value' => '')),
  5121. '/option',
  5122. '*/select',
  5123. ':',
  5124. array('select' => array('name' => 'Contact[date][min]', 'id' => 'ContactDateMin')),
  5125. $minutesRegex,
  5126. array('option' => array('value' => '')),
  5127. '/option',
  5128. '*/select',
  5129. ' ',
  5130. array('select' => array('name' => 'Contact[date][meridian]', 'id' => 'ContactDateMeridian')),
  5131. $meridianRegex,
  5132. array('option' => array('value' => '')),
  5133. '/option',
  5134. '*/select'
  5135. );
  5136. $this->assertTags($result, $expected);
  5137. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  5138. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  5139. $now = strtotime($this->Form->data['Model']['field']);
  5140. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  5141. $expected = array(
  5142. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5143. $daysRegex,
  5144. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5145. date('j', $now),
  5146. '/option',
  5147. '*/select',
  5148. '-',
  5149. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5150. $monthsRegex,
  5151. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5152. date('F', $now),
  5153. '/option',
  5154. '*/select',
  5155. '-',
  5156. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  5157. $yearsRegex,
  5158. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5159. date('Y', $now),
  5160. '/option',
  5161. '*/select',
  5162. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5163. $hoursRegex,
  5164. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5165. date('g', $now),
  5166. '/option',
  5167. '*/select',
  5168. ':',
  5169. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5170. $minutesRegex,
  5171. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  5172. date('i', $now),
  5173. '/option',
  5174. '*/select',
  5175. ' ',
  5176. array('select' => array('name' => 'Model[field][meridian]', 'id' => 'ModelFieldMeridian')),
  5177. $meridianRegex,
  5178. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  5179. date('a', $now),
  5180. '/option',
  5181. '*/select'
  5182. );
  5183. $this->assertTags($result, $expected);
  5184. $selected = strtotime('2008-10-26 12:33:00');
  5185. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  5186. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  5187. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  5188. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  5189. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  5190. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  5191. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  5192. $this->Form->create('Contact');
  5193. $result = $this->Form->input('published');
  5194. $now = strtotime('now');
  5195. $expected = array(
  5196. 'div' => array('class' => 'input date'),
  5197. 'label' => array('for' => 'ContactPublishedMonth'),
  5198. 'Published',
  5199. '/label',
  5200. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5201. $monthsRegex,
  5202. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5203. date('F', $now),
  5204. '/option',
  5205. '*/select',
  5206. '-',
  5207. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5208. $daysRegex,
  5209. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5210. date('j', $now),
  5211. '/option',
  5212. '*/select',
  5213. '-',
  5214. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5215. $yearsRegex,
  5216. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5217. date('Y', $now),
  5218. '/option',
  5219. '*/select',
  5220. '/div'
  5221. );
  5222. $this->assertTags($result, $expected);
  5223. $result = $this->Form->input('published2', array('type' => 'date'));
  5224. $now = strtotime('now');
  5225. $expected = array(
  5226. 'div' => array('class' => 'input date'),
  5227. 'label' => array('for' => 'ContactPublished2Month'),
  5228. 'Published2',
  5229. '/label',
  5230. array('select' => array('name' => 'Contact[published2][month]', 'id' => 'ContactPublished2Month')),
  5231. $monthsRegex,
  5232. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5233. date('F', $now),
  5234. '/option',
  5235. '*/select',
  5236. '-',
  5237. array('select' => array('name' => 'Contact[published2][day]', 'id' => 'ContactPublished2Day')),
  5238. $daysRegex,
  5239. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5240. date('j', $now),
  5241. '/option',
  5242. '*/select',
  5243. '-',
  5244. array('select' => array('name' => 'Contact[published2][year]', 'id' => 'ContactPublished2Year')),
  5245. $yearsRegex,
  5246. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5247. date('Y', $now),
  5248. '/option',
  5249. '*/select',
  5250. '/div'
  5251. );
  5252. $this->assertTags($result, $expected);
  5253. $this->Form->create('Contact');
  5254. $result = $this->Form->input('published', array('monthNames' => false));
  5255. $now = strtotime('now');
  5256. $expected = array(
  5257. 'div' => array('class' => 'input date'),
  5258. 'label' => array('for' => 'ContactPublishedMonth'),
  5259. 'Published',
  5260. '/label',
  5261. array('select' => array('name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth')),
  5262. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5263. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  5264. date('m', $now),
  5265. '/option',
  5266. '*/select',
  5267. '-',
  5268. array('select' => array('name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay')),
  5269. $daysRegex,
  5270. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  5271. date('j', $now),
  5272. '/option',
  5273. '*/select',
  5274. '-',
  5275. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5276. $yearsRegex,
  5277. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  5278. date('Y', $now),
  5279. '/option',
  5280. '*/select',
  5281. '/div'
  5282. );
  5283. $this->assertTags($result, $expected);
  5284. $result = $this->Form->input('published', array('type' => 'time'));
  5285. $now = strtotime('now');
  5286. $expected = array(
  5287. 'div' => array('class' => 'input time'),
  5288. 'label' => array('for' => 'ContactPublishedHour'),
  5289. 'Published',
  5290. '/label',
  5291. array('select' => array('name' => 'Contact[published][hour]', 'id' => 'ContactPublishedHour')),
  5292. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  5293. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  5294. date('g', $now),
  5295. '/option',
  5296. '*/select',
  5297. ':',
  5298. );
  5299. $this->assertTags($result, $expected);
  5300. $result = $this->Form->input('published', array(
  5301. 'timeFormat' => 24,
  5302. 'interval' => 5,
  5303. 'selected' => strtotime('2009-09-03 13:37:00'),
  5304. 'type' => 'datetime'
  5305. ));
  5306. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5307. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  5308. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  5309. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  5310. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  5311. }
  5312. /**
  5313. * Test dateTime with rounding
  5314. *
  5315. * @return void
  5316. */
  5317. public function testDateTimeRounding() {
  5318. $this->markTestIncomplete('Need to revisit once models work again.');
  5319. $this->Form->request->data['Contact'] = array(
  5320. 'date' => array(
  5321. 'day' => '13',
  5322. 'month' => '12',
  5323. 'year' => '2010',
  5324. 'hour' => '04',
  5325. 'min' => '19',
  5326. 'meridian' => 'AM'
  5327. )
  5328. );
  5329. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15));
  5330. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5331. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 15, 'round' => 'up'));
  5332. $this->assertTextContains('<option value="30" selected="selected">30</option>', $result);
  5333. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'round' => 'down'));
  5334. $this->assertTextContains('<option value="15" selected="selected">15</option>', $result);
  5335. }
  5336. /**
  5337. * Test that empty values don't trigger errors.
  5338. *
  5339. * @return void
  5340. */
  5341. public function testDateTimeNoErrorsOnEmptyData() {
  5342. $this->markTestIncomplete('Need to revisit once models work again.');
  5343. $this->Form->request->data['Contact'] = array(
  5344. 'date' => array(
  5345. 'day' => '',
  5346. 'month' => '',
  5347. 'year' => '',
  5348. 'hour' => '',
  5349. 'min' => '',
  5350. 'meridian' => ''
  5351. )
  5352. );
  5353. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  5354. $this->assertNotEmpty($result);
  5355. }
  5356. /**
  5357. * test that datetime() and default values work.
  5358. *
  5359. * @return void
  5360. */
  5361. public function testDatetimeWithDefault() {
  5362. $this->markTestIncomplete('Need to revisit once models work again.');
  5363. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  5364. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5365. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5366. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5367. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  5368. 'default' => '2009-06-01 11:15:30'
  5369. ));
  5370. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  5371. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  5372. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  5373. }
  5374. /**
  5375. * test that bogus non-date time data doesn't cause errors.
  5376. *
  5377. * @return void
  5378. */
  5379. public function testDateTimeWithBogusData() {
  5380. $this->markTestIncomplete('Need to revisit once models work again.');
  5381. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  5382. $this->assertNotRegExp('/selected="selected">\d/', $result);
  5383. }
  5384. /**
  5385. * testDateTime all zeros
  5386. *
  5387. * @return void
  5388. */
  5389. public function testDateTimeAllZeros() {
  5390. $this->markTestIncomplete('Need to revisit once models work again.');
  5391. $result = $this->Form->dateTime('Contact.date',
  5392. 'DMY',
  5393. false,
  5394. array(
  5395. 'empty' => array('day' => '-', 'month' => '-', 'year' => '-'),
  5396. 'value' => '0000-00-00'
  5397. )
  5398. );
  5399. $this->assertRegExp('/<option value="">-<\/option>/', $result);
  5400. $this->assertNotRegExp('/<option value="0" selected="selected">0<\/option>/', $result);
  5401. }
  5402. /**
  5403. * testDateTimeEmptyAsArray
  5404. *
  5405. * @return void
  5406. */
  5407. public function testDateTimeEmptyAsArray() {
  5408. $this->markTestIncomplete('Need to revisit once models work again.');
  5409. $result = $this->Form->dateTime('Contact.date',
  5410. 'DMY',
  5411. '12',
  5412. array(
  5413. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  5414. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  5415. )
  5416. )
  5417. );
  5418. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5419. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5420. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5421. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  5422. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  5423. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  5424. $result = $this->Form->dateTime('Contact.date',
  5425. 'DMY',
  5426. '12',
  5427. array(
  5428. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  5429. )
  5430. );
  5431. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  5432. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  5433. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  5434. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  5435. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  5436. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  5437. }
  5438. /**
  5439. * testFormDateTimeMulti method
  5440. *
  5441. * test multiple datetime element generation
  5442. *
  5443. * @return void
  5444. */
  5445. public function testFormDateTimeMulti() {
  5446. $this->markTestIncomplete('Need to revisit once models work again.');
  5447. extract($this->dateRegex);
  5448. $result = $this->Form->dateTime('Contact.1.updated');
  5449. $expected = array(
  5450. array('select' => array('name' => 'Contact[1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  5451. $daysRegex,
  5452. array('option' => array('value' => '')),
  5453. '/option',
  5454. '*/select',
  5455. '-',
  5456. array('select' => array('name' => 'Contact[1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  5457. $monthsRegex,
  5458. array('option' => array('value' => '')),
  5459. '/option',
  5460. '*/select',
  5461. '-',
  5462. array('select' => array('name' => 'Contact[1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  5463. $yearsRegex,
  5464. array('option' => array('value' => '')),
  5465. '/option',
  5466. '*/select',
  5467. array('select' => array('name' => 'Contact[1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5468. $hoursRegex,
  5469. array('option' => array('value' => '')),
  5470. '/option',
  5471. '*/select',
  5472. ':',
  5473. array('select' => array('name' => 'Contact[1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5474. $minutesRegex,
  5475. array('option' => array('value' => '')),
  5476. '/option',
  5477. '*/select',
  5478. ' ',
  5479. array('select' => array('name' => 'Contact[1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5480. $meridianRegex,
  5481. array('option' => array('value' => '')),
  5482. '/option',
  5483. '*/select'
  5484. );
  5485. $this->assertTags($result, $expected);
  5486. $result = $this->Form->dateTime('Contact.2.updated');
  5487. $expected = array(
  5488. array('select' => array('name' => 'Contact[2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5489. $daysRegex,
  5490. array('option' => array('value' => '')),
  5491. '/option',
  5492. '*/select',
  5493. '-',
  5494. array('select' => array('name' => 'Contact[2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5495. $monthsRegex,
  5496. array('option' => array('value' => '')),
  5497. '/option',
  5498. '*/select',
  5499. '-',
  5500. array('select' => array('name' => 'Contact[2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5501. $yearsRegex,
  5502. array('option' => array('value' => '')),
  5503. '/option',
  5504. '*/select',
  5505. array('select' => array('name' => 'Contact[2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5506. $hoursRegex,
  5507. array('option' => array('value' => '')),
  5508. '/option',
  5509. '*/select',
  5510. ':',
  5511. array('select' => array('name' => 'Contact[2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5512. $minutesRegex,
  5513. array('option' => array('value' => '')),
  5514. '/option',
  5515. '*/select',
  5516. ' ',
  5517. array('select' => array('name' => 'Contact[2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5518. $meridianRegex,
  5519. array('option' => array('value' => '')),
  5520. '/option',
  5521. '*/select'
  5522. );
  5523. $this->assertTags($result, $expected);
  5524. }
  5525. /**
  5526. * When changing the date format, the label should always focus the first select box when
  5527. * clicked.
  5528. *
  5529. * @return void
  5530. */
  5531. public function testDateTimeLabelIdMatchesFirstInput() {
  5532. $this->markTestIncomplete('Need to revisit once models work again.');
  5533. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5534. $this->assertContains('label for="ModelDateMonth"', $result);
  5535. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5536. $this->assertContains('label for="ModelDateDay"', $result);
  5537. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5538. $this->assertContains('label for="ModelDateYear"', $result);
  5539. }
  5540. /**
  5541. * testMonth method
  5542. *
  5543. * @return void
  5544. */
  5545. public function testMonth() {
  5546. $this->markTestIncomplete('Need to revisit once models work again.');
  5547. $result = $this->Form->month('Model.field');
  5548. $expected = array(
  5549. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5550. array('option' => array('value' => '')),
  5551. '/option',
  5552. array('option' => array('value' => '01')),
  5553. date('F', strtotime('2008-01-01 00:00:00')),
  5554. '/option',
  5555. array('option' => array('value' => '02')),
  5556. date('F', strtotime('2008-02-01 00:00:00')),
  5557. '/option',
  5558. '*/select',
  5559. );
  5560. $this->assertTags($result, $expected);
  5561. $result = $this->Form->month('Model.field', array('empty' => true));
  5562. $expected = array(
  5563. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5564. array('option' => array('value' => '')),
  5565. '/option',
  5566. array('option' => array('value' => '01')),
  5567. date('F', strtotime('2008-01-01 00:00:00')),
  5568. '/option',
  5569. array('option' => array('value' => '02')),
  5570. date('F', strtotime('2008-02-01 00:00:00')),
  5571. '/option',
  5572. '*/select',
  5573. );
  5574. $this->assertTags($result, $expected);
  5575. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5576. $expected = array(
  5577. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5578. array('option' => array('value' => '')),
  5579. '/option',
  5580. array('option' => array('value' => '01')),
  5581. '01',
  5582. '/option',
  5583. array('option' => array('value' => '02')),
  5584. '02',
  5585. '/option',
  5586. '*/select',
  5587. );
  5588. $this->assertTags($result, $expected);
  5589. $monthNames = array(
  5590. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5591. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5592. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5593. $expected = array(
  5594. array('select' => array('name' => 'Model[field][month]', 'id' => 'ModelFieldMonth')),
  5595. array('option' => array('value' => '')),
  5596. '/option',
  5597. array('option' => array('value' => '01')),
  5598. 'Jan',
  5599. '/option',
  5600. array('option' => array('value' => '02')),
  5601. 'Feb',
  5602. '/option',
  5603. '*/select',
  5604. );
  5605. $this->assertTags($result, $expected);
  5606. $this->Form->request->data['Project']['release'] = '2050-02-10';
  5607. $result = $this->Form->month('Project.release');
  5608. $expected = array(
  5609. array('select' => array('name' => 'Project[release][month]', 'id' => 'ProjectReleaseMonth')),
  5610. array('option' => array('value' => '')),
  5611. '/option',
  5612. array('option' => array('value' => '01')),
  5613. 'January',
  5614. '/option',
  5615. array('option' => array('value' => '02', 'selected' => 'selected')),
  5616. 'February',
  5617. '/option',
  5618. '*/select',
  5619. );
  5620. $this->assertTags($result, $expected);
  5621. }
  5622. /**
  5623. * testDay method
  5624. *
  5625. * @return void
  5626. */
  5627. public function testDay() {
  5628. $this->markTestIncomplete('Need to revisit once models work again.');
  5629. extract($this->dateRegex);
  5630. $result = $this->Form->day('Model.field', array('value' => false));
  5631. $expected = array(
  5632. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5633. array('option' => array('value' => '')),
  5634. '/option',
  5635. array('option' => array('value' => '01')),
  5636. '1',
  5637. '/option',
  5638. array('option' => array('value' => '02')),
  5639. '2',
  5640. '/option',
  5641. $daysRegex,
  5642. '/select',
  5643. );
  5644. $this->assertTags($result, $expected);
  5645. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5646. $result = $this->Form->day('Model.field');
  5647. $expected = array(
  5648. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5649. array('option' => array('value' => '')),
  5650. '/option',
  5651. array('option' => array('value' => '01')),
  5652. '1',
  5653. '/option',
  5654. array('option' => array('value' => '02')),
  5655. '2',
  5656. '/option',
  5657. $daysRegex,
  5658. array('option' => array('value' => '10', 'selected' => 'selected')),
  5659. '10',
  5660. '/option',
  5661. $daysRegex,
  5662. '/select',
  5663. );
  5664. $this->assertTags($result, $expected);
  5665. $this->Form->request->data['Model']['field'] = '';
  5666. $result = $this->Form->day('Model.field', array('value' => '10'));
  5667. $expected = array(
  5668. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5669. array('option' => array('value' => '')),
  5670. '/option',
  5671. array('option' => array('value' => '01')),
  5672. '1',
  5673. '/option',
  5674. array('option' => array('value' => '02')),
  5675. '2',
  5676. '/option',
  5677. $daysRegex,
  5678. array('option' => array('value' => '10', 'selected' => 'selected')),
  5679. '10',
  5680. '/option',
  5681. $daysRegex,
  5682. '/select',
  5683. );
  5684. $this->assertTags($result, $expected);
  5685. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5686. $result = $this->Form->day('Model.field', array('value' => true));
  5687. $expected = array(
  5688. array('select' => array('name' => 'Model[field][day]', 'id' => 'ModelFieldDay')),
  5689. array('option' => array('value' => '')),
  5690. '/option',
  5691. array('option' => array('value' => '01')),
  5692. '1',
  5693. '/option',
  5694. array('option' => array('value' => '02')),
  5695. '2',
  5696. '/option',
  5697. $daysRegex,
  5698. array('option' => array('value' => '10', 'selected' => 'selected')),
  5699. '10',
  5700. '/option',
  5701. $daysRegex,
  5702. '/select',
  5703. );
  5704. $this->assertTags($result, $expected);
  5705. $this->Form->request->data['Project']['release'] = '2050-10-10';
  5706. $result = $this->Form->day('Project.release');
  5707. $expected = array(
  5708. array('select' => array('name' => 'Project[release][day]', 'id' => 'ProjectReleaseDay')),
  5709. array('option' => array('value' => '')),
  5710. '/option',
  5711. array('option' => array('value' => '01')),
  5712. '1',
  5713. '/option',
  5714. array('option' => array('value' => '02')),
  5715. '2',
  5716. '/option',
  5717. $daysRegex,
  5718. array('option' => array('value' => '10', 'selected' => 'selected')),
  5719. '10',
  5720. '/option',
  5721. $daysRegex,
  5722. '/select',
  5723. );
  5724. $this->assertTags($result, $expected);
  5725. }
  5726. /**
  5727. * testMinute method
  5728. *
  5729. * @return void
  5730. */
  5731. public function testMinute() {
  5732. $this->markTestIncomplete('Need to revisit once models work again.');
  5733. extract($this->dateRegex);
  5734. $result = $this->Form->minute('Model.field');
  5735. $expected = array(
  5736. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5737. array('option' => array('value' => '')),
  5738. '/option',
  5739. array('option' => array('value' => '00')),
  5740. '00',
  5741. '/option',
  5742. array('option' => array('value' => '01')),
  5743. '01',
  5744. '/option',
  5745. array('option' => array('value' => '02')),
  5746. '02',
  5747. '/option',
  5748. $minutesRegex,
  5749. '/select',
  5750. );
  5751. $this->assertTags($result, $expected);
  5752. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5753. $result = $this->Form->minute('Model.field');
  5754. $expected = array(
  5755. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5756. array('option' => array('value' => '')),
  5757. '/option',
  5758. array('option' => array('value' => '00')),
  5759. '00',
  5760. '/option',
  5761. array('option' => array('value' => '01')),
  5762. '01',
  5763. '/option',
  5764. array('option' => array('value' => '02')),
  5765. '02',
  5766. '/option',
  5767. $minutesRegex,
  5768. array('option' => array('value' => '12', 'selected' => 'selected')),
  5769. '12',
  5770. '/option',
  5771. $minutesRegex,
  5772. '/select',
  5773. );
  5774. $this->assertTags($result, $expected);
  5775. $this->Form->request->data['Model']['field'] = '';
  5776. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5777. $expected = array(
  5778. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5779. array('option' => array('value' => '')),
  5780. '/option',
  5781. array('option' => array('value' => '00')),
  5782. '00',
  5783. '/option',
  5784. array('option' => array('value' => '05')),
  5785. '05',
  5786. '/option',
  5787. array('option' => array('value' => '10')),
  5788. '10',
  5789. '/option',
  5790. $minutesRegex,
  5791. '/select',
  5792. );
  5793. $this->assertTags($result, $expected);
  5794. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5795. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5796. $expected = array(
  5797. array('select' => array('name' => 'Model[field][min]', 'id' => 'ModelFieldMin')),
  5798. array('option' => array('value' => '')),
  5799. '/option',
  5800. array('option' => array('value' => '00')),
  5801. '00',
  5802. '/option',
  5803. array('option' => array('value' => '05')),
  5804. '05',
  5805. '/option',
  5806. array('option' => array('value' => '10', 'selected' => 'selected')),
  5807. '10',
  5808. '/option',
  5809. $minutesRegex,
  5810. '/select',
  5811. );
  5812. $this->assertTags($result, $expected);
  5813. }
  5814. /**
  5815. * testHour method
  5816. *
  5817. * @return void
  5818. */
  5819. public function testHour() {
  5820. $this->markTestIncomplete('Need to revisit once models work again.');
  5821. extract($this->dateRegex);
  5822. $result = $this->Form->hour('Model.field', false);
  5823. $expected = array(
  5824. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5825. array('option' => array('value' => '')),
  5826. '/option',
  5827. array('option' => array('value' => '01')),
  5828. '1',
  5829. '/option',
  5830. array('option' => array('value' => '02')),
  5831. '2',
  5832. '/option',
  5833. $hoursRegex,
  5834. '/select',
  5835. );
  5836. $this->assertTags($result, $expected);
  5837. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5838. $result = $this->Form->hour('Model.field', false);
  5839. $expected = array(
  5840. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5841. array('option' => array('value' => '')),
  5842. '/option',
  5843. array('option' => array('value' => '01')),
  5844. '1',
  5845. '/option',
  5846. array('option' => array('value' => '02')),
  5847. '2',
  5848. '/option',
  5849. $hoursRegex,
  5850. array('option' => array('value' => '12', 'selected' => 'selected')),
  5851. '12',
  5852. '/option',
  5853. '/select',
  5854. );
  5855. $this->assertTags($result, $expected);
  5856. $this->Form->request->data['Model']['field'] = '';
  5857. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5858. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5859. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5860. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5861. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5862. $result = $this->Form->hour('Model.field', true);
  5863. $expected = array(
  5864. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5865. array('option' => array('value' => '')),
  5866. '/option',
  5867. array('option' => array('value' => '00', 'selected' => 'selected')),
  5868. '0',
  5869. '/option',
  5870. array('option' => array('value' => '01')),
  5871. '1',
  5872. '/option',
  5873. array('option' => array('value' => '02')),
  5874. '2',
  5875. '/option',
  5876. $hoursRegex,
  5877. '/select',
  5878. );
  5879. $this->assertTags($result, $expected);
  5880. unset($this->Form->request->data['Model']['field']);
  5881. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5882. $thisHour = date('H');
  5883. $optValue = date('G');
  5884. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5885. $this->Form->request->data['Model']['field'] = '2050-10-10 01:12:32';
  5886. $result = $this->Form->hour('Model.field', true);
  5887. $expected = array(
  5888. array('select' => array('name' => 'Model[field][hour]', 'id' => 'ModelFieldHour')),
  5889. array('option' => array('value' => '')),
  5890. '/option',
  5891. array('option' => array('value' => '00')),
  5892. '0',
  5893. '/option',
  5894. array('option' => array('value' => '01', 'selected' => 'selected')),
  5895. '1',
  5896. '/option',
  5897. array('option' => array('value' => '02')),
  5898. '2',
  5899. '/option',
  5900. $hoursRegex,
  5901. '/select',
  5902. );
  5903. $this->assertTags($result, $expected);
  5904. }
  5905. /**
  5906. * testYear method
  5907. *
  5908. * @return void
  5909. */
  5910. public function testYear() {
  5911. $this->markTestIncomplete('Need to revisit once models work again.');
  5912. $result = $this->Form->year('Model.field', 2006, 2007);
  5913. $expected = array(
  5914. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  5915. array('option' => array('value' => '')),
  5916. '/option',
  5917. array('option' => array('value' => '2007')),
  5918. '2007',
  5919. '/option',
  5920. array('option' => array('value' => '2006')),
  5921. '2006',
  5922. '/option',
  5923. '/select',
  5924. );
  5925. $this->assertTags($result, $expected);
  5926. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5927. $expected = array(
  5928. array('select' => array('name' => 'Model[field][year]', 'id' => 'ModelFieldYear')),
  5929. array('option' => array('value' => '')),
  5930. '/option',
  5931. array('option' => array('value' => '2006')),
  5932. '2006',
  5933. '/option',
  5934. array('option' => array('value' => '2007')),
  5935. '2007',
  5936. '/option',
  5937. '/select',
  5938. );
  5939. $this->assertTags($result, $expected);
  5940. $this->request->data['Contact']['published'] = '';
  5941. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5942. $expected = array(
  5943. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5944. array('option' => array('value' => '')),
  5945. '/option',
  5946. array('option' => array('value' => '2007')),
  5947. '2007',
  5948. '/option',
  5949. array('option' => array('value' => '2006')),
  5950. '2006',
  5951. '/option',
  5952. '/select',
  5953. );
  5954. $this->assertTags($result, $expected);
  5955. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5956. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5957. $expected = array(
  5958. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5959. array('option' => array('value' => '2007')),
  5960. '2007',
  5961. '/option',
  5962. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5963. '2006',
  5964. '/option',
  5965. '/select',
  5966. );
  5967. $this->assertTags($result, $expected);
  5968. $this->Form->request->data['Contact']['published'] = '';
  5969. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5970. $expected = array(
  5971. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5972. array('option' => array('value' => '')),
  5973. '/option',
  5974. array('option' => array('value' => '2007')),
  5975. '2007',
  5976. '/option',
  5977. array('option' => array('value' => '2006')),
  5978. '2006',
  5979. '/option',
  5980. '/select',
  5981. );
  5982. $this->assertTags($result, $expected);
  5983. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5984. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5985. $expected = array(
  5986. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  5987. array('option' => array('value' => '2007')),
  5988. '2007',
  5989. '/option',
  5990. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5991. '2006',
  5992. '/option',
  5993. '/select',
  5994. );
  5995. $this->assertTags($result, $expected);
  5996. $this->Form->request->data['Contact']['published'] = '';
  5997. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5998. $expected = array(
  5999. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6000. array('option' => array('value' => '')),
  6001. '/option',
  6002. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6003. '2007',
  6004. '/option',
  6005. array('option' => array('value' => '2006')),
  6006. '2006',
  6007. '/option',
  6008. '/select',
  6009. );
  6010. $this->assertTags($result, $expected);
  6011. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6012. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  6013. $expected = array(
  6014. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6015. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6016. '2007',
  6017. '/option',
  6018. array('option' => array('value' => '2006')),
  6019. '2006',
  6020. '/option',
  6021. '/select',
  6022. );
  6023. $this->assertTags($result, $expected);
  6024. $this->Form->request->data['Contact']['published'] = '';
  6025. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  6026. $expected = array(
  6027. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6028. array('option' => array('value' => '2008')),
  6029. '2008',
  6030. '/option',
  6031. array('option' => array('value' => '2007', 'selected' => 'selected')),
  6032. '2007',
  6033. '/option',
  6034. array('option' => array('value' => '2006')),
  6035. '2006',
  6036. '/option',
  6037. '/select',
  6038. );
  6039. $this->assertTags($result, $expected);
  6040. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  6041. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  6042. $expected = array(
  6043. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6044. array('option' => array('value' => '2008')),
  6045. '2008',
  6046. '/option',
  6047. array('option' => array('value' => '2007')),
  6048. '2007',
  6049. '/option',
  6050. array('option' => array('value' => '2006', 'selected' => 'selected')),
  6051. '2006',
  6052. '/option',
  6053. '/select',
  6054. );
  6055. $this->assertTags($result, $expected);
  6056. $this->Form->request->data = array();
  6057. $this->Form->create('Contact');
  6058. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  6059. $expected = array(
  6060. array('select' => array('name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear')),
  6061. array('option' => array('value' => '2008')),
  6062. '2008',
  6063. '/option',
  6064. array('option' => array('value' => '2007')),
  6065. '2007',
  6066. '/option',
  6067. array('option' => array('value' => '2006')),
  6068. '2006',
  6069. '/option',
  6070. '/select',
  6071. );
  6072. $this->assertTags($result, $expected);
  6073. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  6074. $this->assertContains('Contact[published][year]', $result);
  6075. }
  6076. /**
  6077. * testYearAutoExpandRange method
  6078. *
  6079. * @return void
  6080. */
  6081. public function testYearAutoExpandRange() {
  6082. $this->markTestIncomplete('Need to revisit once models work again.');
  6083. $this->Form->request->data['User']['birthday'] = '1930-10-10';
  6084. $result = $this->Form->year('User.birthday');
  6085. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6086. $result = $matches[1];
  6087. $expected = range(date('Y') + 20, 1930);
  6088. $this->assertEquals($expected, $result);
  6089. $this->Form->request->data['Project']['release'] = '2050-10-10';
  6090. $result = $this->Form->year('Project.release');
  6091. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6092. $result = $matches[1];
  6093. $expected = range(2050, date('Y') - 20);
  6094. $this->assertEquals($expected, $result);
  6095. $this->Form->request->data['Project']['release'] = '1881-10-10';
  6096. $result = $this->Form->year('Project.release', 1890, 1900);
  6097. preg_match_all('/<option value="([\d]+)"/', $result, $matches);
  6098. $result = $matches[1];
  6099. $expected = range(1900, 1881);
  6100. $this->assertEquals($expected, $result);
  6101. }
  6102. /**
  6103. * testInputDate method
  6104. *
  6105. * Test various inputs with type date and different dateFormat values.
  6106. * Failing to provide a dateFormat key should not error.
  6107. * It should simply not pre-select any value then.
  6108. *
  6109. * @return void
  6110. */
  6111. public function testInputDate() {
  6112. $this->markTestIncomplete('Need to revisit once models work again.');
  6113. $this->Form->request->data = array(
  6114. 'User' => array(
  6115. 'month_year' => array('month' => date('m')),
  6116. 'just_year' => array('month' => date('m')),
  6117. 'just_month' => array('year' => date('Y')),
  6118. 'just_day' => array('month' => date('m')),
  6119. )
  6120. );
  6121. $this->Form->create('User');
  6122. $result = $this->Form->input('month_year',
  6123. array(
  6124. 'label' => false,
  6125. 'div' => false,
  6126. 'type' => 'date',
  6127. 'dateFormat' => 'MY',
  6128. 'minYear' => 2006,
  6129. 'maxYear' => 2008
  6130. )
  6131. );
  6132. $this->assertContains('value="' . date('m') . '" selected="selected"', $result);
  6133. $this->assertNotContains('value="2008" selected="selected"', $result);
  6134. $result = $this->Form->input('just_year',
  6135. array(
  6136. 'type' => 'date',
  6137. 'label' => false,
  6138. 'dateFormat' => 'Y',
  6139. 'minYear' => date('Y'),
  6140. 'maxYear' => date('Y', strtotime('+20 years'))
  6141. )
  6142. );
  6143. $this->assertNotContains('value="' . date('Y') . '" selected="selected"', $result);
  6144. $result = $this->Form->input('just_month',
  6145. array(
  6146. 'type' => 'date',
  6147. 'label' => false,
  6148. 'dateFormat' => 'M',
  6149. 'empty' => false,
  6150. )
  6151. );
  6152. $this->assertNotContains('value="' . date('m') . '" selected="selected"', $result);
  6153. $result = $this->Form->input('just_day',
  6154. array(
  6155. 'type' => 'date',
  6156. 'label' => false,
  6157. 'dateFormat' => 'D',
  6158. 'empty' => false,
  6159. )
  6160. );
  6161. $this->assertNotContains('value="' . date('d') . '" selected="selected"', $result);
  6162. }
  6163. /**
  6164. * testInputDateMaxYear method
  6165. *
  6166. * Let's say we want to only allow users born from 2006 to 2008 to register
  6167. * This being the first singup page, we still don't have any data
  6168. *
  6169. * @return void
  6170. */
  6171. public function testInputDateMaxYear() {
  6172. $this->markTestIncomplete('Need to revisit once models work again.');
  6173. $this->Form->request->data = array();
  6174. $this->Form->create('User');
  6175. $result = $this->Form->input('birthday',
  6176. array(
  6177. 'label' => false,
  6178. 'div' => false,
  6179. 'type' => 'date',
  6180. 'dateFormat' => 'DMY',
  6181. 'minYear' => 2006,
  6182. 'maxYear' => 2008
  6183. )
  6184. );
  6185. $this->assertContains('value="2008" selected="selected"', $result);
  6186. }
  6187. /**
  6188. * testTextArea method
  6189. *
  6190. * @return void
  6191. */
  6192. public function testTextArea() {
  6193. $this->Form->request->data = array('field' => 'some test data');
  6194. $result = $this->Form->textarea('field');
  6195. $expected = array(
  6196. 'textarea' => array('name' => 'field'),
  6197. 'some test data',
  6198. '/textarea',
  6199. );
  6200. $this->assertTags($result, $expected);
  6201. $result = $this->Form->textarea('user.bio');
  6202. $expected = array(
  6203. 'textarea' => array('name' => 'user[bio]'),
  6204. '/textarea',
  6205. );
  6206. $this->assertTags($result, $expected);
  6207. $this->Form->request->data = array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars');
  6208. $result = $this->Form->textarea('field');
  6209. $expected = array(
  6210. 'textarea' => array('name' => 'field'),
  6211. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  6212. '/textarea',
  6213. );
  6214. $this->assertTags($result, $expected);
  6215. $this->Form->request->data = [
  6216. 'Model' => ['field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars']
  6217. ];
  6218. $result = $this->Form->textarea('Model.field', ['escape' => false]);
  6219. $expected = array(
  6220. 'textarea' => array('name' => 'Model[field]'),
  6221. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  6222. '/textarea',
  6223. );
  6224. $this->assertTags($result, $expected);
  6225. $result = $this->Form->textarea('0.OtherModel.field');
  6226. $expected = array(
  6227. 'textarea' => array('name' => '0[OtherModel][field]'),
  6228. '/textarea'
  6229. );
  6230. $this->assertTags($result, $expected);
  6231. }
  6232. /**
  6233. * testTextAreaWithStupidCharacters method
  6234. *
  6235. * test text area with non-ascii characters
  6236. *
  6237. * @return void
  6238. */
  6239. public function testTextAreaWithStupidCharacters() {
  6240. $result = $this->Form->textarea('Post.content', [
  6241. 'value' => "GREAT®",
  6242. 'rows' => '15',
  6243. 'cols' => '75'
  6244. ]);
  6245. $expected = [
  6246. 'textarea' => ['name' => 'Post[content]', 'rows' => '15', 'cols' => '75'],
  6247. 'GREAT®',
  6248. '/textarea',
  6249. ];
  6250. $this->assertTags($result, $expected);
  6251. }
  6252. /**
  6253. * testHiddenField method
  6254. *
  6255. * @return void
  6256. */
  6257. public function testHiddenField() {
  6258. $this->article['errors'] = [
  6259. 'field' => true
  6260. ];
  6261. $this->Form->request->data['field'] = 'test';
  6262. $this->Form->create($this->article);
  6263. $result = $this->Form->hidden('field', array('id' => 'theID'));
  6264. $this->assertTags($result, array(
  6265. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'id' => 'theID', 'value' => 'test'))
  6266. );
  6267. $result = $this->Form->hidden('field', ['value' => 'my value']);
  6268. $expected = [
  6269. 'input' => ['type' => 'hidden', 'class' => 'form-error', 'name' => 'field', 'value' => 'my value']
  6270. ];
  6271. $this->assertTags($result, $expected);
  6272. }
  6273. /**
  6274. * testFileUploadField method
  6275. *
  6276. * @return void
  6277. */
  6278. public function testFileUploadField() {
  6279. $this->markTestIncomplete('Need to revisit once models work again.');
  6280. $result = $this->Form->file('Model.upload');
  6281. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6282. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  6283. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  6284. $expected = array(
  6285. 'div' => array('class' => 'input file'),
  6286. 'label' => array('for' => 'ModelUpload'),
  6287. 'Upload',
  6288. '/label',
  6289. 'input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload'),
  6290. '/div'
  6291. );
  6292. $this->assertTags($result, $expected);
  6293. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  6294. $result = $this->Form->file('Model.upload');
  6295. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'Model[upload]', 'id' => 'ModelUpload')));
  6296. }
  6297. /**
  6298. * test File upload input on a model not used in create();
  6299. *
  6300. * @return void
  6301. */
  6302. public function testFileUploadOnOtherModel() {
  6303. $this->markTestIncomplete('Need to revisit once models work again.');
  6304. $this->Form->create('ValidateUser', array('type' => 'file'));
  6305. $result = $this->Form->file('ValidateProfile.city');
  6306. $expected = array(
  6307. 'input' => array('type' => 'file', 'name' => 'ValidateProfile[city]', 'id' => 'ValidateProfileCity')
  6308. );
  6309. $this->assertTags($result, $expected);
  6310. }
  6311. /**
  6312. * testButton method
  6313. *
  6314. * @return void
  6315. */
  6316. public function testButton() {
  6317. $result = $this->Form->button('Hi');
  6318. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  6319. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  6320. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  6321. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  6322. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  6323. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  6324. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  6325. $result = $this->Form->button('No type', array('type' => false));
  6326. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  6327. $result = $this->Form->button('Upload Text', array(
  6328. 'onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'",
  6329. 'escape' => false
  6330. ));
  6331. $this->assertNotRegExp('/\&039/', $result);
  6332. }
  6333. /**
  6334. * Test that button() makes unlocked fields by default.
  6335. *
  6336. * @return void
  6337. */
  6338. public function testButtonUnlockedByDefault() {
  6339. $this->Form->request->params['_csrfToken'] = 'secured';
  6340. $this->Form->button('Save', array('name' => 'save'));
  6341. $this->Form->button('Clear');
  6342. $result = $this->Form->unlockField();
  6343. $this->assertEquals(array('save'), $result);
  6344. }
  6345. /**
  6346. * testPostButton method
  6347. *
  6348. * @return void
  6349. */
  6350. public function testPostButton() {
  6351. $result = $this->Form->postButton('Hi', '/controller/action');
  6352. $this->assertTags($result, array(
  6353. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  6354. 'div' => array('style' => 'display:none;'),
  6355. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6356. '/div',
  6357. 'button' => array('type' => 'submit'),
  6358. 'Hi',
  6359. '/button',
  6360. '/form'
  6361. ));
  6362. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  6363. $this->assertTrue(strpos($result, '<input type="hidden" name="extra" value="value"') !== false);
  6364. }
  6365. /**
  6366. * Test that postButton adds _Token fields.
  6367. *
  6368. * @return void
  6369. */
  6370. public function testSecurePostButton() {
  6371. $this->Form->request->params['_csrfToken'] = 'testkey';
  6372. $this->Form->request->params['_Token'] = ['unlockedFields' => []];
  6373. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  6374. $expected = array(
  6375. 'form' => array(
  6376. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  6377. ),
  6378. array('div' => array('style' => 'display:none;')),
  6379. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6380. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  6381. '/div',
  6382. 'button' => array('type' => 'submit'),
  6383. 'Delete',
  6384. '/button',
  6385. array('div' => array('style' => 'display:none;')),
  6386. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  6387. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  6388. '/div',
  6389. '/form',
  6390. );
  6391. $this->assertTags($result, $expected);
  6392. }
  6393. /**
  6394. * testPostLink method
  6395. *
  6396. * @return void
  6397. */
  6398. public function testPostLink() {
  6399. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6400. $this->assertTags($result, array(
  6401. 'form' => array(
  6402. 'method' => 'post', 'action' => '/posts/delete/1',
  6403. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6404. ),
  6405. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6406. '/form',
  6407. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6408. 'Delete',
  6409. '/a'
  6410. ));
  6411. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  6412. $this->assertTags($result, array(
  6413. 'form' => array(
  6414. 'method' => 'post', 'action' => '/posts/delete/1',
  6415. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6416. ),
  6417. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  6418. '/form',
  6419. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6420. 'Delete',
  6421. '/a'
  6422. ));
  6423. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  6424. $this->assertTags($result, array(
  6425. 'form' => array(
  6426. 'method' => 'post', 'action' => '/posts/delete/1',
  6427. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6428. ),
  6429. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6430. '/form',
  6431. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&quot;Confirm\?&quot;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6432. 'Delete',
  6433. '/a'
  6434. ));
  6435. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('escape' => false), '\'Confirm\' this "deletion"?');
  6436. $this->assertTags($result, array(
  6437. 'form' => array(
  6438. 'method' => 'post', 'action' => '/posts/delete/1',
  6439. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6440. ),
  6441. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6442. '/form',
  6443. '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;/'),
  6444. 'Delete',
  6445. '/a'
  6446. ));
  6447. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  6448. $this->assertContains('<input type="hidden" name="id" value="1"', $result);
  6449. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  6450. $this->assertTags($result, array(
  6451. 'form' => array(
  6452. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  6453. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6454. ),
  6455. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6456. '/form',
  6457. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6458. 'Delete',
  6459. '/a'
  6460. ));
  6461. $result = $this->Form->postLink(
  6462. '',
  6463. array('controller' => 'items', 'action' => 'delete', 10),
  6464. array('class' => 'btn btn-danger', 'escape' => false),
  6465. 'Confirm thing'
  6466. );
  6467. $this->assertTags($result, array(
  6468. 'form' => array(
  6469. 'method' => 'post', 'action' => '/items/delete/10',
  6470. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6471. ),
  6472. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6473. '/form',
  6474. 'a' => array('class' => 'btn btn-danger', 'href' => '#', 'onclick' => 'preg:/if \(confirm\(\&quot\;Confirm thing\&quot\;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  6475. '/a'
  6476. ));
  6477. }
  6478. /**
  6479. * test creating postLinks after a GET form.
  6480. *
  6481. * @return void
  6482. */
  6483. public function testPostLinkAfterGetForm() {
  6484. $this->Form->request->params['_csrfToken'] = 'testkey';
  6485. $this->Form->request->params['_Token'] = 'val';
  6486. $this->Form->create($this->article, array('type' => 'get'));
  6487. $this->Form->end();
  6488. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  6489. $this->assertTags($result, array(
  6490. 'form' => array(
  6491. 'method' => 'post', 'action' => '/posts/delete/1',
  6492. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6493. ),
  6494. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  6495. array('input' => array('type' => 'hidden', 'name' => '_csrfToken', 'value' => 'testkey')),
  6496. 'div' => array('style' => 'display:none;'),
  6497. array('input' => array('type' => 'hidden', 'name' => '_Token[fields]', 'value' => 'preg:/[\w\d%]+/')),
  6498. array('input' => array('type' => 'hidden', 'name' => '_Token[unlocked]', 'value' => '')),
  6499. '/div',
  6500. '/form',
  6501. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6502. 'Delete',
  6503. '/a'
  6504. ));
  6505. }
  6506. /**
  6507. * Test that postLink adds form tags to view block
  6508. *
  6509. * @return void
  6510. */
  6511. public function testPostLinkFormBuffer() {
  6512. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('inline' => false));
  6513. $this->assertTags($result, array(
  6514. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6515. 'Delete',
  6516. '/a'
  6517. ));
  6518. $result = $this->View->fetch('postLink');
  6519. $this->assertTags($result, array(
  6520. 'form' => array(
  6521. 'method' => 'post', 'action' => '/posts/delete/1',
  6522. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6523. ),
  6524. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6525. '/form'
  6526. ));
  6527. $result = $this->Form->postLink('Delete', '/posts/delete/2',
  6528. array('inline' => false, 'method' => 'DELETE')
  6529. );
  6530. $this->assertTags($result, array(
  6531. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6532. 'Delete',
  6533. '/a'
  6534. ));
  6535. $result = $this->View->fetch('postLink');
  6536. $this->assertTags($result, array(
  6537. 'form' => array(
  6538. 'method' => 'post', 'action' => '/posts/delete/1',
  6539. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6540. ),
  6541. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6542. '/form',
  6543. array(
  6544. 'form' => array(
  6545. 'method' => 'post', 'action' => '/posts/delete/2',
  6546. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6547. ),
  6548. ),
  6549. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE')),
  6550. '/form'
  6551. ));
  6552. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('block' => 'foobar'));
  6553. $this->assertTags($result, array(
  6554. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  6555. 'Delete',
  6556. '/a'
  6557. ));
  6558. $result = $this->View->fetch('foobar');
  6559. $this->assertTags($result, array(
  6560. 'form' => array(
  6561. 'method' => 'post', 'action' => '/posts/delete/1',
  6562. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  6563. ),
  6564. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6565. '/form'
  6566. ));
  6567. }
  6568. /**
  6569. * testSubmitButton method
  6570. *
  6571. * @return void
  6572. */
  6573. public function testSubmitButton() {
  6574. $this->markTestIncomplete('Need to revisit once models work again.');
  6575. $result = $this->Form->submit('');
  6576. $expected = array(
  6577. 'div' => array('class' => 'submit'),
  6578. 'input' => array('type' => 'submit', 'value' => ''),
  6579. '/div'
  6580. );
  6581. $this->assertTags($result, $expected);
  6582. $result = $this->Form->submit('Test Submit');
  6583. $expected = array(
  6584. 'div' => array('class' => 'submit'),
  6585. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6586. '/div'
  6587. );
  6588. $this->assertTags($result, $expected);
  6589. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  6590. $expected = array(
  6591. 'span' => array('class' => 'submit'),
  6592. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6593. '/span'
  6594. );
  6595. $this->assertTags($result, $expected);
  6596. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  6597. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  6598. $this->assertTags($result, $expected);
  6599. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  6600. $expected = array(
  6601. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  6602. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  6603. '/div'
  6604. );
  6605. $this->assertTags($result, $expected);
  6606. $result = $this->Form->submit('Next >');
  6607. $expected = array(
  6608. 'div' => array('class' => 'submit'),
  6609. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  6610. '/div'
  6611. );
  6612. $this->assertTags($result, $expected);
  6613. $result = $this->Form->submit('Next >', array('escape' => false));
  6614. $expected = array(
  6615. 'div' => array('class' => 'submit'),
  6616. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  6617. '/div'
  6618. );
  6619. $this->assertTags($result, $expected);
  6620. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  6621. $expected = array(
  6622. 'div' => array('class' => 'submit'),
  6623. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  6624. '/div'
  6625. );
  6626. $this->assertTags($result, $expected);
  6627. $before = '--before--';
  6628. $after = '--after--';
  6629. $result = $this->Form->submit('Test', array('before' => $before));
  6630. $expected = array(
  6631. 'div' => array('class' => 'submit'),
  6632. '--before--',
  6633. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6634. '/div'
  6635. );
  6636. $this->assertTags($result, $expected);
  6637. $result = $this->Form->submit('Test', array('after' => $after));
  6638. $expected = array(
  6639. 'div' => array('class' => 'submit'),
  6640. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6641. '--after--',
  6642. '/div'
  6643. );
  6644. $this->assertTags($result, $expected);
  6645. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  6646. $expected = array(
  6647. 'div' => array('class' => 'submit'),
  6648. '--before--',
  6649. 'input' => array('type' => 'submit', 'value' => 'Test'),
  6650. '--after--',
  6651. '/div'
  6652. );
  6653. $this->assertTags($result, $expected);
  6654. }
  6655. /**
  6656. * test image submit types.
  6657. *
  6658. * @return void
  6659. */
  6660. public function testSubmitImage() {
  6661. $this->markTestIncomplete('Need to revisit once models work again.');
  6662. $result = $this->Form->submit('http://example.com/cake.power.gif');
  6663. $expected = array(
  6664. 'div' => array('class' => 'submit'),
  6665. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  6666. '/div'
  6667. );
  6668. $this->assertTags($result, $expected);
  6669. $result = $this->Form->submit('/relative/cake.power.gif');
  6670. $expected = array(
  6671. 'div' => array('class' => 'submit'),
  6672. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  6673. '/div'
  6674. );
  6675. $this->assertTags($result, $expected);
  6676. $result = $this->Form->submit('cake.power.gif');
  6677. $expected = array(
  6678. 'div' => array('class' => 'submit'),
  6679. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6680. '/div'
  6681. );
  6682. $this->assertTags($result, $expected);
  6683. $result = $this->Form->submit('Not.an.image');
  6684. $expected = array(
  6685. 'div' => array('class' => 'submit'),
  6686. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6687. '/div'
  6688. );
  6689. $this->assertTags($result, $expected);
  6690. $after = '--after--';
  6691. $before = '--before--';
  6692. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  6693. $expected = array(
  6694. 'div' => array('class' => 'submit'),
  6695. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6696. '--after--',
  6697. '/div'
  6698. );
  6699. $this->assertTags($result, $expected);
  6700. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  6701. $expected = array(
  6702. 'div' => array('class' => 'submit'),
  6703. '--before--',
  6704. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6705. '/div'
  6706. );
  6707. $this->assertTags($result, $expected);
  6708. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  6709. $expected = array(
  6710. 'div' => array('class' => 'submit'),
  6711. '--before--',
  6712. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  6713. '--after--',
  6714. '/div'
  6715. );
  6716. $this->assertTags($result, $expected);
  6717. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  6718. $expected = array(
  6719. 'div' => array('class' => 'submit'),
  6720. '--before--',
  6721. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  6722. '--after--',
  6723. '/div'
  6724. );
  6725. $this->assertTags($result, $expected);
  6726. }
  6727. /**
  6728. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  6729. * be submitted at a time.
  6730. *
  6731. * @return void
  6732. */
  6733. public function testSubmitUnlockedByDefault() {
  6734. $this->markTestIncomplete('Need to revisit once models work again.');
  6735. $this->Form->request->params['_csrfToken'] = 'secured';
  6736. $this->Form->submit('Go go');
  6737. $this->Form->submit('Save', array('name' => 'save'));
  6738. $result = $this->Form->unlockField();
  6739. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  6740. }
  6741. /**
  6742. * Test submit image with timestamps.
  6743. *
  6744. * @return void
  6745. */
  6746. public function testSubmitImageTimestamp() {
  6747. $this->markTestIncomplete('Need to revisit once models work again.');
  6748. Configure::write('Asset.timestamp', 'force');
  6749. $result = $this->Form->submit('cake.power.gif');
  6750. $expected = array(
  6751. 'div' => array('class' => 'submit'),
  6752. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6753. '/div'
  6754. );
  6755. $this->assertTags($result, $expected);
  6756. }
  6757. /**
  6758. * test that datetime() works with GET style forms.
  6759. *
  6760. * @return void
  6761. */
  6762. public function testDateTimeWithGetForms() {
  6763. $this->markTestIncomplete('Need to revisit once models work again.');
  6764. extract($this->dateRegex);
  6765. $this->Form->create('Contact', array('type' => 'get'));
  6766. $result = $this->Form->datetime('created');
  6767. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6768. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6769. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6770. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6771. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6772. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6773. }
  6774. /**
  6775. * testEditFormWithData method
  6776. *
  6777. * test auto populating form elements from submitted data.
  6778. *
  6779. * @return void
  6780. */
  6781. public function testEditFormWithData() {
  6782. $this->markTestIncomplete('Need to revisit once models work again.');
  6783. $this->Form->request->data = array('Person' => array(
  6784. 'id' => 1,
  6785. 'first_name' => 'Nate',
  6786. 'last_name' => 'Abele',
  6787. 'email' => 'nate@example.com'
  6788. ));
  6789. $this->Form->request->addParams(array(
  6790. 'models' => array('Person'),
  6791. 'controller' => 'people',
  6792. 'action' => 'add'
  6793. ));
  6794. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6795. $this->Form->create();
  6796. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6797. $expected = array(
  6798. 'input' => array('type' => 'hidden', 'name' => 'People[People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6799. 'select' => array(
  6800. 'name' => 'People[People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6801. ),
  6802. array('option' => array('value' => 1)), 'Nate', '/option',
  6803. array('option' => array('value' => 2)), 'Garrett', '/option',
  6804. array('option' => array('value' => 3)), 'Larry', '/option',
  6805. '/select'
  6806. );
  6807. $this->assertTags($result, $expected);
  6808. }
  6809. /**
  6810. * Test that required fields are created for various types of validation.
  6811. *
  6812. * @return void
  6813. */
  6814. public function testFormInputRequiredDetection() {
  6815. $this->markTestIncomplete('Need to revisit once models work again.');
  6816. $this->Form->create('Contact');
  6817. $result = $this->Form->input('Contact.non_existing');
  6818. $expected = array(
  6819. 'div' => array('class' => 'input text'),
  6820. 'label' => array('for' => 'ContactNonExisting'),
  6821. 'Non Existing',
  6822. '/label',
  6823. 'input' => array(
  6824. 'type' => 'text', 'name' => 'Contact[non_existing]',
  6825. 'id' => 'ContactNonExisting'
  6826. ),
  6827. '/div'
  6828. );
  6829. $this->assertTags($result, $expected);
  6830. $result = $this->Form->input('Contact.imrequired');
  6831. $expected = array(
  6832. 'div' => array('class' => 'input text required'),
  6833. 'label' => array('for' => 'ContactImrequired'),
  6834. 'Imrequired',
  6835. '/label',
  6836. 'input' => array(
  6837. 'type' => 'text', 'name' => 'Contact[imrequired]',
  6838. 'id' => 'ContactImrequired',
  6839. 'required' => 'required'
  6840. ),
  6841. '/div'
  6842. );
  6843. $this->assertTags($result, $expected);
  6844. $result = $this->Form->input('Contact.imalsorequired');
  6845. $expected = array(
  6846. 'div' => array('class' => 'input text required'),
  6847. 'label' => array('for' => 'ContactImalsorequired'),
  6848. 'Imalsorequired',
  6849. '/label',
  6850. 'input' => array(
  6851. 'type' => 'text', 'name' => 'Contact[imalsorequired]',
  6852. 'id' => 'ContactImalsorequired',
  6853. 'required' => 'required'
  6854. ),
  6855. '/div'
  6856. );
  6857. $this->assertTags($result, $expected);
  6858. $result = $this->Form->input('Contact.imrequiredtoo');
  6859. $expected = array(
  6860. 'div' => array('class' => 'input text required'),
  6861. 'label' => array('for' => 'ContactImrequiredtoo'),
  6862. 'Imrequiredtoo',
  6863. '/label',
  6864. 'input' => array(
  6865. 'type' => 'text', 'name' => 'Contact[imrequiredtoo]',
  6866. 'id' => 'ContactImrequiredtoo',
  6867. 'required' => 'required'
  6868. ),
  6869. '/div'
  6870. );
  6871. $this->assertTags($result, $expected);
  6872. $result = $this->Form->input('Contact.required_one');
  6873. $expected = array(
  6874. 'div' => array('class' => 'input text required'),
  6875. 'label' => array('for' => 'ContactRequiredOne'),
  6876. 'Required One',
  6877. '/label',
  6878. 'input' => array(
  6879. 'type' => 'text', 'name' => 'Contact[required_one]',
  6880. 'id' => 'ContactRequiredOne',
  6881. 'required' => 'required'
  6882. ),
  6883. '/div'
  6884. );
  6885. $this->assertTags($result, $expected);
  6886. $result = $this->Form->input('Contact.string_required');
  6887. $expected = array(
  6888. 'div' => array('class' => 'input text required'),
  6889. 'label' => array('for' => 'ContactStringRequired'),
  6890. 'String Required',
  6891. '/label',
  6892. 'input' => array(
  6893. 'type' => 'text', 'name' => 'Contact[string_required]',
  6894. 'id' => 'ContactStringRequired',
  6895. 'required' => 'required'
  6896. ),
  6897. '/div'
  6898. );
  6899. $this->assertTags($result, $expected);
  6900. $result = $this->Form->input('Contact.imnotrequired');
  6901. $expected = array(
  6902. 'div' => array('class' => 'input text'),
  6903. 'label' => array('for' => 'ContactImnotrequired'),
  6904. 'Imnotrequired',
  6905. '/label',
  6906. 'input' => array(
  6907. 'type' => 'text', 'name' => 'Contact[imnotrequired]',
  6908. 'id' => 'ContactImnotrequired'
  6909. ),
  6910. '/div'
  6911. );
  6912. $this->assertTags($result, $expected);
  6913. $result = $this->Form->input('Contact.imalsonotrequired');
  6914. $expected = array(
  6915. 'div' => array('class' => 'input text'),
  6916. 'label' => array('for' => 'ContactImalsonotrequired'),
  6917. 'Imalsonotrequired',
  6918. '/label',
  6919. 'input' => array(
  6920. 'type' => 'text', 'name' => 'Contact[imalsonotrequired]',
  6921. 'id' => 'ContactImalsonotrequired'
  6922. ),
  6923. '/div'
  6924. );
  6925. $this->assertTags($result, $expected);
  6926. $result = $this->Form->input('Contact.imalsonotrequired2');
  6927. $expected = array(
  6928. 'div' => array('class' => 'input text'),
  6929. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6930. 'Imalsonotrequired2',
  6931. '/label',
  6932. 'input' => array(
  6933. 'type' => 'text', 'name' => 'Contact[imalsonotrequired2]',
  6934. 'id' => 'ContactImalsonotrequired2'
  6935. ),
  6936. '/div'
  6937. );
  6938. $this->assertTags($result, $expected);
  6939. $result = $this->Form->input('Contact.imnotrequiredeither');
  6940. $expected = array(
  6941. 'div' => array('class' => 'input text'),
  6942. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6943. 'Imnotrequiredeither',
  6944. '/label',
  6945. 'input' => array(
  6946. 'type' => 'text', 'name' => 'Contact[imnotrequiredeither]',
  6947. 'id' => 'ContactImnotrequiredeither'
  6948. ),
  6949. '/div'
  6950. );
  6951. $this->assertTags($result, $expected);
  6952. $result = $this->Form->input('Contact.iamrequiredalways');
  6953. $expected = array(
  6954. 'div' => array('class' => 'input text required'),
  6955. 'label' => array('for' => 'ContactIamrequiredalways'),
  6956. 'Iamrequiredalways',
  6957. '/label',
  6958. 'input' => array(
  6959. 'type' => 'text', 'name' => 'Contact[iamrequiredalways]',
  6960. 'id' => 'ContactIamrequiredalways',
  6961. 'required' => 'required'
  6962. ),
  6963. '/div'
  6964. );
  6965. $this->assertTags($result, $expected);
  6966. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6967. $expected = array(
  6968. 'div' => array('class' => 'input checkbox required'),
  6969. array('input' => array(
  6970. 'type' => 'hidden',
  6971. 'name' => 'Contact[boolean_field]',
  6972. 'id' => 'ContactBooleanField_',
  6973. 'value' => '0'
  6974. )),
  6975. array('input' => array(
  6976. 'type' => 'checkbox',
  6977. 'name' => 'Contact[boolean_field]',
  6978. 'value' => '1',
  6979. 'id' => 'ContactBooleanField'
  6980. )),
  6981. 'label' => array('for' => 'ContactBooleanField'),
  6982. 'Boolean Field',
  6983. '/label',
  6984. '/div'
  6985. );
  6986. $this->assertTags($result, $expected);
  6987. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox', 'required' => true));
  6988. $expected = array(
  6989. 'div' => array('class' => 'input checkbox required'),
  6990. array('input' => array(
  6991. 'type' => 'hidden',
  6992. 'name' => 'Contact[boolean_field]',
  6993. 'id' => 'ContactBooleanField_',
  6994. 'value' => '0'
  6995. )),
  6996. array('input' => array(
  6997. 'type' => 'checkbox',
  6998. 'name' => 'Contact[boolean_field]',
  6999. 'value' => '1',
  7000. 'id' => 'ContactBooleanField',
  7001. 'required' => 'required'
  7002. )),
  7003. 'label' => array('for' => 'ContactBooleanField'),
  7004. 'Boolean Field',
  7005. '/label',
  7006. '/div'
  7007. );
  7008. $this->assertTags($result, $expected);
  7009. $result = $this->Form->input('Contact.iamrequiredalways', array('type' => 'file'));
  7010. $expected = array(
  7011. 'div' => array('class' => 'input file required'),
  7012. 'label' => array('for' => 'ContactIamrequiredalways'),
  7013. 'Iamrequiredalways',
  7014. '/label',
  7015. 'input' => array(
  7016. 'type' => 'file',
  7017. 'name' => 'data[Contact][iamrequiredalways]',
  7018. 'id' => 'ContactIamrequiredalways',
  7019. 'required' => 'required'
  7020. ),
  7021. '/div'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. }
  7025. /**
  7026. * Test that required fields are created when only using ModelValidator::add().
  7027. *
  7028. * @return void
  7029. */
  7030. public function testFormInputRequiredDetectionModelValidator() {
  7031. $this->markTestIncomplete('Need to revisit once models work again.');
  7032. $this->Form->create('ContactTag');
  7033. $result = $this->Form->input('ContactTag.iwillberequired');
  7034. $expected = array(
  7035. 'div' => array('class' => 'input text required'),
  7036. 'label' => array('for' => 'ContactTagIwillberequired'),
  7037. 'Iwillberequired',
  7038. '/label',
  7039. 'input' => array(
  7040. 'name' => 'data[ContactTag][iwillberequired]',
  7041. 'type' => 'text',
  7042. 'id' => 'ContactTagIwillberequired',
  7043. 'required' => 'required'
  7044. ),
  7045. '/div'
  7046. );
  7047. $this->assertTags($result, $expected);
  7048. }
  7049. /**
  7050. * testFormMagicInput method
  7051. *
  7052. * @return void
  7053. */
  7054. public function testFormMagicInput() {
  7055. $this->markTestIncomplete('Need to revisit once models work again.');
  7056. $encoding = strtolower(Configure::read('App.encoding'));
  7057. $result = $this->Form->create('Contact');
  7058. $expected = array(
  7059. 'form' => array(
  7060. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7061. 'accept-charset' => $encoding
  7062. ),
  7063. 'div' => array('style' => 'display:none;'),
  7064. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7065. '/div'
  7066. );
  7067. $this->assertTags($result, $expected);
  7068. $result = $this->Form->input('name');
  7069. $expected = array(
  7070. 'div' => array('class' => 'input text'),
  7071. 'label' => array('for' => 'ContactName'),
  7072. 'Name',
  7073. '/label',
  7074. 'input' => array(
  7075. 'type' => 'text', 'name' => 'Contact[name]',
  7076. 'id' => 'ContactName', 'maxlength' => '255'
  7077. ),
  7078. '/div'
  7079. );
  7080. $this->assertTags($result, $expected);
  7081. $result = $this->Form->input('non_existing_field_in_contact_model');
  7082. $expected = array(
  7083. 'div' => array('class' => 'input text'),
  7084. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  7085. 'Non Existing Field In Contact Model',
  7086. '/label',
  7087. 'input' => array(
  7088. 'type' => 'text', 'name' => 'Contact[non_existing_field_in_contact_model]',
  7089. 'id' => 'ContactNonExistingFieldInContactModel'
  7090. ),
  7091. '/div'
  7092. );
  7093. $this->assertTags($result, $expected);
  7094. $result = $this->Form->input('Address.street');
  7095. $expected = array(
  7096. 'div' => array('class' => 'input text'),
  7097. 'label' => array('for' => 'AddressStreet'),
  7098. 'Street',
  7099. '/label',
  7100. 'input' => array(
  7101. 'type' => 'text', 'name' => 'Address[street]',
  7102. 'id' => 'AddressStreet'
  7103. ),
  7104. '/div'
  7105. );
  7106. $this->assertTags($result, $expected);
  7107. $result = $this->Form->input('Address.non_existing_field_in_model');
  7108. $expected = array(
  7109. 'div' => array('class' => 'input text'),
  7110. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  7111. 'Non Existing Field In Model',
  7112. '/label',
  7113. 'input' => array(
  7114. 'type' => 'text', 'name' => 'Address[non_existing_field_in_model]',
  7115. 'id' => 'AddressNonExistingFieldInModel'
  7116. ),
  7117. '/div'
  7118. );
  7119. $this->assertTags($result, $expected);
  7120. $result = $this->Form->input('name', array('div' => false));
  7121. $expected = array(
  7122. 'label' => array('for' => 'ContactName'),
  7123. 'Name',
  7124. '/label',
  7125. 'input' => array(
  7126. 'type' => 'text', 'name' => 'Contact[name]',
  7127. 'id' => 'ContactName', 'maxlength' => '255'
  7128. )
  7129. );
  7130. $this->assertTags($result, $expected);
  7131. extract($this->dateRegex);
  7132. $now = strtotime('now');
  7133. $result = $this->Form->input('Contact.published', array('div' => false));
  7134. $expected = array(
  7135. 'label' => array('for' => 'ContactPublishedMonth'),
  7136. 'Published',
  7137. '/label',
  7138. array('select' => array(
  7139. 'name' => 'Contact[published][month]', 'id' => 'ContactPublishedMonth'
  7140. )),
  7141. $monthsRegex,
  7142. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7143. date('F', $now),
  7144. '/option',
  7145. '*/select',
  7146. '-',
  7147. array('select' => array(
  7148. 'name' => 'Contact[published][day]', 'id' => 'ContactPublishedDay'
  7149. )),
  7150. $daysRegex,
  7151. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7152. date('j', $now),
  7153. '/option',
  7154. '*/select',
  7155. '-',
  7156. array('select' => array(
  7157. 'name' => 'Contact[published][year]', 'id' => 'ContactPublishedYear'
  7158. )),
  7159. $yearsRegex,
  7160. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7161. date('Y', $now),
  7162. '*/select'
  7163. );
  7164. $this->assertTags($result, $expected);
  7165. $result = $this->Form->input('Contact.updated', array('div' => false));
  7166. $expected = array(
  7167. 'label' => array('for' => 'ContactUpdatedMonth'),
  7168. 'Updated',
  7169. '/label',
  7170. array('select' => array(
  7171. 'name' => 'Contact[updated][month]', 'id' => 'ContactUpdatedMonth'
  7172. )),
  7173. $monthsRegex,
  7174. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  7175. date('F', $now),
  7176. '/option',
  7177. '*/select',
  7178. '-',
  7179. array('select' => array(
  7180. 'name' => 'Contact[updated][day]', 'id' => 'ContactUpdatedDay'
  7181. )),
  7182. $daysRegex,
  7183. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  7184. date('j', $now),
  7185. '/option',
  7186. '*/select',
  7187. '-',
  7188. array('select' => array(
  7189. 'name' => 'Contact[updated][year]', 'id' => 'ContactUpdatedYear'
  7190. )),
  7191. $yearsRegex,
  7192. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  7193. date('Y', $now),
  7194. '*/select'
  7195. );
  7196. $this->assertTags($result, $expected);
  7197. $result = $this->Form->input('UserForm.stuff');
  7198. $expected = array(
  7199. 'div' => array('class' => 'input text'),
  7200. 'label' => array('for' => 'UserFormStuff'),
  7201. 'Stuff',
  7202. '/label',
  7203. 'input' => array(
  7204. 'type' => 'text', 'name' => 'UserForm[stuff]',
  7205. 'id' => 'UserFormStuff', 'maxlength' => 10
  7206. ),
  7207. '/div'
  7208. );
  7209. $this->assertTags($result, $expected);
  7210. }
  7211. /**
  7212. * testForMagicInputNonExistingNorValidated method
  7213. *
  7214. * @return void
  7215. */
  7216. public function testForMagicInputNonExistingNorValidated() {
  7217. $this->markTestIncomplete('Need to revisit once models work again.');
  7218. $encoding = strtolower(Configure::read('App.encoding'));
  7219. $result = $this->Form->create('Contact');
  7220. $expected = array(
  7221. 'form' => array(
  7222. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7223. 'accept-charset' => $encoding
  7224. ),
  7225. 'div' => array('style' => 'display:none;'),
  7226. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7227. '/div'
  7228. );
  7229. $this->assertTags($result, $expected);
  7230. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7231. $expected = array(
  7232. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7233. 'Non Existing Nor Validated',
  7234. '/label',
  7235. 'input' => array(
  7236. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7237. 'id' => 'ContactNonExistingNorValidated'
  7238. )
  7239. );
  7240. $this->assertTags($result, $expected);
  7241. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  7242. 'div' => false, 'value' => 'my value'
  7243. ));
  7244. $expected = array(
  7245. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7246. 'Non Existing Nor Validated',
  7247. '/label',
  7248. 'input' => array(
  7249. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7250. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  7251. )
  7252. );
  7253. $this->assertTags($result, $expected);
  7254. $this->Form->request->data = array(
  7255. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  7256. ));
  7257. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  7258. $expected = array(
  7259. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  7260. 'Non Existing Nor Validated',
  7261. '/label',
  7262. 'input' => array(
  7263. 'type' => 'text', 'name' => 'Contact[non_existing_nor_validated]',
  7264. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  7265. )
  7266. );
  7267. $this->assertTags($result, $expected);
  7268. }
  7269. /**
  7270. * testFormMagicInputLabel method
  7271. *
  7272. * @return void
  7273. */
  7274. public function testFormMagicInputLabel() {
  7275. $this->markTestIncomplete('Need to revisit once models work again.');
  7276. $encoding = strtolower(Configure::read('App.encoding'));
  7277. $result = $this->Form->create('Contact');
  7278. $expected = array(
  7279. 'form' => array(
  7280. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  7281. 'accept-charset' => $encoding
  7282. ),
  7283. 'div' => array('style' => 'display:none;'),
  7284. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  7285. '/div'
  7286. );
  7287. $this->assertTags($result, $expected);
  7288. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  7289. $this->assertTags($result, array('input' => array(
  7290. 'name' => 'Contact[name]', 'type' => 'text',
  7291. 'id' => 'ContactName', 'maxlength' => '255')
  7292. ));
  7293. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  7294. $expected = array(
  7295. 'label' => array('for' => 'ContactName'),
  7296. 'My label',
  7297. '/label',
  7298. 'input' => array(
  7299. 'type' => 'text', 'name' => 'Contact[name]',
  7300. 'id' => 'ContactName', 'maxlength' => '255'
  7301. )
  7302. );
  7303. $this->assertTags($result, $expected);
  7304. $result = $this->Form->input('Contact.name', array(
  7305. 'div' => false, 'label' => array('class' => 'mandatory')
  7306. ));
  7307. $expected = array(
  7308. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7309. 'Name',
  7310. '/label',
  7311. 'input' => array(
  7312. 'type' => 'text', 'name' => 'Contact[name]',
  7313. 'id' => 'ContactName', 'maxlength' => '255'
  7314. )
  7315. );
  7316. $this->assertTags($result, $expected);
  7317. $result = $this->Form->input('Contact.name', array(
  7318. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  7319. ));
  7320. $expected = array(
  7321. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  7322. 'My label',
  7323. '/label',
  7324. 'input' => array(
  7325. 'type' => 'text', 'name' => 'Contact[name]',
  7326. 'id' => 'ContactName', 'maxlength' => '255'
  7327. )
  7328. );
  7329. $this->assertTags($result, $expected);
  7330. $result = $this->Form->input('Contact.name', array(
  7331. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  7332. ));
  7333. $expected = array(
  7334. 'label' => array('for' => 'my_id'),
  7335. 'Name',
  7336. '/label',
  7337. 'input' => array(
  7338. 'type' => 'text', 'name' => 'Contact[name]',
  7339. 'id' => 'my_id', 'maxlength' => '255'
  7340. )
  7341. );
  7342. $this->assertTags($result, $expected);
  7343. $result = $this->Form->input('1.id');
  7344. $this->assertTags($result, array('input' => array(
  7345. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  7346. 'id' => 'Contact1Id'
  7347. )));
  7348. $result = $this->Form->input("1.name");
  7349. $expected = array(
  7350. 'div' => array('class' => 'input text'),
  7351. 'label' => array('for' => 'Contact1Name'),
  7352. 'Name',
  7353. '/label',
  7354. 'input' => array(
  7355. 'type' => 'text', 'name' => 'Contact[1][name]',
  7356. 'id' => 'Contact1Name', 'maxlength' => '255'
  7357. ),
  7358. '/div'
  7359. );
  7360. $this->assertTags($result, $expected);
  7361. $result = $this->Form->input('Contact.1.id');
  7362. $this->assertTags($result, array(
  7363. 'input' => array(
  7364. 'type' => 'hidden', 'name' => 'Contact[1][id]',
  7365. 'id' => 'Contact1Id'
  7366. )
  7367. ));
  7368. $result = $this->Form->input("Model.1.name");
  7369. $expected = array(
  7370. 'div' => array('class' => 'input text'),
  7371. 'label' => array('for' => 'Model1Name'),
  7372. 'Name',
  7373. '/label',
  7374. 'input' => array(
  7375. 'type' => 'text', 'name' => 'Model[1][name]',
  7376. 'id' => 'Model1Name'
  7377. ),
  7378. '/div'
  7379. );
  7380. $this->assertTags($result, $expected);
  7381. }
  7382. /**
  7383. * testFormEnd method
  7384. *
  7385. * @return void
  7386. */
  7387. public function testFormEnd() {
  7388. $this->markTestIncomplete('Need to revisit once models work again.');
  7389. $this->assertEquals('</form>', $this->Form->end());
  7390. $result = $this->Form->end('');
  7391. $expected = array(
  7392. 'div' => array('class' => 'submit'),
  7393. 'input' => array('type' => 'submit', 'value' => ''),
  7394. '/div',
  7395. '/form'
  7396. );
  7397. $this->assertTags($result, $expected);
  7398. $result = $this->Form->end(array('label' => ''));
  7399. $expected = array(
  7400. 'div' => array('class' => 'submit'),
  7401. 'input' => array('type' => 'submit', 'value' => ''),
  7402. '/div',
  7403. '/form'
  7404. );
  7405. $this->assertTags($result, $expected);
  7406. $result = $this->Form->end('save');
  7407. $expected = array(
  7408. 'div' => array('class' => 'submit'),
  7409. 'input' => array('type' => 'submit', 'value' => 'save'),
  7410. '/div',
  7411. '/form'
  7412. );
  7413. $this->assertTags($result, $expected);
  7414. $result = $this->Form->end(array('label' => 'save'));
  7415. $expected = array(
  7416. 'div' => array('class' => 'submit'),
  7417. 'input' => array('type' => 'submit', 'value' => 'save'),
  7418. '/div',
  7419. '/form'
  7420. );
  7421. $this->assertTags($result, $expected);
  7422. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7423. $expected = array(
  7424. 'div' => array('class' => 'submit'),
  7425. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7426. '/div',
  7427. '/form'
  7428. );
  7429. $this->assertTags($result, $expected);
  7430. $result = $this->Form->end(array('name' => 'Whatever'));
  7431. $expected = array(
  7432. 'div' => array('class' => 'submit'),
  7433. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7434. '/div',
  7435. '/form'
  7436. );
  7437. $this->assertTags($result, $expected);
  7438. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7439. $expected = array(
  7440. 'div' => array('class' => 'good'),
  7441. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7442. '/div',
  7443. '/form'
  7444. );
  7445. $this->assertTags($result, $expected);
  7446. $result = $this->Form->end(array(
  7447. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7448. ));
  7449. $expected = array(
  7450. 'div' => array('class' => 'good'),
  7451. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7452. '/div',
  7453. '/form'
  7454. );
  7455. $this->assertTags($result, $expected);
  7456. }
  7457. /**
  7458. * testMultipleFormWithIdFields method
  7459. *
  7460. * @return void
  7461. */
  7462. public function testMultipleFormWithIdFields() {
  7463. $this->markTestIncomplete('Need to revisit once models work again.');
  7464. $this->Form->create('UserForm');
  7465. $result = $this->Form->input('id');
  7466. $this->assertTags($result, array('input' => array(
  7467. 'type' => 'hidden', 'name' => 'UserForm[id]', 'id' => 'UserFormId'
  7468. )));
  7469. $result = $this->Form->input('ValidateItem.id');
  7470. $this->assertTags($result, array('input' => array(
  7471. 'type' => 'hidden', 'name' => 'ValidateItem[id]',
  7472. 'id' => 'ValidateItemId'
  7473. )));
  7474. $result = $this->Form->input('ValidateUser.id');
  7475. $this->assertTags($result, array('input' => array(
  7476. 'type' => 'hidden', 'name' => 'ValidateUser[id]',
  7477. 'id' => 'ValidateUserId'
  7478. )));
  7479. }
  7480. /**
  7481. * testDbLessModel method
  7482. *
  7483. * @return void
  7484. */
  7485. public function testDbLessModel() {
  7486. $this->markTestIncomplete('Need to revisit once models work again.');
  7487. $this->Form->create('TestMail');
  7488. $result = $this->Form->input('name');
  7489. $expected = array(
  7490. 'div' => array('class' => 'input text'),
  7491. 'label' => array('for' => 'TestMailName'),
  7492. 'Name',
  7493. '/label',
  7494. 'input' => array(
  7495. 'name' => 'TestMail[name]', 'type' => 'text',
  7496. 'id' => 'TestMailName'
  7497. ),
  7498. '/div'
  7499. );
  7500. $this->assertTags($result, $expected);
  7501. $this->Form->create('TestMail');
  7502. $result = $this->Form->input('name');
  7503. $expected = array(
  7504. 'div' => array('class' => 'input text'),
  7505. 'label' => array('for' => 'TestMailName'),
  7506. 'Name',
  7507. '/label',
  7508. 'input' => array(
  7509. 'name' => 'TestMail[name]', 'type' => 'text',
  7510. 'id' => 'TestMailName'
  7511. ),
  7512. '/div'
  7513. );
  7514. $this->assertTags($result, $expected);
  7515. }
  7516. /**
  7517. * testBrokenness method
  7518. *
  7519. * @return void
  7520. */
  7521. public function testBrokenness() {
  7522. $this->markTestIncomplete('Need to revisit once models work again.');
  7523. /*
  7524. * #4 This test has two parents and four children. By default (as of r7117) both
  7525. * parents are show but the first parent is missing a child. This is the inconsistency
  7526. * in the default behaviour - one parent has all children, the other does not - dependent
  7527. * on the data values.
  7528. */
  7529. $result = $this->Form->select('Model.field', array(
  7530. 'Fred' => array(
  7531. 'freds_son_1' => 'Fred',
  7532. 'freds_son_2' => 'Freddie'
  7533. ),
  7534. 'Bert' => array(
  7535. 'berts_son_1' => 'Albert',
  7536. 'berts_son_2' => 'Bertie')
  7537. ),
  7538. array('showParents' => true, 'empty' => false)
  7539. );
  7540. $expected = array(
  7541. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  7542. array('optgroup' => array('label' => 'Fred')),
  7543. array('option' => array('value' => 'freds_son_1')),
  7544. 'Fred',
  7545. '/option',
  7546. array('option' => array('value' => 'freds_son_2')),
  7547. 'Freddie',
  7548. '/option',
  7549. '/optgroup',
  7550. array('optgroup' => array('label' => 'Bert')),
  7551. array('option' => array('value' => 'berts_son_1')),
  7552. 'Albert',
  7553. '/option',
  7554. array('option' => array('value' => 'berts_son_2')),
  7555. 'Bertie',
  7556. '/option',
  7557. '/optgroup',
  7558. '/select'
  7559. );
  7560. $this->assertTags($result, $expected);
  7561. /*
  7562. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7563. * changed, so we should expect the same select list data, just with a different name
  7564. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7565. * This is where data corruption can occur, because when a select value is missing from
  7566. * a list a form will substitute the first value in the list - without the user knowing.
  7567. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7568. * affect the availability of 3 => 'Three' as a valid option.
  7569. */
  7570. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7571. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7572. ));
  7573. $result = $this->Form->select(
  7574. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7575. );
  7576. $expected = array(
  7577. 'select' => array('name' => 'Model[field]', 'id' => 'ModelField'),
  7578. array('option' => array('value' => 1)),
  7579. 'One',
  7580. '/option',
  7581. array('option' => array('value' => 2)),
  7582. 'Two',
  7583. '/option',
  7584. array('optgroup' => array('label' => 'Three')),
  7585. array('option' => array('value' => 3)),
  7586. 'Three',
  7587. '/option',
  7588. array('option' => array('value' => 4)),
  7589. 'Four',
  7590. '/option',
  7591. array('option' => array('value' => 5)),
  7592. 'Five',
  7593. '/option',
  7594. '/optgroup',
  7595. '/select'
  7596. );
  7597. $this->assertTags($result, $expected);
  7598. }
  7599. /**
  7600. * Test the generation of fields for a multi record form.
  7601. *
  7602. * @return void
  7603. */
  7604. public function testMultiRecordForm() {
  7605. $this->markTestIncomplete('Need to revisit once models work again.');
  7606. $this->Form->create('ValidateProfile');
  7607. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7608. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7609. $expected = array(
  7610. 'div' => array('class' => 'input textarea'),
  7611. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7612. 'Name',
  7613. '/label',
  7614. 'textarea' => array(
  7615. 'id' => 'ValidateProfile1ValidateItem2Name',
  7616. 'name' => 'ValidateProfile[1][ValidateItem][2][name]',
  7617. 'cols' => 30,
  7618. 'rows' => 6
  7619. ),
  7620. 'Value',
  7621. '/textarea',
  7622. '/div'
  7623. );
  7624. $this->assertTags($result, $expected);
  7625. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created', array('empty' => true));
  7626. $expected = array(
  7627. 'div' => array('class' => 'input date'),
  7628. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7629. 'Created',
  7630. '/label',
  7631. array('select' => array(
  7632. 'name' => 'ValidateProfile[1][ValidateItem][2][created][month]',
  7633. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7634. )
  7635. ),
  7636. array('option' => array('value' => '')), '/option',
  7637. $this->dateRegex['monthsRegex'],
  7638. '/select', '-',
  7639. array('select' => array(
  7640. 'name' => 'ValidateProfile[1][ValidateItem][2][created][day]',
  7641. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7642. )
  7643. ),
  7644. array('option' => array('value' => '')), '/option',
  7645. $this->dateRegex['daysRegex'],
  7646. '/select', '-',
  7647. array('select' => array(
  7648. 'name' => 'ValidateProfile[1][ValidateItem][2][created][year]',
  7649. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7650. )
  7651. ),
  7652. array('option' => array('value' => '')), '/option',
  7653. $this->dateRegex['yearsRegex'],
  7654. '/select',
  7655. '/div'
  7656. );
  7657. $this->assertTags($result, $expected);
  7658. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7659. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7660. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7661. $expected = array(
  7662. 'div' => array('class' => 'input select error'),
  7663. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7664. 'Profile',
  7665. '/label',
  7666. 'select' => array(
  7667. 'name' => 'ValidateProfile[1][ValidateItem][2][profile_id]',
  7668. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7669. 'class' => 'form-error'
  7670. ),
  7671. '/select',
  7672. array('div' => array('class' => 'error-message')),
  7673. 'Error',
  7674. '/div',
  7675. '/div'
  7676. );
  7677. $this->assertTags($result, $expected);
  7678. }
  7679. /**
  7680. * test the correct display of multi-record form validation errors.
  7681. *
  7682. * @return void
  7683. */
  7684. public function testMultiRecordFormValidationErrors() {
  7685. $this->markTestIncomplete('Need to revisit once models work again.');
  7686. $this->Form->create('ValidateProfile');
  7687. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7688. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7689. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7690. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7691. $result = $this->Form->error('ValidateProfile.2.city');
  7692. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7693. $result = $this->Form->error('2.city');
  7694. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7695. }
  7696. /**
  7697. * test the correct display of multi-record form validation errors.
  7698. *
  7699. * @return void
  7700. */
  7701. public function testSaveManyRecordFormValidationErrors() {
  7702. $this->markTestIncomplete('Need to revisit once models work again.');
  7703. $this->Form->create('ValidateUser');
  7704. $ValidateUser->validationErrors[0]['ValidateItem']['name'] = array('Error in field name');
  7705. $result = $this->Form->error('0.ValidateUser.ValidateItem.name');
  7706. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7707. $ValidateUser->validationErrors[0]['city'] = array('Error in field city');
  7708. $result = $this->Form->error('ValidateUser.0.city');
  7709. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7710. }
  7711. /**
  7712. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7713. *
  7714. * @return void
  7715. */
  7716. public function testInputTemplate() {
  7717. $this->markTestIncomplete('Need to revisit once models work again.');
  7718. $result = $this->Form->input('Contact.email', array(
  7719. 'type' => 'text', 'format' => array('input')
  7720. ));
  7721. $expected = array(
  7722. 'div' => array('class' => 'input text'),
  7723. 'input' => array(
  7724. 'type' => 'text', 'name' => 'Contact[email]',
  7725. 'id' => 'ContactEmail'
  7726. ),
  7727. '/div'
  7728. );
  7729. $this->assertTags($result, $expected);
  7730. $result = $this->Form->input('Contact.email', array(
  7731. 'type' => 'text', 'format' => array('input', 'label'),
  7732. 'label' => '<em>Email (required)</em>'
  7733. ));
  7734. $expected = array(
  7735. 'div' => array('class' => 'input text'),
  7736. array('input' => array(
  7737. 'type' => 'text', 'name' => 'Contact[email]',
  7738. 'id' => 'ContactEmail'
  7739. )),
  7740. 'label' => array('for' => 'ContactEmail'),
  7741. 'em' => array(),
  7742. 'Email (required)',
  7743. '/em',
  7744. '/label',
  7745. '/div'
  7746. );
  7747. $this->assertTags($result, $expected);
  7748. $result = $this->Form->input('Contact.email', array(
  7749. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7750. 'between' => '<div>Something in the middle</div>',
  7751. 'after' => '<span>Some text at the end</span>'
  7752. ));
  7753. $expected = array(
  7754. 'div' => array('class' => 'input text'),
  7755. array('input' => array(
  7756. 'type' => 'text', 'name' => 'Contact[email]',
  7757. 'id' => 'ContactEmail'
  7758. )),
  7759. array('div' => array()),
  7760. 'Something in the middle',
  7761. '/div',
  7762. 'label' => array('for' => 'ContactEmail'),
  7763. 'Email',
  7764. '/label',
  7765. 'span' => array(),
  7766. 'Some text at the end',
  7767. '/span',
  7768. '/div'
  7769. );
  7770. $this->assertTags($result, $expected);
  7771. $result = $this->Form->input('Contact.method', array(
  7772. 'type' => 'radio',
  7773. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7774. 'between' => 'I am between',
  7775. ));
  7776. $expected = array(
  7777. 'div' => array('class' => 'input radio'),
  7778. 'fieldset' => array(),
  7779. 'legend' => array(),
  7780. 'Method',
  7781. '/legend',
  7782. 'I am between',
  7783. 'input' => array(
  7784. 'type' => 'hidden', 'name' => 'Contact[method]',
  7785. 'value' => '', 'id' => 'ContactMethod_'
  7786. ),
  7787. array('input' => array(
  7788. 'type' => 'radio', 'name' => 'Contact[method]',
  7789. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7790. )),
  7791. array('label' => array('for' => 'ContactMethodEmail')),
  7792. 'Email',
  7793. '/label',
  7794. array('input' => array(
  7795. 'type' => 'radio', 'name' => 'Contact[method]',
  7796. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7797. )),
  7798. array('label' => array('for' => 'ContactMethodPigeon')),
  7799. 'Pigeon',
  7800. '/label',
  7801. '/fieldset',
  7802. '/div',
  7803. );
  7804. $this->assertTags($result, $expected);
  7805. }
  7806. /**
  7807. * test that some html5 inputs + FormHelper::__call() work
  7808. *
  7809. * @return void
  7810. */
  7811. public function testHtml5Inputs() {
  7812. $result = $this->Form->email('User.email');
  7813. $expected = array(
  7814. 'input' => array('type' => 'email', 'name' => 'User[email]')
  7815. );
  7816. $this->assertTags($result, $expected);
  7817. $result = $this->Form->search('User.query');
  7818. $expected = array(
  7819. 'input' => array('type' => 'search', 'name' => 'User[query]')
  7820. );
  7821. $this->assertTags($result, $expected);
  7822. $result = $this->Form->search('User.query', array('value' => 'test'));
  7823. $expected = array(
  7824. 'input' => array('type' => 'search', 'name' => 'User[query]', 'value' => 'test')
  7825. );
  7826. $this->assertTags($result, $expected);
  7827. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7828. $expected = array(
  7829. 'input' => array('type' => 'text', 'name' => 'User[query]', 'value' => 'test')
  7830. );
  7831. $this->assertTags($result, $expected);
  7832. }
  7833. /**
  7834. * Test accessing html5 inputs through input().
  7835. *
  7836. * @return void
  7837. */
  7838. public function testHtml5InputWithInput() {
  7839. $this->markTestIncomplete('Need to revisit once models work again.');
  7840. $result = $this->Form->input('User.website', array(
  7841. 'type' => 'url',
  7842. 'value' => 'http://domain.tld',
  7843. 'div' => false,
  7844. 'label' => false));
  7845. $expected = array(
  7846. 'input' => array('type' => 'url', 'name' => 'User[website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7847. );
  7848. $this->assertTags($result, $expected);
  7849. }
  7850. /**
  7851. * Test errors when field name is missing.
  7852. *
  7853. * @expectedException Cake\Error\Exception
  7854. * @return void
  7855. */
  7856. public function testHtml5InputException() {
  7857. $this->Form->email();
  7858. }
  7859. /**
  7860. * Tests that a model can be loaded from the model names passed in the request object
  7861. *
  7862. * @return void
  7863. */
  7864. public function testIntrospectModelFromRequest() {
  7865. $this->markTestIncomplete('Need to revisit once models work again.');
  7866. $this->loadFixtures('Post');
  7867. App::build(array(
  7868. 'Plugin' => array(CAKE . 'Test/TestApp/Plugin/')
  7869. ));
  7870. Plugin::load('TestPlugin');
  7871. $this->Form->request['models'] = array(
  7872. 'TestPluginPost' => array(
  7873. 'plugin' => 'TestPlugin',
  7874. 'className' => 'TestPluginPost'
  7875. )
  7876. );
  7877. $this->Form->create('TestPluginPost');
  7878. Plugin::unload();
  7879. }
  7880. /**
  7881. * Tests that it is possible to set the validation errors directly in the helper for a field
  7882. *
  7883. * @return void
  7884. */
  7885. public function testCustomValidationErrors() {
  7886. $this->markTestIncomplete('Need to revisit once models work again.');
  7887. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7888. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7889. $this->assertEquals('Badness!', $result);
  7890. }
  7891. /**
  7892. * Tests that the 'on' key validates as expected on create
  7893. *
  7894. * @return void
  7895. */
  7896. public function testRequiredOnCreate() {
  7897. $this->markTestIncomplete('Need to revisit once models work again.');
  7898. $this->Form->create('Contact');
  7899. $result = $this->Form->input('Contact.imrequiredonupdate');
  7900. $expected = array(
  7901. 'div' => array('class' => 'input text'),
  7902. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7903. 'Imrequiredonupdate',
  7904. '/label',
  7905. 'input' => array(
  7906. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7907. 'id' => 'ContactImrequiredonupdate'
  7908. ),
  7909. '/div'
  7910. );
  7911. $this->assertTags($result, $expected);
  7912. $result = $this->Form->input('Contact.imrequiredoncreate');
  7913. $expected = array(
  7914. 'div' => array('class' => 'input text required'),
  7915. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7916. 'Imrequiredoncreate',
  7917. '/label',
  7918. 'input' => array(
  7919. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  7920. 'id' => 'ContactImrequiredoncreate',
  7921. 'required' => 'required'
  7922. ),
  7923. '/div'
  7924. );
  7925. $this->assertTags($result, $expected);
  7926. $result = $this->Form->input('Contact.imrequiredonboth');
  7927. $expected = array(
  7928. 'div' => array('class' => 'input text required'),
  7929. 'label' => array('for' => 'ContactImrequiredonboth'),
  7930. 'Imrequiredonboth',
  7931. '/label',
  7932. 'input' => array(
  7933. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  7934. 'id' => 'ContactImrequiredonboth',
  7935. 'required' => 'required'
  7936. ),
  7937. '/div'
  7938. );
  7939. $this->assertTags($result, $expected);
  7940. $this->Form->inputDefaults(array('required' => false));
  7941. $result = $this->Form->input('Contact.imrequired');
  7942. $expected = array(
  7943. 'div' => array('class' => 'input text'),
  7944. 'label' => array('for' => 'ContactImrequired'),
  7945. 'Imrequired',
  7946. '/label',
  7947. 'input' => array(
  7948. 'type' => 'text', 'name' => 'Contact[imrequired]',
  7949. 'id' => 'ContactImrequired'
  7950. ),
  7951. '/div'
  7952. );
  7953. $this->assertTags($result, $expected);
  7954. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7955. $this->assertTags($result, $expected);
  7956. $result = $this->Form->input('Contact.imrequired', array('required' => true));
  7957. $expected = array(
  7958. 'div' => array('class' => 'input text required'),
  7959. 'label' => array('for' => 'ContactImrequired'),
  7960. 'Imrequired',
  7961. '/label',
  7962. 'input' => array(
  7963. 'required' => 'required', 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7964. 'id' => 'ContactImrequired'
  7965. ),
  7966. '/div'
  7967. );
  7968. $this->assertTags($result, $expected);
  7969. $result = $this->Form->input('Contact.imrequired', array('required' => null));
  7970. $this->assertTags($result, $expected);
  7971. }
  7972. /**
  7973. * Tests that the 'on' key validates as expected on update
  7974. *
  7975. * @return void
  7976. */
  7977. public function testRequiredOnUpdate() {
  7978. $this->markTestIncomplete('Need to revisit once models work again.');
  7979. $this->Form->request->data['Contact']['id'] = 1;
  7980. $this->Form->create('Contact');
  7981. $result = $this->Form->input('Contact.imrequiredonupdate');
  7982. $expected = array(
  7983. 'div' => array('class' => 'input text required'),
  7984. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7985. 'Imrequiredonupdate',
  7986. '/label',
  7987. 'input' => array(
  7988. 'type' => 'text', 'name' => 'Contact[imrequiredonupdate]',
  7989. 'id' => 'ContactImrequiredonupdate',
  7990. 'required' => 'required'
  7991. ),
  7992. '/div'
  7993. );
  7994. $this->assertTags($result, $expected);
  7995. $result = $this->Form->input('Contact.imrequiredoncreate');
  7996. $expected = array(
  7997. 'div' => array('class' => 'input text'),
  7998. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7999. 'Imrequiredoncreate',
  8000. '/label',
  8001. 'input' => array(
  8002. 'type' => 'text', 'name' => 'Contact[imrequiredoncreate]',
  8003. 'id' => 'ContactImrequiredoncreate'
  8004. ),
  8005. '/div'
  8006. );
  8007. $this->assertTags($result, $expected);
  8008. $result = $this->Form->input('Contact.imrequiredonboth');
  8009. $expected = array(
  8010. 'div' => array('class' => 'input text required'),
  8011. 'label' => array('for' => 'ContactImrequiredonboth'),
  8012. 'Imrequiredonboth',
  8013. '/label',
  8014. 'input' => array(
  8015. 'type' => 'text', 'name' => 'Contact[imrequiredonboth]',
  8016. 'id' => 'ContactImrequiredonboth',
  8017. 'required' => 'required'
  8018. ),
  8019. '/div'
  8020. );
  8021. $this->assertTags($result, $expected);
  8022. $result = $this->Form->input('Contact.imrequired');
  8023. $expected = array(
  8024. 'div' => array('class' => 'input text required'),
  8025. 'label' => array('for' => 'ContactImrequired'),
  8026. 'Imrequired',
  8027. '/label',
  8028. 'input' => array(
  8029. 'type' => 'text', 'name' => 'Contact[imrequired]',
  8030. 'id' => 'ContactImrequired',
  8031. 'required' => 'required'
  8032. ),
  8033. '/div'
  8034. );
  8035. $this->assertTags($result, $expected);
  8036. }
  8037. /**
  8038. * Test inputDefaults setter and getter
  8039. *
  8040. * @return void
  8041. */
  8042. public function testInputDefaults() {
  8043. $this->markTestIncomplete('Need to revisit once models work again.');
  8044. $this->Form->create('Contact');
  8045. $this->Form->inputDefaults(array(
  8046. 'label' => false,
  8047. 'div' => array(
  8048. 'style' => 'color: #000;'
  8049. )
  8050. ));
  8051. $result = $this->Form->input('Contact.field1');
  8052. $expected = array(
  8053. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  8054. 'input' => array(
  8055. 'type' => 'text', 'name' => 'Contact[field1]',
  8056. 'id' => 'ContactField1'
  8057. ),
  8058. '/div'
  8059. );
  8060. $this->assertTags($result, $expected);
  8061. $this->Form->inputDefaults(array(
  8062. 'div' => false,
  8063. 'label' => 'Label',
  8064. ));
  8065. $result = $this->Form->input('Contact.field1');
  8066. $expected = array(
  8067. 'label' => array('for' => 'ContactField1'),
  8068. 'Label',
  8069. '/label',
  8070. 'input' => array(
  8071. 'type' => 'text', 'name' => 'Contact[field1]',
  8072. 'id' => 'ContactField1'
  8073. ),
  8074. );
  8075. $this->assertTags($result, $expected);
  8076. $this->Form->inputDefaults(array(
  8077. 'label' => false,
  8078. ), true);
  8079. $result = $this->Form->input('Contact.field1');
  8080. $expected = array(
  8081. 'input' => array(
  8082. 'type' => 'text', 'name' => 'Contact[field1]',
  8083. 'id' => 'ContactField1'
  8084. ),
  8085. );
  8086. $this->assertTags($result, $expected);
  8087. $result = $this->Form->inputDefaults();
  8088. $expected = array(
  8089. 'div' => false,
  8090. 'label' => false,
  8091. );
  8092. $this->assertEquals($expected, $result);
  8093. }
  8094. }