FormHelperTest.php 255 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric'),
  102. ),
  103. 'string_required' => 'notEmpty',
  104. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  105. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  106. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  107. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  108. 'imalsonotrequired' => array(
  109. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  110. 'between' => array('rule' => array('between', 5, 30)),
  111. ),
  112. 'imalsonotrequired2' => array(
  113. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  114. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  115. ),
  116. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  117. 'iamrequiredalways' => array(
  118. 'email' => array('rule' => 'email'),
  119. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  120. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  121. ),
  122. );
  123. /**
  124. * schema method
  125. *
  126. * @return void
  127. */
  128. public function setSchema($schema) {
  129. $this->_schema = $schema;
  130. }
  131. /**
  132. * hasAndBelongsToMany property
  133. *
  134. * @var array
  135. */
  136. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  137. /**
  138. * hasAndBelongsToMany property
  139. *
  140. * @var array
  141. */
  142. public $belongsTo = array('User' => array('className' => 'UserForm'));
  143. }
  144. /**
  145. * ContactTagsContact class
  146. *
  147. * @package cake
  148. * @package Cake.Test.Case.View.Helper
  149. */
  150. class ContactTagsContact extends CakeTestModel {
  151. /**
  152. * useTable property
  153. *
  154. * @var bool false
  155. */
  156. public $useTable = false;
  157. /**
  158. * name property
  159. *
  160. * @var string 'Contact'
  161. */
  162. public $name = 'ContactTagsContact';
  163. /**
  164. * Default schema
  165. *
  166. * @var array
  167. */
  168. protected $_schema = array(
  169. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  170. 'contact_tag_id' => array(
  171. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  172. )
  173. );
  174. /**
  175. * schema method
  176. *
  177. * @return void
  178. */
  179. public function setSchema($schema) {
  180. $this->_schema = $schema;
  181. }
  182. }
  183. /**
  184. * ContactNonStandardPk class
  185. *
  186. * @package cake
  187. * @package Cake.Test.Case.View.Helper
  188. */
  189. class ContactNonStandardPk extends Contact {
  190. /**
  191. * primaryKey property
  192. *
  193. * @var string 'pk'
  194. */
  195. public $primaryKey = 'pk';
  196. /**
  197. * name property
  198. *
  199. * @var string 'ContactNonStandardPk'
  200. */
  201. public $name = 'ContactNonStandardPk';
  202. /**
  203. * schema method
  204. *
  205. * @return void
  206. */
  207. public function schema($field = false) {
  208. $this->_schema = parent::schema();
  209. $this->_schema['pk'] = $this->_schema['id'];
  210. unset($this->_schema['id']);
  211. return $this->_schema;
  212. }
  213. }
  214. /**
  215. * ContactTag class
  216. *
  217. * @package cake
  218. * @package Cake.Test.Case.View.Helper
  219. */
  220. class ContactTag extends Model {
  221. /**
  222. * useTable property
  223. *
  224. * @var bool false
  225. */
  226. public $useTable = false;
  227. /**
  228. * schema definition
  229. *
  230. * @var array
  231. */
  232. protected $_schema = array(
  233. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  234. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  235. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  236. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  237. );
  238. }
  239. /**
  240. * UserForm class
  241. *
  242. * @package cake
  243. * @package Cake.Test.Case.View.Helper
  244. */
  245. class UserForm extends CakeTestModel {
  246. /**
  247. * useTable property
  248. *
  249. * @var bool false
  250. */
  251. public $useTable = false;
  252. /**
  253. * primaryKey property
  254. *
  255. * @var string 'id'
  256. */
  257. public $primaryKey = 'id';
  258. /**
  259. * name property
  260. *
  261. * @var string 'UserForm'
  262. */
  263. public $name = 'UserForm';
  264. /**
  265. * hasMany property
  266. *
  267. * @var array
  268. */
  269. public $hasMany = array(
  270. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  271. ));
  272. /**
  273. * schema definition
  274. *
  275. * @var array
  276. */
  277. protected $_schema = array(
  278. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  279. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  280. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  281. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  282. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  283. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  284. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  285. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  286. );
  287. }
  288. /**
  289. * OpenidUrl class
  290. *
  291. * @package cake
  292. * @package Cake.Test.Case.View.Helper
  293. */
  294. class OpenidUrl extends CakeTestModel {
  295. /**
  296. * useTable property
  297. *
  298. * @var bool false
  299. */
  300. public $useTable = false;
  301. /**
  302. * primaryKey property
  303. *
  304. * @var string 'id'
  305. */
  306. public $primaryKey = 'id';
  307. /**
  308. * name property
  309. *
  310. * @var string 'OpenidUrl'
  311. */
  312. public $name = 'OpenidUrl';
  313. /**
  314. * belongsTo property
  315. *
  316. * @var array
  317. */
  318. public $belongsTo = array('UserForm' => array(
  319. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  320. ));
  321. /**
  322. * validate property
  323. *
  324. * @var array
  325. */
  326. public $validate = array('openid_not_registered' => array());
  327. /**
  328. * schema method
  329. *
  330. * @var array
  331. */
  332. protected $_schema = array(
  333. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  334. 'user_form_id' => array(
  335. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  336. ),
  337. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  338. );
  339. /**
  340. * beforeValidate method
  341. *
  342. * @return void
  343. */
  344. public function beforeValidate($options = array()) {
  345. $this->invalidate('openid_not_registered');
  346. return true;
  347. }
  348. }
  349. /**
  350. * ValidateUser class
  351. *
  352. * @package cake
  353. * @package Cake.Test.Case.View.Helper
  354. */
  355. class ValidateUser extends CakeTestModel {
  356. /**
  357. * primaryKey property
  358. *
  359. * @var string 'id'
  360. */
  361. public $primaryKey = 'id';
  362. /**
  363. * useTable property
  364. *
  365. * @var bool false
  366. */
  367. public $useTable = false;
  368. /**
  369. * name property
  370. *
  371. * @var string 'ValidateUser'
  372. */
  373. public $name = 'ValidateUser';
  374. /**
  375. * hasOne property
  376. *
  377. * @var array
  378. */
  379. public $hasOne = array('ValidateProfile' => array(
  380. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * schema method
  384. *
  385. * @var array
  386. */
  387. protected $_schema = array(
  388. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  389. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  390. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  391. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  392. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  393. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  394. );
  395. /**
  396. * beforeValidate method
  397. *
  398. * @return void
  399. */
  400. public function beforeValidate($options = array()) {
  401. $this->invalidate('email');
  402. return false;
  403. }
  404. }
  405. /**
  406. * ValidateProfile class
  407. *
  408. * @package cake
  409. * @package Cake.Test.Case.View.Helper
  410. */
  411. class ValidateProfile extends CakeTestModel {
  412. /**
  413. * primaryKey property
  414. *
  415. * @var string 'id'
  416. */
  417. public $primaryKey = 'id';
  418. /**
  419. * useTable property
  420. *
  421. * @var bool false
  422. */
  423. public $useTable = false;
  424. /**
  425. * schema property
  426. *
  427. * @var array
  428. */
  429. protected $_schema = array(
  430. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  431. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  432. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  433. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  434. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  435. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  436. );
  437. /**
  438. * name property
  439. *
  440. * @var string 'ValidateProfile'
  441. */
  442. public $name = 'ValidateProfile';
  443. /**
  444. * hasOne property
  445. *
  446. * @var array
  447. */
  448. public $hasOne = array('ValidateItem' => array(
  449. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  450. ));
  451. /**
  452. * belongsTo property
  453. *
  454. * @var array
  455. */
  456. public $belongsTo = array('ValidateUser' => array(
  457. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  458. ));
  459. /**
  460. * beforeValidate method
  461. *
  462. * @return void
  463. */
  464. public function beforeValidate($options = array()) {
  465. $this->invalidate('full_name');
  466. $this->invalidate('city');
  467. return false;
  468. }
  469. }
  470. /**
  471. * ValidateItem class
  472. *
  473. * @package cake
  474. * @package Cake.Test.Case.View.Helper
  475. */
  476. class ValidateItem extends CakeTestModel {
  477. /**
  478. * primaryKey property
  479. *
  480. * @var string 'id'
  481. */
  482. public $primaryKey = 'id';
  483. /**
  484. * useTable property
  485. *
  486. * @var bool false
  487. */
  488. public $useTable = false;
  489. /**
  490. * name property
  491. *
  492. * @var string 'ValidateItem'
  493. */
  494. public $name = 'ValidateItem';
  495. /**
  496. * schema property
  497. *
  498. * @var array
  499. */
  500. protected $_schema = array(
  501. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  502. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  503. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  504. 'description' => array(
  505. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  506. ),
  507. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  508. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  509. );
  510. /**
  511. * belongsTo property
  512. *
  513. * @var array
  514. */
  515. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  516. /**
  517. * beforeValidate method
  518. *
  519. * @return void
  520. */
  521. public function beforeValidate($options = array()) {
  522. $this->invalidate('description');
  523. return false;
  524. }
  525. }
  526. /**
  527. * TestMail class
  528. *
  529. * @package cake
  530. * @package Cake.Test.Case.View.Helper
  531. */
  532. class TestMail extends CakeTestModel {
  533. /**
  534. * primaryKey property
  535. *
  536. * @var string 'id'
  537. */
  538. public $primaryKey = 'id';
  539. /**
  540. * useTable property
  541. *
  542. * @var bool false
  543. */
  544. public $useTable = false;
  545. /**
  546. * name property
  547. *
  548. * @var string 'TestMail'
  549. */
  550. public $name = 'TestMail';
  551. }
  552. /**
  553. * FormHelperTest class
  554. *
  555. * @package cake
  556. * @subpackage Cake.Test.Case.View.Helper
  557. * @property FormHelper $Form
  558. */
  559. class FormHelperTest extends CakeTestCase {
  560. /**
  561. * Fixtures to be used
  562. *
  563. * @var array
  564. */
  565. public $fixtures = array('core.post');
  566. /**
  567. * Do not load the fixtures by default
  568. *
  569. * @var boolean
  570. */
  571. public $autoFixtures = false;
  572. /**
  573. * setUp method
  574. *
  575. * @return void
  576. */
  577. public function setUp() {
  578. parent::setUp();
  579. Configure::write('App.base', '');
  580. $this->Controller = new ContactTestController();
  581. $this->View = new View($this->Controller);
  582. $this->Form = new FormHelper($this->View);
  583. $this->Form->Html = new HtmlHelper($this->View);
  584. $this->Form->request = new CakeRequest('contacts/add', false);
  585. $this->Form->request->here = '/contacts/add';
  586. $this->Form->request['action'] = 'add';
  587. $this->Form->request->webroot = '';
  588. $this->Form->request->base = '';
  589. ClassRegistry::addObject('Contact', new Contact());
  590. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  591. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  592. ClassRegistry::addObject('User', new UserForm());
  593. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  594. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  595. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  596. $this->oldSalt = Configure::read('Security.salt');
  597. $this->dateRegex = array(
  598. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  599. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  600. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  601. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  602. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  603. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  604. );
  605. Configure::write('Security.salt', 'foo!');
  606. }
  607. /**
  608. * tearDown method
  609. *
  610. * @return void
  611. */
  612. public function tearDown() {
  613. parent::tearDown();
  614. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  615. Configure::write('Security.salt', $this->oldSalt);
  616. }
  617. /**
  618. * testFormCreateWithSecurity method
  619. *
  620. * Test form->create() with security key.
  621. *
  622. * @return void
  623. */
  624. public function testCreateWithSecurity() {
  625. $this->Form->request['_Token'] = array('key' => 'testKey');
  626. $encoding = strtolower(Configure::read('App.encoding'));
  627. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  628. $expected = array(
  629. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  630. 'div' => array('style' => 'display:none;'),
  631. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  634. )),
  635. '/div'
  636. );
  637. $this->assertTags($result, $expected);
  638. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  639. $expected['form']['id'] = 'MyForm';
  640. $this->assertTags($result, $expected);
  641. }
  642. /**
  643. * test that create() clears the fields property so it starts fresh
  644. *
  645. * @return void
  646. */
  647. public function testCreateClearingFields() {
  648. $this->Form->fields = array('model_id');
  649. $this->Form->create('Contact');
  650. $this->assertEquals(array(), $this->Form->fields);
  651. }
  652. /**
  653. * Tests form hash generation with model-less data
  654. *
  655. * @return void
  656. */
  657. public function testValidateHashNoModel() {
  658. $this->Form->request['_Token'] = array('key' => 'foo');
  659. $result = $this->Form->secure(array('anything'));
  660. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  661. }
  662. /**
  663. * Tests that models with identical field names get resolved properly
  664. *
  665. * @return void
  666. */
  667. public function testDuplicateFieldNameResolution() {
  668. $result = $this->Form->create('ValidateUser');
  669. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  670. $result = $this->Form->input('ValidateItem.name');
  671. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  672. $result = $this->Form->input('ValidateUser.name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. $result = $this->Form->input('ValidateItem.name');
  677. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  678. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  679. $this->assertRegExp('/<textarea/', $result);
  680. $result = $this->Form->input('name');
  681. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  682. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  683. $this->assertRegExp('/type="text"/', $result);
  684. }
  685. /**
  686. * Tests that hidden fields generated for checkboxes don't get locked
  687. *
  688. * @return void
  689. */
  690. public function testNoCheckboxLocking() {
  691. $this->Form->request['_Token'] = array('key' => 'foo');
  692. $this->assertSame(array(), $this->Form->fields);
  693. $this->Form->checkbox('check', array('value' => '1'));
  694. $this->assertSame($this->Form->fields, array('check'));
  695. }
  696. /**
  697. * testFormSecurityFields method
  698. *
  699. * Test generation of secure form hash generation.
  700. *
  701. * @return void
  702. */
  703. public function testFormSecurityFields() {
  704. $key = 'testKey';
  705. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  706. $this->Form->request['_Token'] = array('key' => $key);
  707. $result = $this->Form->secure($fields);
  708. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  709. $expected .= ':' . 'Model.valid';
  710. $expected = array(
  711. 'div' => array('style' => 'display:none;'),
  712. array('input' => array(
  713. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  714. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  715. )),
  716. array('input' => array(
  717. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  718. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  719. )),
  720. '/div'
  721. );
  722. $this->assertTags($result, $expected);
  723. }
  724. /**
  725. * Tests correct generation of number fields for double and float fields
  726. *
  727. * @return void
  728. */
  729. public function testTextFieldGenerationForFloats() {
  730. $model = ClassRegistry::getObject('Contact');
  731. $model->setSchema(array('foo' => array(
  732. 'type' => 'float',
  733. 'null' => false,
  734. 'default' => null,
  735. 'length' => null
  736. )));
  737. $this->Form->create('Contact');
  738. $result = $this->Form->input('foo');
  739. $expected = array(
  740. 'div' => array('class' => 'input number'),
  741. 'label' => array('for' => 'ContactFoo'),
  742. 'Foo',
  743. '/label',
  744. array('input' => array(
  745. 'type' => 'number',
  746. 'name' => 'data[Contact][foo]',
  747. 'id' => 'ContactFoo',
  748. 'step' => 'any'
  749. )),
  750. '/div'
  751. );
  752. $this->assertTags($result, $expected);
  753. $result = $this->Form->input('foo', array('step' => 0.5));
  754. $expected = array(
  755. 'div' => array('class' => 'input number'),
  756. 'label' => array('for' => 'ContactFoo'),
  757. 'Foo',
  758. '/label',
  759. array('input' => array(
  760. 'type' => 'number',
  761. 'name' => 'data[Contact][foo]',
  762. 'id' => 'ContactFoo',
  763. 'step' => '0.5'
  764. )),
  765. '/div'
  766. );
  767. $this->assertTags($result, $expected);
  768. }
  769. /**
  770. * Tests correct generation of number fields for integer fields
  771. *
  772. * @access public
  773. * @return void
  774. */
  775. public function testTextFieldTypeNumberGenerationForIntegers() {
  776. $model = ClassRegistry::getObject('Contact');
  777. $model->setSchema(array('foo' => array(
  778. 'type' => 'integer',
  779. 'null' => false,
  780. 'default' => null,
  781. 'length' => null
  782. )));
  783. $this->Form->create('Contact');
  784. $result = $this->Form->input('foo');
  785. $expected = array(
  786. 'div' => array('class' => 'input number'),
  787. 'label' => array('for' => 'ContactFoo'),
  788. 'Foo',
  789. '/label',
  790. array('input' => array(
  791. 'type' => 'number', 'name' => 'data[Contact][foo]',
  792. 'id' => 'ContactFoo'
  793. )),
  794. '/div'
  795. );
  796. $this->assertTags($result, $expected);
  797. }
  798. /**
  799. * testFormSecurityMultipleFields method
  800. *
  801. * Test secure() with multiple row form. Ensure hash is correct.
  802. *
  803. * @return void
  804. */
  805. public function testFormSecurityMultipleFields() {
  806. $key = 'testKey';
  807. $fields = array(
  808. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  809. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  810. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  811. );
  812. $this->Form->request['_Token'] = array('key' => $key);
  813. $result = $this->Form->secure($fields);
  814. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  815. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  816. $expected = array(
  817. 'div' => array('style' => 'display:none;'),
  818. array('input' => array(
  819. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  820. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  821. )),
  822. array('input' => array(
  823. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  824. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  825. )),
  826. '/div'
  827. );
  828. $this->assertTags($result, $expected);
  829. }
  830. /**
  831. * testFormSecurityMultipleSubmitButtons
  832. *
  833. * test form submit generation and ensure that _Token is only created on end()
  834. *
  835. * @return void
  836. */
  837. public function testFormSecurityMultipleSubmitButtons() {
  838. $key = 'testKey';
  839. $this->Form->request['_Token'] = array('key' => $key);
  840. $this->Form->create('Addresses');
  841. $this->Form->input('Address.title');
  842. $this->Form->input('Address.first_name');
  843. $result = $this->Form->submit('Save', array('name' => 'save'));
  844. $expected = array(
  845. 'div' => array('class' => 'submit'),
  846. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  847. '/div',
  848. );
  849. $this->assertTags($result, $expected);
  850. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  851. $expected = array(
  852. 'div' => array('class' => 'submit'),
  853. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  854. '/div',
  855. );
  856. $this->assertTags($result, $expected);
  857. $result = $this->Form->end(null);
  858. $expected = array(
  859. 'div' => array('style' => 'display:none;'),
  860. array('input' => array(
  861. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  862. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  863. )),
  864. array('input' => array(
  865. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  866. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  867. )),
  868. '/div'
  869. );
  870. $this->assertTags($result, $expected);
  871. }
  872. /**
  873. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  874. *
  875. * @return void
  876. */
  877. public function testSecurityButtonNestedNamed() {
  878. $key = 'testKey';
  879. $this->Form->request['_Token'] = array('key' => $key);
  880. $this->Form->create('Addresses');
  881. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  882. $result = $this->Form->unlockField();
  883. $this->assertEquals(array('Address.button'), $result);
  884. }
  885. /**
  886. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  887. *
  888. * @return void
  889. */
  890. public function testSecuritySubmitNestedNamed() {
  891. $key = 'testKey';
  892. $this->Form->request['_Token'] = array('key' => $key);
  893. $this->Form->create('Addresses');
  894. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  895. $result = $this->Form->unlockField();
  896. $this->assertEquals(array('Address.button'), $result);
  897. }
  898. /**
  899. * Test that the correct fields are unlocked for image submits with no names.
  900. *
  901. * @return void
  902. */
  903. public function testSecuritySubmitImageNoName() {
  904. $key = 'testKey';
  905. $this->Form->request['_Token'] = array('key' => $key);
  906. $this->Form->create('User');
  907. $result = $this->Form->submit('save.png');
  908. $expected = array(
  909. 'div' => array('class' => 'submit'),
  910. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  911. '/div'
  912. );
  913. $this->assertTags($result, $expected);
  914. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  915. }
  916. /**
  917. * Test that the correct fields are unlocked for image submits with names.
  918. *
  919. * @return void
  920. */
  921. public function testSecuritySubmitImageName() {
  922. $key = 'testKey';
  923. $this->Form->request['_Token'] = array('key' => $key);
  924. $this->Form->create('User');
  925. $result = $this->Form->submit('save.png', array('name' => 'test'));
  926. $expected = array(
  927. 'div' => array('class' => 'submit'),
  928. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  929. '/div'
  930. );
  931. $this->assertTags($result, $expected);
  932. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  933. }
  934. /**
  935. * testFormSecurityMultipleInputFields method
  936. *
  937. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  938. *
  939. * @return void
  940. */
  941. public function testFormSecurityMultipleInputFields() {
  942. $key = 'testKey';
  943. $this->Form->request['_Token'] = array('key' => $key);
  944. $this->Form->create('Addresses');
  945. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  946. $this->Form->input('Addresses.0.title');
  947. $this->Form->input('Addresses.0.first_name');
  948. $this->Form->input('Addresses.0.last_name');
  949. $this->Form->input('Addresses.0.address');
  950. $this->Form->input('Addresses.0.city');
  951. $this->Form->input('Addresses.0.phone');
  952. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  953. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  954. $this->Form->input('Addresses.1.title');
  955. $this->Form->input('Addresses.1.first_name');
  956. $this->Form->input('Addresses.1.last_name');
  957. $this->Form->input('Addresses.1.address');
  958. $this->Form->input('Addresses.1.city');
  959. $this->Form->input('Addresses.1.phone');
  960. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  961. $result = $this->Form->secure($this->Form->fields);
  962. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  963. $expected = array(
  964. 'div' => array('style' => 'display:none;'),
  965. array('input' => array(
  966. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  967. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  968. )),
  969. array('input' => array(
  970. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  971. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  972. )),
  973. '/div'
  974. );
  975. $this->assertTags($result, $expected);
  976. }
  977. /**
  978. * Test form security with Model.field.0 style inputs
  979. *
  980. * @return void
  981. */
  982. public function testFormSecurityArrayFields() {
  983. $key = 'testKey';
  984. $this->Form->request->params['_Token']['key'] = $key;
  985. $this->Form->create('Address');
  986. $this->Form->input('Address.primary.1');
  987. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  988. $this->Form->input('Address.secondary.1.0');
  989. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  990. }
  991. /**
  992. * testFormSecurityMultipleInputDisabledFields method
  993. *
  994. * test secure form generation with multiple records and disabled fields.
  995. *
  996. * @return void
  997. */
  998. public function testFormSecurityMultipleInputDisabledFields() {
  999. $key = 'testKey';
  1000. $this->Form->request->params['_Token'] = array(
  1001. 'key' => $key,
  1002. 'unlockedFields' => array('first_name', 'address')
  1003. );
  1004. $this->Form->create();
  1005. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1006. $this->Form->input('Addresses.0.title');
  1007. $this->Form->input('Addresses.0.first_name');
  1008. $this->Form->input('Addresses.0.last_name');
  1009. $this->Form->input('Addresses.0.address');
  1010. $this->Form->input('Addresses.0.city');
  1011. $this->Form->input('Addresses.0.phone');
  1012. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1013. $this->Form->input('Addresses.1.title');
  1014. $this->Form->input('Addresses.1.first_name');
  1015. $this->Form->input('Addresses.1.last_name');
  1016. $this->Form->input('Addresses.1.address');
  1017. $this->Form->input('Addresses.1.city');
  1018. $this->Form->input('Addresses.1.phone');
  1019. $result = $this->Form->secure($this->Form->fields);
  1020. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1021. $expected = array(
  1022. 'div' => array('style' => 'display:none;'),
  1023. array('input' => array(
  1024. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1025. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1026. )),
  1027. array('input' => array(
  1028. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1029. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1030. )),
  1031. '/div'
  1032. );
  1033. $this->assertTags($result, $expected);
  1034. }
  1035. /**
  1036. * testFormSecurityInputDisabledFields method
  1037. *
  1038. * Test single record form with disabled fields.
  1039. *
  1040. * @return void
  1041. */
  1042. public function testFormSecurityInputUnlockedFields() {
  1043. $key = 'testKey';
  1044. $this->Form->request['_Token'] = array(
  1045. 'key' => $key,
  1046. 'unlockedFields' => array('first_name', 'address')
  1047. );
  1048. $this->Form->create();
  1049. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1050. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1051. $this->Form->input('Addresses.title');
  1052. $this->Form->input('Addresses.first_name');
  1053. $this->Form->input('Addresses.last_name');
  1054. $this->Form->input('Addresses.address');
  1055. $this->Form->input('Addresses.city');
  1056. $this->Form->input('Addresses.phone');
  1057. $result = $this->Form->fields;
  1058. $expected = array(
  1059. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1060. 'Addresses.city', 'Addresses.phone'
  1061. );
  1062. $this->assertEquals($expected, $result);
  1063. $result = $this->Form->secure($expected);
  1064. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1065. $expected = array(
  1066. 'div' => array('style' => 'display:none;'),
  1067. array('input' => array(
  1068. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1069. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1070. )),
  1071. array('input' => array(
  1072. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1073. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1074. )),
  1075. '/div'
  1076. );
  1077. $this->assertTags($result, $expected);
  1078. }
  1079. /**
  1080. * test securing inputs with custom name attributes.
  1081. *
  1082. * @return void
  1083. */
  1084. public function testFormSecureWithCustomNameAttribute() {
  1085. $this->Form->request->params['_Token']['key'] = 'testKey';
  1086. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1087. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1088. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1089. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1090. }
  1091. /**
  1092. * testFormSecuredInput method
  1093. *
  1094. * Test generation of entire secure form, assertions made on input() output.
  1095. *
  1096. * @return void
  1097. */
  1098. public function testFormSecuredInput() {
  1099. $this->Form->request['_Token'] = array('key' => 'testKey');
  1100. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1101. $encoding = strtolower(Configure::read('App.encoding'));
  1102. $expected = array(
  1103. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1104. 'div' => array('style' => 'display:none;'),
  1105. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1106. array('input' => array(
  1107. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1108. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1109. )),
  1110. '/div'
  1111. );
  1112. $this->assertTags($result, $expected);
  1113. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1114. $expected = array(
  1115. 'div' => array('class' => 'input text'),
  1116. 'label' => array('for' => 'UserFormPublished'),
  1117. 'Published',
  1118. '/label',
  1119. array('input' => array(
  1120. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1121. 'id' => 'UserFormPublished'
  1122. )),
  1123. '/div'
  1124. );
  1125. $this->assertTags($result, $expected);
  1126. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1127. $expected = array(
  1128. 'div' => array('class' => 'input text'),
  1129. 'label' => array('for' => 'UserFormOther'),
  1130. 'Other',
  1131. '/label',
  1132. array('input' => array(
  1133. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1134. 'id' => 'UserFormOther'
  1135. )),
  1136. '/div'
  1137. );
  1138. $this->assertTags($result, $expected);
  1139. $result = $this->Form->hidden('UserForm.stuff');
  1140. $expected = array('input' => array(
  1141. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1142. 'id' => 'UserFormStuff'
  1143. ));
  1144. $this->assertTags($result, $expected);
  1145. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1146. $expected = array('input' => array(
  1147. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1148. 'value' => '0', 'id' => 'UserFormHidden'
  1149. ));
  1150. $this->assertTags($result, $expected);
  1151. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1152. $expected = array(
  1153. 'div' => array('class' => 'input checkbox'),
  1154. array('input' => array(
  1155. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1156. 'value' => '0', 'id' => 'UserFormSomething_'
  1157. )),
  1158. array('input' => array(
  1159. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1160. 'value' => '1', 'id' => 'UserFormSomething'
  1161. )),
  1162. 'label' => array('for' => 'UserFormSomething'),
  1163. 'Something',
  1164. '/label',
  1165. '/div'
  1166. );
  1167. $this->assertTags($result, $expected);
  1168. $result = $this->Form->fields;
  1169. $expected = array(
  1170. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1171. 'UserForm.hidden' => '0', 'UserForm.something'
  1172. );
  1173. $this->assertEquals($expected, $result);
  1174. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1175. $result = $this->Form->secure($this->Form->fields);
  1176. $expected = array(
  1177. 'div' => array('style' => 'display:none;'),
  1178. array('input' => array(
  1179. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1180. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1181. )),
  1182. array('input' => array(
  1183. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1184. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1185. )),
  1186. '/div'
  1187. );
  1188. $this->assertTags($result, $expected);
  1189. }
  1190. /**
  1191. * Tests that the correct keys are added to the field hash index
  1192. *
  1193. * @return void
  1194. */
  1195. public function testFormSecuredFileInput() {
  1196. $this->Form->request['_Token'] = array('key' => 'testKey');
  1197. $this->assertEquals(array(), $this->Form->fields);
  1198. $result = $this->Form->file('Attachment.file');
  1199. $expected = array(
  1200. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1201. 'Attachment.file.error', 'Attachment.file.size'
  1202. );
  1203. $this->assertEquals($expected, $this->Form->fields);
  1204. }
  1205. /**
  1206. * test that multiple selects keys are added to field hash
  1207. *
  1208. * @return void
  1209. */
  1210. public function testFormSecuredMultipleSelect() {
  1211. $this->Form->request['_Token'] = array('key' => 'testKey');
  1212. $this->assertEquals(array(), $this->Form->fields);
  1213. $options = array('1' => 'one', '2' => 'two');
  1214. $this->Form->select('Model.select', $options);
  1215. $expected = array('Model.select');
  1216. $this->assertEquals($expected, $this->Form->fields);
  1217. $this->Form->fields = array();
  1218. $this->Form->select('Model.select', $options, array('multiple' => true));
  1219. $this->assertEquals($expected, $this->Form->fields);
  1220. }
  1221. /**
  1222. * testFormSecuredRadio method
  1223. *
  1224. * @return void
  1225. */
  1226. public function testFormSecuredRadio() {
  1227. $this->Form->request['_Token'] = array('key' => 'testKey');
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. $options = array('1' => 'option1', '2' => 'option2');
  1230. $this->Form->radio('Test.test', $options);
  1231. $expected = array('Test.test');
  1232. $this->assertEquals($expected, $this->Form->fields);
  1233. }
  1234. /**
  1235. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1236. * hashing.
  1237. *
  1238. * @return void
  1239. */
  1240. public function testFormSecuredAndDisabled() {
  1241. $this->Form->request['_Token'] = array('key' => 'testKey');
  1242. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1243. $this->Form->text('Model.text', array('disabled' => true));
  1244. $this->Form->password('Model.text', array('disabled' => true));
  1245. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1246. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1247. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1248. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1249. $this->Form->month('Model.month', array('disabled' => true));
  1250. $this->Form->day('Model.day', array('disabled' => true));
  1251. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1252. $this->Form->minute('Model.minute', array('disabled' => true));
  1253. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1254. $expected = array(
  1255. 'Model.radio' => ''
  1256. );
  1257. $this->assertEquals($expected, $this->Form->fields);
  1258. }
  1259. /**
  1260. * testDisableSecurityUsingForm method
  1261. *
  1262. * @return void
  1263. */
  1264. public function testDisableSecurityUsingForm() {
  1265. $this->Form->request['_Token'] = array(
  1266. 'key' => 'testKey',
  1267. 'disabledFields' => array()
  1268. );
  1269. $this->Form->create();
  1270. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1271. $this->Form->input('Addresses.title');
  1272. $this->Form->input('Addresses.first_name', array('secure' => false));
  1273. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1274. $this->Form->input('Addresses.zip', array(
  1275. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1276. ));
  1277. $result = $this->Form->fields;
  1278. $expected = array(
  1279. 'Addresses.id' => '123456', 'Addresses.title',
  1280. );
  1281. $this->assertEquals($expected, $result);
  1282. }
  1283. /**
  1284. * test disableField
  1285. *
  1286. * @return void
  1287. */
  1288. public function testUnlockFieldAddsToList() {
  1289. $this->Form->request['_Token'] = array(
  1290. 'key' => 'testKey',
  1291. 'unlockedFields' => array()
  1292. );
  1293. $this->Form->create('Contact');
  1294. $this->Form->unlockField('Contact.name');
  1295. $this->Form->text('Contact.name');
  1296. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1297. $this->assertEquals(array(), $this->Form->fields);
  1298. }
  1299. /**
  1300. * test unlockField removing from fields array.
  1301. *
  1302. * @return void
  1303. */
  1304. public function testUnlockFieldRemovingFromFields() {
  1305. $this->Form->request['_Token'] = array(
  1306. 'key' => 'testKey',
  1307. 'unlockedFields' => array()
  1308. );
  1309. $this->Form->create('Contact');
  1310. $this->Form->hidden('Contact.id', array('value' => 1));
  1311. $this->Form->text('Contact.name');
  1312. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1313. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1314. $this->Form->unlockField('Contact.name');
  1315. $this->Form->unlockField('Contact.id');
  1316. $this->assertEquals(array(), $this->Form->fields);
  1317. }
  1318. /**
  1319. * testTagIsInvalid method
  1320. *
  1321. * @return void
  1322. */
  1323. public function testTagIsInvalid() {
  1324. $Contact = ClassRegistry::getObject('Contact');
  1325. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1326. $this->Form->setEntity('Contact.0.email');
  1327. $result = $this->Form->tagIsInvalid();
  1328. $expected = array('Please provide an email');
  1329. $this->assertEquals($expected, $result);
  1330. $this->Form->setEntity('Contact.1.email');
  1331. $result = $this->Form->tagIsInvalid();
  1332. $expected = false;
  1333. $this->assertSame($expected, $result);
  1334. $this->Form->setEntity('Contact.0.name');
  1335. $result = $this->Form->tagIsInvalid();
  1336. $expected = false;
  1337. $this->assertSame($expected, $result);
  1338. }
  1339. /**
  1340. * testPasswordValidation method
  1341. *
  1342. * test validation errors on password input.
  1343. *
  1344. * @return void
  1345. */
  1346. public function testPasswordValidation() {
  1347. $Contact = ClassRegistry::getObject('Contact');
  1348. $Contact->validationErrors['password'] = array('Please provide a password');
  1349. $result = $this->Form->input('Contact.password');
  1350. $expected = array(
  1351. 'div' => array('class' => 'input password error'),
  1352. 'label' => array('for' => 'ContactPassword'),
  1353. 'Password',
  1354. '/label',
  1355. 'input' => array(
  1356. 'type' => 'password', 'name' => 'data[Contact][password]',
  1357. 'id' => 'ContactPassword', 'class' => 'form-error'
  1358. ),
  1359. array('div' => array('class' => 'error-message')),
  1360. 'Please provide a password',
  1361. '/div',
  1362. '/div'
  1363. );
  1364. $this->assertTags($result, $expected);
  1365. }
  1366. /**
  1367. * testEmptyErrorValidation method
  1368. *
  1369. * test validation error div when validation message is an empty string
  1370. *
  1371. * @access public
  1372. * @return void
  1373. */
  1374. public function testEmptyErrorValidation() {
  1375. $this->Form->validationErrors['Contact']['password'] = '';
  1376. $result = $this->Form->input('Contact.password');
  1377. $expected = array(
  1378. 'div' => array('class' => 'input password error'),
  1379. 'label' => array('for' => 'ContactPassword'),
  1380. 'Password',
  1381. '/label',
  1382. 'input' => array(
  1383. 'type' => 'password', 'name' => 'data[Contact][password]',
  1384. 'id' => 'ContactPassword', 'class' => 'form-error'
  1385. ),
  1386. array('div' => array('class' => 'error-message')),
  1387. array(),
  1388. '/div',
  1389. '/div'
  1390. );
  1391. $this->assertTags($result, $expected);
  1392. }
  1393. /**
  1394. * testEmptyInputErrorValidation method
  1395. *
  1396. * test validation error div when validation message is overridden by an empty string when calling input()
  1397. *
  1398. * @access public
  1399. * @return void
  1400. */
  1401. public function testEmptyInputErrorValidation() {
  1402. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1403. $result = $this->Form->input('Contact.password', array('error' => ''));
  1404. $expected = array(
  1405. 'div' => array('class' => 'input password error'),
  1406. 'label' => array('for' => 'ContactPassword'),
  1407. 'Password',
  1408. '/label',
  1409. 'input' => array(
  1410. 'type' => 'password', 'name' => 'data[Contact][password]',
  1411. 'id' => 'ContactPassword', 'class' => 'form-error'
  1412. ),
  1413. array('div' => array('class' => 'error-message')),
  1414. array(),
  1415. '/div',
  1416. '/div'
  1417. );
  1418. $this->assertTags($result, $expected);
  1419. }
  1420. /**
  1421. * testFormValidationAssociated method
  1422. *
  1423. * test display of form errors in conjunction with model::validates.
  1424. *
  1425. * @return void
  1426. */
  1427. public function testFormValidationAssociated() {
  1428. $this->UserForm = ClassRegistry::getObject('UserForm');
  1429. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1430. $data = array(
  1431. 'UserForm' => array('name' => 'user'),
  1432. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1433. );
  1434. $result = $this->UserForm->OpenidUrl->create($data);
  1435. $this->assertFalse(empty($result));
  1436. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1437. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1438. $encoding = strtolower(Configure::read('App.encoding'));
  1439. $expected = array(
  1440. 'form' => array(
  1441. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1442. 'accept-charset' => $encoding
  1443. ),
  1444. 'div' => array('style' => 'display:none;'),
  1445. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1446. '/div'
  1447. );
  1448. $this->assertTags($result, $expected);
  1449. $result = $this->Form->error(
  1450. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1451. );
  1452. $this->assertEquals('Error, not registered', $result);
  1453. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1454. }
  1455. /**
  1456. * testFormValidationAssociatedFirstLevel method
  1457. *
  1458. * test form error display with associated model.
  1459. *
  1460. * @return void
  1461. */
  1462. public function testFormValidationAssociatedFirstLevel() {
  1463. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1464. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1465. $data = array(
  1466. 'ValidateUser' => array('name' => 'mariano'),
  1467. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1468. );
  1469. $result = $this->ValidateUser->create($data);
  1470. $this->assertFalse(empty($result));
  1471. $this->assertFalse($this->ValidateUser->validates());
  1472. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1473. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1474. $encoding = strtolower(Configure::read('App.encoding'));
  1475. $expected = array(
  1476. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1477. 'div' => array('style' => 'display:none;'),
  1478. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1479. '/div'
  1480. );
  1481. $this->assertTags($result, $expected);
  1482. $result = $this->Form->error(
  1483. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1484. );
  1485. $this->assertEquals('Invalid email', $result);
  1486. $result = $this->Form->error(
  1487. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1488. );
  1489. $this->assertEquals('Invalid name', $result);
  1490. $result = $this->Form->error(
  1491. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1492. );
  1493. $this->assertEquals('Invalid city', $result);
  1494. unset($this->ValidateUser->ValidateProfile);
  1495. unset($this->ValidateUser);
  1496. }
  1497. /**
  1498. * testFormValidationAssociatedSecondLevel method
  1499. *
  1500. * test form error display with associated model.
  1501. *
  1502. * @return void
  1503. */
  1504. public function testFormValidationAssociatedSecondLevel() {
  1505. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1506. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1507. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1508. $data = array(
  1509. 'ValidateUser' => array('name' => 'mariano'),
  1510. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1511. 'ValidateItem' => array('name' => 'Item')
  1512. );
  1513. $result = $this->ValidateUser->create($data);
  1514. $this->assertFalse(empty($result));
  1515. $this->assertFalse($this->ValidateUser->validates());
  1516. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1517. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1518. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1519. $encoding = strtolower(Configure::read('App.encoding'));
  1520. $expected = array(
  1521. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1522. 'div' => array('style' => 'display:none;'),
  1523. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1524. '/div'
  1525. );
  1526. $this->assertTags($result, $expected);
  1527. $result = $this->Form->error(
  1528. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1529. );
  1530. $this->assertEquals('Invalid email', $result);
  1531. $result = $this->Form->error(
  1532. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1533. );
  1534. $this->assertEquals('Invalid name', $result);
  1535. $result = $this->Form->error(
  1536. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1537. );
  1538. $result = $this->Form->error(
  1539. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1540. );
  1541. $this->assertEquals('Invalid description', $result);
  1542. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1543. unset($this->ValidateUser->ValidateProfile);
  1544. unset($this->ValidateUser);
  1545. }
  1546. /**
  1547. * testFormValidationMultiRecord method
  1548. *
  1549. * test form error display with multiple records.
  1550. *
  1551. * @return void
  1552. */
  1553. public function testFormValidationMultiRecord() {
  1554. $Contact = ClassRegistry::getObject('Contact');
  1555. $Contact->validationErrors[2] = array(
  1556. 'name' => array('This field cannot be left blank')
  1557. );
  1558. $result = $this->Form->input('Contact.2.name');
  1559. $expected = array(
  1560. 'div' => array('class' => 'input text error'),
  1561. 'label' => array('for' => 'Contact2Name'),
  1562. 'Name',
  1563. '/label',
  1564. 'input' => array(
  1565. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1566. 'class' => 'form-error', 'maxlength' => 255
  1567. ),
  1568. array('div' => array('class' => 'error-message')),
  1569. 'This field cannot be left blank',
  1570. '/div',
  1571. '/div'
  1572. );
  1573. $this->assertTags($result, $expected);
  1574. }
  1575. /**
  1576. * testMultipleInputValidation method
  1577. *
  1578. * test multiple record form validation error display.
  1579. *
  1580. * @return void
  1581. */
  1582. public function testMultipleInputValidation() {
  1583. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1584. $Address->validationErrors[0] = array(
  1585. 'title' => array('This field cannot be empty'),
  1586. 'first_name' => array('This field cannot be empty')
  1587. );
  1588. $Address->validationErrors[1] = array(
  1589. 'last_name' => array('You must have a last name')
  1590. );
  1591. $this->Form->create();
  1592. $result = $this->Form->input('Address.0.title');
  1593. $expected = array(
  1594. 'div' => array('class'),
  1595. 'label' => array('for'),
  1596. 'preg:/[^<]+/',
  1597. '/label',
  1598. 'input' => array(
  1599. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1600. ),
  1601. array('div' => array('class' => 'error-message')),
  1602. 'This field cannot be empty',
  1603. '/div',
  1604. '/div'
  1605. );
  1606. $this->assertTags($result, $expected);
  1607. $result = $this->Form->input('Address.0.first_name');
  1608. $expected = array(
  1609. 'div' => array('class'),
  1610. 'label' => array('for'),
  1611. 'preg:/[^<]+/',
  1612. '/label',
  1613. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1614. array('div' => array('class' => 'error-message')),
  1615. 'This field cannot be empty',
  1616. '/div',
  1617. '/div'
  1618. );
  1619. $this->assertTags($result, $expected);
  1620. $result = $this->Form->input('Address.0.last_name');
  1621. $expected = array(
  1622. 'div' => array('class'),
  1623. 'label' => array('for'),
  1624. 'preg:/[^<]+/',
  1625. '/label',
  1626. 'input' => array('type' => 'text', 'name', 'id'),
  1627. '/div'
  1628. );
  1629. $this->assertTags($result, $expected);
  1630. $result = $this->Form->input('Address.1.last_name');
  1631. $expected = array(
  1632. 'div' => array('class'),
  1633. 'label' => array('for'),
  1634. 'preg:/[^<]+/',
  1635. '/label',
  1636. 'input' => array(
  1637. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1638. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1639. ),
  1640. array('div' => array('class' => 'error-message')),
  1641. 'You must have a last name',
  1642. '/div',
  1643. '/div'
  1644. );
  1645. $this->assertTags($result, $expected);
  1646. }
  1647. /**
  1648. * testInput method
  1649. *
  1650. * Test various incarnations of input().
  1651. *
  1652. * @return void
  1653. */
  1654. public function testInput() {
  1655. $result = $this->Form->input('ValidateUser.balance');
  1656. $expected = array(
  1657. 'div' => array('class'),
  1658. 'label' => array('for'),
  1659. 'Balance',
  1660. '/label',
  1661. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1662. '/div',
  1663. );
  1664. $this->assertTags($result, $expected);
  1665. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1666. $expected = array(
  1667. 'div' => array('class' => 'input text'),
  1668. 'label' => array('for' => 'custom'),
  1669. 'Email',
  1670. '/label',
  1671. array('input' => array(
  1672. 'type' => 'text', 'name' => 'data[Contact][email]',
  1673. 'id' => 'custom', 'maxlength' => 255
  1674. )),
  1675. '/div'
  1676. );
  1677. $this->assertTags($result, $expected);
  1678. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1679. $expected = array(
  1680. '<div',
  1681. 'label' => array('for' => 'ContactEmail'),
  1682. 'Email',
  1683. '/label',
  1684. array('input' => array(
  1685. 'type' => 'text', 'name' => 'data[Contact][email]',
  1686. 'id' => 'ContactEmail', 'maxlength' => 255
  1687. )),
  1688. '/div'
  1689. );
  1690. $this->assertTags($result, $expected);
  1691. $result = $this->Form->hidden('Contact.idontexist');
  1692. $expected = array('input' => array(
  1693. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1694. 'id' => 'ContactIdontexist'
  1695. ));
  1696. $this->assertTags($result, $expected);
  1697. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1698. $expected = array(
  1699. 'div' => array('class' => 'input text'),
  1700. 'label' => array('for' => 'ContactEmail'),
  1701. 'Email',
  1702. '/label',
  1703. array('input' => array(
  1704. 'type' => 'text', 'name' => 'data[Contact][email]',
  1705. 'id' => 'ContactEmail'
  1706. )),
  1707. '/div'
  1708. );
  1709. $this->assertTags($result, $expected);
  1710. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1711. $expected = array(
  1712. 'div' => array('class' => 'input text'),
  1713. 'label' => array('for' => 'Contact5Email'),
  1714. 'Email',
  1715. '/label',
  1716. array('input' => array(
  1717. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1718. 'id' => 'Contact5Email'
  1719. )),
  1720. '/div'
  1721. );
  1722. $this->assertTags($result, $expected);
  1723. $result = $this->Form->input('Contact.password');
  1724. $expected = array(
  1725. 'div' => array('class' => 'input password'),
  1726. 'label' => array('for' => 'ContactPassword'),
  1727. 'Password',
  1728. '/label',
  1729. array('input' => array(
  1730. 'type' => 'password', 'name' => 'data[Contact][password]',
  1731. 'id' => 'ContactPassword'
  1732. )),
  1733. '/div'
  1734. );
  1735. $this->assertTags($result, $expected);
  1736. $result = $this->Form->input('Contact.email', array(
  1737. 'type' => 'file', 'class' => 'textbox'
  1738. ));
  1739. $expected = array(
  1740. 'div' => array('class' => 'input file'),
  1741. 'label' => array('for' => 'ContactEmail'),
  1742. 'Email',
  1743. '/label',
  1744. array('input' => array(
  1745. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1746. 'id' => 'ContactEmail'
  1747. )),
  1748. '/div'
  1749. );
  1750. $this->assertTags($result, $expected);
  1751. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1752. $result = $this->Form->input('Contact.phone');
  1753. $expected = array(
  1754. 'div' => array('class' => 'input text'),
  1755. 'label' => array('for' => 'ContactPhone'),
  1756. 'Phone',
  1757. '/label',
  1758. array('input' => array(
  1759. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1760. 'value' => 'Hello &amp; World &gt; weird chars',
  1761. 'id' => 'ContactPhone', 'maxlength' => 255
  1762. )),
  1763. '/div'
  1764. );
  1765. $this->assertTags($result, $expected);
  1766. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1767. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1768. $expected = array(
  1769. 'div' => array('class' => 'input text'),
  1770. 'label' => array('for' => 'myId'),
  1771. 'Field',
  1772. '/label',
  1773. 'input' => array(
  1774. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1775. 'value' => 'My value', 'id' => 'myId'
  1776. ),
  1777. '/div'
  1778. );
  1779. $this->assertTags($result, $expected);
  1780. unset($this->Form->request->data);
  1781. $Contact = ClassRegistry::getObject('Contact');
  1782. $Contact->validationErrors['field'] = array('Badness!');
  1783. $result = $this->Form->input('Contact.field');
  1784. $expected = array(
  1785. 'div' => array('class' => 'input text error'),
  1786. 'label' => array('for' => 'ContactField'),
  1787. 'Field',
  1788. '/label',
  1789. 'input' => array(
  1790. 'type' => 'text', 'name' => 'data[Contact][field]',
  1791. 'id' => 'ContactField', 'class' => 'form-error'
  1792. ),
  1793. array('div' => array('class' => 'error-message')),
  1794. 'Badness!',
  1795. '/div',
  1796. '/div'
  1797. );
  1798. $this->assertTags($result, $expected);
  1799. $result = $this->Form->input('Contact.field', array(
  1800. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1801. ));
  1802. $expected = array(
  1803. 'label' => array('for' => 'ContactField'),
  1804. 'Field',
  1805. '/label',
  1806. 'input' => array(
  1807. 'type' => 'text', 'name' => 'data[Contact][field]',
  1808. 'id' => 'ContactField', 'class' => 'form-error'
  1809. ),
  1810. array('span' => array('class' => 'error-message')),
  1811. 'Badness!',
  1812. '/span'
  1813. );
  1814. $this->assertTags($result, $expected);
  1815. $result = $this->Form->input('Contact.field', array(
  1816. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1817. ));
  1818. $expected = array(
  1819. 'div' => array('class' => 'input text error'),
  1820. 'label' => array('for' => 'ContactField'),
  1821. 'Field',
  1822. '/label',
  1823. 'input' => array(
  1824. 'type' => 'text', 'name' => 'data[Contact][field]',
  1825. 'id' => 'ContactField', 'class' => 'form-error'
  1826. ),
  1827. array('div' => array('class' => 'error')),
  1828. 'Badness!',
  1829. '/div'
  1830. );
  1831. $this->assertTags($result, $expected);
  1832. $result = $this->Form->input('Contact.field', array(
  1833. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1834. ));
  1835. $expected = array(
  1836. 'span' => array('class' => 'input text error'),
  1837. 'label' => array('for' => 'ContactField'),
  1838. 'Field',
  1839. '/label',
  1840. 'input' => array(
  1841. 'type' => 'text', 'name' => 'data[Contact][field]',
  1842. 'id' => 'ContactField', 'class' => 'form-error'
  1843. ),
  1844. 'Badness!',
  1845. '/span'
  1846. );
  1847. $this->assertTags($result, $expected);
  1848. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1849. $expected = array(
  1850. 'div' => array('class' => 'input text error'),
  1851. 'label' => array('for' => 'ContactField'),
  1852. 'Field',
  1853. '/label',
  1854. 'input' => array(
  1855. 'type' => 'text', 'name' => 'data[Contact][field]',
  1856. 'id' => 'ContactField', 'class' => 'form-error'
  1857. ),
  1858. 'A message to you, Rudy',
  1859. array('div' => array('class' => 'error-message')),
  1860. 'Badness!',
  1861. '/div',
  1862. '/div'
  1863. );
  1864. $this->assertTags($result, $expected);
  1865. $this->Form->setEntity(null);
  1866. $this->Form->setEntity('Contact.field');
  1867. $result = $this->Form->input('Contact.field', array(
  1868. 'after' => 'A message to you, Rudy', 'error' => false
  1869. ));
  1870. $expected = array(
  1871. 'div' => array('class' => 'input text'),
  1872. 'label' => array('for' => 'ContactField'),
  1873. 'Field',
  1874. '/label',
  1875. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1876. 'A message to you, Rudy',
  1877. '/div'
  1878. );
  1879. $this->assertTags($result, $expected);
  1880. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1881. $expected = array(
  1882. 'div' => array('class' => 'input text'),
  1883. 'label' => array('for' => 'ObjectField'),
  1884. 'Field',
  1885. '/label',
  1886. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1887. 'A message to you, Rudy',
  1888. '/div'
  1889. );
  1890. $this->assertTags($result, $expected);
  1891. $Contact->validationErrors['field'] = array('minLength');
  1892. $result = $this->Form->input('Contact.field', array(
  1893. 'error' => array(
  1894. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1895. 'maxLength' => 'login too large'
  1896. )
  1897. ));
  1898. $expected = array(
  1899. 'div' => array('class' => 'input text error'),
  1900. 'label' => array('for' => 'ContactField'),
  1901. 'Field',
  1902. '/label',
  1903. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1904. array('div' => array('class' => 'error-message')),
  1905. 'Le login doit contenir au moins 2 caractères',
  1906. '/div',
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. $Contact->validationErrors['field'] = array('maxLength');
  1911. $result = $this->Form->input('Contact.field', array(
  1912. 'error' => array(
  1913. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1914. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1915. 'maxLength' => 'login too large',
  1916. )
  1917. ));
  1918. $expected = array(
  1919. 'div' => array('class' => 'input text error'),
  1920. 'label' => array('for' => 'ContactField'),
  1921. 'Field',
  1922. '/label',
  1923. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1924. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1925. 'login too large',
  1926. '/span',
  1927. '/div'
  1928. );
  1929. $this->assertTags($result, $expected);
  1930. }
  1931. /**
  1932. * Test that inputs with 0 can be created.
  1933. *
  1934. * @return void
  1935. */
  1936. public function testInputZero() {
  1937. $this->Form->create('User');
  1938. $result = $this->Form->input('0');
  1939. $expected = array(
  1940. 'div' => array('class' => 'input text'),
  1941. 'label' => array('for' => 'User0'), '/label',
  1942. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1943. '/div'
  1944. );
  1945. $this->assertTags($result, $expected);
  1946. }
  1947. /**
  1948. * test input() with checkbox creation
  1949. *
  1950. * @return void
  1951. */
  1952. public function testInputCheckbox() {
  1953. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1954. $expected = array(
  1955. 'div' => array('class' => 'input checkbox'),
  1956. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1957. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1958. '/div'
  1959. );
  1960. $this->assertTags($result, $expected);
  1961. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1962. $expected = array(
  1963. 'div' => array('class' => 'input checkbox'),
  1964. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1965. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1966. '/div'
  1967. );
  1968. $this->assertTags($result, $expected);
  1969. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1970. $expected = array(
  1971. 'div' => array('class' => 'input checkbox'),
  1972. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1973. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1974. '/div'
  1975. );
  1976. $this->assertTags($result, $expected);
  1977. }
  1978. /**
  1979. * test form->input() with time types.
  1980. *
  1981. */
  1982. public function testInputTime() {
  1983. extract($this->dateRegex);
  1984. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1985. $result = explode(':', $result);
  1986. $this->assertRegExp('/option value="23"/', $result[0]);
  1987. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1988. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1989. $result = explode(':', $result);
  1990. $this->assertRegExp('/option value="23"/', $result[0]);
  1991. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1992. $result = $this->Form->input('Model.field', array(
  1993. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1994. ));
  1995. $result = explode(':', $result);
  1996. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1997. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1998. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1999. $result = $this->Form->input('Model.field', array(
  2000. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  2001. ));
  2002. $result = explode(':', $result);
  2003. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2004. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2005. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2006. $result = $this->Form->input('prueba', array(
  2007. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2008. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2009. ));
  2010. $result = explode(':', $result);
  2011. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2012. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2013. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2014. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2015. $result = $this->Form->input('Random.start_time', array(
  2016. 'type' => 'time',
  2017. 'selected' => '18:15'
  2018. ));
  2019. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  2020. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  2021. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2022. }
  2023. /**
  2024. * Test interval + selected near the hour roll over.
  2025. *
  2026. * @return void
  2027. */
  2028. public function testTimeSelectedWithInterval() {
  2029. $result = $this->Form->input('Model.start_time', array(
  2030. 'type' => 'time',
  2031. 'interval' => 15,
  2032. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  2033. ));
  2034. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2035. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2036. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2037. $result = $this->Form->input('Model.start_time', array(
  2038. 'type' => 'time',
  2039. 'interval' => 15,
  2040. 'selected' => '2012-10-23 15:57:00'
  2041. ));
  2042. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  2043. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2044. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  2045. $result = $this->Form->input('Model.start_time', array(
  2046. 'timeFormat' => 24,
  2047. 'type' => 'time',
  2048. 'interval' => 15,
  2049. 'selected' => '15:57'
  2050. ));
  2051. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2052. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2053. $result = $this->Form->input('Model.start_time', array(
  2054. 'timeFormat' => 24,
  2055. 'type' => 'time',
  2056. 'interval' => 15,
  2057. 'selected' => '23:57'
  2058. ));
  2059. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2060. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2061. $result = $this->Form->input('Model.created', array(
  2062. 'timeFormat' => 24,
  2063. 'type' => 'datetime',
  2064. 'interval' => 15,
  2065. 'selected' => '2012-09-30 23:56'
  2066. ));
  2067. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2068. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2069. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2070. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2071. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2072. }
  2073. /**
  2074. * test form->input() with datetime, date and time types
  2075. *
  2076. * @return void
  2077. */
  2078. public function testInputDatetime() {
  2079. extract($this->dateRegex);
  2080. $result = $this->Form->input('prueba', array(
  2081. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2082. 'maxYear' => date('Y') + 1, 'interval' => 15
  2083. ));
  2084. $result = explode(':', $result);
  2085. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2086. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2087. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2088. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2089. //related to ticket #5013
  2090. $result = $this->Form->input('Contact.date', array(
  2091. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2092. ));
  2093. $this->assertRegExp('/class="customClass"/', $result);
  2094. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2095. $result = $this->Form->input('Contact.date', array(
  2096. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2097. ));
  2098. $this->assertRegExp('/id="customIdDay"/', $result);
  2099. $this->assertRegExp('/id="customIdMonth"/', $result);
  2100. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2101. $result = $this->Form->input('Model.field', array(
  2102. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2103. ));
  2104. $this->assertRegExp('/id="customIDDay"/', $result);
  2105. $this->assertRegExp('/id="customIDHour"/', $result);
  2106. $result = explode('</select><select', $result);
  2107. $result = explode(':', $result[1]);
  2108. $this->assertRegExp('/option value="23"/', $result[0]);
  2109. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2110. $result = $this->Form->input('Model.field', array(
  2111. 'type' => 'datetime', 'timeFormat' => 12
  2112. ));
  2113. $result = explode('</select><select', $result);
  2114. $result = explode(':', $result[1]);
  2115. $this->assertRegExp('/option value="12"/', $result[0]);
  2116. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2117. $this->Form->request->data = array('Contact' => array('created' => null));
  2118. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2119. $expected = array(
  2120. 'div' => array('class' => 'input date'),
  2121. 'label' => array('for' => 'ContactCreatedMonth'),
  2122. 'Created',
  2123. '/label',
  2124. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2125. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2126. $monthsRegex,
  2127. '/select', '-',
  2128. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2129. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2130. $daysRegex,
  2131. '/select', '-',
  2132. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2133. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2134. $yearsRegex,
  2135. '/select',
  2136. '/div'
  2137. );
  2138. $this->assertTags($result, $expected);
  2139. $this->Form->request->data = array('Contact' => array('created' => null));
  2140. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2141. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2142. $this->Form->request->data = array('Contact' => array('created' => null));
  2143. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2144. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2145. $result = $this->Form->input('Contact.created', array(
  2146. 'type' => 'date',
  2147. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2148. 'timeFormat' => 'NONE'
  2149. ));
  2150. $this->assertRegExp('/for\="created-month"/', $result);
  2151. }
  2152. /**
  2153. * Test generating checkboxes in a loop.
  2154. *
  2155. * @return void
  2156. */
  2157. public function testInputCheckboxesInLoop() {
  2158. for ($i = 1; $i < 5; $i++) {
  2159. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2160. $expected = array(
  2161. 'div' => array('class' => 'input checkbox'),
  2162. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2163. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2164. 'label' => array('for' => "Contact{$i}Email"),
  2165. 'Email',
  2166. '/label',
  2167. '/div'
  2168. );
  2169. $this->assertTags($result, $expected);
  2170. }
  2171. }
  2172. /**
  2173. * test input name with leading integer, ensure attributes are generated correctly.
  2174. *
  2175. * @return void
  2176. */
  2177. public function testInputWithLeadingInteger() {
  2178. $result = $this->Form->text('0.Node.title');
  2179. $expected = array(
  2180. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2181. );
  2182. $this->assertTags($result, $expected);
  2183. }
  2184. /**
  2185. * test form->input() with select type inputs.
  2186. *
  2187. * @return void
  2188. */
  2189. public function testInputSelectType() {
  2190. $result = $this->Form->input('email', array(
  2191. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2192. );
  2193. $expected = array(
  2194. 'div' => array('class' => 'input select'),
  2195. 'label' => array('for' => 'email'),
  2196. 'Email',
  2197. '/label',
  2198. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2199. array('option' => array('value' => '')),
  2200. '/option',
  2201. array('option' => array('value' => 'è')),
  2202. 'Firést',
  2203. '/option',
  2204. array('option' => array('value' => 'é')),
  2205. 'Secoènd',
  2206. '/option',
  2207. '/select',
  2208. '/div'
  2209. );
  2210. $this->assertTags($result, $expected);
  2211. $result = $this->Form->input('email', array(
  2212. 'options' => array('First', 'Second'), 'empty' => true)
  2213. );
  2214. $expected = array(
  2215. 'div' => array('class' => 'input select'),
  2216. 'label' => array('for' => 'email'),
  2217. 'Email',
  2218. '/label',
  2219. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2220. array('option' => array('value' => '')),
  2221. '/option',
  2222. array('option' => array('value' => '0')),
  2223. 'First',
  2224. '/option',
  2225. array('option' => array('value' => '1')),
  2226. 'Second',
  2227. '/option',
  2228. '/select',
  2229. '/div'
  2230. );
  2231. $this->assertTags($result, $expected);
  2232. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2233. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2234. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2235. $expected = array(
  2236. 'div' => array('class' => 'input select'),
  2237. 'label' => array('for' => 'ModelUserId'),
  2238. 'User',
  2239. '/label',
  2240. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2241. array('option' => array('value' => '')),
  2242. '/option',
  2243. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2244. 'good',
  2245. '/option',
  2246. array('option' => array('value' => 'other')),
  2247. 'bad',
  2248. '/option',
  2249. '/select',
  2250. '/div'
  2251. );
  2252. $this->assertTags($result, $expected);
  2253. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2254. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2255. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2256. $expected = array(
  2257. 'div' => array('class' => 'input select'),
  2258. 'label' => array('for' => 'ThingUserId'),
  2259. 'User',
  2260. '/label',
  2261. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2262. array('option' => array('value' => '')),
  2263. 'Some Empty',
  2264. '/option',
  2265. array('option' => array('value' => 'value')),
  2266. 'good',
  2267. '/option',
  2268. array('option' => array('value' => 'other')),
  2269. 'bad',
  2270. '/option',
  2271. '/select',
  2272. '/div'
  2273. );
  2274. $this->assertTags($result, $expected);
  2275. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2276. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2277. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2278. $expected = array(
  2279. 'div' => array('class' => 'input select'),
  2280. 'label' => array('for' => 'ThingUserId'),
  2281. 'User',
  2282. '/label',
  2283. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2284. array('option' => array('value' => '')),
  2285. 'Some Empty',
  2286. '/option',
  2287. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2288. 'good',
  2289. '/option',
  2290. array('option' => array('value' => 'other')),
  2291. 'bad',
  2292. '/option',
  2293. '/select',
  2294. '/div'
  2295. );
  2296. $this->assertTags($result, $expected);
  2297. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2298. $this->Form->request->data = array('User' => array('User' => array('value')));
  2299. $result = $this->Form->input('User.User', array('empty' => true));
  2300. $expected = array(
  2301. 'div' => array('class' => 'input select'),
  2302. 'label' => array('for' => 'UserUser'),
  2303. 'User',
  2304. '/label',
  2305. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2306. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2307. array('option' => array('value' => '')),
  2308. '/option',
  2309. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2310. 'good',
  2311. '/option',
  2312. array('option' => array('value' => 'other')),
  2313. 'bad',
  2314. '/option',
  2315. '/select',
  2316. '/div'
  2317. );
  2318. $this->assertTags($result, $expected);
  2319. $this->Form->data = array();
  2320. $result = $this->Form->input('Publisher.id', array(
  2321. 'label' => 'Publisher',
  2322. 'type' => 'select',
  2323. 'multiple' => 'checkbox',
  2324. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2325. ));
  2326. $expected = array(
  2327. array('div' => array('class' => 'input select')),
  2328. array('label' => array('for' => 'PublisherId')),
  2329. 'Publisher',
  2330. '/label',
  2331. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2332. array('div' => array('class' => 'checkbox')),
  2333. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2334. array('label' => array('for' => 'PublisherIdValue1')),
  2335. 'Label 1',
  2336. '/label',
  2337. '/div',
  2338. array('div' => array('class' => 'checkbox')),
  2339. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2340. array('label' => array('for' => 'PublisherIdValue2')),
  2341. 'Label 2',
  2342. '/label',
  2343. '/div',
  2344. '/div'
  2345. );
  2346. $this->assertTags($result, $expected);
  2347. }
  2348. /**
  2349. * test that input() and a non standard primary key makes a hidden input by default.
  2350. *
  2351. * @return void
  2352. */
  2353. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2354. $this->Form->create('User');
  2355. $this->Form->fieldset = array(
  2356. 'User' => array(
  2357. 'fields' => array(
  2358. 'model_id' => array('type' => 'integer')
  2359. ),
  2360. 'validates' => array(),
  2361. 'key' => 'model_id'
  2362. )
  2363. );
  2364. $result = $this->Form->input('model_id');
  2365. $expected = array(
  2366. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2367. );
  2368. $this->assertTags($result, $expected);
  2369. }
  2370. /**
  2371. * test that overriding the magic select type widget is possible
  2372. *
  2373. * @return void
  2374. */
  2375. public function testInputOverridingMagicSelectType() {
  2376. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2377. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2378. $expected = array(
  2379. 'div' => array('class' => 'input text'),
  2380. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2381. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2382. '/div'
  2383. );
  2384. $this->assertTags($result, $expected);
  2385. //Check that magic types still work for plural/singular vars
  2386. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2387. $result = $this->Form->input('Model.type');
  2388. $expected = array(
  2389. 'div' => array('class' => 'input select'),
  2390. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2391. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2392. array('option' => array('value' => 'value')), 'good', '/option',
  2393. array('option' => array('value' => 'other')), 'bad', '/option',
  2394. '/select',
  2395. '/div'
  2396. );
  2397. $this->assertTags($result, $expected);
  2398. }
  2399. /**
  2400. * Test that magic input() selects are created for type=number
  2401. *
  2402. * @return void
  2403. */
  2404. public function testInputMagicSelectForTypeNumber() {
  2405. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2406. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2407. $result = $this->Form->input('ValidateUser.balance');
  2408. $expected = array(
  2409. 'div' => array('class' => 'input select'),
  2410. 'label' => array('for' => 'ValidateUserBalance'),
  2411. 'Balance',
  2412. '/label',
  2413. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2414. array('option' => array('value' => '0')),
  2415. 'nothing',
  2416. '/option',
  2417. array('option' => array('value' => '1', 'selected' => 'selected')),
  2418. 'some',
  2419. '/option',
  2420. array('option' => array('value' => '100')),
  2421. 'a lot',
  2422. '/option',
  2423. '/select',
  2424. '/div'
  2425. );
  2426. $this->assertTags($result, $expected);
  2427. }
  2428. /**
  2429. * Test that magic input() selects can easily be converted into radio types without error.
  2430. *
  2431. * @return void
  2432. */
  2433. public function testInputMagicSelectChangeToRadio() {
  2434. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2435. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2436. $this->assertRegExp('/input type="radio"/', $result);
  2437. }
  2438. /**
  2439. * fields with the same name as the model should work.
  2440. *
  2441. * @return void
  2442. */
  2443. public function testInputWithMatchingFieldAndModelName() {
  2444. $this->Form->create('User');
  2445. $this->Form->fieldset = array(
  2446. 'User' => array(
  2447. 'fields' => array(
  2448. 'User' => array('type' => 'text')
  2449. ),
  2450. 'validates' => array(),
  2451. 'key' => 'id'
  2452. )
  2453. );
  2454. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2455. $result = $this->Form->input('User', array('type' => 'text'));
  2456. $expected = array(
  2457. 'div' => array('class' => 'input text'),
  2458. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2459. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2460. '/div'
  2461. );
  2462. $this->assertTags($result, $expected);
  2463. }
  2464. /**
  2465. * testFormInputs method
  2466. *
  2467. * test correct results from form::inputs().
  2468. *
  2469. * @return void
  2470. */
  2471. public function testFormInputs() {
  2472. $this->Form->create('Contact');
  2473. $result = $this->Form->inputs('The Legend');
  2474. $expected = array(
  2475. '<fieldset',
  2476. '<legend',
  2477. 'The Legend',
  2478. '/legend',
  2479. '*/fieldset',
  2480. );
  2481. $this->assertTags($result, $expected);
  2482. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2483. $expected = array(
  2484. 'fieldset' => array('class' => 'classy-stuff'),
  2485. '<legend',
  2486. 'Field of Dreams',
  2487. '/legend',
  2488. '*/fieldset'
  2489. );
  2490. $this->assertTags($result, $expected);
  2491. $this->Form->create('Contact');
  2492. $this->Form->request['prefix'] = 'admin';
  2493. $this->Form->request['action'] = 'admin_edit';
  2494. $result = $this->Form->inputs();
  2495. $expected = array(
  2496. '<fieldset',
  2497. '<legend',
  2498. 'Edit Contact',
  2499. '/legend',
  2500. '*/fieldset',
  2501. );
  2502. $this->assertTags($result, $expected);
  2503. $this->Form->create('Contact');
  2504. $result = $this->Form->inputs(false);
  2505. $expected = array(
  2506. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2507. array('div' => array('class' => 'input text')),
  2508. '*/div',
  2509. array('div' => array('class' => 'input text')),
  2510. '*/div',
  2511. array('div' => array('class' => 'input text')),
  2512. '*/div',
  2513. array('div' => array('class' => 'input password')),
  2514. '*/div',
  2515. array('div' => array('class' => 'input date')),
  2516. '*/div',
  2517. array('div' => array('class' => 'input date')),
  2518. '*/div',
  2519. array('div' => array('class' => 'input datetime')),
  2520. '*/div',
  2521. array('div' => array('class' => 'input number')),
  2522. '*/div',
  2523. array('div' => array('class' => 'input select')),
  2524. '*/div',
  2525. );
  2526. $this->assertTags($result, $expected);
  2527. $this->Form->create('Contact');
  2528. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2529. $expected = array(
  2530. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2531. array('div' => array('class' => 'input text')),
  2532. '*/div',
  2533. array('div' => array('class' => 'input text')),
  2534. '*/div',
  2535. array('div' => array('class' => 'input text')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input password')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input date')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input date')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input datetime')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input number')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input select')),
  2548. '*/div',
  2549. );
  2550. $this->assertTags($result, $expected);
  2551. $this->Form->create('Contact');
  2552. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2553. $expected = array(
  2554. 'fieldset' => array(),
  2555. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2556. array('div' => array('class' => 'input text')),
  2557. '*/div',
  2558. array('div' => array('class' => 'input text')),
  2559. '*/div',
  2560. array('div' => array('class' => 'input text')),
  2561. '*/div',
  2562. array('div' => array('class' => 'input password')),
  2563. '*/div',
  2564. array('div' => array('class' => 'input date')),
  2565. '*/div',
  2566. array('div' => array('class' => 'input date')),
  2567. '*/div',
  2568. array('div' => array('class' => 'input datetime')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input number')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input select')),
  2573. '*/div',
  2574. '/fieldset'
  2575. );
  2576. $this->assertTags($result, $expected);
  2577. $this->Form->create('Contact');
  2578. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2579. $expected = array(
  2580. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2581. array('div' => array('class' => 'input text')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input text')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input text')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input password')),
  2588. '*/div',
  2589. array('div' => array('class' => 'input date')),
  2590. '*/div',
  2591. array('div' => array('class' => 'input date')),
  2592. '*/div',
  2593. array('div' => array('class' => 'input datetime')),
  2594. '*/div',
  2595. array('div' => array('class' => 'input number')),
  2596. '*/div',
  2597. array('div' => array('class' => 'input select')),
  2598. '*/div',
  2599. );
  2600. $this->assertTags($result, $expected);
  2601. $this->Form->create('Contact');
  2602. $result = $this->Form->inputs('Hello');
  2603. $expected = array(
  2604. 'fieldset' => array(),
  2605. 'legend' => array(),
  2606. 'Hello',
  2607. '/legend',
  2608. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2609. array('div' => array('class' => 'input text')),
  2610. '*/div',
  2611. array('div' => array('class' => 'input text')),
  2612. '*/div',
  2613. array('div' => array('class' => 'input text')),
  2614. '*/div',
  2615. array('div' => array('class' => 'input password')),
  2616. '*/div',
  2617. array('div' => array('class' => 'input date')),
  2618. '*/div',
  2619. array('div' => array('class' => 'input date')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input datetime')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input number')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input select')),
  2626. '*/div',
  2627. '/fieldset'
  2628. );
  2629. $this->assertTags($result, $expected);
  2630. $this->Form->create('Contact');
  2631. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2632. $expected = array(
  2633. 'fieldset' => array(),
  2634. 'legend' => array(),
  2635. 'Hello',
  2636. '/legend',
  2637. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2638. array('div' => array('class' => 'input text')),
  2639. '*/div',
  2640. array('div' => array('class' => 'input text')),
  2641. '*/div',
  2642. array('div' => array('class' => 'input text')),
  2643. '*/div',
  2644. array('div' => array('class' => 'input password')),
  2645. '*/div',
  2646. array('div' => array('class' => 'input date')),
  2647. '*/div',
  2648. array('div' => array('class' => 'input date')),
  2649. '*/div',
  2650. array('div' => array('class' => 'input datetime')),
  2651. '*/div',
  2652. array('div' => array('class' => 'input number')),
  2653. '*/div',
  2654. array('div' => array('class' => 'input select')),
  2655. '*/div',
  2656. '/fieldset'
  2657. );
  2658. $this->assertTags($result, $expected);
  2659. }
  2660. /**
  2661. * testSelectAsCheckbox method
  2662. *
  2663. * test multi-select widget with checkbox formatting.
  2664. *
  2665. * @return void
  2666. */
  2667. public function testSelectAsCheckbox() {
  2668. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2669. $expected = array(
  2670. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2671. array('div' => array('class' => 'checkbox')),
  2672. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2673. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2674. 'first',
  2675. '/label',
  2676. '/div',
  2677. array('div' => array('class' => 'checkbox')),
  2678. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2679. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2680. 'second',
  2681. '/label',
  2682. '/div',
  2683. array('div' => array('class' => 'checkbox')),
  2684. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2685. array('label' => array('for' => 'ModelMultiField2')),
  2686. 'third',
  2687. '/label',
  2688. '/div',
  2689. );
  2690. $this->assertTags($result, $expected);
  2691. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2692. $expected = array(
  2693. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2694. array('div' => array('class' => 'checkbox')),
  2695. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2696. array('label' => array('for' => 'ModelMultiField12')),
  2697. 'half',
  2698. '/label',
  2699. '/div',
  2700. );
  2701. $this->assertTags($result, $expected);
  2702. }
  2703. /**
  2704. * testLabel method
  2705. *
  2706. * test label generation.
  2707. *
  2708. * @return void
  2709. */
  2710. public function testLabel() {
  2711. $this->Form->text('Person.name');
  2712. $result = $this->Form->label();
  2713. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2714. $this->Form->text('Person.name');
  2715. $result = $this->Form->label();
  2716. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2717. $result = $this->Form->label('Person.first_name');
  2718. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2719. $result = $this->Form->label('Person.first_name', 'Your first name');
  2720. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2721. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2722. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2723. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2724. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2725. $result = $this->Form->label('Person.first_name', '');
  2726. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2727. $result = $this->Form->label('Person.2.name', '');
  2728. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2729. }
  2730. /**
  2731. * testTextbox method
  2732. *
  2733. * test textbox element generation
  2734. *
  2735. * @return void
  2736. */
  2737. public function testTextbox() {
  2738. $result = $this->Form->text('Model.field');
  2739. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2740. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2741. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2742. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2743. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2744. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2745. $result = $this->Form->text('Model.text');
  2746. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2747. $Contact = ClassRegistry::getObject('Contact');
  2748. $Contact->validationErrors['text'] = array(true);
  2749. $this->Form->request->data['Contact']['text'] = 'test';
  2750. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2751. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2752. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2753. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2754. $expected = array(
  2755. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2756. );
  2757. $this->assertTags($result, $expected);
  2758. }
  2759. /**
  2760. * testDefaultValue method
  2761. *
  2762. * Test default value setting
  2763. *
  2764. * @return void
  2765. */
  2766. public function testDefaultValue() {
  2767. $this->Form->request->data['Model']['field'] = 'test';
  2768. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2769. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2770. unset($this->Form->request->data['Model']['field']);
  2771. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2772. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2773. }
  2774. /**
  2775. * testCheckboxDefaultValue method
  2776. *
  2777. * Test default value setting on checkbox() method
  2778. *
  2779. * @return void
  2780. */
  2781. public function testCheckboxDefaultValue() {
  2782. $this->Form->request->data['Model']['field'] = false;
  2783. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2784. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2785. unset($this->Form->request->data['Model']['field']);
  2786. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2787. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2788. $this->Form->request->data['Model']['field'] = true;
  2789. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2790. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2791. unset($this->Form->request->data['Model']['field']);
  2792. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2793. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2794. }
  2795. /**
  2796. * testError method
  2797. *
  2798. * Test field error generation
  2799. *
  2800. * @return void
  2801. */
  2802. public function testError() {
  2803. $Contact = ClassRegistry::getObject('Contact');
  2804. $Contact->validationErrors['field'] = array(1);
  2805. $result = $this->Form->error('Contact.field');
  2806. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2807. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2808. $this->assertEquals('Error in field Field', $result);
  2809. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2810. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2811. $this->assertEquals('This field contains invalid input', $result);
  2812. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2813. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2814. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2815. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2816. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2817. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2818. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2819. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2820. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2821. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2822. $this->assertEquals('<strong>Badness!</strong>', $result);
  2823. $Contact->validationErrors['field'] = array("email");
  2824. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2825. $expected = array(
  2826. 'div' => array('class' => 'field-error'),
  2827. 'No good!',
  2828. '/div'
  2829. );
  2830. $this->assertTags($result, $expected);
  2831. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2832. $result = $this->Form->error('Contact.field', array(
  2833. 'notEmpty' => 'Cannot be empty',
  2834. 'email' => 'No good!'
  2835. ));
  2836. $expected = array(
  2837. 'div' => array('class' => 'error-message'),
  2838. 'ul' => array(),
  2839. '<li', 'Cannot be empty', '/li',
  2840. '<li', 'No good!', '/li',
  2841. '<li', 'Something else', '/li',
  2842. '/ul',
  2843. '/div'
  2844. );
  2845. $this->assertTags($result, $expected);
  2846. /** Testing error messages list options **/
  2847. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2848. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2849. $expected = array(
  2850. 'div' => array('class' => 'error-message'),
  2851. 'ol' => array(),
  2852. '<li', 'notEmpty', '/li',
  2853. '<li', 'email', '/li',
  2854. '/ol',
  2855. '/div'
  2856. );
  2857. $this->assertTags($result, $expected);
  2858. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2859. $expected = array(
  2860. 'div' => array('class' => 'error-message'),
  2861. 'ol' => array(),
  2862. '<li', 'notEmpty', '/li',
  2863. '<li', 'email', '/li',
  2864. '/ol',
  2865. '/div'
  2866. );
  2867. $this->assertTags($result, $expected);
  2868. $result = $this->Form->error('Contact.field', null, array(
  2869. 'listOptions' => array(
  2870. 'class' => 'ul-class',
  2871. 'itemOptions' => array(
  2872. 'class' => 'li-class'
  2873. )
  2874. )
  2875. ));
  2876. $expected = array(
  2877. 'div' => array('class' => 'error-message'),
  2878. 'ul' => array('class' => 'ul-class'),
  2879. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2880. array('li' => array('class' => 'li-class')), 'email', '/li',
  2881. '/ul',
  2882. '/div'
  2883. );
  2884. $this->assertTags($result, $expected);
  2885. }
  2886. /**
  2887. * test error options when using form->input();
  2888. *
  2889. * @return void
  2890. */
  2891. public function testInputErrorEscape() {
  2892. $this->Form->create('ValidateProfile');
  2893. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2894. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2895. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2896. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2897. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2898. $this->assertRegExp('/required<br>/', $result);
  2899. }
  2900. /**
  2901. * testPassword method
  2902. *
  2903. * Test password element generation
  2904. *
  2905. * @return void
  2906. */
  2907. public function testPassword() {
  2908. $Contact = ClassRegistry::getObject('Contact');
  2909. $result = $this->Form->password('Contact.field');
  2910. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2911. $Contact->validationErrors['passwd'] = 1;
  2912. $this->Form->request->data['Contact']['passwd'] = 'test';
  2913. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2914. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2915. }
  2916. /**
  2917. * testRadio method
  2918. *
  2919. * Test radio element set generation
  2920. *
  2921. * @return void
  2922. */
  2923. public function testRadio() {
  2924. $result = $this->Form->radio('Model.field', array('option A'));
  2925. $expected = array(
  2926. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2927. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2928. 'label' => array('for' => 'ModelField0'),
  2929. 'option A',
  2930. '/label'
  2931. );
  2932. $this->assertTags($result, $expected);
  2933. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2934. $expected = array(
  2935. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2936. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2937. 'label' => array('for' => 'ModelField12'),
  2938. 'half',
  2939. '/label'
  2940. );
  2941. $this->assertTags($result, $expected);
  2942. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2943. $expected = array(
  2944. 'fieldset' => array(),
  2945. 'legend' => array(),
  2946. 'Field',
  2947. '/legend',
  2948. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2949. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2950. array('label' => array('for' => 'ModelField0')),
  2951. 'option A',
  2952. '/label',
  2953. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2954. array('label' => array('for' => 'ModelField1')),
  2955. 'option B',
  2956. '/label',
  2957. '/fieldset'
  2958. );
  2959. $this->assertTags($result, $expected);
  2960. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2961. $expected = array(
  2962. 'fieldset' => array(),
  2963. 'legend' => array(),
  2964. 'Field',
  2965. '/legend',
  2966. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2967. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2968. array('label' => array('for' => 'ModelField0')),
  2969. 'option A',
  2970. '/label',
  2971. 'br' => array(),
  2972. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2973. array('label' => array('for' => 'ModelField1')),
  2974. 'option B',
  2975. '/label',
  2976. '/fieldset'
  2977. );
  2978. $this->assertTags($result, $expected);
  2979. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2980. $expected = array(
  2981. 'fieldset' => array(),
  2982. 'legend' => array(),
  2983. 'Gender',
  2984. '/legend',
  2985. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  2986. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  2987. array('label' => array('for' => 'EmployeeGenderMale')),
  2988. 'Male',
  2989. '/label',
  2990. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  2991. array('label' => array('for' => 'EmployeeGenderFemale')),
  2992. 'Female',
  2993. '/label',
  2994. '/fieldset',
  2995. );
  2996. $this->assertTags($result, $expected);
  2997. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  2998. $expected = array(
  2999. 'fieldset' => array(),
  3000. 'legend' => array(),
  3001. 'Gender',
  3002. '/legend',
  3003. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3004. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3005. array('label' => array('for' => 'OfficerGenderMale')),
  3006. 'Male',
  3007. '/label',
  3008. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3009. array('label' => array('for' => 'OfficerGenderFemale')),
  3010. 'Female',
  3011. '/label',
  3012. '/fieldset',
  3013. );
  3014. $this->assertTags($result, $expected);
  3015. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3016. $expected = array(
  3017. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3018. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3019. 'label' => array('for' => 'Contact1Imrequired0'),
  3020. 'option A',
  3021. '/label'
  3022. );
  3023. $this->assertTags($result, $expected);
  3024. $result = $this->Form->radio('Model.1.field', array('option A'));
  3025. $expected = array(
  3026. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3027. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3028. 'label' => array('for' => 'Model1Field0'),
  3029. 'option A',
  3030. '/label'
  3031. );
  3032. $this->assertTags($result, $expected);
  3033. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3034. $expected = array(
  3035. 'fieldset' => array(),
  3036. 'legend' => array(),
  3037. 'Field',
  3038. '/legend',
  3039. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3040. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3041. array('label' => array('for' => 'ModelField0')),
  3042. 'option A',
  3043. '/label',
  3044. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3045. array('label' => array('for' => 'ModelField1')),
  3046. 'option B',
  3047. '/label',
  3048. '/fieldset'
  3049. );
  3050. $this->assertTags($result, $expected);
  3051. $result = $this->Form->radio(
  3052. 'Model.field',
  3053. array('option A', 'option B'),
  3054. array('between' => 'I am between')
  3055. );
  3056. $expected = array(
  3057. 'fieldset' => array(),
  3058. 'legend' => array(),
  3059. 'Field',
  3060. '/legend',
  3061. 'I am between',
  3062. 'input' => array(
  3063. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3064. 'value' => '', 'id' => 'ModelField_'
  3065. ),
  3066. array('input' => array(
  3067. 'type' => 'radio', 'name' => 'data[Model][field]',
  3068. 'value' => '0', 'id' => 'ModelField0'
  3069. )),
  3070. array('label' => array('for' => 'ModelField0')),
  3071. 'option A',
  3072. '/label',
  3073. array('input' => array(
  3074. 'type' => 'radio', 'name' => 'data[Model][field]',
  3075. 'value' => '1', 'id' => 'ModelField1'
  3076. )),
  3077. array('label' => array('for' => 'ModelField1')),
  3078. 'option B',
  3079. '/label',
  3080. '/fieldset'
  3081. );
  3082. $this->assertTags($result, $expected);
  3083. }
  3084. /**
  3085. * Test that radios with a 0 value are selected under the correct conditions.
  3086. * Also ensure that values that are booleanish are handled correctly.
  3087. *
  3088. * @return void
  3089. */
  3090. public function testRadioOptionWithBooleanishValues() {
  3091. $expected = array(
  3092. 'fieldset' => array(),
  3093. 'legend' => array(),
  3094. 'Field',
  3095. '/legend',
  3096. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3097. array('label' => array('for' => 'ModelField1')),
  3098. 'Yes',
  3099. '/label',
  3100. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3101. array('label' => array('for' => 'ModelField0')),
  3102. 'No',
  3103. '/label',
  3104. '/fieldset'
  3105. );
  3106. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3107. $this->assertTags($result, $expected);
  3108. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3109. $this->assertTags($result, $expected);
  3110. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3111. $this->assertTags($result, $expected);
  3112. $expected = array(
  3113. 'fieldset' => array(),
  3114. 'legend' => array(),
  3115. 'Field',
  3116. '/legend',
  3117. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3118. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3119. array('label' => array('for' => 'ModelField1')),
  3120. 'Yes',
  3121. '/label',
  3122. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3123. array('label' => array('for' => 'ModelField0')),
  3124. 'No',
  3125. '/label',
  3126. '/fieldset'
  3127. );
  3128. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3129. $this->assertTags($result, $expected);
  3130. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3131. $this->assertTags($result, $expected);
  3132. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3133. $this->assertTags($result, $expected);
  3134. $expected = array(
  3135. 'fieldset' => array(),
  3136. 'legend' => array(),
  3137. 'Field',
  3138. '/legend',
  3139. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3140. array('label' => array('for' => 'ModelField1')),
  3141. 'Yes',
  3142. '/label',
  3143. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3144. array('label' => array('for' => 'ModelField0')),
  3145. 'No',
  3146. '/label',
  3147. '/fieldset'
  3148. );
  3149. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3150. $this->assertTags($result, $expected);
  3151. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3152. $this->assertTags($result, $expected);
  3153. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3154. $this->assertTags($result, $expected);
  3155. }
  3156. /**
  3157. * test disabled radio options
  3158. *
  3159. * @return void
  3160. */
  3161. public function testRadioDisabled() {
  3162. $result = $this->Form->radio(
  3163. 'Model.field',
  3164. array('option A', 'option B'),
  3165. array('disabled' => array('option A'), 'value' => '0')
  3166. );
  3167. $expected = array(
  3168. 'fieldset' => array(),
  3169. 'legend' => array(),
  3170. 'Field',
  3171. '/legend',
  3172. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3173. array('label' => array('for' => 'ModelField0')),
  3174. 'option A',
  3175. '/label',
  3176. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3177. array('label' => array('for' => 'ModelField1')),
  3178. 'option B',
  3179. '/label',
  3180. '/fieldset'
  3181. );
  3182. $this->assertTags($result, $expected);
  3183. $result = $this->Form->radio(
  3184. 'Model.field',
  3185. array('option A', 'option B'),
  3186. array('disabled' => true, 'value' => '0')
  3187. );
  3188. $expected = array(
  3189. 'fieldset' => array(),
  3190. 'legend' => array(),
  3191. 'Field',
  3192. '/legend',
  3193. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3194. array('label' => array('for' => 'ModelField0')),
  3195. 'option A',
  3196. '/label',
  3197. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3198. array('label' => array('for' => 'ModelField1')),
  3199. 'option B',
  3200. '/label',
  3201. '/fieldset'
  3202. );
  3203. $this->assertTags($result, $expected);
  3204. $result = $this->Form->radio(
  3205. 'Model.field',
  3206. array('option A', 'option B'),
  3207. array('disabled' => 'disabled', 'value' => '0')
  3208. );
  3209. $expected = array(
  3210. 'fieldset' => array(),
  3211. 'legend' => array(),
  3212. 'Field',
  3213. '/legend',
  3214. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3215. array('label' => array('for' => 'ModelField0')),
  3216. 'option A',
  3217. '/label',
  3218. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3219. array('label' => array('for' => 'ModelField1')),
  3220. 'option B',
  3221. '/label',
  3222. '/fieldset'
  3223. );
  3224. $this->assertTags($result, $expected);
  3225. }
  3226. /**
  3227. * test disabling the hidden input for radio buttons
  3228. *
  3229. * @return void
  3230. */
  3231. public function testRadioHiddenInputDisabling() {
  3232. $result = $this->Form->input('Model.1.field', array(
  3233. 'type' => 'radio',
  3234. 'options' => array('option A'),
  3235. 'hiddenField' => false
  3236. )
  3237. );
  3238. $expected = array(
  3239. 'div' => array('class' => 'input radio'),
  3240. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3241. 'label' => array('for' => 'Model1Field0'),
  3242. 'option A',
  3243. '/label',
  3244. '/div'
  3245. );
  3246. $this->assertTags($result, $expected);
  3247. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3248. $expected = array(
  3249. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3250. 'label' => array('for' => 'Model1Field0'),
  3251. 'option A',
  3252. '/label'
  3253. );
  3254. $this->assertTags($result, $expected);
  3255. }
  3256. /**
  3257. * test adding an empty option for radio buttons
  3258. *
  3259. * @return void
  3260. */
  3261. public function testRadioAddEmptyOption() {
  3262. $result = $this->Form->input('Model.1.field', array(
  3263. 'type' => 'radio',
  3264. 'options' => array('option A'),
  3265. 'empty' => true,
  3266. 'hiddenField' => false
  3267. ));
  3268. $expected = array(
  3269. 'div' => array('class' => 'input radio'),
  3270. 'fieldset' => array(),
  3271. 'legend' => array(),
  3272. 'Field',
  3273. '/legend',
  3274. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3275. array('label' => array('for' => 'Model1Field')),
  3276. __('empty'),
  3277. '/label',
  3278. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3279. array('label' => array('for' => 'Model1Field0')),
  3280. 'option A',
  3281. '/label',
  3282. '/fieldset',
  3283. '/div'
  3284. );
  3285. $this->assertTags($result, $expected);
  3286. $result = $this->Form->input('Model.1.field', array(
  3287. 'type' => 'radio',
  3288. 'options' => array('option A'),
  3289. 'empty' => 'CustomEmptyLabel',
  3290. 'hiddenField' => false
  3291. ));
  3292. $expected = array(
  3293. 'div' => array('class' => 'input radio'),
  3294. 'fieldset' => array(),
  3295. 'legend' => array(),
  3296. 'Field',
  3297. '/legend',
  3298. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3299. array('label' => array('for' => 'Model1Field')),
  3300. 'CustomEmptyLabel',
  3301. '/label',
  3302. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3303. array('label' => array('for' => 'Model1Field0')),
  3304. 'option A',
  3305. '/label',
  3306. '/fieldset',
  3307. '/div'
  3308. );
  3309. $this->assertTags($result, $expected);
  3310. $result = $this->Form->input('Model.1.field', array(
  3311. 'type' => 'radio',
  3312. 'options' => array('option A'),
  3313. 'empty' => false,
  3314. 'hiddenField' => false
  3315. ));
  3316. $this->assertTextNotContains('"Model1Field"', $result);
  3317. }
  3318. /**
  3319. * testSelect method
  3320. *
  3321. * Test select element generation.
  3322. *
  3323. * @return void
  3324. */
  3325. public function testSelect() {
  3326. $result = $this->Form->select('Model.field', array());
  3327. $expected = array(
  3328. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3329. array('option' => array('value' => '')),
  3330. '/option',
  3331. '/select'
  3332. );
  3333. $this->assertTags($result, $expected);
  3334. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3335. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3336. $expected = array(
  3337. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3338. array('option' => array('value' => '')),
  3339. '/option',
  3340. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3341. 'good',
  3342. '/option',
  3343. array('option' => array('value' => 'other')),
  3344. 'bad',
  3345. '/option',
  3346. '/select'
  3347. );
  3348. $this->assertTags($result, $expected);
  3349. $this->Form->request->data = array();
  3350. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3351. $expected = array(
  3352. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3353. array('option' => array('value' => '')),
  3354. '/option',
  3355. array('option' => array('value' => 'value')),
  3356. 'good',
  3357. '/option',
  3358. array('option' => array('value' => 'other')),
  3359. 'bad',
  3360. '/option',
  3361. '/select'
  3362. );
  3363. $this->assertTags($result, $expected);
  3364. $result = $this->Form->select(
  3365. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3366. array('empty' => false)
  3367. );
  3368. $expected = array(
  3369. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3370. array('option' => array('value' => 'first')),
  3371. 'first &quot;html&quot; &lt;chars&gt;',
  3372. '/option',
  3373. array('option' => array('value' => 'second')),
  3374. 'value',
  3375. '/option',
  3376. '/select'
  3377. );
  3378. $this->assertTags($result, $expected);
  3379. $result = $this->Form->select(
  3380. 'Model.field',
  3381. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3382. array('escape' => false, 'empty' => false)
  3383. );
  3384. $expected = array(
  3385. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3386. array('option' => array('value' => 'first')),
  3387. 'first "html" <chars>',
  3388. '/option',
  3389. array('option' => array('value' => 'second')),
  3390. 'value',
  3391. '/option',
  3392. '/select'
  3393. );
  3394. $this->assertTags($result, $expected);
  3395. $options = array(
  3396. array('value' => 'first', 'name' => 'First'),
  3397. array('value' => 'first', 'name' => 'Another First'),
  3398. );
  3399. $result = $this->Form->select(
  3400. 'Model.field',
  3401. $options,
  3402. array('escape' => false, 'empty' => false)
  3403. );
  3404. $expected = array(
  3405. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3406. array('option' => array('value' => 'first')),
  3407. 'First',
  3408. '/option',
  3409. array('option' => array('value' => 'first')),
  3410. 'Another First',
  3411. '/option',
  3412. '/select'
  3413. );
  3414. $this->assertTags($result, $expected);
  3415. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3416. $result = $this->Form->select(
  3417. 'Model.contact_id',
  3418. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3419. array('escape' => false, 'empty' => 'pick something')
  3420. );
  3421. $expected = array(
  3422. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3423. array('option' => array('value' => '')), 'pick something', '/option',
  3424. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3425. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3426. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3427. '/select'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $this->Form->request->data['Model']['field'] = 0;
  3431. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3432. $expected = array(
  3433. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3434. array('option' => array('value' => '')), '/option',
  3435. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3436. array('option' => array('value' => '1')), 'Yes', '/option',
  3437. '/select'
  3438. );
  3439. $this->assertTags($result, $expected);
  3440. }
  3441. /**
  3442. * test that select() with optiongroups listens to the escape param.
  3443. *
  3444. * @return void
  3445. */
  3446. public function testSelectOptionGroupEscaping() {
  3447. $options = array(
  3448. '>< Key' => array(
  3449. 1 => 'One',
  3450. 2 => 'Two'
  3451. )
  3452. );
  3453. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3454. $expected = array(
  3455. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3456. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3457. array('option' => array('value' => '1')), 'One', '/option',
  3458. array('option' => array('value' => '2')), 'Two', '/option',
  3459. '/optgroup',
  3460. '/select'
  3461. );
  3462. $this->assertTags($result, $expected);
  3463. $options = array(
  3464. '>< Key' => array(
  3465. 1 => 'One',
  3466. 2 => 'Two'
  3467. )
  3468. );
  3469. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3470. $expected = array(
  3471. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3472. 'optgroup' => array('label' => '>< Key'),
  3473. array('option' => array('value' => '1')), 'One', '/option',
  3474. array('option' => array('value' => '2')), 'Two', '/option',
  3475. '/optgroup',
  3476. '/select'
  3477. );
  3478. $this->assertTags($result, $expected);
  3479. }
  3480. /**
  3481. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3482. *
  3483. * @return void
  3484. */
  3485. public function testSelectWithNullAttributes() {
  3486. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3487. $expected = array(
  3488. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3489. array('option' => array('value' => '0')),
  3490. 'first',
  3491. '/option',
  3492. array('option' => array('value' => '1')),
  3493. 'second',
  3494. '/option',
  3495. '/select'
  3496. );
  3497. $this->assertTags($result, $expected);
  3498. }
  3499. /**
  3500. * testNestedSelect method
  3501. *
  3502. * test select element generation with optgroups
  3503. *
  3504. * @return void
  3505. */
  3506. public function testNestedSelect() {
  3507. $result = $this->Form->select(
  3508. 'Model.field',
  3509. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3510. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3511. )), array('empty' => false)
  3512. );
  3513. $expected = array(
  3514. 'select' => array('name' => 'data[Model][field]',
  3515. 'id' => 'ModelField'),
  3516. array('option' => array('value' => 1)),
  3517. 'One',
  3518. '/option',
  3519. array('option' => array('value' => 2)),
  3520. 'Two',
  3521. '/option',
  3522. array('optgroup' => array('label' => 'Three')),
  3523. array('option' => array('value' => 4)),
  3524. 'Four',
  3525. '/option',
  3526. array('option' => array('value' => 5)),
  3527. 'Five',
  3528. '/option',
  3529. '/optgroup',
  3530. '/select'
  3531. );
  3532. $this->assertTags($result, $expected);
  3533. $result = $this->Form->select(
  3534. 'Model.field',
  3535. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3536. array('showParents' => true, 'empty' => false)
  3537. );
  3538. $expected = array(
  3539. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3540. array('option' => array('value' => 1)),
  3541. 'One',
  3542. '/option',
  3543. array('option' => array('value' => 2)),
  3544. 'Two',
  3545. '/option',
  3546. array('optgroup' => array('label' => 'Three')),
  3547. array('option' => array('value' => 3)),
  3548. 'Three',
  3549. '/option',
  3550. array('option' => array('value' => 4)),
  3551. 'Four',
  3552. '/option',
  3553. '/optgroup',
  3554. '/select'
  3555. );
  3556. $this->assertTags($result, $expected);
  3557. }
  3558. /**
  3559. * testSelectMultiple method
  3560. *
  3561. * test generation of multiple select elements
  3562. *
  3563. * @return void
  3564. */
  3565. public function testSelectMultiple() {
  3566. $options = array('first', 'second', 'third');
  3567. $result = $this->Form->select(
  3568. 'Model.multi_field', $options, array('multiple' => true)
  3569. );
  3570. $expected = array(
  3571. 'input' => array(
  3572. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3573. ),
  3574. 'select' => array(
  3575. 'name' => 'data[Model][multi_field][]',
  3576. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3577. ),
  3578. array('option' => array('value' => '0')),
  3579. 'first',
  3580. '/option',
  3581. array('option' => array('value' => '1')),
  3582. 'second',
  3583. '/option',
  3584. array('option' => array('value' => '2')),
  3585. 'third',
  3586. '/option',
  3587. '/select'
  3588. );
  3589. $this->assertTags($result, $expected);
  3590. $result = $this->Form->select(
  3591. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3592. );
  3593. $expected = array(
  3594. 'input' => array(
  3595. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3596. ),
  3597. 'select' => array(
  3598. 'name' => 'data[Model][multi_field][]',
  3599. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3600. ),
  3601. array('option' => array('value' => '0')),
  3602. 'first',
  3603. '/option',
  3604. array('option' => array('value' => '1')),
  3605. 'second',
  3606. '/option',
  3607. array('option' => array('value' => '2')),
  3608. 'third',
  3609. '/option',
  3610. '/select'
  3611. );
  3612. $this->assertTags($result, $expected);
  3613. $result = $this->Form->select(
  3614. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3615. );
  3616. $expected = array(
  3617. 'input' => array(
  3618. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3619. ),
  3620. 'select' => array(
  3621. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3622. 'multiple' => 'multiple'
  3623. ),
  3624. array('option' => array('value' => '0', 'selected' => 'selected')),
  3625. 'first',
  3626. '/option',
  3627. array('option' => array('value' => '1', 'selected' => 'selected')),
  3628. 'second',
  3629. '/option',
  3630. array('option' => array('value' => '2')),
  3631. 'third',
  3632. '/option',
  3633. '/select'
  3634. );
  3635. $this->assertTags($result, $expected);
  3636. $result = $this->Form->select(
  3637. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3638. );
  3639. $expected = array(
  3640. 'select' => array(
  3641. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3642. ),
  3643. array('option' => array('value' => '0', 'selected' => 'selected')),
  3644. 'first',
  3645. '/option',
  3646. array('option' => array('value' => '1', 'selected' => 'selected')),
  3647. 'second',
  3648. '/option',
  3649. array('option' => array('value' => '2')),
  3650. 'third',
  3651. '/option',
  3652. '/select'
  3653. );
  3654. $this->assertTags($result, $expected);
  3655. }
  3656. /**
  3657. * test generation of habtm select boxes.
  3658. *
  3659. * @return void
  3660. */
  3661. public function testHabtmSelectBox() {
  3662. $this->View->viewVars['contactTags'] = array(
  3663. 1 => 'blue',
  3664. 2 => 'red',
  3665. 3 => 'green'
  3666. );
  3667. $this->Form->request->data = array(
  3668. 'Contact' => array(),
  3669. 'ContactTag' => array(
  3670. array(
  3671. 'id' => 1,
  3672. 'name' => 'blue'
  3673. ),
  3674. array(
  3675. 'id' => 3,
  3676. 'name' => 'green'
  3677. )
  3678. )
  3679. );
  3680. $this->Form->create('Contact');
  3681. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3682. $expected = array(
  3683. 'input' => array(
  3684. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3685. ),
  3686. 'select' => array(
  3687. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3688. 'multiple' => 'multiple'
  3689. ),
  3690. array('option' => array('value' => '1', 'selected' => 'selected')),
  3691. 'blue',
  3692. '/option',
  3693. array('option' => array('value' => '2')),
  3694. 'red',
  3695. '/option',
  3696. array('option' => array('value' => '3', 'selected' => 'selected')),
  3697. 'green',
  3698. '/option',
  3699. '/select'
  3700. );
  3701. $this->assertTags($result, $expected);
  3702. }
  3703. /**
  3704. * test generation of multi select elements in checkbox format
  3705. *
  3706. * @return void
  3707. */
  3708. public function testSelectMultipleCheckboxes() {
  3709. $result = $this->Form->select(
  3710. 'Model.multi_field',
  3711. array('first', 'second', 'third'),
  3712. array('multiple' => 'checkbox')
  3713. );
  3714. $expected = array(
  3715. 'input' => array(
  3716. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3717. ),
  3718. array('div' => array('class' => 'checkbox')),
  3719. array('input' => array(
  3720. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3721. 'value' => '0', 'id' => 'ModelMultiField0'
  3722. )),
  3723. array('label' => array('for' => 'ModelMultiField0')),
  3724. 'first',
  3725. '/label',
  3726. '/div',
  3727. array('div' => array('class' => 'checkbox')),
  3728. array('input' => array(
  3729. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3730. 'value' => '1', 'id' => 'ModelMultiField1'
  3731. )),
  3732. array('label' => array('for' => 'ModelMultiField1')),
  3733. 'second',
  3734. '/label',
  3735. '/div',
  3736. array('div' => array('class' => 'checkbox')),
  3737. array('input' => array(
  3738. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3739. 'value' => '2', 'id' => 'ModelMultiField2'
  3740. )),
  3741. array('label' => array('for' => 'ModelMultiField2')),
  3742. 'third',
  3743. '/label',
  3744. '/div'
  3745. );
  3746. $this->assertTags($result, $expected);
  3747. $result = $this->Form->select(
  3748. 'Model.multi_field',
  3749. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3750. array('multiple' => 'checkbox')
  3751. );
  3752. $expected = array(
  3753. 'input' => array(
  3754. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3755. ),
  3756. array('div' => array('class' => 'checkbox')),
  3757. array('input' => array(
  3758. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3759. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3760. )),
  3761. array('label' => array('for' => 'ModelMultiFieldA')),
  3762. 'first',
  3763. '/label',
  3764. '/div',
  3765. array('div' => array('class' => 'checkbox')),
  3766. array('input' => array(
  3767. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3768. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3769. )),
  3770. array('label' => array('for' => 'ModelMultiFieldB')),
  3771. 'second',
  3772. '/label',
  3773. '/div',
  3774. array('div' => array('class' => 'checkbox')),
  3775. array('input' => array(
  3776. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3777. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3778. )),
  3779. array('label' => array('for' => 'ModelMultiFieldC')),
  3780. 'third',
  3781. '/label',
  3782. '/div'
  3783. );
  3784. $this->assertTags($result, $expected);
  3785. $result = $this->Form->select(
  3786. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3787. );
  3788. $expected = array(
  3789. 'input' => array(
  3790. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3791. ),
  3792. array('div' => array('class' => 'checkbox')),
  3793. array('input' => array(
  3794. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3795. 'value' => '1', 'id' => 'ModelMultiField1'
  3796. )),
  3797. array('label' => array('for' => 'ModelMultiField1')),
  3798. 'first',
  3799. '/label',
  3800. '/div'
  3801. );
  3802. $this->assertTags($result, $expected);
  3803. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3804. $result = $this->Form->select(
  3805. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3806. );
  3807. $expected = array(
  3808. 'input' => array(
  3809. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3810. ),
  3811. array('div' => array('class' => 'checkbox')),
  3812. array('input' => array(
  3813. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3814. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3815. )),
  3816. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3817. 'first',
  3818. '/label',
  3819. '/div',
  3820. array('div' => array('class' => 'checkbox')),
  3821. array('input' => array(
  3822. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3823. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3824. )),
  3825. array('label' => array('for' => 'ModelTagsArray')),
  3826. 'Array',
  3827. '/label',
  3828. '/div'
  3829. );
  3830. $this->assertTags($result, $expected);
  3831. }
  3832. /**
  3833. * test multiple checkboxes with div styles.
  3834. *
  3835. * @return void
  3836. */
  3837. public function testSelectMultipleCheckboxDiv() {
  3838. $result = $this->Form->select(
  3839. 'Model.tags',
  3840. array('first', 'second'),
  3841. array('multiple' => 'checkbox', 'class' => 'my-class')
  3842. );
  3843. $expected = array(
  3844. 'input' => array(
  3845. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3846. ),
  3847. array('div' => array('class' => 'my-class')),
  3848. array('input' => array(
  3849. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3850. 'value' => '0', 'id' => 'ModelTags0'
  3851. )),
  3852. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3853. '/div',
  3854. array('div' => array('class' => 'my-class')),
  3855. array('input' => array(
  3856. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3857. 'value' => '1', 'id' => 'ModelTags1'
  3858. )),
  3859. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3860. '/div'
  3861. );
  3862. $this->assertTags($result, $expected);
  3863. $result = $this->Form->input('Model.tags', array(
  3864. 'options' => array('first', 'second'),
  3865. 'multiple' => 'checkbox',
  3866. 'class' => 'my-class',
  3867. 'div' => false,
  3868. 'label' => false
  3869. ));
  3870. $this->assertTags($result, $expected);
  3871. $Contact = ClassRegistry::getObject('Contact');
  3872. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3873. $result = $this->Form->input('Contact.tags', array(
  3874. 'options' => array('one'),
  3875. 'multiple' => 'checkbox',
  3876. 'label' => false,
  3877. 'div' => false
  3878. ));
  3879. $expected = array(
  3880. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3881. array('div' => array('class' => 'checkbox form-error')),
  3882. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3883. array('label' => array('for' => 'ContactTags0')),
  3884. 'one',
  3885. '/label',
  3886. '/div'
  3887. );
  3888. $this->assertTags($result, $expected);
  3889. $result = $this->Form->input('Contact.tags', array(
  3890. 'options' => array('one'),
  3891. 'multiple' => 'checkbox',
  3892. 'class' => 'mycheckbox',
  3893. 'label' => false,
  3894. 'div' => false
  3895. ));
  3896. $expected = array(
  3897. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3898. array('div' => array('class' => 'mycheckbox form-error')),
  3899. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3900. array('label' => array('for' => 'ContactTags0')),
  3901. 'one',
  3902. '/label',
  3903. '/div'
  3904. );
  3905. $this->assertTags($result, $expected);
  3906. }
  3907. /**
  3908. * Checks the security hash array generated for multiple-input checkbox elements
  3909. *
  3910. * @return void
  3911. */
  3912. public function testSelectMultipleCheckboxSecurity() {
  3913. $this->Form->request['_Token'] = array('key' => 'testKey');
  3914. $this->assertEquals(array(), $this->Form->fields);
  3915. $result = $this->Form->select(
  3916. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3917. array('multiple' => 'checkbox')
  3918. );
  3919. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3920. $result = $this->Form->secure($this->Form->fields);
  3921. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3922. $this->assertRegExp('/"' . $key . '"/', $result);
  3923. }
  3924. /**
  3925. * Multiple select elements should always be secured as they always participate
  3926. * in the POST data.
  3927. *
  3928. * @return void
  3929. */
  3930. public function testSelectMultipleSecureWithNoOptions() {
  3931. $this->Form->request['_Token'] = array('key' => 'testkey');
  3932. $this->assertEquals(array(), $this->Form->fields);
  3933. $result = $this->Form->select(
  3934. 'Model.select',
  3935. array(),
  3936. array('multiple' => true)
  3937. );
  3938. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3939. }
  3940. /**
  3941. * When a select box has no options it should not be added to the fields list
  3942. * as it always fail post validation.
  3943. *
  3944. * @return void
  3945. */
  3946. public function testSelectNoSecureWithNoOptions() {
  3947. $this->Form->request['_Token'] = array('key' => 'testkey');
  3948. $this->assertEquals(array(), $this->Form->fields);
  3949. $this->Form->select(
  3950. 'Model.select',
  3951. array()
  3952. );
  3953. $this->assertEquals(array(), $this->Form->fields);
  3954. $this->Form->select(
  3955. 'Model.select',
  3956. array(),
  3957. array('empty' => true)
  3958. );
  3959. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3960. }
  3961. /**
  3962. * testInputMultipleCheckboxes method
  3963. *
  3964. * test input() resulting in multi select elements being generated.
  3965. *
  3966. * @return void
  3967. */
  3968. public function testInputMultipleCheckboxes() {
  3969. $result = $this->Form->input('Model.multi_field', array(
  3970. 'options' => array('first', 'second', 'third'),
  3971. 'multiple' => 'checkbox'
  3972. ));
  3973. $expected = array(
  3974. array('div' => array('class' => 'input select')),
  3975. array('label' => array('for' => 'ModelMultiField')),
  3976. 'Multi Field',
  3977. '/label',
  3978. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3979. array('div' => array('class' => 'checkbox')),
  3980. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3981. array('label' => array('for' => 'ModelMultiField0')),
  3982. 'first',
  3983. '/label',
  3984. '/div',
  3985. array('div' => array('class' => 'checkbox')),
  3986. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3987. array('label' => array('for' => 'ModelMultiField1')),
  3988. 'second',
  3989. '/label',
  3990. '/div',
  3991. array('div' => array('class' => 'checkbox')),
  3992. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3993. array('label' => array('for' => 'ModelMultiField2')),
  3994. 'third',
  3995. '/label',
  3996. '/div',
  3997. '/div'
  3998. );
  3999. $this->assertTags($result, $expected);
  4000. $result = $this->Form->input('Model.multi_field', array(
  4001. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4002. 'multiple' => 'checkbox'
  4003. ));
  4004. $expected = array(
  4005. array('div' => array('class' => 'input select')),
  4006. array('label' => array('for' => 'ModelMultiField')),
  4007. 'Multi Field',
  4008. '/label',
  4009. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4010. array('div' => array('class' => 'checkbox')),
  4011. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4012. array('label' => array('for' => 'ModelMultiFieldA')),
  4013. 'first',
  4014. '/label',
  4015. '/div',
  4016. array('div' => array('class' => 'checkbox')),
  4017. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4018. array('label' => array('for' => 'ModelMultiFieldB')),
  4019. 'second',
  4020. '/label',
  4021. '/div',
  4022. array('div' => array('class' => 'checkbox')),
  4023. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4024. array('label' => array('for' => 'ModelMultiFieldC')),
  4025. 'third',
  4026. '/label',
  4027. '/div',
  4028. '/div'
  4029. );
  4030. $this->assertTags($result, $expected);
  4031. $result = $this->Form->input('Model.multi_field', array(
  4032. 'options' => array('1' => 'first'),
  4033. 'multiple' => 'checkbox',
  4034. 'label' => false,
  4035. 'div' => false
  4036. ));
  4037. $expected = array(
  4038. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4039. array('div' => array('class' => 'checkbox')),
  4040. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4041. array('label' => array('for' => 'ModelMultiField1')),
  4042. 'first',
  4043. '/label',
  4044. '/div'
  4045. );
  4046. $this->assertTags($result, $expected);
  4047. $result = $this->Form->input('Model.multi_field', array(
  4048. 'options' => array('2' => 'second'),
  4049. 'multiple' => 'checkbox',
  4050. 'label' => false,
  4051. 'div' => false
  4052. ));
  4053. $expected = array(
  4054. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4055. array('div' => array('class' => 'checkbox')),
  4056. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4057. array('label' => array('for' => 'ModelMultiField2')),
  4058. 'second',
  4059. '/label',
  4060. '/div'
  4061. );
  4062. $this->assertTags($result, $expected);
  4063. }
  4064. /**
  4065. * testSelectHiddenFieldOmission method
  4066. *
  4067. * test that select() with 'hiddenField' => false omits the hidden field
  4068. *
  4069. * @return void
  4070. */
  4071. public function testSelectHiddenFieldOmission() {
  4072. $result = $this->Form->select('Model.multi_field',
  4073. array('first', 'second'),
  4074. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4075. );
  4076. $expected = array(
  4077. array('div' => array('class' => 'checkbox')),
  4078. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4079. array('label' => array('for' => 'ModelMultiField0')),
  4080. 'first',
  4081. '/label',
  4082. '/div',
  4083. array('div' => array('class' => 'checkbox')),
  4084. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4085. array('label' => array('for' => 'ModelMultiField1')),
  4086. 'second',
  4087. '/label',
  4088. '/div'
  4089. );
  4090. $this->assertTags($result, $expected);
  4091. $result = $this->Form->input('Model.multi_field', array(
  4092. 'options' => array('first', 'second'),
  4093. 'multiple' => 'checkbox',
  4094. 'hiddenField' => false
  4095. ));
  4096. $expected = array(
  4097. array('div' => array('class' => 'input select')),
  4098. array('label' => array('for' => 'ModelMultiField')),
  4099. 'Multi Field',
  4100. '/label',
  4101. array('div' => array('class' => 'checkbox')),
  4102. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4103. array('label' => array('for' => 'ModelMultiField0')),
  4104. 'first',
  4105. '/label',
  4106. '/div',
  4107. array('div' => array('class' => 'checkbox')),
  4108. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4109. array('label' => array('for' => 'ModelMultiField1')),
  4110. 'second',
  4111. '/label',
  4112. '/div',
  4113. '/div'
  4114. );
  4115. $this->assertTags($result, $expected);
  4116. }
  4117. /**
  4118. * test that select() with multiple = checkbox works with overriding name attribute.
  4119. *
  4120. * @return void
  4121. */
  4122. public function testSelectCheckboxMultipleOverrideName() {
  4123. $result = $this->Form->input('category', array(
  4124. 'type' => 'select',
  4125. 'multiple' => 'checkbox',
  4126. 'name' => 'data[fish]',
  4127. 'options' => array('1', '2'),
  4128. 'div' => false,
  4129. 'label' => false,
  4130. ));
  4131. $expected = array(
  4132. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4133. array('div' => array('class' => 'checkbox')),
  4134. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4135. array('label' => array('for' => 'Category0')), '1', '/label',
  4136. '/div',
  4137. array('div' => array('class' => 'checkbox')),
  4138. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4139. array('label' => array('for' => 'Category1')), '2', '/label',
  4140. '/div'
  4141. );
  4142. $this->assertTags($result, $expected);
  4143. }
  4144. /**
  4145. * Test that 'id' overrides all the checkbox id's as well.
  4146. *
  4147. * @return void
  4148. */
  4149. public function testSelectCheckboxMultipleId() {
  4150. $result = $this->Form->select(
  4151. 'Model.multi_field',
  4152. array('first', 'second', 'third'),
  4153. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4154. );
  4155. $expected = array(
  4156. 'input' => array(
  4157. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4158. ),
  4159. array('div' => array('class' => 'checkbox')),
  4160. array('input' => array(
  4161. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4162. 'value' => '0', 'id' => 'CustomId0'
  4163. )),
  4164. array('label' => array('for' => 'CustomId0')),
  4165. 'first',
  4166. '/label',
  4167. '/div',
  4168. array('div' => array('class' => 'checkbox')),
  4169. array('input' => array(
  4170. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4171. 'value' => '1', 'id' => 'CustomId1'
  4172. )),
  4173. array('label' => array('for' => 'CustomId1')),
  4174. 'second',
  4175. '/label',
  4176. '/div',
  4177. array('div' => array('class' => 'checkbox')),
  4178. array('input' => array(
  4179. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4180. 'value' => '2', 'id' => 'CustomId2'
  4181. )),
  4182. array('label' => array('for' => 'CustomId2')),
  4183. 'third',
  4184. '/label',
  4185. '/div'
  4186. );
  4187. $this->assertTags($result, $expected);
  4188. }
  4189. /**
  4190. * testCheckbox method
  4191. *
  4192. * Test generation of checkboxes
  4193. *
  4194. * @return void
  4195. */
  4196. public function testCheckbox() {
  4197. $result = $this->Form->checkbox('Model.field');
  4198. $expected = array(
  4199. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4200. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4201. );
  4202. $this->assertTags($result, $expected);
  4203. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4204. $expected = array(
  4205. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4206. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4207. );
  4208. $this->assertTags($result, $expected);
  4209. $Contact = ClassRegistry::getObject('Contact');
  4210. $Contact->validationErrors['field'] = 1;
  4211. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4212. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4213. $expected = array(
  4214. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4215. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4216. );
  4217. $this->assertTags($result, $expected);
  4218. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4219. $expected = array(
  4220. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4221. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4222. );
  4223. $this->assertTags($result, $expected);
  4224. $this->Form->request->data['Contact']['field'] = '';
  4225. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4226. $expected = array(
  4227. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4228. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4229. );
  4230. $this->assertTags($result, $expected);
  4231. $Contact->validationErrors = array();
  4232. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4233. $expected = array(
  4234. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4235. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4236. );
  4237. $this->assertTags($result, $expected);
  4238. $this->Form->request->data['Contact']['published'] = 1;
  4239. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4240. $expected = array(
  4241. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4242. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4243. );
  4244. $this->assertTags($result, $expected);
  4245. $this->Form->request->data['Contact']['published'] = 0;
  4246. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4247. $expected = array(
  4248. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4249. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4250. );
  4251. $this->assertTags($result, $expected);
  4252. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4253. $expected = array(
  4254. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4255. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4256. );
  4257. $this->assertTags($result, $expected);
  4258. $result = $this->Form->checkbox('CustomField.1.value');
  4259. $expected = array(
  4260. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4261. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4262. );
  4263. $this->assertTags($result, $expected);
  4264. }
  4265. /**
  4266. * test checkbox() with a custom name attribute
  4267. *
  4268. * @return void
  4269. */
  4270. public function testCheckboxCustomNameAttribute() {
  4271. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4272. $expected = array(
  4273. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4274. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4275. );
  4276. $this->assertTags($result, $expected);
  4277. }
  4278. /**
  4279. * test the checked option for checkboxes.
  4280. *
  4281. * @return void
  4282. */
  4283. public function testCheckboxCheckedOption() {
  4284. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4285. $expected = array(
  4286. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4287. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4288. );
  4289. $this->assertTags($result, $expected);
  4290. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4291. $expected = array(
  4292. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4293. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4294. );
  4295. $this->assertTags($result, $expected);
  4296. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4297. $expected = array(
  4298. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4299. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4300. );
  4301. $this->assertTags($result, $expected);
  4302. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4303. $expected = array(
  4304. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4305. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4306. );
  4307. $this->assertTags($result, $expected);
  4308. $this->Form->request->data['Model']['field'] = 1;
  4309. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4310. $expected = array(
  4311. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4312. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4313. );
  4314. $this->assertTags($result, $expected);
  4315. }
  4316. /**
  4317. * Test that disabled attribute works on both the checkbox and hidden input.
  4318. *
  4319. * @return void
  4320. */
  4321. public function testCheckboxDisabling() {
  4322. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4323. $expected = array(
  4324. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4325. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4326. );
  4327. $this->assertTags($result, $expected);
  4328. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4329. $expected = array(
  4330. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4331. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4332. );
  4333. $this->assertTags($result, $expected);
  4334. }
  4335. /**
  4336. * Test that the hidden input for checkboxes can be omitted or set to a
  4337. * specific value.
  4338. *
  4339. * @return void
  4340. */
  4341. public function testCheckboxHiddenField() {
  4342. $result = $this->Form->input('UserForm.something', array(
  4343. 'type' => 'checkbox',
  4344. 'hiddenField' => false
  4345. ));
  4346. $expected = array(
  4347. 'div' => array('class' => 'input checkbox'),
  4348. array('input' => array(
  4349. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4350. 'value' => '1', 'id' => 'UserFormSomething'
  4351. )),
  4352. 'label' => array('for' => 'UserFormSomething'),
  4353. 'Something',
  4354. '/label',
  4355. '/div'
  4356. );
  4357. $this->assertTags($result, $expected);
  4358. $result = $this->Form->input('UserForm.something', array(
  4359. 'type' => 'checkbox',
  4360. 'value' => 'Y',
  4361. 'hiddenField' => 'N',
  4362. ));
  4363. $expected = array(
  4364. 'div' => array('class' => 'input checkbox'),
  4365. array('input' => array(
  4366. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4367. 'value' => 'N', 'id' => 'UserFormSomething_'
  4368. )),
  4369. array('input' => array(
  4370. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4371. 'value' => 'Y', 'id' => 'UserFormSomething'
  4372. )),
  4373. 'label' => array('for' => 'UserFormSomething'),
  4374. 'Something',
  4375. '/label',
  4376. '/div'
  4377. );
  4378. $this->assertTags($result, $expected);
  4379. }
  4380. /**
  4381. * testDateTime method
  4382. *
  4383. * Test generation of date/time select elements
  4384. *
  4385. * @return void
  4386. */
  4387. public function testDateTime() {
  4388. extract($this->dateRegex);
  4389. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4390. $now = strtotime('now');
  4391. $expected = array(
  4392. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4393. $daysRegex,
  4394. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4395. date('j', $now),
  4396. '/option',
  4397. '*/select',
  4398. '-',
  4399. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4400. $monthsRegex,
  4401. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4402. date('F', $now),
  4403. '/option',
  4404. '*/select',
  4405. '-',
  4406. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4407. $yearsRegex,
  4408. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4409. date('Y', $now),
  4410. '/option',
  4411. '*/select',
  4412. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4413. $hoursRegex,
  4414. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4415. date('g', $now),
  4416. '/option',
  4417. '*/select',
  4418. ':',
  4419. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4420. $minutesRegex,
  4421. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4422. date('i', $now),
  4423. '/option',
  4424. '*/select',
  4425. ' ',
  4426. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4427. $meridianRegex,
  4428. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4429. date('a', $now),
  4430. '/option',
  4431. '*/select'
  4432. );
  4433. $this->assertTags($result, $expected);
  4434. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4435. $expected = array(
  4436. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4437. $daysRegex,
  4438. array('option' => array('value' => '')),
  4439. '/option',
  4440. '*/select',
  4441. '-',
  4442. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4443. $monthsRegex,
  4444. array('option' => array('value' => '')),
  4445. '/option',
  4446. '*/select',
  4447. '-',
  4448. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4449. $yearsRegex,
  4450. array('option' => array('value' => '')),
  4451. '/option',
  4452. '*/select',
  4453. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4454. $hoursRegex,
  4455. array('option' => array('value' => '')),
  4456. '/option',
  4457. '*/select',
  4458. ':',
  4459. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4460. $minutesRegex,
  4461. array('option' => array('value' => '')),
  4462. '/option',
  4463. '*/select',
  4464. ' ',
  4465. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4466. $meridianRegex,
  4467. array('option' => array('value' => '')),
  4468. '/option',
  4469. '*/select'
  4470. );
  4471. $this->assertTags($result, $expected);
  4472. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4473. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4474. $this->assertTags($result, $expected);
  4475. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4476. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4477. $this->assertTags($result, $expected);
  4478. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4479. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4480. $expected = array(
  4481. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4482. $daysRegex,
  4483. array('option' => array('value' => '')),
  4484. '/option',
  4485. '*/select',
  4486. '-',
  4487. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4488. $monthsRegex,
  4489. array('option' => array('value' => '')),
  4490. '/option',
  4491. '*/select',
  4492. '-',
  4493. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4494. $yearsRegex,
  4495. array('option' => array('value' => '')),
  4496. '/option',
  4497. '*/select',
  4498. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4499. $hoursRegex,
  4500. array('option' => array('value' => '')),
  4501. '/option',
  4502. '*/select',
  4503. ':',
  4504. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4505. $minutesRegex,
  4506. array('option' => array('value' => '')),
  4507. '/option',
  4508. array('option' => array('value' => '00')),
  4509. '00',
  4510. '/option',
  4511. array('option' => array('value' => '05')),
  4512. '05',
  4513. '/option',
  4514. array('option' => array('value' => '10')),
  4515. '10',
  4516. '/option',
  4517. '*/select',
  4518. ' ',
  4519. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4520. $meridianRegex,
  4521. array('option' => array('value' => '')),
  4522. '/option',
  4523. '*/select'
  4524. );
  4525. $this->assertTags($result, $expected);
  4526. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4527. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4528. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4529. $this->Form->request->data['Contact']['data'] = null;
  4530. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4531. $expected = array(
  4532. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4533. $daysRegex,
  4534. array('option' => array('value' => '')),
  4535. '/option',
  4536. '*/select',
  4537. '-',
  4538. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4539. $monthsRegex,
  4540. array('option' => array('value' => '')),
  4541. '/option',
  4542. '*/select',
  4543. '-',
  4544. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4545. $yearsRegex,
  4546. array('option' => array('value' => '')),
  4547. '/option',
  4548. '*/select',
  4549. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4550. $hoursRegex,
  4551. array('option' => array('value' => '')),
  4552. '/option',
  4553. '*/select',
  4554. ':',
  4555. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4556. $minutesRegex,
  4557. array('option' => array('value' => '')),
  4558. '/option',
  4559. '*/select',
  4560. ' ',
  4561. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4562. $meridianRegex,
  4563. array('option' => array('value' => '')),
  4564. '/option',
  4565. '*/select'
  4566. );
  4567. $this->assertTags($result, $expected);
  4568. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4569. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4570. $now = strtotime($this->Form->data['Model']['field']);
  4571. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4572. $expected = array(
  4573. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4574. $daysRegex,
  4575. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4576. date('j', $now),
  4577. '/option',
  4578. '*/select',
  4579. '-',
  4580. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4581. $monthsRegex,
  4582. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4583. date('F', $now),
  4584. '/option',
  4585. '*/select',
  4586. '-',
  4587. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4588. $yearsRegex,
  4589. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4590. date('Y', $now),
  4591. '/option',
  4592. '*/select',
  4593. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4594. $hoursRegex,
  4595. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4596. date('g', $now),
  4597. '/option',
  4598. '*/select',
  4599. ':',
  4600. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4601. $minutesRegex,
  4602. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4603. date('i', $now),
  4604. '/option',
  4605. '*/select',
  4606. ' ',
  4607. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4608. $meridianRegex,
  4609. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4610. date('a', $now),
  4611. '/option',
  4612. '*/select'
  4613. );
  4614. $this->assertTags($result, $expected);
  4615. $selected = strtotime('2008-10-26 12:33:00');
  4616. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4617. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4618. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4619. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4620. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4621. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4622. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4623. $this->Form->create('Contact');
  4624. $result = $this->Form->input('published');
  4625. $now = strtotime('now');
  4626. $expected = array(
  4627. 'div' => array('class' => 'input date'),
  4628. 'label' => array('for' => 'ContactPublishedMonth'),
  4629. 'Published',
  4630. '/label',
  4631. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4632. $monthsRegex,
  4633. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4634. date('F', $now),
  4635. '/option',
  4636. '*/select',
  4637. '-',
  4638. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4639. $daysRegex,
  4640. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4641. date('j', $now),
  4642. '/option',
  4643. '*/select',
  4644. '-',
  4645. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4646. $yearsRegex,
  4647. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4648. date('Y', $now),
  4649. '/option',
  4650. '*/select',
  4651. '/div'
  4652. );
  4653. $this->assertTags($result, $expected);
  4654. $result = $this->Form->input('published2', array('type' => 'date'));
  4655. $now = strtotime('now');
  4656. $expected = array(
  4657. 'div' => array('class' => 'input date'),
  4658. 'label' => array('for' => 'ContactPublished2Month'),
  4659. 'Published2',
  4660. '/label',
  4661. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4662. $monthsRegex,
  4663. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4664. date('F', $now),
  4665. '/option',
  4666. '*/select',
  4667. '-',
  4668. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4669. $daysRegex,
  4670. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4671. date('j', $now),
  4672. '/option',
  4673. '*/select',
  4674. '-',
  4675. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4676. $yearsRegex,
  4677. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4678. date('Y', $now),
  4679. '/option',
  4680. '*/select',
  4681. '/div'
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. $this->Form->create('Contact');
  4685. $result = $this->Form->input('published', array('monthNames' => false));
  4686. $now = strtotime('now');
  4687. $expected = array(
  4688. 'div' => array('class' => 'input date'),
  4689. 'label' => array('for' => 'ContactPublishedMonth'),
  4690. 'Published',
  4691. '/label',
  4692. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4693. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4694. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4695. date('m', $now),
  4696. '/option',
  4697. '*/select',
  4698. '-',
  4699. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4700. $daysRegex,
  4701. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4702. date('j', $now),
  4703. '/option',
  4704. '*/select',
  4705. '-',
  4706. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4707. $yearsRegex,
  4708. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4709. date('Y', $now),
  4710. '/option',
  4711. '*/select',
  4712. '/div'
  4713. );
  4714. $this->assertTags($result, $expected);
  4715. $result = $this->Form->input('published', array('type' => 'time'));
  4716. $now = strtotime('now');
  4717. $expected = array(
  4718. 'div' => array('class' => 'input time'),
  4719. 'label' => array('for' => 'ContactPublishedHour'),
  4720. 'Published',
  4721. '/label',
  4722. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4723. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4724. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4725. date('g', $now),
  4726. '/option',
  4727. '*/select',
  4728. ':',
  4729. );
  4730. $this->assertTags($result, $expected);
  4731. $result = $this->Form->input('published', array(
  4732. 'timeFormat' => 24,
  4733. 'interval' => 5,
  4734. 'selected' => strtotime('2009-09-03 13:37:00'),
  4735. 'type' => 'datetime'
  4736. ));
  4737. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4738. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4739. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4740. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4741. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4742. $this->assertNoErrors();
  4743. $this->Form->request->data['Contact'] = array(
  4744. 'date' => array(
  4745. 'day' => '',
  4746. 'month' => '',
  4747. 'year' => '',
  4748. 'hour' => '',
  4749. 'min' => '',
  4750. 'meridian' => ''
  4751. )
  4752. );
  4753. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4754. }
  4755. /**
  4756. * test that datetime() and default values work.
  4757. *
  4758. * @return void
  4759. */
  4760. public function testDatetimeWithDefault() {
  4761. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4762. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4763. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4764. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4765. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4766. 'default' => '2009-06-01 11:15:30'
  4767. ));
  4768. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4769. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4770. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4771. }
  4772. /**
  4773. * test that bogus non-date time data doesn't cause errors.
  4774. *
  4775. * @return void
  4776. */
  4777. public function testDateTimeWithBogusData() {
  4778. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4779. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4780. }
  4781. /**
  4782. * testDateTimeEmptyAsArray
  4783. *
  4784. * @return void
  4785. */
  4786. public function testDateTimeEmptyAsArray() {
  4787. $result = $this->Form->dateTime('Contact.date',
  4788. 'DMY',
  4789. '12',
  4790. array(
  4791. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4792. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4793. )
  4794. )
  4795. );
  4796. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4797. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4798. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4799. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4800. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4801. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4802. $result = $this->Form->dateTime('Contact.date',
  4803. 'DMY',
  4804. '12',
  4805. array(
  4806. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4807. )
  4808. );
  4809. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4810. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4811. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4812. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4813. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4814. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4815. }
  4816. /**
  4817. * testFormDateTimeMulti method
  4818. *
  4819. * test multiple datetime element generation
  4820. *
  4821. * @return void
  4822. */
  4823. public function testFormDateTimeMulti() {
  4824. extract($this->dateRegex);
  4825. $result = $this->Form->dateTime('Contact.1.updated');
  4826. $expected = array(
  4827. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4828. $daysRegex,
  4829. array('option' => array('value' => '')),
  4830. '/option',
  4831. '*/select',
  4832. '-',
  4833. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4834. $monthsRegex,
  4835. array('option' => array('value' => '')),
  4836. '/option',
  4837. '*/select',
  4838. '-',
  4839. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4840. $yearsRegex,
  4841. array('option' => array('value' => '')),
  4842. '/option',
  4843. '*/select',
  4844. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4845. $hoursRegex,
  4846. array('option' => array('value' => '')),
  4847. '/option',
  4848. '*/select',
  4849. ':',
  4850. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4851. $minutesRegex,
  4852. array('option' => array('value' => '')),
  4853. '/option',
  4854. '*/select',
  4855. ' ',
  4856. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4857. $meridianRegex,
  4858. array('option' => array('value' => '')),
  4859. '/option',
  4860. '*/select'
  4861. );
  4862. $this->assertTags($result, $expected);
  4863. $result = $this->Form->dateTime('Contact.2.updated');
  4864. $expected = array(
  4865. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4866. $daysRegex,
  4867. array('option' => array('value' => '')),
  4868. '/option',
  4869. '*/select',
  4870. '-',
  4871. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4872. $monthsRegex,
  4873. array('option' => array('value' => '')),
  4874. '/option',
  4875. '*/select',
  4876. '-',
  4877. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4878. $yearsRegex,
  4879. array('option' => array('value' => '')),
  4880. '/option',
  4881. '*/select',
  4882. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4883. $hoursRegex,
  4884. array('option' => array('value' => '')),
  4885. '/option',
  4886. '*/select',
  4887. ':',
  4888. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4889. $minutesRegex,
  4890. array('option' => array('value' => '')),
  4891. '/option',
  4892. '*/select',
  4893. ' ',
  4894. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4895. $meridianRegex,
  4896. array('option' => array('value' => '')),
  4897. '/option',
  4898. '*/select'
  4899. );
  4900. $this->assertTags($result, $expected);
  4901. }
  4902. /**
  4903. * When changing the date format, the label should always focus the first select box when
  4904. * clicked.
  4905. *
  4906. * @return void
  4907. */
  4908. public function testDateTimeLabelIdMatchesFirstInput() {
  4909. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4910. $this->assertContains('label for="ModelDateMonth"', $result);
  4911. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4912. $this->assertContains('label for="ModelDateDay"', $result);
  4913. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4914. $this->assertContains('label for="ModelDateYear"', $result);
  4915. }
  4916. /**
  4917. * testMonth method
  4918. *
  4919. * @return void
  4920. */
  4921. public function testMonth() {
  4922. $result = $this->Form->month('Model.field');
  4923. $expected = array(
  4924. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4925. array('option' => array('value' => '')),
  4926. '/option',
  4927. array('option' => array('value' => '01')),
  4928. date('F', strtotime('2008-01-01 00:00:00')),
  4929. '/option',
  4930. array('option' => array('value' => '02')),
  4931. date('F', strtotime('2008-02-01 00:00:00')),
  4932. '/option',
  4933. '*/select',
  4934. );
  4935. $this->assertTags($result, $expected);
  4936. $result = $this->Form->month('Model.field', array('empty' => true));
  4937. $expected = array(
  4938. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4939. array('option' => array('value' => '')),
  4940. '/option',
  4941. array('option' => array('value' => '01')),
  4942. date('F', strtotime('2008-01-01 00:00:00')),
  4943. '/option',
  4944. array('option' => array('value' => '02')),
  4945. date('F', strtotime('2008-02-01 00:00:00')),
  4946. '/option',
  4947. '*/select',
  4948. );
  4949. $this->assertTags($result, $expected);
  4950. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4951. $expected = array(
  4952. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4953. array('option' => array('value' => '')),
  4954. '/option',
  4955. array('option' => array('value' => '01')),
  4956. '01',
  4957. '/option',
  4958. array('option' => array('value' => '02')),
  4959. '02',
  4960. '/option',
  4961. '*/select',
  4962. );
  4963. $this->assertTags($result, $expected);
  4964. $monthNames = array(
  4965. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4966. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4967. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4968. $expected = array(
  4969. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4970. array('option' => array('value' => '')),
  4971. '/option',
  4972. array('option' => array('value' => '01')),
  4973. 'Jan',
  4974. '/option',
  4975. array('option' => array('value' => '02')),
  4976. 'Feb',
  4977. '/option',
  4978. '*/select',
  4979. );
  4980. $this->assertTags($result, $expected);
  4981. }
  4982. /**
  4983. * testDay method
  4984. *
  4985. * @return void
  4986. */
  4987. public function testDay() {
  4988. extract($this->dateRegex);
  4989. $result = $this->Form->day('Model.field', array('value' => false));
  4990. $expected = array(
  4991. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4992. array('option' => array('value' => '')),
  4993. '/option',
  4994. array('option' => array('value' => '01')),
  4995. '1',
  4996. '/option',
  4997. array('option' => array('value' => '02')),
  4998. '2',
  4999. '/option',
  5000. $daysRegex,
  5001. '/select',
  5002. );
  5003. $this->assertTags($result, $expected);
  5004. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5005. $result = $this->Form->day('Model.field');
  5006. $expected = array(
  5007. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5008. array('option' => array('value' => '')),
  5009. '/option',
  5010. array('option' => array('value' => '01')),
  5011. '1',
  5012. '/option',
  5013. array('option' => array('value' => '02')),
  5014. '2',
  5015. '/option',
  5016. $daysRegex,
  5017. array('option' => array('value' => '10', 'selected' => 'selected')),
  5018. '10',
  5019. '/option',
  5020. $daysRegex,
  5021. '/select',
  5022. );
  5023. $this->assertTags($result, $expected);
  5024. $this->Form->request->data['Model']['field'] = '';
  5025. $result = $this->Form->day('Model.field', array('value' => '10'));
  5026. $expected = array(
  5027. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5028. array('option' => array('value' => '')),
  5029. '/option',
  5030. array('option' => array('value' => '01')),
  5031. '1',
  5032. '/option',
  5033. array('option' => array('value' => '02')),
  5034. '2',
  5035. '/option',
  5036. $daysRegex,
  5037. array('option' => array('value' => '10', 'selected' => 'selected')),
  5038. '10',
  5039. '/option',
  5040. $daysRegex,
  5041. '/select',
  5042. );
  5043. $this->assertTags($result, $expected);
  5044. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5045. $result = $this->Form->day('Model.field', array('value' => true));
  5046. $expected = array(
  5047. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5048. array('option' => array('value' => '')),
  5049. '/option',
  5050. array('option' => array('value' => '01')),
  5051. '1',
  5052. '/option',
  5053. array('option' => array('value' => '02')),
  5054. '2',
  5055. '/option',
  5056. $daysRegex,
  5057. array('option' => array('value' => '10', 'selected' => 'selected')),
  5058. '10',
  5059. '/option',
  5060. $daysRegex,
  5061. '/select',
  5062. );
  5063. $this->assertTags($result, $expected);
  5064. }
  5065. /**
  5066. * testMinute method
  5067. *
  5068. * @return void
  5069. */
  5070. public function testMinute() {
  5071. extract($this->dateRegex);
  5072. $result = $this->Form->minute('Model.field');
  5073. $expected = array(
  5074. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5075. array('option' => array('value' => '')),
  5076. '/option',
  5077. array('option' => array('value' => '00')),
  5078. '00',
  5079. '/option',
  5080. array('option' => array('value' => '01')),
  5081. '01',
  5082. '/option',
  5083. array('option' => array('value' => '02')),
  5084. '02',
  5085. '/option',
  5086. $minutesRegex,
  5087. '/select',
  5088. );
  5089. $this->assertTags($result, $expected);
  5090. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5091. $result = $this->Form->minute('Model.field');
  5092. $expected = array(
  5093. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5094. array('option' => array('value' => '')),
  5095. '/option',
  5096. array('option' => array('value' => '00')),
  5097. '00',
  5098. '/option',
  5099. array('option' => array('value' => '01')),
  5100. '01',
  5101. '/option',
  5102. array('option' => array('value' => '02')),
  5103. '02',
  5104. '/option',
  5105. $minutesRegex,
  5106. array('option' => array('value' => '12', 'selected' => 'selected')),
  5107. '12',
  5108. '/option',
  5109. $minutesRegex,
  5110. '/select',
  5111. );
  5112. $this->assertTags($result, $expected);
  5113. $this->Form->request->data['Model']['field'] = '';
  5114. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5115. $expected = array(
  5116. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5117. array('option' => array('value' => '')),
  5118. '/option',
  5119. array('option' => array('value' => '00')),
  5120. '00',
  5121. '/option',
  5122. array('option' => array('value' => '05')),
  5123. '05',
  5124. '/option',
  5125. array('option' => array('value' => '10')),
  5126. '10',
  5127. '/option',
  5128. $minutesRegex,
  5129. '/select',
  5130. );
  5131. $this->assertTags($result, $expected);
  5132. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5133. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5134. $expected = array(
  5135. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5136. array('option' => array('value' => '')),
  5137. '/option',
  5138. array('option' => array('value' => '00')),
  5139. '00',
  5140. '/option',
  5141. array('option' => array('value' => '05')),
  5142. '05',
  5143. '/option',
  5144. array('option' => array('value' => '10', 'selected' => 'selected')),
  5145. '10',
  5146. '/option',
  5147. $minutesRegex,
  5148. '/select',
  5149. );
  5150. $this->assertTags($result, $expected);
  5151. }
  5152. /**
  5153. * testHour method
  5154. *
  5155. * @return void
  5156. */
  5157. public function testHour() {
  5158. extract($this->dateRegex);
  5159. $result = $this->Form->hour('Model.field', false);
  5160. $expected = array(
  5161. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5162. array('option' => array('value' => '')),
  5163. '/option',
  5164. array('option' => array('value' => '01')),
  5165. '1',
  5166. '/option',
  5167. array('option' => array('value' => '02')),
  5168. '2',
  5169. '/option',
  5170. $hoursRegex,
  5171. '/select',
  5172. );
  5173. $this->assertTags($result, $expected);
  5174. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5175. $result = $this->Form->hour('Model.field', false);
  5176. $expected = array(
  5177. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5178. array('option' => array('value' => '')),
  5179. '/option',
  5180. array('option' => array('value' => '01')),
  5181. '1',
  5182. '/option',
  5183. array('option' => array('value' => '02')),
  5184. '2',
  5185. '/option',
  5186. $hoursRegex,
  5187. array('option' => array('value' => '12', 'selected' => 'selected')),
  5188. '12',
  5189. '/option',
  5190. '/select',
  5191. );
  5192. $this->assertTags($result, $expected);
  5193. $this->Form->request->data['Model']['field'] = '';
  5194. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5195. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5196. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5197. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5198. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5199. $result = $this->Form->hour('Model.field', true);
  5200. $expected = array(
  5201. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5202. array('option' => array('value' => '')),
  5203. '/option',
  5204. array('option' => array('value' => '00', 'selected' => 'selected')),
  5205. '0',
  5206. '/option',
  5207. array('option' => array('value' => '01')),
  5208. '1',
  5209. '/option',
  5210. array('option' => array('value' => '02')),
  5211. '2',
  5212. '/option',
  5213. $hoursRegex,
  5214. '/select',
  5215. );
  5216. $this->assertTags($result, $expected);
  5217. unset($this->Form->request->data['Model']['field']);
  5218. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5219. $thisHour = date('H');
  5220. $optValue = date('G');
  5221. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5222. }
  5223. /**
  5224. * testYear method
  5225. *
  5226. * @return void
  5227. */
  5228. public function testYear() {
  5229. $result = $this->Form->year('Model.field', 2006, 2007);
  5230. $expected = array(
  5231. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5232. array('option' => array('value' => '')),
  5233. '/option',
  5234. array('option' => array('value' => '2007')),
  5235. '2007',
  5236. '/option',
  5237. array('option' => array('value' => '2006')),
  5238. '2006',
  5239. '/option',
  5240. '/select',
  5241. );
  5242. $this->assertTags($result, $expected);
  5243. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5244. $expected = array(
  5245. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5246. array('option' => array('value' => '')),
  5247. '/option',
  5248. array('option' => array('value' => '2006')),
  5249. '2006',
  5250. '/option',
  5251. array('option' => array('value' => '2007')),
  5252. '2007',
  5253. '/option',
  5254. '/select',
  5255. );
  5256. $this->assertTags($result, $expected);
  5257. $this->request->data['Contact']['published'] = '';
  5258. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5259. $expected = array(
  5260. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5261. array('option' => array('value' => '')),
  5262. '/option',
  5263. array('option' => array('value' => '2007')),
  5264. '2007',
  5265. '/option',
  5266. array('option' => array('value' => '2006')),
  5267. '2006',
  5268. '/option',
  5269. '/select',
  5270. );
  5271. $this->assertTags($result, $expected);
  5272. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5273. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5274. $expected = array(
  5275. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5276. array('option' => array('value' => '2007')),
  5277. '2007',
  5278. '/option',
  5279. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5280. '2006',
  5281. '/option',
  5282. '/select',
  5283. );
  5284. $this->assertTags($result, $expected);
  5285. $this->Form->request->data['Contact']['published'] = '';
  5286. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5287. $expected = array(
  5288. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5289. array('option' => array('value' => '')),
  5290. '/option',
  5291. array('option' => array('value' => '2007')),
  5292. '2007',
  5293. '/option',
  5294. array('option' => array('value' => '2006')),
  5295. '2006',
  5296. '/option',
  5297. '/select',
  5298. );
  5299. $this->assertTags($result, $expected);
  5300. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5301. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5302. $expected = array(
  5303. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5304. array('option' => array('value' => '2007')),
  5305. '2007',
  5306. '/option',
  5307. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5308. '2006',
  5309. '/option',
  5310. '/select',
  5311. );
  5312. $this->assertTags($result, $expected);
  5313. $this->Form->request->data['Contact']['published'] = '';
  5314. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5315. $expected = array(
  5316. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5317. array('option' => array('value' => '')),
  5318. '/option',
  5319. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5320. '2007',
  5321. '/option',
  5322. array('option' => array('value' => '2006')),
  5323. '2006',
  5324. '/option',
  5325. '/select',
  5326. );
  5327. $this->assertTags($result, $expected);
  5328. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5329. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5330. $expected = array(
  5331. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5332. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5333. '2007',
  5334. '/option',
  5335. array('option' => array('value' => '2006')),
  5336. '2006',
  5337. '/option',
  5338. '/select',
  5339. );
  5340. $this->assertTags($result, $expected);
  5341. $this->Form->request->data['Contact']['published'] = '';
  5342. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5343. $expected = array(
  5344. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5345. array('option' => array('value' => '2008')),
  5346. '2008',
  5347. '/option',
  5348. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5349. '2007',
  5350. '/option',
  5351. array('option' => array('value' => '2006')),
  5352. '2006',
  5353. '/option',
  5354. '/select',
  5355. );
  5356. $this->assertTags($result, $expected);
  5357. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5358. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5359. $expected = array(
  5360. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5361. array('option' => array('value' => '2008')),
  5362. '2008',
  5363. '/option',
  5364. array('option' => array('value' => '2007')),
  5365. '2007',
  5366. '/option',
  5367. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5368. '2006',
  5369. '/option',
  5370. '/select',
  5371. );
  5372. $this->assertTags($result, $expected);
  5373. $this->Form->request->data = array();
  5374. $this->Form->create('Contact');
  5375. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5376. $expected = array(
  5377. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5378. array('option' => array('value' => '2008')),
  5379. '2008',
  5380. '/option',
  5381. array('option' => array('value' => '2007')),
  5382. '2007',
  5383. '/option',
  5384. array('option' => array('value' => '2006')),
  5385. '2006',
  5386. '/option',
  5387. '/select',
  5388. );
  5389. $this->assertTags($result, $expected);
  5390. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5391. $this->assertContains('data[Contact][published][year]', $result);
  5392. }
  5393. /**
  5394. * testTextArea method
  5395. *
  5396. * @return void
  5397. */
  5398. public function testTextArea() {
  5399. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5400. $result = $this->Form->textarea('Model.field');
  5401. $expected = array(
  5402. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5403. 'some test data',
  5404. '/textarea',
  5405. );
  5406. $this->assertTags($result, $expected);
  5407. $result = $this->Form->textarea('Model.tmp');
  5408. $expected = array(
  5409. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5410. '/textarea',
  5411. );
  5412. $this->assertTags($result, $expected);
  5413. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5414. $result = $this->Form->textarea('Model.field');
  5415. $expected = array(
  5416. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5417. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5418. '/textarea',
  5419. );
  5420. $this->assertTags($result, $expected);
  5421. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5422. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5423. $expected = array(
  5424. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5425. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5426. '/textarea',
  5427. );
  5428. $this->assertTags($result, $expected);
  5429. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5430. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5431. $expected = array(
  5432. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5433. '/textarea'
  5434. );
  5435. $this->assertTags($result, $expected);
  5436. }
  5437. /**
  5438. * testTextAreaWithStupidCharacters method
  5439. *
  5440. * test text area with non-ascii characters
  5441. *
  5442. * @return void
  5443. */
  5444. public function testTextAreaWithStupidCharacters() {
  5445. $this->loadFixtures('Post');
  5446. $result = $this->Form->input('Post.content', array(
  5447. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5448. ));
  5449. $expected = array(
  5450. 'div' => array('class' => 'input text'),
  5451. 'label' => array('for' => 'PostContent'),
  5452. 'Current Text',
  5453. '/label',
  5454. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5455. 'GREAT®',
  5456. '/textarea',
  5457. '/div'
  5458. );
  5459. $this->assertTags($result, $expected);
  5460. }
  5461. /**
  5462. * testHiddenField method
  5463. *
  5464. * @return void
  5465. */
  5466. public function testHiddenField() {
  5467. $Contact = ClassRegistry::getObject('Contact');
  5468. $Contact->validationErrors['field'] = 1;
  5469. $this->Form->request->data['Contact']['field'] = 'test';
  5470. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5471. $this->assertTags($result, array(
  5472. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5473. );
  5474. }
  5475. /**
  5476. * testFileUploadField method
  5477. *
  5478. * @return void
  5479. */
  5480. public function testFileUploadField() {
  5481. $result = $this->Form->file('Model.upload');
  5482. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5483. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5484. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5485. $expected = array(
  5486. 'div' => array('class' => 'input file'),
  5487. 'label' => array('for' => 'ModelUpload'),
  5488. 'Upload',
  5489. '/label',
  5490. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5491. '/div'
  5492. );
  5493. $this->assertTags($result, $expected);
  5494. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5495. $result = $this->Form->file('Model.upload');
  5496. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5497. }
  5498. /**
  5499. * test File upload input on a model not used in create();
  5500. *
  5501. * @return void
  5502. */
  5503. public function testFileUploadOnOtherModel() {
  5504. $this->Form->create('ValidateUser', array('type' => 'file'));
  5505. $result = $this->Form->file('ValidateProfile.city');
  5506. $expected = array(
  5507. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5508. );
  5509. $this->assertTags($result, $expected);
  5510. }
  5511. /**
  5512. * testButton method
  5513. *
  5514. * @return void
  5515. */
  5516. public function testButton() {
  5517. $result = $this->Form->button('Hi');
  5518. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5519. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5520. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5521. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5522. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5523. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5524. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5525. $result = $this->Form->button('No type', array('type' => false));
  5526. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5527. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5528. $this->assertNotRegExp('/\&039/', $result);
  5529. }
  5530. /**
  5531. * Test that button() makes unlocked fields by default.
  5532. *
  5533. * @return void
  5534. */
  5535. public function testButtonUnlockedByDefault() {
  5536. $this->Form->request->params['_Token']['key'] = 'secured';
  5537. $this->Form->button('Save', array('name' => 'save'));
  5538. $this->Form->button('Clear');
  5539. $result = $this->Form->unlockField();
  5540. $this->assertEquals(array('save'), $result);
  5541. }
  5542. /**
  5543. * testPostButton method
  5544. *
  5545. * @return void
  5546. */
  5547. public function testPostButton() {
  5548. $result = $this->Form->postButton('Hi', '/controller/action');
  5549. $this->assertTags($result, array(
  5550. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5551. 'div' => array('style' => 'display:none;'),
  5552. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5553. '/div',
  5554. 'button' => array('type' => 'submit'),
  5555. 'Hi',
  5556. '/button',
  5557. '/form'
  5558. ));
  5559. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5560. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5561. }
  5562. /**
  5563. * Test that postButton adds _Token fields.
  5564. *
  5565. * @return void
  5566. */
  5567. public function testSecurePostButton() {
  5568. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5569. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5570. $expected = array(
  5571. 'form' => array(
  5572. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5573. ),
  5574. array('div' => array('style' => 'display:none;')),
  5575. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5576. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5577. '/div',
  5578. 'button' => array('type' => 'submit'),
  5579. 'Delete',
  5580. '/button',
  5581. array('div' => array('style' => 'display:none;')),
  5582. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5583. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5584. '/div',
  5585. '/form',
  5586. );
  5587. $this->assertTags($result, $expected);
  5588. }
  5589. /**
  5590. * testPostLink method
  5591. *
  5592. * @return void
  5593. */
  5594. public function testPostLink() {
  5595. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5596. $this->assertTags($result, array(
  5597. 'form' => array(
  5598. 'method' => 'post', 'action' => '/posts/delete/1',
  5599. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5600. ),
  5601. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5602. '/form',
  5603. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5604. 'Delete',
  5605. '/a'
  5606. ));
  5607. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5608. $this->assertTags($result, array(
  5609. 'form' => array(
  5610. 'method' => 'post', 'action' => '/posts/delete/1',
  5611. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5612. ),
  5613. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5614. '/form',
  5615. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5616. 'Delete',
  5617. '/a'
  5618. ));
  5619. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5620. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5621. }
  5622. /**
  5623. * Test that postLink adds _Token fields.
  5624. *
  5625. * @return void
  5626. */
  5627. public function testSecurePostLink() {
  5628. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5629. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5630. $expected = array(
  5631. 'form' => array(
  5632. 'method' => 'post', 'action' => '/posts/delete/1',
  5633. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5634. ),
  5635. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5636. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5637. 'div' => array('style' => 'display:none;'),
  5638. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5639. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5640. '/div',
  5641. '/form',
  5642. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5643. 'Delete',
  5644. '/a'
  5645. );
  5646. $this->assertTags($result, $expected);
  5647. }
  5648. /**
  5649. * testSubmitButton method
  5650. *
  5651. * @return void
  5652. */
  5653. public function testSubmitButton() {
  5654. $result = $this->Form->submit('');
  5655. $expected = array(
  5656. 'div' => array('class' => 'submit'),
  5657. 'input' => array('type' => 'submit', 'value' => ''),
  5658. '/div'
  5659. );
  5660. $this->assertTags($result, $expected);
  5661. $result = $this->Form->submit('Test Submit');
  5662. $expected = array(
  5663. 'div' => array('class' => 'submit'),
  5664. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5665. '/div'
  5666. );
  5667. $this->assertTags($result, $expected);
  5668. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5669. $expected = array(
  5670. 'span' => array('class' => 'submit'),
  5671. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5672. '/span'
  5673. );
  5674. $this->assertTags($result, $expected);
  5675. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5676. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5677. $this->assertTags($result, $expected);
  5678. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5679. $expected = array(
  5680. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5681. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5682. '/div'
  5683. );
  5684. $this->assertTags($result, $expected);
  5685. $result = $this->Form->submit('Next >');
  5686. $expected = array(
  5687. 'div' => array('class' => 'submit'),
  5688. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5689. '/div'
  5690. );
  5691. $this->assertTags($result, $expected);
  5692. $result = $this->Form->submit('Next >', array('escape' => false));
  5693. $expected = array(
  5694. 'div' => array('class' => 'submit'),
  5695. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5696. '/div'
  5697. );
  5698. $this->assertTags($result, $expected);
  5699. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5700. $expected = array(
  5701. 'div' => array('class' => 'submit'),
  5702. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5703. '/div'
  5704. );
  5705. $this->assertTags($result, $expected);
  5706. $before = '--before--';
  5707. $after = '--after--';
  5708. $result = $this->Form->submit('Test', array('before' => $before));
  5709. $expected = array(
  5710. 'div' => array('class' => 'submit'),
  5711. '--before--',
  5712. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5713. '/div'
  5714. );
  5715. $this->assertTags($result, $expected);
  5716. $result = $this->Form->submit('Test', array('after' => $after));
  5717. $expected = array(
  5718. 'div' => array('class' => 'submit'),
  5719. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5720. '--after--',
  5721. '/div'
  5722. );
  5723. $this->assertTags($result, $expected);
  5724. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5725. $expected = array(
  5726. 'div' => array('class' => 'submit'),
  5727. '--before--',
  5728. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5729. '--after--',
  5730. '/div'
  5731. );
  5732. $this->assertTags($result, $expected);
  5733. }
  5734. /**
  5735. * test image submit types.
  5736. *
  5737. * @return void
  5738. */
  5739. public function testSubmitImage() {
  5740. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5741. $expected = array(
  5742. 'div' => array('class' => 'submit'),
  5743. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5744. '/div'
  5745. );
  5746. $this->assertTags($result, $expected);
  5747. $result = $this->Form->submit('/relative/cake.power.gif');
  5748. $expected = array(
  5749. 'div' => array('class' => 'submit'),
  5750. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5751. '/div'
  5752. );
  5753. $this->assertTags($result, $expected);
  5754. $result = $this->Form->submit('cake.power.gif');
  5755. $expected = array(
  5756. 'div' => array('class' => 'submit'),
  5757. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5758. '/div'
  5759. );
  5760. $this->assertTags($result, $expected);
  5761. $result = $this->Form->submit('Not.an.image');
  5762. $expected = array(
  5763. 'div' => array('class' => 'submit'),
  5764. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5765. '/div'
  5766. );
  5767. $this->assertTags($result, $expected);
  5768. $after = '--after--';
  5769. $before = '--before--';
  5770. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5771. $expected = array(
  5772. 'div' => array('class' => 'submit'),
  5773. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5774. '--after--',
  5775. '/div'
  5776. );
  5777. $this->assertTags($result, $expected);
  5778. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5779. $expected = array(
  5780. 'div' => array('class' => 'submit'),
  5781. '--before--',
  5782. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5783. '/div'
  5784. );
  5785. $this->assertTags($result, $expected);
  5786. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5787. $expected = array(
  5788. 'div' => array('class' => 'submit'),
  5789. '--before--',
  5790. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5791. '--after--',
  5792. '/div'
  5793. );
  5794. $this->assertTags($result, $expected);
  5795. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5796. $expected = array(
  5797. 'div' => array('class' => 'submit'),
  5798. '--before--',
  5799. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5800. '--after--',
  5801. '/div'
  5802. );
  5803. $this->assertTags($result, $expected);
  5804. }
  5805. /**
  5806. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5807. * be submitted at a time.
  5808. *
  5809. * @return void
  5810. */
  5811. public function testSubmitUnlockedByDefault() {
  5812. $this->Form->request->params['_Token']['key'] = 'secured';
  5813. $this->Form->submit('Go go');
  5814. $this->Form->submit('Save', array('name' => 'save'));
  5815. $result = $this->Form->unlockField();
  5816. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5817. }
  5818. /**
  5819. * Test submit image with timestamps.
  5820. *
  5821. * @return void
  5822. */
  5823. public function testSubmitImageTimestamp() {
  5824. Configure::write('Asset.timestamp', 'force');
  5825. $result = $this->Form->submit('cake.power.gif');
  5826. $expected = array(
  5827. 'div' => array('class' => 'submit'),
  5828. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5829. '/div'
  5830. );
  5831. $this->assertTags($result, $expected);
  5832. }
  5833. /**
  5834. * test the create() method
  5835. *
  5836. * @return void
  5837. */
  5838. public function testCreate() {
  5839. $result = $this->Form->create('Contact');
  5840. $encoding = strtolower(Configure::read('App.encoding'));
  5841. $expected = array(
  5842. 'form' => array(
  5843. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5844. 'accept-charset' => $encoding
  5845. ),
  5846. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5847. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5848. '/div'
  5849. );
  5850. $this->assertTags($result, $expected);
  5851. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5852. $expected = array('form' => array(
  5853. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5854. 'accept-charset' => $encoding
  5855. ));
  5856. $this->assertTags($result, $expected);
  5857. $result = $this->Form->create('Contact', array('type' => 'get'));
  5858. $expected = array('form' => array(
  5859. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5860. 'accept-charset' => $encoding
  5861. ));
  5862. $this->assertTags($result, $expected);
  5863. $result = $this->Form->create('Contact', array('type' => 'put'));
  5864. $expected = array(
  5865. 'form' => array(
  5866. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5867. 'accept-charset' => $encoding
  5868. ),
  5869. 'div' => array('style' => 'display:none;'),
  5870. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5871. '/div'
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. $result = $this->Form->create('Contact', array('type' => 'file'));
  5875. $expected = array(
  5876. 'form' => array(
  5877. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5878. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5879. ),
  5880. 'div' => array('style' => 'display:none;'),
  5881. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5882. '/div'
  5883. );
  5884. $this->assertTags($result, $expected);
  5885. $this->Form->request->data['Contact']['id'] = 1;
  5886. $this->Form->request->here = '/contacts/edit/1';
  5887. $this->Form->request['action'] = 'edit';
  5888. $result = $this->Form->create('Contact');
  5889. $expected = array(
  5890. 'form' => array(
  5891. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5892. 'accept-charset' => $encoding
  5893. ),
  5894. 'div' => array('style' => 'display:none;'),
  5895. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5896. '/div'
  5897. );
  5898. $this->assertTags($result, $expected);
  5899. $this->Form->request->data['Contact']['id'] = 1;
  5900. $this->Form->request->here = '/contacts/edit/1';
  5901. $this->Form->request['action'] = 'edit';
  5902. $result = $this->Form->create('Contact', array('type' => 'file'));
  5903. $expected = array(
  5904. 'form' => array(
  5905. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5906. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5907. ),
  5908. 'div' => array('style' => 'display:none;'),
  5909. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5910. '/div'
  5911. );
  5912. $this->assertTags($result, $expected);
  5913. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5914. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5915. $expected = array(
  5916. 'form' => array(
  5917. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5918. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5919. ),
  5920. 'div' => array('style' => 'display:none;'),
  5921. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5922. '/div'
  5923. );
  5924. $this->assertTags($result, $expected);
  5925. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5926. $expected = array(
  5927. 'form' => array(
  5928. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5929. 'accept-charset' => $encoding
  5930. ),
  5931. 'div' => array('style' => 'display:none;'),
  5932. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5933. '/div'
  5934. );
  5935. $this->assertTags($result, $expected);
  5936. $this->Form->request['action'] = 'add';
  5937. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5938. $expected = array(
  5939. 'form' => array(
  5940. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5941. 'accept-charset' => $encoding
  5942. ),
  5943. 'div' => array('style' => 'display:none;'),
  5944. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5945. '/div'
  5946. );
  5947. $this->assertTags($result, $expected);
  5948. $result = $this->Form->create('User', array('action' => 'login'));
  5949. $expected = array(
  5950. 'form' => array(
  5951. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5952. 'accept-charset' => $encoding
  5953. ),
  5954. 'div' => array('style' => 'display:none;'),
  5955. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5956. '/div'
  5957. );
  5958. $this->assertTags($result, $expected);
  5959. $result = $this->Form->create('User', array('url' => '/users/login'));
  5960. $expected = array(
  5961. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5962. 'div' => array('style' => 'display:none;'),
  5963. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5964. '/div'
  5965. );
  5966. $this->assertTags($result, $expected);
  5967. $this->Form->request['controller'] = 'pages';
  5968. $result = $this->Form->create('User', array('action' => 'signup'));
  5969. $expected = array(
  5970. 'form' => array(
  5971. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5972. 'accept-charset' => $encoding
  5973. ),
  5974. 'div' => array('style' => 'display:none;'),
  5975. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5976. '/div'
  5977. );
  5978. $this->assertTags($result, $expected);
  5979. $this->Form->request->data = array();
  5980. $this->Form->request['controller'] = 'contacts';
  5981. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5982. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5983. $expected = array(
  5984. 'form' => array(
  5985. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5986. 'accept-charset' => 'utf-8'
  5987. ),
  5988. 'div' => array('style' => 'display:none;'),
  5989. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5990. '/div'
  5991. );
  5992. $this->assertTags($result, $expected);
  5993. }
  5994. /**
  5995. * Test the onsubmit option for create()
  5996. *
  5997. * @return void
  5998. */
  5999. public function testCreateOnSubmit() {
  6000. $this->Form->request->data = array();
  6001. $this->Form->request['controller'] = 'contacts';
  6002. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6003. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6004. $expected = array(
  6005. 'form' => array(
  6006. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6007. 'accept-charset' => 'utf-8'
  6008. ),
  6009. 'div' => array('style' => 'display:none;'),
  6010. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6011. '/div'
  6012. );
  6013. $this->assertTags($result, $expected);
  6014. $this->Form->request->data = array();
  6015. $this->Form->request['controller'] = 'contacts';
  6016. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6017. $result = $this->Form->create(array(
  6018. 'url' => array('action' => 'index', 'param'),
  6019. 'default' => false,
  6020. 'onsubmit' => 'someFunction();'
  6021. ));
  6022. $expected = array(
  6023. 'form' => array(
  6024. 'id' => 'ContactAddForm', 'method' => 'post',
  6025. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6026. 'action' => '/contacts/index/param',
  6027. 'accept-charset' => 'utf-8'
  6028. ),
  6029. 'div' => array('style' => 'display:none;'),
  6030. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6031. '/div'
  6032. );
  6033. $this->assertTags($result, $expected);
  6034. }
  6035. /**
  6036. * test create() with automatic url generation
  6037. *
  6038. * @return void
  6039. */
  6040. public function testCreateAutoUrl() {
  6041. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6042. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6043. $this->Form->request->base = '/base_url';
  6044. $result = $this->Form->create('Contact');
  6045. $expected = array(
  6046. 'form' => array(
  6047. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6048. 'accept-charset' => 'utf-8'
  6049. ),
  6050. 'div' => array('style' => 'display:none;'),
  6051. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6052. '/div'
  6053. );
  6054. $this->assertTags($result, $expected);
  6055. $this->Form->request['action'] = 'delete';
  6056. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6057. $this->Form->request->base = '/base_url';
  6058. $result = $this->Form->create('Contact');
  6059. $expected = array(
  6060. 'form' => array(
  6061. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6062. 'accept-charset' => 'utf-8'
  6063. ),
  6064. 'div' => array('style' => 'display:none;'),
  6065. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6066. '/div'
  6067. );
  6068. $this->assertTags($result, $expected);
  6069. }
  6070. /**
  6071. * test create() with a custom route
  6072. *
  6073. * @return void
  6074. */
  6075. public function testCreateCustomRoute() {
  6076. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6077. $encoding = strtolower(Configure::read('App.encoding'));
  6078. $result = $this->Form->create('User', array('action' => 'login'));
  6079. $expected = array(
  6080. 'form' => array(
  6081. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6082. 'accept-charset' => $encoding
  6083. ),
  6084. 'div' => array('style' => 'display:none;'),
  6085. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6086. '/div'
  6087. );
  6088. $this->assertTags($result, $expected);
  6089. }
  6090. /**
  6091. * test that inputDefaults are stored and used.
  6092. *
  6093. * @return void
  6094. */
  6095. public function testCreateWithInputDefaults() {
  6096. $this->Form->create('User', array(
  6097. 'inputDefaults' => array(
  6098. 'div' => false,
  6099. 'label' => false,
  6100. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6101. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6102. )
  6103. ));
  6104. $result = $this->Form->input('username');
  6105. $expected = array(
  6106. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6107. );
  6108. $this->assertTags($result, $expected);
  6109. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6110. $expected = array(
  6111. 'div' => array('class' => 'input text'),
  6112. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6113. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6114. '/div'
  6115. );
  6116. $this->assertTags($result, $expected);
  6117. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6118. $expected = array(
  6119. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6120. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6121. );
  6122. $this->assertTags($result, $expected);
  6123. $this->Form->create('User', array(
  6124. 'inputDefaults' => array(
  6125. 'div' => false,
  6126. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6127. )
  6128. ));
  6129. $result = $this->Form->input('username', array('div' => true));
  6130. $expected = array(
  6131. 'div' => array('class' => 'input text'),
  6132. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6133. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6134. '/div'
  6135. );
  6136. $this->assertTags($result, $expected);
  6137. }
  6138. /**
  6139. * test automatic accept-charset overriding
  6140. *
  6141. * @return void
  6142. */
  6143. public function testCreateWithAcceptCharset() {
  6144. $result = $this->Form->create('UserForm', array(
  6145. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6146. )
  6147. );
  6148. $expected = array(
  6149. 'form' => array(
  6150. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6151. 'accept-charset' => 'iso-8859-1'
  6152. ),
  6153. 'div' => array('style' => 'display:none;'),
  6154. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6155. '/div'
  6156. );
  6157. $this->assertTags($result, $expected);
  6158. }
  6159. /**
  6160. * Test base form url when url param is passed with multiple parameters (&)
  6161. *
  6162. */
  6163. public function testCreateQuerystringrequest() {
  6164. $encoding = strtolower(Configure::read('App.encoding'));
  6165. $result = $this->Form->create('Contact', array(
  6166. 'type' => 'post',
  6167. 'escape' => false,
  6168. 'url' => array(
  6169. 'controller' => 'controller',
  6170. 'action' => 'action',
  6171. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6172. )
  6173. ));
  6174. $expected = array(
  6175. 'form' => array(
  6176. 'id' => 'ContactAddForm',
  6177. 'method' => 'post',
  6178. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6179. 'accept-charset' => $encoding
  6180. ),
  6181. 'div' => array('style' => 'display:none;'),
  6182. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6183. '/div'
  6184. );
  6185. $this->assertTags($result, $expected);
  6186. $result = $this->Form->create('Contact', array(
  6187. 'type' => 'post',
  6188. 'url' => array(
  6189. 'controller' => 'controller',
  6190. 'action' => 'action',
  6191. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6192. )
  6193. ));
  6194. $expected = array(
  6195. 'form' => array(
  6196. 'id' => 'ContactAddForm',
  6197. 'method' => 'post',
  6198. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6199. 'accept-charset' => $encoding
  6200. ),
  6201. 'div' => array('style' => 'display:none;'),
  6202. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6203. '/div'
  6204. );
  6205. $this->assertTags($result, $expected);
  6206. }
  6207. /**
  6208. * test that create() doesn't cause errors by multiple id's being in the primary key
  6209. * as could happen with multiple select or checkboxes.
  6210. *
  6211. * @return void
  6212. */
  6213. public function testCreateWithMultipleIdInData() {
  6214. $encoding = strtolower(Configure::read('App.encoding'));
  6215. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6216. $result = $this->Form->create('Contact');
  6217. $expected = array(
  6218. 'form' => array(
  6219. 'id' => 'ContactAddForm',
  6220. 'method' => 'post',
  6221. 'action' => '/contacts/add',
  6222. 'accept-charset' => $encoding
  6223. ),
  6224. 'div' => array('style' => 'display:none;'),
  6225. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6226. '/div'
  6227. );
  6228. $this->assertTags($result, $expected);
  6229. }
  6230. /**
  6231. * test that create() doesn't add in extra passed params.
  6232. *
  6233. * @return void
  6234. */
  6235. public function testCreatePassedArgs() {
  6236. $encoding = strtolower(Configure::read('App.encoding'));
  6237. $this->Form->request->data['Contact']['id'] = 1;
  6238. $result = $this->Form->create('Contact', array(
  6239. 'type' => 'post',
  6240. 'escape' => false,
  6241. 'url' => array(
  6242. 'action' => 'edit',
  6243. 'myparam'
  6244. )
  6245. ));
  6246. $expected = array(
  6247. 'form' => array(
  6248. 'id' => 'ContactAddForm',
  6249. 'method' => 'post',
  6250. 'action' => '/contacts/edit/myparam',
  6251. 'accept-charset' => $encoding
  6252. ),
  6253. 'div' => array('style' => 'display:none;'),
  6254. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6255. '/div'
  6256. );
  6257. $this->assertTags($result, $expected);
  6258. }
  6259. /**
  6260. * test creating a get form, and get form inputs.
  6261. *
  6262. * @return void
  6263. */
  6264. public function testGetFormCreate() {
  6265. $encoding = strtolower(Configure::read('App.encoding'));
  6266. $result = $this->Form->create('Contact', array('type' => 'get'));
  6267. $this->assertTags($result, array('form' => array(
  6268. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6269. 'accept-charset' => $encoding
  6270. )));
  6271. $result = $this->Form->text('Contact.name');
  6272. $this->assertTags($result, array('input' => array(
  6273. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6274. )));
  6275. $result = $this->Form->password('password');
  6276. $this->assertTags($result, array('input' => array(
  6277. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6278. )));
  6279. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6280. $result = $this->Form->text('user_form');
  6281. $this->assertTags($result, array('input' => array(
  6282. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6283. )));
  6284. }
  6285. /**
  6286. * test get form, and inputs when the model param is false
  6287. *
  6288. * @return void
  6289. */
  6290. public function testGetFormWithFalseModel() {
  6291. $encoding = strtolower(Configure::read('App.encoding'));
  6292. $this->Form->request['controller'] = 'contact_test';
  6293. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6294. $expected = array('form' => array(
  6295. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6296. 'accept-charset' => $encoding
  6297. ));
  6298. $this->assertTags($result, $expected);
  6299. $result = $this->Form->text('reason');
  6300. $expected = array(
  6301. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6302. );
  6303. $this->assertTags($result, $expected);
  6304. }
  6305. /**
  6306. * test that datetime() works with GET style forms.
  6307. *
  6308. * @return void
  6309. */
  6310. public function testDateTimeWithGetForms() {
  6311. extract($this->dateRegex);
  6312. $this->Form->create('Contact', array('type' => 'get'));
  6313. $result = $this->Form->datetime('created');
  6314. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6315. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6316. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6317. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6318. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6319. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6320. }
  6321. /**
  6322. * testEditFormWithData method
  6323. *
  6324. * test auto populating form elements from submitted data.
  6325. *
  6326. * @return void
  6327. */
  6328. public function testEditFormWithData() {
  6329. $this->Form->request->data = array('Person' => array(
  6330. 'id' => 1,
  6331. 'first_name' => 'Nate',
  6332. 'last_name' => 'Abele',
  6333. 'email' => 'nate@example.com'
  6334. ));
  6335. $this->Form->request->addParams(array(
  6336. 'models' => array('Person'),
  6337. 'controller' => 'people',
  6338. 'action' => 'add'
  6339. ));
  6340. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6341. $this->Form->create();
  6342. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6343. $expected = array(
  6344. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6345. 'select' => array(
  6346. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6347. ),
  6348. array('option' => array('value' => 1)), 'Nate', '/option',
  6349. array('option' => array('value' => 2)), 'Garrett', '/option',
  6350. array('option' => array('value' => 3)), 'Larry', '/option',
  6351. '/select'
  6352. );
  6353. $this->assertTags($result, $expected);
  6354. }
  6355. /**
  6356. * Test that required fields are created for various types of validation.
  6357. *
  6358. * @return void
  6359. */
  6360. public function testFormInputRequiredDetection() {
  6361. $this->Form->create('Contact');
  6362. $result = $this->Form->input('Contact.non_existing');
  6363. $expected = array(
  6364. 'div' => array('class' => 'input text'),
  6365. 'label' => array('for' => 'ContactNonExisting'),
  6366. 'Non Existing',
  6367. '/label',
  6368. 'input' => array(
  6369. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6370. 'id' => 'ContactNonExisting'
  6371. ),
  6372. '/div'
  6373. );
  6374. $this->assertTags($result, $expected);
  6375. $result = $this->Form->input('Contact.imrequired');
  6376. $expected = array(
  6377. 'div' => array('class' => 'input text required'),
  6378. 'label' => array('for' => 'ContactImrequired'),
  6379. 'Imrequired',
  6380. '/label',
  6381. 'input' => array(
  6382. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6383. 'id' => 'ContactImrequired'
  6384. ),
  6385. '/div'
  6386. );
  6387. $this->assertTags($result, $expected);
  6388. $result = $this->Form->input('Contact.imalsorequired');
  6389. $expected = array(
  6390. 'div' => array('class' => 'input text required'),
  6391. 'label' => array('for' => 'ContactImalsorequired'),
  6392. 'Imalsorequired',
  6393. '/label',
  6394. 'input' => array(
  6395. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6396. 'id' => 'ContactImalsorequired'
  6397. ),
  6398. '/div'
  6399. );
  6400. $this->assertTags($result, $expected);
  6401. $result = $this->Form->input('Contact.imrequiredtoo');
  6402. $expected = array(
  6403. 'div' => array('class' => 'input text required'),
  6404. 'label' => array('for' => 'ContactImrequiredtoo'),
  6405. 'Imrequiredtoo',
  6406. '/label',
  6407. 'input' => array(
  6408. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6409. 'id' => 'ContactImrequiredtoo'
  6410. ),
  6411. '/div'
  6412. );
  6413. $this->assertTags($result, $expected);
  6414. $result = $this->Form->input('Contact.required_one');
  6415. $expected = array(
  6416. 'div' => array('class' => 'input text required'),
  6417. 'label' => array('for' => 'ContactRequiredOne'),
  6418. 'Required One',
  6419. '/label',
  6420. 'input' => array(
  6421. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6422. 'id' => 'ContactRequiredOne'
  6423. ),
  6424. '/div'
  6425. );
  6426. $this->assertTags($result, $expected);
  6427. $result = $this->Form->input('Contact.string_required');
  6428. $expected = array(
  6429. 'div' => array('class' => 'input text required'),
  6430. 'label' => array('for' => 'ContactStringRequired'),
  6431. 'String Required',
  6432. '/label',
  6433. 'input' => array(
  6434. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6435. 'id' => 'ContactStringRequired'
  6436. ),
  6437. '/div'
  6438. );
  6439. $this->assertTags($result, $expected);
  6440. $result = $this->Form->input('Contact.imnotrequired');
  6441. $expected = array(
  6442. 'div' => array('class' => 'input text'),
  6443. 'label' => array('for' => 'ContactImnotrequired'),
  6444. 'Imnotrequired',
  6445. '/label',
  6446. 'input' => array(
  6447. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6448. 'id' => 'ContactImnotrequired'
  6449. ),
  6450. '/div'
  6451. );
  6452. $this->assertTags($result, $expected);
  6453. $result = $this->Form->input('Contact.imalsonotrequired');
  6454. $expected = array(
  6455. 'div' => array('class' => 'input text'),
  6456. 'label' => array('for' => 'ContactImalsonotrequired'),
  6457. 'Imalsonotrequired',
  6458. '/label',
  6459. 'input' => array(
  6460. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6461. 'id' => 'ContactImalsonotrequired'
  6462. ),
  6463. '/div'
  6464. );
  6465. $this->assertTags($result, $expected);
  6466. $result = $this->Form->input('Contact.imalsonotrequired2');
  6467. $expected = array(
  6468. 'div' => array('class' => 'input text'),
  6469. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6470. 'Imalsonotrequired2',
  6471. '/label',
  6472. 'input' => array(
  6473. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6474. 'id' => 'ContactImalsonotrequired2'
  6475. ),
  6476. '/div'
  6477. );
  6478. $this->assertTags($result, $expected);
  6479. $result = $this->Form->input('Contact.imnotrequiredeither');
  6480. $expected = array(
  6481. 'div' => array('class' => 'input text'),
  6482. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6483. 'Imnotrequiredeither',
  6484. '/label',
  6485. 'input' => array(
  6486. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6487. 'id' => 'ContactImnotrequiredeither'
  6488. ),
  6489. '/div'
  6490. );
  6491. $this->assertTags($result, $expected);
  6492. $result = $this->Form->input('Contact.iamrequiredalways');
  6493. $expected = array(
  6494. 'div' => array('class' => 'input text required'),
  6495. 'label' => array('for' => 'ContactIamrequiredalways'),
  6496. 'Iamrequiredalways',
  6497. '/label',
  6498. 'input' => array(
  6499. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6500. 'id' => 'ContactIamrequiredalways'
  6501. ),
  6502. '/div'
  6503. );
  6504. $this->assertTags($result, $expected);
  6505. }
  6506. /**
  6507. * testFormMagicInput method
  6508. *
  6509. * @return void
  6510. */
  6511. public function testFormMagicInput() {
  6512. $encoding = strtolower(Configure::read('App.encoding'));
  6513. $result = $this->Form->create('Contact');
  6514. $expected = array(
  6515. 'form' => array(
  6516. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6517. 'accept-charset' => $encoding
  6518. ),
  6519. 'div' => array('style' => 'display:none;'),
  6520. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6521. '/div'
  6522. );
  6523. $this->assertTags($result, $expected);
  6524. $result = $this->Form->input('name');
  6525. $expected = array(
  6526. 'div' => array('class' => 'input text'),
  6527. 'label' => array('for' => 'ContactName'),
  6528. 'Name',
  6529. '/label',
  6530. 'input' => array(
  6531. 'type' => 'text', 'name' => 'data[Contact][name]',
  6532. 'id' => 'ContactName', 'maxlength' => '255'
  6533. ),
  6534. '/div'
  6535. );
  6536. $this->assertTags($result, $expected);
  6537. $result = $this->Form->input('non_existing_field_in_contact_model');
  6538. $expected = array(
  6539. 'div' => array('class' => 'input text'),
  6540. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6541. 'Non Existing Field In Contact Model',
  6542. '/label',
  6543. 'input' => array(
  6544. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6545. 'id' => 'ContactNonExistingFieldInContactModel'
  6546. ),
  6547. '/div'
  6548. );
  6549. $this->assertTags($result, $expected);
  6550. $result = $this->Form->input('Address.street');
  6551. $expected = array(
  6552. 'div' => array('class' => 'input text'),
  6553. 'label' => array('for' => 'AddressStreet'),
  6554. 'Street',
  6555. '/label',
  6556. 'input' => array(
  6557. 'type' => 'text', 'name' => 'data[Address][street]',
  6558. 'id' => 'AddressStreet'
  6559. ),
  6560. '/div'
  6561. );
  6562. $this->assertTags($result, $expected);
  6563. $result = $this->Form->input('Address.non_existing_field_in_model');
  6564. $expected = array(
  6565. 'div' => array('class' => 'input text'),
  6566. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6567. 'Non Existing Field In Model',
  6568. '/label',
  6569. 'input' => array(
  6570. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6571. 'id' => 'AddressNonExistingFieldInModel'
  6572. ),
  6573. '/div'
  6574. );
  6575. $this->assertTags($result, $expected);
  6576. $result = $this->Form->input('name', array('div' => false));
  6577. $expected = array(
  6578. 'label' => array('for' => 'ContactName'),
  6579. 'Name',
  6580. '/label',
  6581. 'input' => array(
  6582. 'type' => 'text', 'name' => 'data[Contact][name]',
  6583. 'id' => 'ContactName', 'maxlength' => '255'
  6584. )
  6585. );
  6586. $this->assertTags($result, $expected);
  6587. extract($this->dateRegex);
  6588. $now = strtotime('now');
  6589. $result = $this->Form->input('Contact.published', array('div' => false));
  6590. $expected = array(
  6591. 'label' => array('for' => 'ContactPublishedMonth'),
  6592. 'Published',
  6593. '/label',
  6594. array('select' => array(
  6595. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6596. )),
  6597. $monthsRegex,
  6598. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6599. date('F', $now),
  6600. '/option',
  6601. '*/select',
  6602. '-',
  6603. array('select' => array(
  6604. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6605. )),
  6606. $daysRegex,
  6607. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6608. date('j', $now),
  6609. '/option',
  6610. '*/select',
  6611. '-',
  6612. array('select' => array(
  6613. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6614. )),
  6615. $yearsRegex,
  6616. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6617. date('Y', $now),
  6618. '*/select'
  6619. );
  6620. $this->assertTags($result, $expected);
  6621. $result = $this->Form->input('Contact.updated', array('div' => false));
  6622. $expected = array(
  6623. 'label' => array('for' => 'ContactUpdatedMonth'),
  6624. 'Updated',
  6625. '/label',
  6626. array('select' => array(
  6627. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6628. )),
  6629. $monthsRegex,
  6630. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6631. date('F', $now),
  6632. '/option',
  6633. '*/select',
  6634. '-',
  6635. array('select' => array(
  6636. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6637. )),
  6638. $daysRegex,
  6639. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6640. date('j', $now),
  6641. '/option',
  6642. '*/select',
  6643. '-',
  6644. array('select' => array(
  6645. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6646. )),
  6647. $yearsRegex,
  6648. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6649. date('Y', $now),
  6650. '*/select'
  6651. );
  6652. $this->assertTags($result, $expected);
  6653. $result = $this->Form->input('UserForm.stuff');
  6654. $expected = array(
  6655. 'div' => array('class' => 'input text'),
  6656. 'label' => array('for' => 'UserFormStuff'),
  6657. 'Stuff',
  6658. '/label',
  6659. 'input' => array(
  6660. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6661. 'id' => 'UserFormStuff', 'maxlength' => 10
  6662. ),
  6663. '/div'
  6664. );
  6665. $this->assertTags($result, $expected);
  6666. }
  6667. /**
  6668. * testForMagicInputNonExistingNorValidated method
  6669. *
  6670. * @return void
  6671. */
  6672. public function testForMagicInputNonExistingNorValidated() {
  6673. $encoding = strtolower(Configure::read('App.encoding'));
  6674. $result = $this->Form->create('Contact');
  6675. $expected = array(
  6676. 'form' => array(
  6677. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6678. 'accept-charset' => $encoding
  6679. ),
  6680. 'div' => array('style' => 'display:none;'),
  6681. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6682. '/div'
  6683. );
  6684. $this->assertTags($result, $expected);
  6685. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6686. $expected = array(
  6687. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6688. 'Non Existing Nor Validated',
  6689. '/label',
  6690. 'input' => array(
  6691. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6692. 'id' => 'ContactNonExistingNorValidated'
  6693. )
  6694. );
  6695. $this->assertTags($result, $expected);
  6696. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6697. 'div' => false, 'value' => 'my value'
  6698. ));
  6699. $expected = array(
  6700. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6701. 'Non Existing Nor Validated',
  6702. '/label',
  6703. 'input' => array(
  6704. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6705. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6706. )
  6707. );
  6708. $this->assertTags($result, $expected);
  6709. $this->Form->request->data = array(
  6710. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6711. ));
  6712. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6713. $expected = array(
  6714. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6715. 'Non Existing Nor Validated',
  6716. '/label',
  6717. 'input' => array(
  6718. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6719. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6720. )
  6721. );
  6722. $this->assertTags($result, $expected);
  6723. }
  6724. /**
  6725. * testFormMagicInputLabel method
  6726. *
  6727. * @return void
  6728. */
  6729. public function testFormMagicInputLabel() {
  6730. $encoding = strtolower(Configure::read('App.encoding'));
  6731. $result = $this->Form->create('Contact');
  6732. $expected = array(
  6733. 'form' => array(
  6734. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6735. 'accept-charset' => $encoding
  6736. ),
  6737. 'div' => array('style' => 'display:none;'),
  6738. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6739. '/div'
  6740. );
  6741. $this->assertTags($result, $expected);
  6742. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6743. $this->assertTags($result, array('input' => array(
  6744. 'name' => 'data[Contact][name]', 'type' => 'text',
  6745. 'id' => 'ContactName', 'maxlength' => '255')
  6746. ));
  6747. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6748. $expected = array(
  6749. 'label' => array('for' => 'ContactName'),
  6750. 'My label',
  6751. '/label',
  6752. 'input' => array(
  6753. 'type' => 'text', 'name' => 'data[Contact][name]',
  6754. 'id' => 'ContactName', 'maxlength' => '255'
  6755. )
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. $result = $this->Form->input('Contact.name', array(
  6759. 'div' => false, 'label' => array('class' => 'mandatory')
  6760. ));
  6761. $expected = array(
  6762. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6763. 'Name',
  6764. '/label',
  6765. 'input' => array(
  6766. 'type' => 'text', 'name' => 'data[Contact][name]',
  6767. 'id' => 'ContactName', 'maxlength' => '255'
  6768. )
  6769. );
  6770. $this->assertTags($result, $expected);
  6771. $result = $this->Form->input('Contact.name', array(
  6772. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6773. ));
  6774. $expected = array(
  6775. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6776. 'My label',
  6777. '/label',
  6778. 'input' => array(
  6779. 'type' => 'text', 'name' => 'data[Contact][name]',
  6780. 'id' => 'ContactName', 'maxlength' => '255'
  6781. )
  6782. );
  6783. $this->assertTags($result, $expected);
  6784. $result = $this->Form->input('Contact.name', array(
  6785. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6786. ));
  6787. $expected = array(
  6788. 'label' => array('for' => 'my_id'),
  6789. 'Name',
  6790. '/label',
  6791. 'input' => array(
  6792. 'type' => 'text', 'name' => 'data[Contact][name]',
  6793. 'id' => 'my_id', 'maxlength' => '255'
  6794. )
  6795. );
  6796. $this->assertTags($result, $expected);
  6797. $result = $this->Form->input('1.id');
  6798. $this->assertTags($result, array('input' => array(
  6799. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6800. 'id' => 'Contact1Id'
  6801. )));
  6802. $result = $this->Form->input("1.name");
  6803. $expected = array(
  6804. 'div' => array('class' => 'input text'),
  6805. 'label' => array('for' => 'Contact1Name'),
  6806. 'Name',
  6807. '/label',
  6808. 'input' => array(
  6809. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6810. 'id' => 'Contact1Name', 'maxlength' => '255'
  6811. ),
  6812. '/div'
  6813. );
  6814. $this->assertTags($result, $expected);
  6815. $result = $this->Form->input('Contact.1.id');
  6816. $this->assertTags($result, array(
  6817. 'input' => array(
  6818. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6819. 'id' => 'Contact1Id'
  6820. )
  6821. ));
  6822. $result = $this->Form->input("Model.1.name");
  6823. $expected = array(
  6824. 'div' => array('class' => 'input text'),
  6825. 'label' => array('for' => 'Model1Name'),
  6826. 'Name',
  6827. '/label',
  6828. 'input' => array(
  6829. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6830. 'id' => 'Model1Name'
  6831. ),
  6832. '/div'
  6833. );
  6834. $this->assertTags($result, $expected);
  6835. }
  6836. /**
  6837. * testFormEnd method
  6838. *
  6839. * @return void
  6840. */
  6841. public function testFormEnd() {
  6842. $this->assertEquals('</form>', $this->Form->end());
  6843. $result = $this->Form->end('');
  6844. $expected = array(
  6845. 'div' => array('class' => 'submit'),
  6846. 'input' => array('type' => 'submit', 'value' => ''),
  6847. '/div',
  6848. '/form'
  6849. );
  6850. $this->assertTags($result, $expected);
  6851. $result = $this->Form->end(array('label' => ''));
  6852. $expected = array(
  6853. 'div' => array('class' => 'submit'),
  6854. 'input' => array('type' => 'submit', 'value' => ''),
  6855. '/div',
  6856. '/form'
  6857. );
  6858. $this->assertTags($result, $expected);
  6859. $result = $this->Form->end('save');
  6860. $expected = array(
  6861. 'div' => array('class' => 'submit'),
  6862. 'input' => array('type' => 'submit', 'value' => 'save'),
  6863. '/div',
  6864. '/form'
  6865. );
  6866. $this->assertTags($result, $expected);
  6867. $result = $this->Form->end(array('label' => 'save'));
  6868. $expected = array(
  6869. 'div' => array('class' => 'submit'),
  6870. 'input' => array('type' => 'submit', 'value' => 'save'),
  6871. '/div',
  6872. '/form'
  6873. );
  6874. $this->assertTags($result, $expected);
  6875. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6876. $expected = array(
  6877. 'div' => array('class' => 'submit'),
  6878. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6879. '/div',
  6880. '/form'
  6881. );
  6882. $this->assertTags($result, $expected);
  6883. $result = $this->Form->end(array('name' => 'Whatever'));
  6884. $expected = array(
  6885. 'div' => array('class' => 'submit'),
  6886. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6887. '/div',
  6888. '/form'
  6889. );
  6890. $this->assertTags($result, $expected);
  6891. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6892. $expected = array(
  6893. 'div' => array('class' => 'good'),
  6894. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6895. '/div',
  6896. '/form'
  6897. );
  6898. $this->assertTags($result, $expected);
  6899. $result = $this->Form->end(array(
  6900. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6901. ));
  6902. $expected = array(
  6903. 'div' => array('class' => 'good'),
  6904. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6905. '/div',
  6906. '/form'
  6907. );
  6908. $this->assertTags($result, $expected);
  6909. }
  6910. /**
  6911. * testMultipleFormWithIdFields method
  6912. *
  6913. * @return void
  6914. */
  6915. public function testMultipleFormWithIdFields() {
  6916. $this->Form->create('UserForm');
  6917. $result = $this->Form->input('id');
  6918. $this->assertTags($result, array('input' => array(
  6919. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6920. )));
  6921. $result = $this->Form->input('ValidateItem.id');
  6922. $this->assertTags($result, array('input' => array(
  6923. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6924. 'id' => 'ValidateItemId'
  6925. )));
  6926. $result = $this->Form->input('ValidateUser.id');
  6927. $this->assertTags($result, array('input' => array(
  6928. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6929. 'id' => 'ValidateUserId'
  6930. )));
  6931. }
  6932. /**
  6933. * testDbLessModel method
  6934. *
  6935. * @return void
  6936. */
  6937. public function testDbLessModel() {
  6938. $this->Form->create('TestMail');
  6939. $result = $this->Form->input('name');
  6940. $expected = array(
  6941. 'div' => array('class' => 'input text'),
  6942. 'label' => array('for' => 'TestMailName'),
  6943. 'Name',
  6944. '/label',
  6945. 'input' => array(
  6946. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6947. 'id' => 'TestMailName'
  6948. ),
  6949. '/div'
  6950. );
  6951. $this->assertTags($result, $expected);
  6952. ClassRegistry::init('TestMail');
  6953. $this->Form->create('TestMail');
  6954. $result = $this->Form->input('name');
  6955. $expected = array(
  6956. 'div' => array('class' => 'input text'),
  6957. 'label' => array('for' => 'TestMailName'),
  6958. 'Name',
  6959. '/label',
  6960. 'input' => array(
  6961. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6962. 'id' => 'TestMailName'
  6963. ),
  6964. '/div'
  6965. );
  6966. $this->assertTags($result, $expected);
  6967. }
  6968. /**
  6969. * testBrokenness method
  6970. *
  6971. * @return void
  6972. */
  6973. public function testBrokenness() {
  6974. /*
  6975. * #4 This test has two parents and four children. By default (as of r7117) both
  6976. * parents are show but the first parent is missing a child. This is the inconsistency
  6977. * in the default behaviour - one parent has all children, the other does not - dependent
  6978. * on the data values.
  6979. */
  6980. $result = $this->Form->select('Model.field', array(
  6981. 'Fred' => array(
  6982. 'freds_son_1' => 'Fred',
  6983. 'freds_son_2' => 'Freddie'
  6984. ),
  6985. 'Bert' => array(
  6986. 'berts_son_1' => 'Albert',
  6987. 'berts_son_2' => 'Bertie')
  6988. ),
  6989. array('showParents' => true, 'empty' => false)
  6990. );
  6991. $expected = array(
  6992. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6993. array('optgroup' => array('label' => 'Fred')),
  6994. array('option' => array('value' => 'freds_son_1')),
  6995. 'Fred',
  6996. '/option',
  6997. array('option' => array('value' => 'freds_son_2')),
  6998. 'Freddie',
  6999. '/option',
  7000. '/optgroup',
  7001. array('optgroup' => array('label' => 'Bert')),
  7002. array('option' => array('value' => 'berts_son_1')),
  7003. 'Albert',
  7004. '/option',
  7005. array('option' => array('value' => 'berts_son_2')),
  7006. 'Bertie',
  7007. '/option',
  7008. '/optgroup',
  7009. '/select'
  7010. );
  7011. $this->assertTags($result, $expected);
  7012. /*
  7013. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7014. * changed, so we should expect the same select list data, just with a different name
  7015. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7016. * This is where data corruption can occur, because when a select value is missing from
  7017. * a list a form will substitute the first value in the list - without the user knowing.
  7018. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7019. * affect the availability of 3 => 'Three' as a valid option.
  7020. */
  7021. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7022. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7023. ));
  7024. $result = $this->Form->select(
  7025. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7026. );
  7027. $expected = array(
  7028. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7029. array('option' => array('value' => 1)),
  7030. 'One',
  7031. '/option',
  7032. array('option' => array('value' => 2)),
  7033. 'Two',
  7034. '/option',
  7035. array('optgroup' => array('label' => 'Three')),
  7036. array('option' => array('value' => 3)),
  7037. 'Three',
  7038. '/option',
  7039. array('option' => array('value' => 4)),
  7040. 'Four',
  7041. '/option',
  7042. array('option' => array('value' => 5)),
  7043. 'Five',
  7044. '/option',
  7045. '/optgroup',
  7046. '/select'
  7047. );
  7048. $this->assertTags($result, $expected);
  7049. }
  7050. /**
  7051. * Test the generation of fields for a multi record form.
  7052. *
  7053. * @return void
  7054. */
  7055. public function testMultiRecordForm() {
  7056. $this->Form->create('ValidateProfile');
  7057. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7058. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7059. $expected = array(
  7060. 'div' => array('class' => 'input textarea'),
  7061. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7062. 'Name',
  7063. '/label',
  7064. 'textarea' => array(
  7065. 'id' => 'ValidateProfile1ValidateItem2Name',
  7066. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7067. 'cols' => 30,
  7068. 'rows' => 6
  7069. ),
  7070. 'Value',
  7071. '/textarea',
  7072. '/div'
  7073. );
  7074. $this->assertTags($result, $expected);
  7075. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7076. $expected = array(
  7077. 'div' => array('class' => 'input date'),
  7078. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7079. 'Created',
  7080. '/label',
  7081. array('select' => array(
  7082. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7083. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7084. )
  7085. ),
  7086. array('option' => array('value' => '')), '/option',
  7087. $this->dateRegex['monthsRegex'],
  7088. '/select', '-',
  7089. array('select' => array(
  7090. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7091. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7092. )
  7093. ),
  7094. array('option' => array('value' => '')), '/option',
  7095. $this->dateRegex['daysRegex'],
  7096. '/select', '-',
  7097. array('select' => array(
  7098. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7099. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7100. )
  7101. ),
  7102. array('option' => array('value' => '')), '/option',
  7103. $this->dateRegex['yearsRegex'],
  7104. '/select',
  7105. '/div'
  7106. );
  7107. $this->assertTags($result, $expected);
  7108. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7109. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7110. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7111. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7112. $expected = array(
  7113. 'div' => array('class' => 'input select error'),
  7114. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7115. 'Profile',
  7116. '/label',
  7117. 'select' => array(
  7118. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7119. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7120. 'class' => 'form-error'
  7121. ),
  7122. '/select',
  7123. array('div' => array('class' => 'error-message')),
  7124. 'Error',
  7125. '/div',
  7126. '/div'
  7127. );
  7128. $this->assertTags($result, $expected);
  7129. }
  7130. /**
  7131. * test the correct display of multi-record form validation errors.
  7132. *
  7133. * @return void
  7134. */
  7135. public function testMultiRecordFormValidationErrors() {
  7136. $this->Form->create('ValidateProfile');
  7137. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7138. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7139. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7140. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7141. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7142. $result = $this->Form->error('ValidateProfile.2.city');
  7143. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7144. $result = $this->Form->error('2.city');
  7145. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7146. }
  7147. /**
  7148. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7149. *
  7150. * @return void
  7151. */
  7152. public function testInputTemplate() {
  7153. $result = $this->Form->input('Contact.email', array(
  7154. 'type' => 'text', 'format' => array('input')
  7155. ));
  7156. $expected = array(
  7157. 'div' => array('class' => 'input text'),
  7158. 'input' => array(
  7159. 'type' => 'text', 'name' => 'data[Contact][email]',
  7160. 'id' => 'ContactEmail'
  7161. ),
  7162. '/div'
  7163. );
  7164. $this->assertTags($result, $expected);
  7165. $result = $this->Form->input('Contact.email', array(
  7166. 'type' => 'text', 'format' => array('input', 'label'),
  7167. 'label' => '<em>Email (required)</em>'
  7168. ));
  7169. $expected = array(
  7170. 'div' => array('class' => 'input text'),
  7171. array('input' => array(
  7172. 'type' => 'text', 'name' => 'data[Contact][email]',
  7173. 'id' => 'ContactEmail'
  7174. )),
  7175. 'label' => array('for' => 'ContactEmail'),
  7176. 'em' => array(),
  7177. 'Email (required)',
  7178. '/em',
  7179. '/label',
  7180. '/div'
  7181. );
  7182. $this->assertTags($result, $expected);
  7183. $result = $this->Form->input('Contact.email', array(
  7184. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7185. 'between' => '<div>Something in the middle</div>',
  7186. 'after' => '<span>Some text at the end</span>'
  7187. ));
  7188. $expected = array(
  7189. 'div' => array('class' => 'input text'),
  7190. array('input' => array(
  7191. 'type' => 'text', 'name' => 'data[Contact][email]',
  7192. 'id' => 'ContactEmail'
  7193. )),
  7194. array('div' => array()),
  7195. 'Something in the middle',
  7196. '/div',
  7197. 'label' => array('for' => 'ContactEmail'),
  7198. 'Email',
  7199. '/label',
  7200. 'span' => array(),
  7201. 'Some text at the end',
  7202. '/span',
  7203. '/div'
  7204. );
  7205. $this->assertTags($result, $expected);
  7206. $result = $this->Form->input('Contact.method', array(
  7207. 'type' => 'radio',
  7208. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7209. 'between' => 'I am between',
  7210. ));
  7211. $expected = array(
  7212. 'div' => array('class' => 'input radio'),
  7213. 'fieldset' => array(),
  7214. 'legend' => array(),
  7215. 'Method',
  7216. '/legend',
  7217. 'I am between',
  7218. 'input' => array(
  7219. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7220. 'value' => '', 'id' => 'ContactMethod_'
  7221. ),
  7222. array('input' => array(
  7223. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7224. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7225. )),
  7226. array('label' => array('for' => 'ContactMethodEmail')),
  7227. 'Email',
  7228. '/label',
  7229. array('input' => array(
  7230. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7231. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7232. )),
  7233. array('label' => array('for' => 'ContactMethodPigeon')),
  7234. 'Pigeon',
  7235. '/label',
  7236. '/fieldset',
  7237. '/div',
  7238. );
  7239. $this->assertTags($result, $expected);
  7240. }
  7241. /**
  7242. * test that some html5 inputs + FormHelper::__call() work
  7243. *
  7244. * @return void
  7245. */
  7246. public function testHtml5Inputs() {
  7247. $result = $this->Form->email('User.email');
  7248. $expected = array(
  7249. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7250. );
  7251. $this->assertTags($result, $expected);
  7252. $result = $this->Form->search('User.query');
  7253. $expected = array(
  7254. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7255. );
  7256. $this->assertTags($result, $expected);
  7257. $result = $this->Form->search('User.query', array('value' => 'test'));
  7258. $expected = array(
  7259. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7260. );
  7261. $this->assertTags($result, $expected);
  7262. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7263. $expected = array(
  7264. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7265. );
  7266. $this->assertTags($result, $expected);
  7267. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7268. $expected = array(
  7269. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7270. );
  7271. $this->assertTags($result, $expected);
  7272. }
  7273. /**
  7274. *
  7275. * @expectedException CakeException
  7276. * @return void
  7277. */
  7278. public function testHtml5InputException() {
  7279. $this->Form->email();
  7280. }
  7281. /**
  7282. * Tests that a model can be loaded from the model names passed in the request object
  7283. *
  7284. * @return void
  7285. */
  7286. public function testIntrospectModelFromRequest() {
  7287. $this->loadFixtures('Post');
  7288. App::build(array(
  7289. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7290. ));
  7291. CakePlugin::load('TestPlugin');
  7292. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7293. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7294. $this->Form->create('TestPluginPost');
  7295. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7296. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7297. CakePlugin::unload();
  7298. App::build();
  7299. }
  7300. /**
  7301. * Tests that it is possible to set the validation errors directly in the helper for a field
  7302. *
  7303. * @return void
  7304. */
  7305. public function testCustomValidationErrors() {
  7306. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7307. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7308. $this->assertEquals('Badness!', $result);
  7309. }
  7310. /**
  7311. * Tests that the 'on' key validates as expected on create
  7312. *
  7313. * @return void
  7314. */
  7315. public function testRequiredOnCreate() {
  7316. $this->Form->create('Contact');
  7317. $result = $this->Form->input('Contact.imrequiredonupdate');
  7318. $expected = array(
  7319. 'div' => array('class' => 'input text'),
  7320. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7321. 'Imrequiredonupdate',
  7322. '/label',
  7323. 'input' => array(
  7324. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7325. 'id' => 'ContactImrequiredonupdate'
  7326. ),
  7327. '/div'
  7328. );
  7329. $this->assertTags($result, $expected);
  7330. $result = $this->Form->input('Contact.imrequiredoncreate');
  7331. $expected = array(
  7332. 'div' => array('class' => 'input text required'),
  7333. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7334. 'Imrequiredoncreate',
  7335. '/label',
  7336. 'input' => array(
  7337. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7338. 'id' => 'ContactImrequiredoncreate'
  7339. ),
  7340. '/div'
  7341. );
  7342. $this->assertTags($result, $expected);
  7343. $result = $this->Form->input('Contact.imrequiredonboth');
  7344. $expected = array(
  7345. 'div' => array('class' => 'input text required'),
  7346. 'label' => array('for' => 'ContactImrequiredonboth'),
  7347. 'Imrequiredonboth',
  7348. '/label',
  7349. 'input' => array(
  7350. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7351. 'id' => 'ContactImrequiredonboth'
  7352. ),
  7353. '/div'
  7354. );
  7355. $this->assertTags($result, $expected);
  7356. $result = $this->Form->input('Contact.imrequired');
  7357. $expected = array(
  7358. 'div' => array('class' => 'input text required'),
  7359. 'label' => array('for' => 'ContactImrequired'),
  7360. 'Imrequired',
  7361. '/label',
  7362. 'input' => array(
  7363. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7364. 'id' => 'ContactImrequired'
  7365. ),
  7366. '/div'
  7367. );
  7368. $this->assertTags($result, $expected);
  7369. }
  7370. /**
  7371. * Tests that the 'on' key validates as expected on update
  7372. *
  7373. * @return void
  7374. */
  7375. public function testRequiredOnUpdate() {
  7376. $this->Form->request->data['Contact']['id'] = 1;
  7377. $this->Form->create('Contact');
  7378. $result = $this->Form->input('Contact.imrequiredonupdate');
  7379. $expected = array(
  7380. 'div' => array('class' => 'input text required'),
  7381. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7382. 'Imrequiredonupdate',
  7383. '/label',
  7384. 'input' => array(
  7385. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7386. 'id' => 'ContactImrequiredonupdate'
  7387. ),
  7388. '/div'
  7389. );
  7390. $this->assertTags($result, $expected);
  7391. $result = $this->Form->input('Contact.imrequiredoncreate');
  7392. $expected = array(
  7393. 'div' => array('class' => 'input text'),
  7394. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7395. 'Imrequiredoncreate',
  7396. '/label',
  7397. 'input' => array(
  7398. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7399. 'id' => 'ContactImrequiredoncreate'
  7400. ),
  7401. '/div'
  7402. );
  7403. $this->assertTags($result, $expected);
  7404. $result = $this->Form->input('Contact.imrequiredonboth');
  7405. $expected = array(
  7406. 'div' => array('class' => 'input text required'),
  7407. 'label' => array('for' => 'ContactImrequiredonboth'),
  7408. 'Imrequiredonboth',
  7409. '/label',
  7410. 'input' => array(
  7411. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7412. 'id' => 'ContactImrequiredonboth'
  7413. ),
  7414. '/div'
  7415. );
  7416. $this->assertTags($result, $expected);
  7417. $result = $this->Form->input('Contact.imrequired');
  7418. $expected = array(
  7419. 'div' => array('class' => 'input text required'),
  7420. 'label' => array('for' => 'ContactImrequired'),
  7421. 'Imrequired',
  7422. '/label',
  7423. 'input' => array(
  7424. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7425. 'id' => 'ContactImrequired'
  7426. ),
  7427. '/div'
  7428. );
  7429. $this->assertTags($result, $expected);
  7430. }
  7431. /**
  7432. * Test inputDefaults setter and getter
  7433. *
  7434. * @return void
  7435. */
  7436. public function testInputDefaults() {
  7437. $this->Form->create('Contact');
  7438. $this->Form->inputDefaults(array(
  7439. 'label' => false,
  7440. 'div' => array(
  7441. 'style' => 'color: #000;'
  7442. )
  7443. ));
  7444. $result = $this->Form->input('Contact.field1');
  7445. $expected = array(
  7446. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7447. 'input' => array(
  7448. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7449. 'id' => 'ContactField1'
  7450. ),
  7451. '/div'
  7452. );
  7453. $this->assertTags($result, $expected);
  7454. $this->Form->inputDefaults(array(
  7455. 'div' => false,
  7456. 'label' => 'Label',
  7457. ));
  7458. $result = $this->Form->input('Contact.field1');
  7459. $expected = array(
  7460. 'label' => array('for' => 'ContactField1'),
  7461. 'Label',
  7462. '/label',
  7463. 'input' => array(
  7464. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7465. 'id' => 'ContactField1'
  7466. ),
  7467. );
  7468. $this->assertTags($result, $expected);
  7469. $this->Form->inputDefaults(array(
  7470. 'label' => false,
  7471. ), true);
  7472. $result = $this->Form->input('Contact.field1');
  7473. $expected = array(
  7474. 'input' => array(
  7475. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7476. 'id' => 'ContactField1'
  7477. ),
  7478. );
  7479. $this->assertTags($result, $expected);
  7480. $result = $this->Form->inputDefaults();
  7481. $expected = array(
  7482. 'div' => false,
  7483. 'label' => false,
  7484. );
  7485. $this->assertEqual($result, $expected);
  7486. }
  7487. }