FormHelperTest.php 260 KB

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