FormHelperTest.php 238 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2011, 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-2011, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/view/1196/Testing 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. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  99. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  100. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  101. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  102. 'imalsonotrequired' => array(
  103. 'alpha' => array('rule' => 'alphaNumeric','allowEmpty' => true),
  104. 'between' => array('rule' => array('between', 5, 30)),
  105. ),
  106. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  107. );
  108. /**
  109. * schema method
  110. *
  111. * @return void
  112. */
  113. public function setSchema($schema) {
  114. $this->_schema = $schema;
  115. }
  116. /**
  117. * hasAndBelongsToMany property
  118. *
  119. * @var array
  120. */
  121. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  122. /**
  123. * hasAndBelongsToMany property
  124. *
  125. * @var array
  126. */
  127. public $belongsTo = array('User' => array('className' => 'UserForm'));
  128. }
  129. /**
  130. * ContactTagsContact class
  131. *
  132. * @package cake
  133. * @package Cake.Test.Case.View.Helper
  134. */
  135. class ContactTagsContact extends CakeTestModel {
  136. /**
  137. * useTable property
  138. *
  139. * @var bool false
  140. */
  141. public $useTable = false;
  142. /**
  143. * name property
  144. *
  145. * @var string 'Contact'
  146. */
  147. public $name = 'ContactTagsContact';
  148. /**
  149. * Default schema
  150. *
  151. * @var array
  152. */
  153. protected $_schema = array(
  154. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  155. 'contact_tag_id' => array(
  156. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  157. )
  158. );
  159. /**
  160. * schema method
  161. *
  162. * @return void
  163. */
  164. public function setSchema($schema) {
  165. $this->_schema = $schema;
  166. }
  167. }
  168. /**
  169. * ContactNonStandardPk class
  170. *
  171. * @package cake
  172. * @package Cake.Test.Case.View.Helper
  173. */
  174. class ContactNonStandardPk extends Contact {
  175. /**
  176. * primaryKey property
  177. *
  178. * @var string 'pk'
  179. */
  180. public $primaryKey = 'pk';
  181. /**
  182. * name property
  183. *
  184. * @var string 'ContactNonStandardPk'
  185. */
  186. public $name = 'ContactNonStandardPk';
  187. /**
  188. * schema method
  189. *
  190. * @return void
  191. */
  192. public function schema($field = false) {
  193. $this->_schema = parent::schema();
  194. $this->_schema['pk'] = $this->_schema['id'];
  195. unset($this->_schema['id']);
  196. return $this->_schema;
  197. }
  198. }
  199. /**
  200. * ContactTag class
  201. *
  202. * @package cake
  203. * @package Cake.Test.Case.View.Helper
  204. */
  205. class ContactTag extends Model {
  206. /**
  207. * useTable property
  208. *
  209. * @var bool false
  210. */
  211. public $useTable = false;
  212. /**
  213. * schema definition
  214. *
  215. * @var array
  216. */
  217. protected $_schema = array(
  218. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  219. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  220. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  221. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  222. );
  223. }
  224. /**
  225. * UserForm class
  226. *
  227. * @package cake
  228. * @package Cake.Test.Case.View.Helper
  229. */
  230. class UserForm extends CakeTestModel {
  231. /**
  232. * useTable property
  233. *
  234. * @var bool false
  235. */
  236. public $useTable = false;
  237. /**
  238. * primaryKey property
  239. *
  240. * @var string 'id'
  241. */
  242. public $primaryKey = 'id';
  243. /**
  244. * name property
  245. *
  246. * @var string 'UserForm'
  247. */
  248. public $name = 'UserForm';
  249. /**
  250. * hasMany property
  251. *
  252. * @var array
  253. */
  254. public $hasMany = array(
  255. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  256. ));
  257. /**
  258. * schema definition
  259. *
  260. * @var array
  261. */
  262. protected $_schema = array(
  263. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  264. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  265. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  266. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  267. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  268. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  269. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  270. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  271. );
  272. }
  273. /**
  274. * OpenidUrl class
  275. *
  276. * @package cake
  277. * @package Cake.Test.Case.View.Helper
  278. */
  279. class OpenidUrl extends CakeTestModel {
  280. /**
  281. * useTable property
  282. *
  283. * @var bool false
  284. */
  285. public $useTable = false;
  286. /**
  287. * primaryKey property
  288. *
  289. * @var string 'id'
  290. */
  291. public $primaryKey = 'id';
  292. /**
  293. * name property
  294. *
  295. * @var string 'OpenidUrl'
  296. */
  297. public $name = 'OpenidUrl';
  298. /**
  299. * belongsTo property
  300. *
  301. * @var array
  302. */
  303. public $belongsTo = array('UserForm' => array(
  304. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  305. ));
  306. /**
  307. * validate property
  308. *
  309. * @var array
  310. */
  311. public $validate = array('openid_not_registered' => array());
  312. /**
  313. * schema method
  314. *
  315. * @var array
  316. */
  317. protected $_schema = array(
  318. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  319. 'user_form_id' => array(
  320. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  321. ),
  322. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  323. );
  324. /**
  325. * beforeValidate method
  326. *
  327. * @return void
  328. */
  329. public function beforeValidate($options = array()) {
  330. $this->invalidate('openid_not_registered');
  331. return true;
  332. }
  333. }
  334. /**
  335. * ValidateUser class
  336. *
  337. * @package cake
  338. * @package Cake.Test.Case.View.Helper
  339. */
  340. class ValidateUser extends CakeTestModel {
  341. /**
  342. * primaryKey property
  343. *
  344. * @var string 'id'
  345. */
  346. public $primaryKey = 'id';
  347. /**
  348. * useTable property
  349. *
  350. * @var bool false
  351. */
  352. public $useTable = false;
  353. /**
  354. * name property
  355. *
  356. * @var string 'ValidateUser'
  357. */
  358. public $name = 'ValidateUser';
  359. /**
  360. * hasOne property
  361. *
  362. * @var array
  363. */
  364. public $hasOne = array('ValidateProfile' => array(
  365. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  366. ));
  367. /**
  368. * schema method
  369. *
  370. * @var array
  371. */
  372. protected $_schema = array(
  373. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  374. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  375. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  376. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  377. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  378. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  379. );
  380. /**
  381. * beforeValidate method
  382. *
  383. * @return void
  384. */
  385. public function beforeValidate($options = array()) {
  386. $this->invalidate('email');
  387. return false;
  388. }
  389. }
  390. /**
  391. * ValidateProfile class
  392. *
  393. * @package cake
  394. * @package Cake.Test.Case.View.Helper
  395. */
  396. class ValidateProfile extends CakeTestModel {
  397. /**
  398. * primaryKey property
  399. *
  400. * @var string 'id'
  401. */
  402. public $primaryKey = 'id';
  403. /**
  404. * useTable property
  405. *
  406. * @var bool false
  407. */
  408. public $useTable = false;
  409. /**
  410. * schema property
  411. *
  412. * @var array
  413. */
  414. protected $_schema = array(
  415. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  416. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  417. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  418. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  419. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  420. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  421. );
  422. /**
  423. * name property
  424. *
  425. * @var string 'ValidateProfile'
  426. */
  427. public $name = 'ValidateProfile';
  428. /**
  429. * hasOne property
  430. *
  431. * @var array
  432. */
  433. public $hasOne = array('ValidateItem' => array(
  434. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  435. ));
  436. /**
  437. * belongsTo property
  438. *
  439. * @var array
  440. */
  441. public $belongsTo = array('ValidateUser' => array(
  442. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  443. ));
  444. /**
  445. * beforeValidate method
  446. *
  447. * @return void
  448. */
  449. public function beforeValidate($options = array()) {
  450. $this->invalidate('full_name');
  451. $this->invalidate('city');
  452. return false;
  453. }
  454. }
  455. /**
  456. * ValidateItem class
  457. *
  458. * @package cake
  459. * @package Cake.Test.Case.View.Helper
  460. */
  461. class ValidateItem extends CakeTestModel {
  462. /**
  463. * primaryKey property
  464. *
  465. * @var string 'id'
  466. */
  467. public $primaryKey = 'id';
  468. /**
  469. * useTable property
  470. *
  471. * @var bool false
  472. */
  473. public $useTable = false;
  474. /**
  475. * name property
  476. *
  477. * @var string 'ValidateItem'
  478. */
  479. public $name = 'ValidateItem';
  480. /**
  481. * schema property
  482. *
  483. * @var array
  484. */
  485. protected $_schema = array(
  486. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  487. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  488. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  489. 'description' => array(
  490. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  491. ),
  492. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  493. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  494. );
  495. /**
  496. * belongsTo property
  497. *
  498. * @var array
  499. */
  500. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  501. /**
  502. * beforeValidate method
  503. *
  504. * @return void
  505. */
  506. public function beforeValidate($options = array()) {
  507. $this->invalidate('description');
  508. return false;
  509. }
  510. }
  511. /**
  512. * TestMail class
  513. *
  514. * @package cake
  515. * @package Cake.Test.Case.View.Helper
  516. */
  517. class TestMail extends CakeTestModel {
  518. /**
  519. * primaryKey property
  520. *
  521. * @var string 'id'
  522. */
  523. public $primaryKey = 'id';
  524. /**
  525. * useTable property
  526. *
  527. * @var bool false
  528. */
  529. public $useTable = false;
  530. /**
  531. * name property
  532. *
  533. * @var string 'TestMail'
  534. */
  535. public $name = 'TestMail';
  536. }
  537. /**
  538. * FormHelperTest class
  539. *
  540. * @package cake
  541. * @package Cake.Test.Case.View.Helper
  542. */
  543. class FormHelperTest extends CakeTestCase {
  544. /**
  545. * Fixtures to be used
  546. *
  547. * @var array
  548. */
  549. public $fixtures = array('core.post');
  550. /**
  551. * Do not load the fixtures by default
  552. *
  553. * @var boolean
  554. */
  555. public $autoFixtures = false;
  556. /**
  557. * setUp method
  558. *
  559. * @return void
  560. */
  561. public function setUp() {
  562. parent::setUp();
  563. Configure::write('App.base', '');
  564. $this->Controller = new ContactTestController();
  565. $this->View = new View($this->Controller);
  566. $this->Form = new FormHelper($this->View);
  567. $this->Form->Html = new HtmlHelper($this->View);
  568. $this->Form->request = new CakeRequest('contacts/add', false);
  569. $this->Form->request->here = '/contacts/add';
  570. $this->Form->request['action'] = 'add';
  571. $this->Form->request->webroot = '';
  572. $this->Form->request->base = '';
  573. ClassRegistry::addObject('Contact', new Contact());
  574. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  575. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  576. ClassRegistry::addObject('User', new UserForm());
  577. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  578. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  579. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  580. $this->oldSalt = Configure::read('Security.salt');
  581. $this->dateRegex = array(
  582. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  583. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  584. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  585. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  586. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  587. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  588. );
  589. Configure::write('Security.salt', 'foo!');
  590. }
  591. /**
  592. * tearDown method
  593. *
  594. * @return void
  595. */
  596. public function tearDown() {
  597. parent::tearDown();
  598. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  599. Configure::write('Security.salt', $this->oldSalt);
  600. }
  601. /**
  602. * testFormCreateWithSecurity method
  603. *
  604. * Test form->create() with security key.
  605. *
  606. * @return void
  607. */
  608. public function testCreateWithSecurity() {
  609. $this->Form->request['_Token'] = array('key' => 'testKey');
  610. $encoding = strtolower(Configure::read('App.encoding'));
  611. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  612. $expected = array(
  613. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  614. 'div' => array('style' => 'display:none;'),
  615. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  616. array('input' => array(
  617. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  618. )),
  619. '/div'
  620. );
  621. $this->assertTags($result, $expected);
  622. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  623. $expected['form']['id'] = 'MyForm';
  624. $this->assertTags($result, $expected);
  625. }
  626. /**
  627. * test that create() clears the fields property so it starts fresh
  628. *
  629. * @return void
  630. */
  631. public function testCreateClearingFields() {
  632. $this->Form->fields = array('model_id');
  633. $this->Form->create('Contact');
  634. $this->assertEqual($this->Form->fields, array());
  635. }
  636. /**
  637. * Tests form hash generation with model-less data
  638. *
  639. * @return void
  640. */
  641. public function testValidateHashNoModel() {
  642. $this->Form->request['_Token'] = array('key' => 'foo');
  643. $result = $this->Form->secure(array('anything'));
  644. $this->assertPattern('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  645. }
  646. /**
  647. * Tests that models with identical field names get resolved properly
  648. *
  649. * @return void
  650. */
  651. public function testDuplicateFieldNameResolution() {
  652. $result = $this->Form->create('ValidateUser');
  653. $this->assertEqual($this->Form->entity(), array('ValidateUser'));
  654. $result = $this->Form->input('ValidateItem.name');
  655. $this->assertEqual($this->Form->entity(), array('ValidateItem', 'name'));
  656. $result = $this->Form->input('ValidateUser.name');
  657. $this->assertEqual($this->Form->entity(), array('ValidateUser', 'name'));
  658. $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
  659. $this->assertPattern('/type="text"/', $result);
  660. $result = $this->Form->input('ValidateItem.name');
  661. $this->assertEqual($this->Form->entity(), array('ValidateItem', 'name'));
  662. $this->assertPattern('/name="data\[ValidateItem\]\[name\]"/', $result);
  663. $this->assertPattern('/<textarea/', $result);
  664. $result = $this->Form->input('name');
  665. $this->assertEqual($this->Form->entity(), array('ValidateUser', 'name'));
  666. $this->assertPattern('/name="data\[ValidateUser\]\[name\]"/', $result);
  667. $this->assertPattern('/type="text"/', $result);
  668. }
  669. /**
  670. * Tests that hidden fields generated for checkboxes don't get locked
  671. *
  672. * @return void
  673. */
  674. public function testNoCheckboxLocking() {
  675. $this->Form->request['_Token'] = array('key' => 'foo');
  676. $this->assertIdentical($this->Form->fields, array());
  677. $this->Form->checkbox('check', array('value' => '1'));
  678. $this->assertIdentical($this->Form->fields, array('check'));
  679. }
  680. /**
  681. * testFormSecurityFields method
  682. *
  683. * Test generation of secure form hash generation.
  684. *
  685. * @return void
  686. */
  687. public function testFormSecurityFields() {
  688. $key = 'testKey';
  689. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  690. $this->Form->request['_Token'] = array('key' => $key);
  691. $result = $this->Form->secure($fields);
  692. $expected = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  693. $expected .= ':' . 'Model.valid';
  694. $expected = array(
  695. 'div' => array('style' => 'display:none;'),
  696. array('input' => array(
  697. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  698. 'value' => urlencode($expected), 'id' => 'preg:/TokenFields\d+/'
  699. )),
  700. array('input' => array(
  701. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  702. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  703. )),
  704. '/div'
  705. );
  706. $this->assertTags($result, $expected);
  707. }
  708. /**
  709. * Tests correct generation of number fields for double and float fields
  710. *
  711. * @return void
  712. */
  713. public function testTextFieldGenerationForFloats() {
  714. $model = ClassRegistry::getObject('Contact');
  715. $model->setSchema(array('foo' => array(
  716. 'type' => 'float',
  717. 'null' => false,
  718. 'default' => null,
  719. 'length' => null
  720. )));
  721. $this->Form->create('Contact');
  722. $result = $this->Form->input('foo');
  723. $expected = array(
  724. 'div' => array('class' => 'input number'),
  725. 'label' => array('for' => 'ContactFoo'),
  726. 'Foo',
  727. '/label',
  728. array('input' => array(
  729. 'type' => 'number',
  730. 'name' => 'data[Contact][foo]',
  731. 'id' => 'ContactFoo',
  732. 'step' => 'any'
  733. )),
  734. '/div'
  735. );
  736. $this->assertTags($result, $expected);
  737. $result = $this->Form->input('foo', array('step' => 0.5));
  738. $expected = array(
  739. 'div' => array('class' => 'input number'),
  740. 'label' => array('for' => 'ContactFoo'),
  741. 'Foo',
  742. '/label',
  743. array('input' => array(
  744. 'type' => 'number',
  745. 'name' => 'data[Contact][foo]',
  746. 'id' => 'ContactFoo',
  747. 'step' => '0.5'
  748. )),
  749. '/div'
  750. );
  751. $this->assertTags($result, $expected);
  752. }
  753. /**
  754. * Tests correct generation of number fields for integer fields
  755. *
  756. * @access public
  757. * @return void
  758. */
  759. public function testTextFieldTypeNumberGenerationForIntegers() {
  760. $model = ClassRegistry::getObject('Contact');
  761. $model->setSchema(array('foo' => array(
  762. 'type' => 'integer',
  763. 'null' => false,
  764. 'default' => null,
  765. 'length' => null
  766. )));
  767. $this->Form->create('Contact');
  768. $result = $this->Form->input('foo');
  769. $expected = array(
  770. 'div' => array('class' => 'input number'),
  771. 'label' => array('for' => 'ContactFoo'),
  772. 'Foo',
  773. '/label',
  774. array('input' => array(
  775. 'type' => 'number', 'name' => 'data[Contact][foo]',
  776. 'id' => 'ContactFoo'
  777. )),
  778. '/div'
  779. );
  780. $this->assertTags($result, $expected);
  781. }
  782. /**
  783. * testFormSecurityMultipleFields method
  784. *
  785. * Test secure() with multiple row form. Ensure hash is correct.
  786. *
  787. * @return void
  788. */
  789. public function testFormSecurityMultipleFields() {
  790. $key = 'testKey';
  791. $fields = array(
  792. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  793. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  794. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  795. );
  796. $this->Form->request['_Token'] = array('key' => $key);
  797. $result = $this->Form->secure($fields);
  798. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  799. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  800. $expected = array(
  801. 'div' => array('style' => 'display:none;'),
  802. array('input' => array(
  803. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  804. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  805. )),
  806. array('input' => array(
  807. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  808. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  809. )),
  810. '/div'
  811. );
  812. $this->assertTags($result, $expected);
  813. }
  814. /**
  815. * testFormSecurityMultipleSubmitButtons
  816. *
  817. * test form submit generation and ensure that _Token is only created on end()
  818. *
  819. * @return void
  820. */
  821. public function testFormSecurityMultipleSubmitButtons() {
  822. $key = 'testKey';
  823. $this->Form->request['_Token'] = array('key' => $key);
  824. $this->Form->create('Addresses');
  825. $this->Form->input('Address.title');
  826. $this->Form->input('Address.first_name');
  827. $result = $this->Form->submit('Save', array('name' => 'save'));
  828. $expected = array(
  829. 'div' => array('class' => 'submit'),
  830. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  831. '/div',
  832. );
  833. $this->assertTags($result, $expected);
  834. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  835. $expected = array(
  836. 'div' => array('class' => 'submit'),
  837. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  838. '/div',
  839. );
  840. $this->assertTags($result, $expected);
  841. $result = $this->Form->end(null);
  842. $expected = array(
  843. 'div' => array('style' => 'display:none;'),
  844. array('input' => array(
  845. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  846. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  847. )),
  848. array('input' => array(
  849. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  850. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  851. )),
  852. '/div'
  853. );
  854. $this->assertTags($result, $expected);
  855. }
  856. /**
  857. * Test that the correct fields are unlocked for image submits with no names.
  858. *
  859. * @return void
  860. */
  861. public function testSecuritySubmitImageNoName() {
  862. $key = 'testKey';
  863. $this->Form->request['_Token'] = array('key' => $key);
  864. $this->Form->create('User');
  865. $result = $this->Form->submit('save.png');
  866. $expected = array(
  867. 'div' => array('class' => 'submit'),
  868. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  869. '/div'
  870. );
  871. $this->assertTags($result, $expected);
  872. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  873. }
  874. /**
  875. * Test that the correct fields are unlocked for image submits with names.
  876. *
  877. * @return void
  878. */
  879. public function testSecuritySubmitImageName() {
  880. $key = 'testKey';
  881. $this->Form->request['_Token'] = array('key' => $key);
  882. $this->Form->create('User');
  883. $result = $this->Form->submit('save.png', array('name' => 'test'));
  884. $expected = array(
  885. 'div' => array('class' => 'submit'),
  886. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  887. '/div'
  888. );
  889. $this->assertTags($result, $expected);
  890. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  891. }
  892. /**
  893. * testFormSecurityMultipleInputFields method
  894. *
  895. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  896. *
  897. * @return void
  898. */
  899. public function testFormSecurityMultipleInputFields() {
  900. $key = 'testKey';
  901. $this->Form->request['_Token'] = array('key' => $key);
  902. $this->Form->create('Addresses');
  903. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  904. $this->Form->input('Addresses.0.title');
  905. $this->Form->input('Addresses.0.first_name');
  906. $this->Form->input('Addresses.0.last_name');
  907. $this->Form->input('Addresses.0.address');
  908. $this->Form->input('Addresses.0.city');
  909. $this->Form->input('Addresses.0.phone');
  910. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  911. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  912. $this->Form->input('Addresses.1.title');
  913. $this->Form->input('Addresses.1.first_name');
  914. $this->Form->input('Addresses.1.last_name');
  915. $this->Form->input('Addresses.1.address');
  916. $this->Form->input('Addresses.1.city');
  917. $this->Form->input('Addresses.1.phone');
  918. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  919. $result = $this->Form->secure($this->Form->fields);
  920. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  921. $expected = array(
  922. 'div' => array('style' => 'display:none;'),
  923. array('input' => array(
  924. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  925. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  926. )),
  927. array('input' => array(
  928. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  929. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  930. )),
  931. '/div'
  932. );
  933. $this->assertTags($result, $expected);
  934. }
  935. /**
  936. * Test form security with Model.field.0 style inputs
  937. *
  938. * @return void
  939. */
  940. function testFormSecurityArrayFields() {
  941. $key = 'testKey';
  942. $this->Form->request->params['_Token']['key'] = $key;
  943. $this->Form->create('Address');
  944. $this->Form->input('Address.primary.1');
  945. $this->assertEqual('Address.primary', $this->Form->fields[0]);
  946. }
  947. /**
  948. * testFormSecurityMultipleInputDisabledFields method
  949. *
  950. * test secure form generation with multiple records and disabled fields.
  951. *
  952. * @return void
  953. */
  954. public function testFormSecurityMultipleInputDisabledFields() {
  955. $key = 'testKey';
  956. $this->Form->request->params['_Token'] = array(
  957. 'key' => $key,
  958. 'unlockedFields' => array('first_name', 'address')
  959. );
  960. $this->Form->create();
  961. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  962. $this->Form->input('Addresses.0.title');
  963. $this->Form->input('Addresses.0.first_name');
  964. $this->Form->input('Addresses.0.last_name');
  965. $this->Form->input('Addresses.0.address');
  966. $this->Form->input('Addresses.0.city');
  967. $this->Form->input('Addresses.0.phone');
  968. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  969. $this->Form->input('Addresses.1.title');
  970. $this->Form->input('Addresses.1.first_name');
  971. $this->Form->input('Addresses.1.last_name');
  972. $this->Form->input('Addresses.1.address');
  973. $this->Form->input('Addresses.1.city');
  974. $this->Form->input('Addresses.1.phone');
  975. $result = $this->Form->secure($this->Form->fields);
  976. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  977. $expected = array(
  978. 'div' => array('style' => 'display:none;'),
  979. array('input' => array(
  980. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  981. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  982. )),
  983. array('input' => array(
  984. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  985. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  986. )),
  987. '/div'
  988. );
  989. $this->assertTags($result, $expected);
  990. }
  991. /**
  992. * testFormSecurityInputDisabledFields method
  993. *
  994. * Test single record form with disabled fields.
  995. *
  996. * @return void
  997. */
  998. public function testFormSecurityInputUnlockedFields() {
  999. $key = 'testKey';
  1000. $this->Form->request['_Token'] = array(
  1001. 'key' => $key,
  1002. 'unlockedFields' => array('first_name', 'address')
  1003. );
  1004. $this->Form->create();
  1005. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1006. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1007. $this->Form->input('Addresses.title');
  1008. $this->Form->input('Addresses.first_name');
  1009. $this->Form->input('Addresses.last_name');
  1010. $this->Form->input('Addresses.address');
  1011. $this->Form->input('Addresses.city');
  1012. $this->Form->input('Addresses.phone');
  1013. $result = $this->Form->fields;
  1014. $expected = array(
  1015. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1016. 'Addresses.city', 'Addresses.phone'
  1017. );
  1018. $this->assertEqual($expected, $result);
  1019. $result = $this->Form->secure($expected);
  1020. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.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. * test securing inputs with custom name attributes.
  1037. *
  1038. * @return void
  1039. */
  1040. public function testFormSecureWithCustomNameAttribute() {
  1041. $this->Form->request->params['_Token']['key'] = 'testKey';
  1042. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1043. $this->assertEqual('User.custom', $this->Form->fields[0]);
  1044. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1045. $this->assertEqual('User.custom.another.value', $this->Form->fields[1]);
  1046. }
  1047. /**
  1048. * testFormSecuredInput method
  1049. *
  1050. * Test generation of entire secure form, assertions made on input() output.
  1051. *
  1052. * @return void
  1053. */
  1054. public function testFormSecuredInput() {
  1055. $this->Form->request['_Token'] = array('key' => 'testKey');
  1056. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1057. $encoding = strtolower(Configure::read('App.encoding'));
  1058. $expected = array(
  1059. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1060. 'div' => array('style' => 'display:none;'),
  1061. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1062. array('input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1064. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1065. )),
  1066. '/div'
  1067. );
  1068. $this->assertTags($result, $expected);
  1069. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1070. $expected = array(
  1071. 'div' => array('class' => 'input text'),
  1072. 'label' => array('for' => 'UserFormPublished'),
  1073. 'Published',
  1074. '/label',
  1075. array('input' => array(
  1076. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1077. 'id' => 'UserFormPublished'
  1078. )),
  1079. '/div'
  1080. );
  1081. $this->assertTags($result, $expected);
  1082. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1083. $expected = array(
  1084. 'div' => array('class' => 'input text'),
  1085. 'label' => array('for' => 'UserFormOther'),
  1086. 'Other',
  1087. '/label',
  1088. array('input' => array(
  1089. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1090. 'id' => 'UserFormOther'
  1091. )),
  1092. '/div'
  1093. );
  1094. $this->assertTags($result, $expected);
  1095. $result = $this->Form->hidden('UserForm.stuff');
  1096. $expected = array('input' => array(
  1097. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1098. 'id' => 'UserFormStuff'
  1099. ));
  1100. $this->assertTags($result, $expected);
  1101. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1102. $expected = array('input' => array(
  1103. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1104. 'value' => '0', 'id' => 'UserFormHidden'
  1105. ));
  1106. $this->assertTags($result, $expected);
  1107. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1108. $expected = array(
  1109. 'div' => array('class' => 'input checkbox'),
  1110. array('input' => array(
  1111. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1112. 'value' => '0', 'id' => 'UserFormSomething_'
  1113. )),
  1114. array('input' => array(
  1115. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1116. 'value' => '1', 'id' => 'UserFormSomething'
  1117. )),
  1118. 'label' => array('for' => 'UserFormSomething'),
  1119. 'Something',
  1120. '/label',
  1121. '/div'
  1122. );
  1123. $this->assertTags($result, $expected);
  1124. $result = $this->Form->fields;
  1125. $expected = array(
  1126. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1127. 'UserForm.hidden' => '0', 'UserForm.something'
  1128. );
  1129. $this->assertEqual($expected, $result);
  1130. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1131. $result = $this->Form->secure($this->Form->fields);
  1132. $expected = array(
  1133. 'div' => array('style' => 'display:none;'),
  1134. array('input' => array(
  1135. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1136. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1137. )),
  1138. array('input' => array(
  1139. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1140. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1141. )),
  1142. '/div'
  1143. );
  1144. $this->assertTags($result, $expected);
  1145. }
  1146. /**
  1147. * Tests that the correct keys are added to the field hash index
  1148. *
  1149. * @return void
  1150. */
  1151. public function testFormSecuredFileInput() {
  1152. $this->Form->request['_Token'] = array('key' => 'testKey');
  1153. $this->assertEqual($this->Form->fields, array());
  1154. $result = $this->Form->file('Attachment.file');
  1155. $expected = array (
  1156. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1157. 'Attachment.file.error', 'Attachment.file.size'
  1158. );
  1159. $this->assertEqual($this->Form->fields, $expected);
  1160. }
  1161. /**
  1162. * test that multiple selects keys are added to field hash
  1163. *
  1164. * @return void
  1165. */
  1166. public function testFormSecuredMultipleSelect() {
  1167. $this->Form->request['_Token'] = array('key' => 'testKey');
  1168. $this->assertEqual($this->Form->fields, array());
  1169. $options = array('1' => 'one', '2' => 'two');
  1170. $this->Form->select('Model.select', $options);
  1171. $expected = array('Model.select');
  1172. $this->assertEqual($this->Form->fields, $expected);
  1173. $this->Form->fields = array();
  1174. $this->Form->select('Model.select', $options, array('multiple' => true));
  1175. $this->assertEqual($this->Form->fields, $expected);
  1176. }
  1177. /**
  1178. * testFormSecuredRadio method
  1179. *
  1180. * @return void
  1181. */
  1182. public function testFormSecuredRadio() {
  1183. $this->Form->request['_Token'] = array('key' => 'testKey');
  1184. $this->assertEqual($this->Form->fields, array());
  1185. $options = array('1' => 'option1', '2' => 'option2');
  1186. $this->Form->radio('Test.test', $options);
  1187. $expected = array('Test.test');
  1188. $this->assertEqual($this->Form->fields, $expected);
  1189. }
  1190. /**
  1191. * testDisableSecurityUsingForm method
  1192. *
  1193. * @return void
  1194. */
  1195. public function testDisableSecurityUsingForm() {
  1196. $this->Form->request['_Token'] = array(
  1197. 'key' => 'testKey',
  1198. 'disabledFields' => array()
  1199. );
  1200. $this->Form->create();
  1201. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1202. $this->Form->input('Addresses.title');
  1203. $this->Form->input('Addresses.first_name', array('secure' => false));
  1204. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1205. $this->Form->input('Addresses.zip', array(
  1206. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1207. ));
  1208. $result = $this->Form->fields;
  1209. $expected = array(
  1210. 'Addresses.id' => '123456', 'Addresses.title',
  1211. );
  1212. $this->assertEqual($expected, $result);
  1213. }
  1214. /**
  1215. * test disableField
  1216. *
  1217. * @return void
  1218. */
  1219. public function testUnlockFieldAddsToList() {
  1220. $this->Form->request['_Token'] = array(
  1221. 'key' => 'testKey',
  1222. 'unlockedFields' => array()
  1223. );
  1224. $this->Form->create('Contact');
  1225. $this->Form->unlockField('Contact.name');
  1226. $this->Form->text('Contact.name');
  1227. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1228. $this->assertEquals(array(), $this->Form->fields);
  1229. }
  1230. /**
  1231. * test unlockField removing from fields array.
  1232. *
  1233. * @return void
  1234. */
  1235. public function testUnlockFieldRemovingFromFields() {
  1236. $this->Form->request['_Token'] = array(
  1237. 'key' => 'testKey',
  1238. 'unlockedFields' => array()
  1239. );
  1240. $this->Form->create('Contact');
  1241. $this->Form->hidden('Contact.id', array('value' => 1));
  1242. $this->Form->text('Contact.name');
  1243. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1244. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1245. $this->Form->unlockField('Contact.name');
  1246. $this->Form->unlockField('Contact.id');
  1247. $this->assertEquals(array(), $this->Form->fields);
  1248. }
  1249. /**
  1250. * testPasswordValidation method
  1251. *
  1252. * test validation errors on password input.
  1253. *
  1254. * @return void
  1255. */
  1256. public function testPasswordValidation() {
  1257. $Contact = ClassRegistry::getObject('Contact');
  1258. $Contact->validationErrors['password'] = array('Please provide a password');
  1259. $result = $this->Form->input('Contact.password');
  1260. $expected = array(
  1261. 'div' => array('class' => 'input password error'),
  1262. 'label' => array('for' => 'ContactPassword'),
  1263. 'Password',
  1264. '/label',
  1265. 'input' => array(
  1266. 'type' => 'password', 'name' => 'data[Contact][password]',
  1267. 'id' => 'ContactPassword', 'class' => 'form-error'
  1268. ),
  1269. array('div' => array('class' => 'error-message')),
  1270. 'Please provide a password',
  1271. '/div',
  1272. '/div'
  1273. );
  1274. $this->assertTags($result, $expected);
  1275. }
  1276. /**
  1277. * testEmptyErrorValidation method
  1278. *
  1279. * test validation error div when validation message is an empty string
  1280. *
  1281. * @access public
  1282. * @return void
  1283. */
  1284. function testEmptyErrorValidation() {
  1285. $this->Form->validationErrors['Contact']['password'] = '';
  1286. $result = $this->Form->input('Contact.password');
  1287. $expected = array(
  1288. 'div' => array('class' => 'input password error'),
  1289. 'label' => array('for' => 'ContactPassword'),
  1290. 'Password',
  1291. '/label',
  1292. 'input' => array(
  1293. 'type' => 'password', 'name' => 'data[Contact][password]',
  1294. 'id' => 'ContactPassword', 'class' => 'form-error'
  1295. ),
  1296. array('div' => array('class' => 'error-message')),
  1297. array(),
  1298. '/div',
  1299. '/div'
  1300. );
  1301. $this->assertTags($result, $expected);
  1302. }
  1303. /**
  1304. * testEmptyInputErrorValidation method
  1305. *
  1306. * test validation error div when validation message is overriden by an empty string when calling input()
  1307. *
  1308. * @access public
  1309. * @return void
  1310. */
  1311. function testEmptyInputErrorValidation() {
  1312. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1313. $result = $this->Form->input('Contact.password', array('error' => ''));
  1314. $expected = array(
  1315. 'div' => array('class' => 'input password error'),
  1316. 'label' => array('for' => 'ContactPassword'),
  1317. 'Password',
  1318. '/label',
  1319. 'input' => array(
  1320. 'type' => 'password', 'name' => 'data[Contact][password]',
  1321. 'id' => 'ContactPassword', 'class' => 'form-error'
  1322. ),
  1323. array('div' => array('class' => 'error-message')),
  1324. array(),
  1325. '/div',
  1326. '/div'
  1327. );
  1328. $this->assertTags($result, $expected);
  1329. }
  1330. /**
  1331. * testFormValidationAssociated method
  1332. *
  1333. * test display of form errors in conjunction with model::validates.
  1334. *
  1335. * @return void
  1336. */
  1337. public function testFormValidationAssociated() {
  1338. $this->UserForm = ClassRegistry::getObject('UserForm');
  1339. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1340. $data = array(
  1341. 'UserForm' => array('name' => 'user'),
  1342. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1343. );
  1344. $result = $this->UserForm->OpenidUrl->create($data);
  1345. $this->assertFalse(empty($result));
  1346. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1347. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1348. $encoding = strtolower(Configure::read('App.encoding'));
  1349. $expected = array(
  1350. 'form' => array(
  1351. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1352. 'accept-charset' => $encoding
  1353. ),
  1354. 'div' => array('style' => 'display:none;'),
  1355. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1356. '/div'
  1357. );
  1358. $this->assertTags($result, $expected);
  1359. $result = $this->Form->error(
  1360. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1361. );
  1362. $this->assertEqual($result, 'Error, not registered');
  1363. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1364. }
  1365. /**
  1366. * testFormValidationAssociatedFirstLevel method
  1367. *
  1368. * test form error display with associated model.
  1369. *
  1370. * @return void
  1371. */
  1372. public function testFormValidationAssociatedFirstLevel() {
  1373. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1374. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1375. $data = array(
  1376. 'ValidateUser' => array('name' => 'mariano'),
  1377. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1378. );
  1379. $result = $this->ValidateUser->create($data);
  1380. $this->assertFalse(empty($result));
  1381. $this->assertFalse($this->ValidateUser->validates());
  1382. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1383. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1384. $encoding = strtolower(Configure::read('App.encoding'));
  1385. $expected = array(
  1386. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1387. 'div' => array('style' => 'display:none;'),
  1388. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1389. '/div'
  1390. );
  1391. $this->assertTags($result, $expected);
  1392. $result = $this->Form->error(
  1393. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1394. );
  1395. $this->assertEqual($result, 'Invalid email');
  1396. $result = $this->Form->error(
  1397. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1398. );
  1399. $this->assertEqual($result, 'Invalid name');
  1400. $result = $this->Form->error(
  1401. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1402. );
  1403. $this->assertEqual($result, 'Invalid city');
  1404. unset($this->ValidateUser->ValidateProfile);
  1405. unset($this->ValidateUser);
  1406. }
  1407. /**
  1408. * testFormValidationAssociatedSecondLevel method
  1409. *
  1410. * test form error display with associated model.
  1411. *
  1412. * @return void
  1413. */
  1414. public function testFormValidationAssociatedSecondLevel() {
  1415. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1416. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1417. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1418. $data = array(
  1419. 'ValidateUser' => array('name' => 'mariano'),
  1420. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1421. 'ValidateItem' => array('name' => 'Item')
  1422. );
  1423. $result = $this->ValidateUser->create($data);
  1424. $this->assertFalse(empty($result));
  1425. $this->assertFalse($this->ValidateUser->validates());
  1426. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1427. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1428. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1429. $encoding = strtolower(Configure::read('App.encoding'));
  1430. $expected = array(
  1431. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1432. 'div' => array('style' => 'display:none;'),
  1433. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1434. '/div'
  1435. );
  1436. $this->assertTags($result, $expected);
  1437. $result = $this->Form->error(
  1438. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1439. );
  1440. $this->assertEqual($result, 'Invalid email');
  1441. $result = $this->Form->error(
  1442. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1443. );
  1444. $this->assertEqual($result, 'Invalid name');
  1445. $result = $this->Form->error(
  1446. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1447. );
  1448. $result = $this->Form->error(
  1449. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1450. );
  1451. $this->assertEqual($result, 'Invalid description');
  1452. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1453. unset($this->ValidateUser->ValidateProfile);
  1454. unset($this->ValidateUser);
  1455. }
  1456. /**
  1457. * testFormValidationMultiRecord method
  1458. *
  1459. * test form error display with multiple records.
  1460. *
  1461. * @return void
  1462. */
  1463. public function testFormValidationMultiRecord() {
  1464. $Contact = ClassRegistry::getObject('Contact');
  1465. $Contact->validationErrors[2] = array(
  1466. 'name' => array('This field cannot be left blank')
  1467. );
  1468. $result = $this->Form->input('Contact.2.name');
  1469. $expected = array(
  1470. 'div' => array('class' => 'input text error'),
  1471. 'label' => array('for' => 'Contact2Name'),
  1472. 'Name',
  1473. '/label',
  1474. 'input' => array(
  1475. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1476. 'class' => 'form-error', 'maxlength' => 255
  1477. ),
  1478. array('div' => array('class' => 'error-message')),
  1479. 'This field cannot be left blank',
  1480. '/div',
  1481. '/div'
  1482. );
  1483. $this->assertTags($result, $expected);
  1484. }
  1485. /**
  1486. * testMultipleInputValidation method
  1487. *
  1488. * test multiple record form validation error display.
  1489. *
  1490. * @return void
  1491. */
  1492. public function testMultipleInputValidation() {
  1493. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1494. $Address->validationErrors[0] = array(
  1495. 'title' => array('This field cannot be empty'),
  1496. 'first_name' => array('This field cannot be empty')
  1497. );
  1498. $Address->validationErrors[1] = array(
  1499. 'last_name' => array('You must have a last name')
  1500. );
  1501. $this->Form->create();
  1502. $result = $this->Form->input('Address.0.title');
  1503. $expected = array(
  1504. 'div' => array('class'),
  1505. 'label' => array('for'),
  1506. 'preg:/[^<]+/',
  1507. '/label',
  1508. 'input' => array(
  1509. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1510. ),
  1511. array('div' => array('class' => 'error-message')),
  1512. 'This field cannot be empty',
  1513. '/div',
  1514. '/div'
  1515. );
  1516. $this->assertTags($result, $expected);
  1517. $result = $this->Form->input('Address.0.first_name');
  1518. $expected = array(
  1519. 'div' => array('class'),
  1520. 'label' => array('for'),
  1521. 'preg:/[^<]+/',
  1522. '/label',
  1523. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1524. array('div' => array('class' => 'error-message')),
  1525. 'This field cannot be empty',
  1526. '/div',
  1527. '/div'
  1528. );
  1529. $this->assertTags($result, $expected);
  1530. $result = $this->Form->input('Address.0.last_name');
  1531. $expected = array(
  1532. 'div' => array('class'),
  1533. 'label' => array('for'),
  1534. 'preg:/[^<]+/',
  1535. '/label',
  1536. 'input' => array('type' => 'text', 'name', 'id'),
  1537. '/div'
  1538. );
  1539. $this->assertTags($result, $expected);
  1540. $result = $this->Form->input('Address.1.last_name');
  1541. $expected = array(
  1542. 'div' => array('class'),
  1543. 'label' => array('for'),
  1544. 'preg:/[^<]+/',
  1545. '/label',
  1546. 'input' => array(
  1547. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1548. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1549. ),
  1550. array('div' => array('class' => 'error-message')),
  1551. 'You must have a last name',
  1552. '/div',
  1553. '/div'
  1554. );
  1555. $this->assertTags($result, $expected);
  1556. }
  1557. /**
  1558. * testInput method
  1559. *
  1560. * Test various incarnations of input().
  1561. *
  1562. * @return void
  1563. */
  1564. public function testInput() {
  1565. $result = $this->Form->input('ValidateUser.balance');
  1566. $expected = array(
  1567. 'div' => array('class'),
  1568. 'label' => array('for'),
  1569. 'Balance',
  1570. '/label',
  1571. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1572. '/div',
  1573. );
  1574. $this->assertTags($result, $expected);
  1575. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1576. $expected = array(
  1577. 'div' => array('class' => 'input text'),
  1578. 'label' => array('for' => 'custom'),
  1579. 'Email',
  1580. '/label',
  1581. array('input' => array(
  1582. 'type' => 'text', 'name' => 'data[Contact][email]',
  1583. 'id' => 'custom', 'maxlength' => 255
  1584. )),
  1585. '/div'
  1586. );
  1587. $this->assertTags($result, $expected);
  1588. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1589. $expected = array(
  1590. '<div',
  1591. 'label' => array('for' => 'ContactEmail'),
  1592. 'Email',
  1593. '/label',
  1594. array('input' => array(
  1595. 'type' => 'text', 'name' => 'data[Contact][email]',
  1596. 'id' => 'ContactEmail', 'maxlength' => 255
  1597. )),
  1598. '/div'
  1599. );
  1600. $this->assertTags($result, $expected);
  1601. $result = $this->Form->hidden('Contact.idontexist');
  1602. $expected = array('input' => array(
  1603. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1604. 'id' => 'ContactIdontexist'
  1605. ));
  1606. $this->assertTags($result, $expected);
  1607. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1608. $expected = array(
  1609. 'div' => array('class' => 'input text'),
  1610. 'label' => array('for' => 'ContactEmail'),
  1611. 'Email',
  1612. '/label',
  1613. array('input' => array(
  1614. 'type' => 'text', 'name' => 'data[Contact][email]',
  1615. 'id' => 'ContactEmail'
  1616. )),
  1617. '/div'
  1618. );
  1619. $this->assertTags($result, $expected);
  1620. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1621. $expected = array(
  1622. 'div' => array('class' => 'input text'),
  1623. 'label' => array('for' => 'Contact5Email'),
  1624. 'Email',
  1625. '/label',
  1626. array('input' => array(
  1627. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1628. 'id' => 'Contact5Email'
  1629. )),
  1630. '/div'
  1631. );
  1632. $this->assertTags($result, $expected);
  1633. $result = $this->Form->input('Contact.password');
  1634. $expected = array(
  1635. 'div' => array('class' => 'input password'),
  1636. 'label' => array('for' => 'ContactPassword'),
  1637. 'Password',
  1638. '/label',
  1639. array('input' => array(
  1640. 'type' => 'password', 'name' => 'data[Contact][password]',
  1641. 'id' => 'ContactPassword'
  1642. )),
  1643. '/div'
  1644. );
  1645. $this->assertTags($result, $expected);
  1646. $result = $this->Form->input('Contact.email', array(
  1647. 'type' => 'file', 'class' => 'textbox'
  1648. ));
  1649. $expected = array(
  1650. 'div' => array('class' => 'input file'),
  1651. 'label' => array('for' => 'ContactEmail'),
  1652. 'Email',
  1653. '/label',
  1654. array('input' => array(
  1655. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1656. 'id' => 'ContactEmail'
  1657. )),
  1658. '/div'
  1659. );
  1660. $this->assertTags($result, $expected);
  1661. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1662. $result = $this->Form->input('Contact.phone');
  1663. $expected = array(
  1664. 'div' => array('class' => 'input text'),
  1665. 'label' => array('for' => 'ContactPhone'),
  1666. 'Phone',
  1667. '/label',
  1668. array('input' => array(
  1669. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1670. 'value' => 'Hello &amp; World &gt; weird chars',
  1671. 'id' => 'ContactPhone', 'maxlength' => 255
  1672. )),
  1673. '/div'
  1674. );
  1675. $this->assertTags($result, $expected);
  1676. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1677. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1678. $expected = array(
  1679. 'div' => array('class' => 'input text'),
  1680. 'label' => array('for' => 'myId'),
  1681. 'Field',
  1682. '/label',
  1683. 'input' => array(
  1684. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1685. 'value' => 'My value', 'id' => 'myId'
  1686. ),
  1687. '/div'
  1688. );
  1689. $this->assertTags($result, $expected);
  1690. unset($this->Form->request->data);
  1691. $Contact = ClassRegistry::getObject('Contact');
  1692. $Contact->validationErrors['field'] = array('Badness!');
  1693. $result = $this->Form->input('Contact.field');
  1694. $expected = array(
  1695. 'div' => array('class' => 'input text error'),
  1696. 'label' => array('for' => 'ContactField'),
  1697. 'Field',
  1698. '/label',
  1699. 'input' => array(
  1700. 'type' => 'text', 'name' => 'data[Contact][field]',
  1701. 'id' => 'ContactField', 'class' => 'form-error'
  1702. ),
  1703. array('div' => array('class' => 'error-message')),
  1704. 'Badness!',
  1705. '/div',
  1706. '/div'
  1707. );
  1708. $this->assertTags($result, $expected);
  1709. $result = $this->Form->input('Contact.field', array(
  1710. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1711. ));
  1712. $expected = array(
  1713. 'label' => array('for' => 'ContactField'),
  1714. 'Field',
  1715. '/label',
  1716. 'input' => array(
  1717. 'type' => 'text', 'name' => 'data[Contact][field]',
  1718. 'id' => 'ContactField', 'class' => 'form-error'
  1719. ),
  1720. array('span' => array('class' => 'error-message')),
  1721. 'Badness!',
  1722. '/span'
  1723. );
  1724. $this->assertTags($result, $expected);
  1725. $result = $this->Form->input('Contact.field', array(
  1726. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1727. ));
  1728. $expected = array(
  1729. 'span' => array('class' => 'input text error'),
  1730. 'label' => array('for' => 'ContactField'),
  1731. 'Field',
  1732. '/label',
  1733. 'input' => array(
  1734. 'type' => 'text', 'name' => 'data[Contact][field]',
  1735. 'id' => 'ContactField', 'class' => 'form-error'
  1736. ),
  1737. 'Badness!',
  1738. '/span'
  1739. );
  1740. $this->assertTags($result, $expected);
  1741. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1742. $expected = array(
  1743. 'div' => array('class' => 'input text error'),
  1744. 'label' => array('for' => 'ContactField'),
  1745. 'Field',
  1746. '/label',
  1747. 'input' => array(
  1748. 'type' => 'text', 'name' => 'data[Contact][field]',
  1749. 'id' => 'ContactField', 'class' => 'form-error'
  1750. ),
  1751. 'A message to you, Rudy',
  1752. array('div' => array('class' => 'error-message')),
  1753. 'Badness!',
  1754. '/div',
  1755. '/div'
  1756. );
  1757. $this->assertTags($result, $expected);
  1758. $this->Form->setEntity(null);
  1759. $this->Form->setEntity('Contact.field');
  1760. $result = $this->Form->input('Contact.field', array(
  1761. 'after' => 'A message to you, Rudy', 'error' => false
  1762. ));
  1763. $expected = array(
  1764. 'div' => array('class' => 'input text'),
  1765. 'label' => array('for' => 'ContactField'),
  1766. 'Field',
  1767. '/label',
  1768. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1769. 'A message to you, Rudy',
  1770. '/div'
  1771. );
  1772. $this->assertTags($result, $expected);
  1773. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1774. $expected = array(
  1775. 'div' => array('class' => 'input text'),
  1776. 'label' => array('for' => 'ObjectField'),
  1777. 'Field',
  1778. '/label',
  1779. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1780. 'A message to you, Rudy',
  1781. '/div'
  1782. );
  1783. $this->assertTags($result, $expected);
  1784. $Contact->validationErrors['field'] = array('minLength');
  1785. $result = $this->Form->input('Contact.field', array(
  1786. 'error' => array(
  1787. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1788. 'maxLength' => 'login too large'
  1789. )
  1790. ));
  1791. $expected = array(
  1792. 'div' => array('class' => 'input text error'),
  1793. 'label' => array('for' => 'ContactField'),
  1794. 'Field',
  1795. '/label',
  1796. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1797. array('div' => array('class' => 'error-message')),
  1798. 'Le login doit contenir au moins 2 caractères',
  1799. '/div',
  1800. '/div'
  1801. );
  1802. $this->assertTags($result, $expected);
  1803. $Contact->validationErrors['field'] = array('maxLength');
  1804. $result = $this->Form->input('Contact.field', array(
  1805. 'error' => array(
  1806. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1807. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1808. 'maxLength' => 'login too large',
  1809. )
  1810. ));
  1811. $expected = array(
  1812. 'div' => array('class' => 'input text error'),
  1813. 'label' => array('for' => 'ContactField'),
  1814. 'Field',
  1815. '/label',
  1816. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1817. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1818. 'login too large',
  1819. '/span',
  1820. '/div'
  1821. );
  1822. $this->assertTags($result, $expected);
  1823. }
  1824. /**
  1825. * test input() with checkbox creation
  1826. *
  1827. * @return void
  1828. */
  1829. public function testInputCheckbox() {
  1830. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1831. $expected = array(
  1832. 'div' => array('class' => 'input checkbox'),
  1833. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1834. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1835. '/div'
  1836. );
  1837. $this->assertTags($result, $expected);
  1838. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1839. $expected = array(
  1840. 'div' => array('class' => 'input checkbox'),
  1841. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1842. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1843. '/div'
  1844. );
  1845. $this->assertTags($result, $expected);
  1846. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1847. $expected = array(
  1848. 'div' => array('class' => 'input checkbox'),
  1849. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1850. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1851. '/div'
  1852. );
  1853. $this->assertTags($result, $expected);
  1854. }
  1855. /**
  1856. * test form->input() with time types.
  1857. *
  1858. */
  1859. public function testInputTime() {
  1860. extract($this->dateRegex);
  1861. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1862. $result = explode(':', $result);
  1863. $this->assertPattern('/option value="23"/', $result[0]);
  1864. $this->assertNoPattern('/option value="24"/', $result[0]);
  1865. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1866. $result = explode(':', $result);
  1867. $this->assertPattern('/option value="23"/', $result[0]);
  1868. $this->assertNoPattern('/option value="24"/', $result[0]);
  1869. $result = $this->Form->input('Model.field', array(
  1870. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1871. ));
  1872. $result = explode(':', $result);
  1873. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1874. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1875. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1876. $result = $this->Form->input('Model.field', array(
  1877. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1878. ));
  1879. $result = explode(':', $result);
  1880. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1881. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1882. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1883. $result = $this->Form->input('prueba', array(
  1884. 'type' => 'time', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
  1885. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1886. ));
  1887. $result = explode(':', $result);
  1888. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1889. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1890. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1891. $this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
  1892. $result = $this->Form->input('Random.start_time', array(
  1893. 'type' => 'time',
  1894. 'selected' => '18:15'
  1895. ));
  1896. $this->assertPattern('#<option value="06"[^>]*>6</option>#', $result);
  1897. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result);
  1898. $this->assertPattern('#<option value="pm"[^>]*>pm</option>#', $result);
  1899. }
  1900. /**
  1901. * test form->input() with datetime, date and time types
  1902. *
  1903. * @return void
  1904. */
  1905. function testInputDatetime() {
  1906. extract($this->dateRegex);
  1907. $result = $this->Form->input('prueba', array(
  1908. 'type' => 'datetime', 'timeFormat'=> 24 , 'dateFormat'=>'DMY' , 'minYear' => 2008,
  1909. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1910. ));
  1911. $result = explode(':', $result);
  1912. $this->assertNoPattern('#<option value="12"[^>]*>12</option>#', $result[1]);
  1913. $this->assertNoPattern('#<option value="50"[^>]*>50</option>#', $result[1]);
  1914. $this->assertPattern('#<option value="15"[^>]*>15</option>#', $result[1]);
  1915. $this->assertPattern('#<option value="30"[^>]*>30</option>#', $result[1]);
  1916. //related to ticket #5013
  1917. $result = $this->Form->input('Contact.date', array(
  1918. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  1919. ));
  1920. $this->assertPattern('/class="customClass"/', $result);
  1921. $this->assertPattern('/onChange="function\(\)\{\}"/', $result);
  1922. $result = $this->Form->input('Contact.date', array(
  1923. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  1924. ));
  1925. $this->assertPattern('/id="customIdDay"/', $result);
  1926. $this->assertPattern('/id="customIdMonth"/', $result);
  1927. $this->assertPattern('/onChange="function\(\)\{\}"/', $result);
  1928. $result = $this->Form->input('Model.field', array(
  1929. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  1930. ));
  1931. $this->assertPattern('/id="customIDDay"/', $result);
  1932. $this->assertPattern('/id="customIDHour"/', $result);
  1933. $result = explode('</select><select', $result);
  1934. $result = explode(':', $result[1]);
  1935. $this->assertPattern('/option value="23"/', $result[0]);
  1936. $this->assertNoPattern('/option value="24"/', $result[0]);
  1937. $result = $this->Form->input('Model.field', array(
  1938. 'type' => 'datetime', 'timeFormat' => 12
  1939. ));
  1940. $result = explode('</select><select', $result);
  1941. $result = explode(':', $result[1]);
  1942. $this->assertPattern('/option value="12"/', $result[0]);
  1943. $this->assertNoPattern('/option value="13"/', $result[0]);
  1944. $this->Form->request->data = array('Contact' => array('created' => null));
  1945. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  1946. $expected = array(
  1947. 'div' => array('class' => 'input date'),
  1948. 'label' => array('for' => 'ContactCreatedMonth'),
  1949. 'Created',
  1950. '/label',
  1951. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  1952. array('option' => array('value' => '')), 'Date Unknown', '/option',
  1953. $monthsRegex,
  1954. '/select', '-',
  1955. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  1956. array('option' => array('value' => '')), 'Date Unknown', '/option',
  1957. $daysRegex,
  1958. '/select', '-',
  1959. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  1960. array('option' => array('value' => '')), 'Date Unknown', '/option',
  1961. $yearsRegex,
  1962. '/select',
  1963. '/div'
  1964. );
  1965. $this->assertTags($result, $expected);
  1966. $this->Form->request->data = array('Contact' => array('created' => null));
  1967. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  1968. $this->assertPattern('/for\="ContactCreatedHour"/', $result);
  1969. $this->Form->request->data = array('Contact' => array('created' => null));
  1970. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  1971. $this->assertPattern('/for\="ContactCreatedMonth"/', $result);
  1972. $result = $this->Form->input('Contact.created', array(
  1973. 'type' => 'date',
  1974. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  1975. 'timeFormat' => 'NONE'
  1976. ));
  1977. $this->assertPattern('/for\="created-month"/', $result);
  1978. }
  1979. /**
  1980. * Test generating checkboxes in a loop.
  1981. *
  1982. * @return void
  1983. */
  1984. public function testInputCheckboxesInLoop() {
  1985. for ($i = 1; $i < 5; $i++) {
  1986. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  1987. $expected = array(
  1988. 'div' => array('class' => 'input checkbox'),
  1989. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  1990. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  1991. 'label' => array('for' => "Contact{$i}Email"),
  1992. 'Email',
  1993. '/label',
  1994. '/div'
  1995. );
  1996. $this->assertTags($result, $expected);
  1997. }
  1998. }
  1999. /**
  2000. * test input name with leading integer, ensure attributes are generated correctly.
  2001. *
  2002. * @return void
  2003. */
  2004. public function testInputWithLeadingInteger() {
  2005. $result = $this->Form->text('0.Node.title');
  2006. $expected = array(
  2007. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2008. );
  2009. $this->assertTags($result, $expected);
  2010. }
  2011. /**
  2012. * test form->input() with select type inputs.
  2013. *
  2014. * @return void
  2015. */
  2016. public function testInputSelectType() {
  2017. $result = $this->Form->input('email', array(
  2018. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2019. );
  2020. $expected = array(
  2021. 'div' => array('class' => 'input select'),
  2022. 'label' => array('for' => 'email'),
  2023. 'Email',
  2024. '/label',
  2025. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2026. array('option' => array('value' => '')),
  2027. '/option',
  2028. array('option' => array('value' => 'è')),
  2029. 'Firést',
  2030. '/option',
  2031. array('option' => array('value' => 'é')),
  2032. 'Secoènd',
  2033. '/option',
  2034. '/select',
  2035. '/div'
  2036. );
  2037. $this->assertTags($result, $expected);
  2038. $result = $this->Form->input('email', array(
  2039. 'options' => array('First', 'Second'), 'empty' => true)
  2040. );
  2041. $expected = array(
  2042. 'div' => array('class' => 'input select'),
  2043. 'label' => array('for' => 'email'),
  2044. 'Email',
  2045. '/label',
  2046. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2047. array('option' => array('value' => '')),
  2048. '/option',
  2049. array('option' => array('value' => '0')),
  2050. 'First',
  2051. '/option',
  2052. array('option' => array('value' => '1')),
  2053. 'Second',
  2054. '/option',
  2055. '/select',
  2056. '/div'
  2057. );
  2058. $this->assertTags($result, $expected);
  2059. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2060. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2061. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2062. $expected = array(
  2063. 'div' => array('class' => 'input select'),
  2064. 'label' => array('for' => 'ModelUserId'),
  2065. 'User',
  2066. '/label',
  2067. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2068. array('option' => array('value' => '')),
  2069. '/option',
  2070. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2071. 'good',
  2072. '/option',
  2073. array('option' => array('value' => 'other')),
  2074. 'bad',
  2075. '/option',
  2076. '/select',
  2077. '/div'
  2078. );
  2079. $this->assertTags($result, $expected);
  2080. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2081. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2082. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2083. $expected = array(
  2084. 'div' => array('class' => 'input select'),
  2085. 'label' => array('for' => 'ThingUserId'),
  2086. 'User',
  2087. '/label',
  2088. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2089. array('option' => array('value' => '')),
  2090. 'Some Empty',
  2091. '/option',
  2092. array('option' => array('value' => 'value')),
  2093. 'good',
  2094. '/option',
  2095. array('option' => array('value' => 'other')),
  2096. 'bad',
  2097. '/option',
  2098. '/select',
  2099. '/div'
  2100. );
  2101. $this->assertTags($result, $expected);
  2102. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2103. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2104. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2105. $expected = array(
  2106. 'div' => array('class' => 'input select'),
  2107. 'label' => array('for' => 'ThingUserId'),
  2108. 'User',
  2109. '/label',
  2110. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2111. array('option' => array('value' => '')),
  2112. 'Some Empty',
  2113. '/option',
  2114. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2115. 'good',
  2116. '/option',
  2117. array('option' => array('value' => 'other')),
  2118. 'bad',
  2119. '/option',
  2120. '/select',
  2121. '/div'
  2122. );
  2123. $this->assertTags($result, $expected);
  2124. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2125. $this->Form->request->data = array('User' => array('User' => array('value')));
  2126. $result = $this->Form->input('User.User', array('empty' => true));
  2127. $expected = array(
  2128. 'div' => array('class' => 'input select'),
  2129. 'label' => array('for' => 'UserUser'),
  2130. 'User',
  2131. '/label',
  2132. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2133. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2134. array('option' => array('value' => '')),
  2135. '/option',
  2136. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2137. 'good',
  2138. '/option',
  2139. array('option' => array('value' => 'other')),
  2140. 'bad',
  2141. '/option',
  2142. '/select',
  2143. '/div'
  2144. );
  2145. $this->assertTags($result, $expected);
  2146. $this->Form->data = array();
  2147. $result = $this->Form->input('Publisher.id', array(
  2148. 'label' => 'Publisher',
  2149. 'type' => 'select',
  2150. 'multiple' => 'checkbox',
  2151. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2152. ));
  2153. $expected = array(
  2154. array('div' => array('class' => 'input select')),
  2155. array('label' => array('for' => 'PublisherId')),
  2156. 'Publisher',
  2157. '/label',
  2158. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2159. array('div' => array('class' => 'checkbox')),
  2160. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2161. array('label' => array('for' => 'PublisherIdValue1')),
  2162. 'Label 1',
  2163. '/label',
  2164. '/div',
  2165. array('div' => array('class' => 'checkbox')),
  2166. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2167. array('label' => array('for' => 'PublisherIdValue2')),
  2168. 'Label 2',
  2169. '/label',
  2170. '/div',
  2171. '/div'
  2172. );
  2173. $this->assertTags($result, $expected);
  2174. }
  2175. /**
  2176. * test that input() and a non standard primary key makes a hidden input by default.
  2177. *
  2178. * @return void
  2179. */
  2180. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2181. $this->Form->create('User');
  2182. $this->Form->fieldset = array(
  2183. 'User' => array(
  2184. 'fields' => array(
  2185. 'model_id' => array('type' => 'integer')
  2186. ),
  2187. 'validates' => array(),
  2188. 'key' => 'model_id'
  2189. )
  2190. );
  2191. $result = $this->Form->input('model_id');
  2192. $expected = array(
  2193. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2194. );
  2195. $this->assertTags($result, $expected);
  2196. }
  2197. /**
  2198. * test that overriding the magic select type widget is possible
  2199. *
  2200. * @return void
  2201. */
  2202. public function testInputOverridingMagicSelectType() {
  2203. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2204. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2205. $expected = array(
  2206. 'div' => array('class' => 'input text'),
  2207. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2208. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2209. '/div'
  2210. );
  2211. $this->assertTags($result, $expected);
  2212. //Check that magic types still work for plural/singular vars
  2213. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2214. $result = $this->Form->input('Model.type');
  2215. $expected = array(
  2216. 'div' => array('class' => 'input select'),
  2217. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2218. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2219. array('option' => array('value' => 'value')), 'good', '/option',
  2220. array('option' => array('value' => 'other')), 'bad', '/option',
  2221. '/select',
  2222. '/div'
  2223. );
  2224. $this->assertTags($result, $expected);
  2225. }
  2226. /**
  2227. * Test that magic input() selects can easily be converted into radio types without error.
  2228. *
  2229. * @return void
  2230. */
  2231. public function testInputMagicSelectChangeToRadio() {
  2232. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2233. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2234. $this->assertPattern('/input type="radio"/', $result);
  2235. }
  2236. /**
  2237. * fields with the same name as the model should work.
  2238. *
  2239. * @return void
  2240. */
  2241. public function testInputWithMatchingFieldAndModelName() {
  2242. $this->Form->create('User');
  2243. $this->Form->fieldset = array(
  2244. 'User' => array(
  2245. 'fields' => array(
  2246. 'User' => array('type' => 'text')
  2247. ),
  2248. 'validates' => array(),
  2249. 'key' => 'id'
  2250. )
  2251. );
  2252. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2253. $result = $this->Form->input('User', array('type' => 'text'));
  2254. $expected = array(
  2255. 'div' => array('class' => 'input text'),
  2256. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2257. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2258. '/div'
  2259. );
  2260. $this->assertTags($result, $expected);
  2261. }
  2262. /**
  2263. * testFormInputs method
  2264. *
  2265. * test correct results from form::inputs().
  2266. *
  2267. * @return void
  2268. */
  2269. public function testFormInputs() {
  2270. $this->Form->create('Contact');
  2271. $result = $this->Form->inputs('The Legend');
  2272. $expected = array(
  2273. '<fieldset',
  2274. '<legend',
  2275. 'The Legend',
  2276. '/legend',
  2277. '*/fieldset',
  2278. );
  2279. $this->assertTags($result, $expected);
  2280. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2281. $expected = array(
  2282. 'fieldset' => array('class' => 'classy-stuff'),
  2283. '<legend',
  2284. 'Field of Dreams',
  2285. '/legend',
  2286. '*/fieldset'
  2287. );
  2288. $this->assertTags($result, $expected);
  2289. $this->Form->create('Contact');
  2290. $this->Form->request['prefix'] = 'admin';
  2291. $this->Form->request['action'] = 'admin_edit';
  2292. $result = $this->Form->inputs();
  2293. $expected = array(
  2294. '<fieldset',
  2295. '<legend',
  2296. 'Edit Contact',
  2297. '/legend',
  2298. '*/fieldset',
  2299. );
  2300. $this->assertTags($result, $expected);
  2301. $this->Form->create('Contact');
  2302. $result = $this->Form->inputs(false);
  2303. $expected = array(
  2304. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2305. array('div' => array('class' => 'input text')),
  2306. '*/div',
  2307. array('div' => array('class' => 'input text')),
  2308. '*/div',
  2309. array('div' => array('class' => 'input text')),
  2310. '*/div',
  2311. array('div' => array('class' => 'input password')),
  2312. '*/div',
  2313. array('div' => array('class' => 'input date')),
  2314. '*/div',
  2315. array('div' => array('class' => 'input date')),
  2316. '*/div',
  2317. array('div' => array('class' => 'input datetime')),
  2318. '*/div',
  2319. array('div' => array('class' => 'input number')),
  2320. '*/div',
  2321. array('div' => array('class' => 'input select')),
  2322. '*/div',
  2323. );
  2324. $this->assertTags($result, $expected);
  2325. $this->Form->create('Contact');
  2326. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2327. $expected = array(
  2328. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2329. array('div' => array('class' => 'input text')),
  2330. '*/div',
  2331. array('div' => array('class' => 'input text')),
  2332. '*/div',
  2333. array('div' => array('class' => 'input text')),
  2334. '*/div',
  2335. array('div' => array('class' => 'input password')),
  2336. '*/div',
  2337. array('div' => array('class' => 'input date')),
  2338. '*/div',
  2339. array('div' => array('class' => 'input date')),
  2340. '*/div',
  2341. array('div' => array('class' => 'input datetime')),
  2342. '*/div',
  2343. array('div' => array('class' => 'input number')),
  2344. '*/div',
  2345. array('div' => array('class' => 'input select')),
  2346. '*/div',
  2347. );
  2348. $this->assertTags($result, $expected);
  2349. $this->Form->create('Contact');
  2350. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2351. $expected = array(
  2352. 'fieldset' => array(),
  2353. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2354. array('div' => array('class' => 'input text')),
  2355. '*/div',
  2356. array('div' => array('class' => 'input text')),
  2357. '*/div',
  2358. array('div' => array('class' => 'input text')),
  2359. '*/div',
  2360. array('div' => array('class' => 'input password')),
  2361. '*/div',
  2362. array('div' => array('class' => 'input date')),
  2363. '*/div',
  2364. array('div' => array('class' => 'input date')),
  2365. '*/div',
  2366. array('div' => array('class' => 'input datetime')),
  2367. '*/div',
  2368. array('div' => array('class' => 'input number')),
  2369. '*/div',
  2370. array('div' => array('class' => 'input select')),
  2371. '*/div',
  2372. '/fieldset'
  2373. );
  2374. $this->assertTags($result, $expected);
  2375. $this->Form->create('Contact');
  2376. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2377. $expected = array(
  2378. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2379. array('div' => array('class' => 'input text')),
  2380. '*/div',
  2381. array('div' => array('class' => 'input text')),
  2382. '*/div',
  2383. array('div' => array('class' => 'input text')),
  2384. '*/div',
  2385. array('div' => array('class' => 'input password')),
  2386. '*/div',
  2387. array('div' => array('class' => 'input date')),
  2388. '*/div',
  2389. array('div' => array('class' => 'input date')),
  2390. '*/div',
  2391. array('div' => array('class' => 'input datetime')),
  2392. '*/div',
  2393. array('div' => array('class' => 'input number')),
  2394. '*/div',
  2395. array('div' => array('class' => 'input select')),
  2396. '*/div',
  2397. );
  2398. $this->assertTags($result, $expected);
  2399. $this->Form->create('Contact');
  2400. $result = $this->Form->inputs('Hello');
  2401. $expected = array(
  2402. 'fieldset' => array(),
  2403. 'legend' => array(),
  2404. 'Hello',
  2405. '/legend',
  2406. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2407. array('div' => array('class' => 'input text')),
  2408. '*/div',
  2409. array('div' => array('class' => 'input text')),
  2410. '*/div',
  2411. array('div' => array('class' => 'input text')),
  2412. '*/div',
  2413. array('div' => array('class' => 'input password')),
  2414. '*/div',
  2415. array('div' => array('class' => 'input date')),
  2416. '*/div',
  2417. array('div' => array('class' => 'input date')),
  2418. '*/div',
  2419. array('div' => array('class' => 'input datetime')),
  2420. '*/div',
  2421. array('div' => array('class' => 'input number')),
  2422. '*/div',
  2423. array('div' => array('class' => 'input select')),
  2424. '*/div',
  2425. '/fieldset'
  2426. );
  2427. $this->assertTags($result, $expected);
  2428. $this->Form->create('Contact');
  2429. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2430. $expected = array(
  2431. 'fieldset' => array(),
  2432. 'legend' => array(),
  2433. 'Hello',
  2434. '/legend',
  2435. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2436. array('div' => array('class' => 'input text')),
  2437. '*/div',
  2438. array('div' => array('class' => 'input text')),
  2439. '*/div',
  2440. array('div' => array('class' => 'input text')),
  2441. '*/div',
  2442. array('div' => array('class' => 'input password')),
  2443. '*/div',
  2444. array('div' => array('class' => 'input date')),
  2445. '*/div',
  2446. array('div' => array('class' => 'input date')),
  2447. '*/div',
  2448. array('div' => array('class' => 'input datetime')),
  2449. '*/div',
  2450. array('div' => array('class' => 'input number')),
  2451. '*/div',
  2452. array('div' => array('class' => 'input select')),
  2453. '*/div',
  2454. '/fieldset'
  2455. );
  2456. $this->assertTags($result, $expected);
  2457. }
  2458. /**
  2459. * testSelectAsCheckbox method
  2460. *
  2461. * test multi-select widget with checkbox formatting.
  2462. *
  2463. * @return void
  2464. */
  2465. public function testSelectAsCheckbox() {
  2466. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2467. $expected = array(
  2468. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2469. array('div' => array('class' => 'checkbox')),
  2470. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2471. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2472. 'first',
  2473. '/label',
  2474. '/div',
  2475. array('div' => array('class' => 'checkbox')),
  2476. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2477. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2478. 'second',
  2479. '/label',
  2480. '/div',
  2481. array('div' => array('class' => 'checkbox')),
  2482. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2483. array('label' => array('for' => 'ModelMultiField2')),
  2484. 'third',
  2485. '/label',
  2486. '/div',
  2487. );
  2488. $this->assertTags($result, $expected);
  2489. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2490. $expected = array(
  2491. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2492. array('div' => array('class' => 'checkbox')),
  2493. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2494. array('label' => array('for' => 'ModelMultiField12')),
  2495. 'half',
  2496. '/label',
  2497. '/div',
  2498. );
  2499. $this->assertTags($result, $expected);
  2500. }
  2501. /**
  2502. * testLabel method
  2503. *
  2504. * test label generation.
  2505. *
  2506. * @return void
  2507. */
  2508. public function testLabel() {
  2509. $this->Form->text('Person.name');
  2510. $result = $this->Form->label();
  2511. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2512. $this->Form->text('Person.name');
  2513. $result = $this->Form->label();
  2514. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2515. $result = $this->Form->label('Person.first_name');
  2516. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2517. $result = $this->Form->label('Person.first_name', 'Your first name');
  2518. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2519. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2520. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2521. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2522. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2523. $result = $this->Form->label('Person.first_name', '');
  2524. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2525. $result = $this->Form->label('Person.2.name', '');
  2526. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2527. }
  2528. /**
  2529. * testTextbox method
  2530. *
  2531. * test textbox element generation
  2532. *
  2533. * @return void
  2534. */
  2535. public function testTextbox() {
  2536. $result = $this->Form->text('Model.field');
  2537. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2538. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2539. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2540. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2541. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2542. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2543. $result = $this->Form->text('Model.text');
  2544. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2545. $Contact = ClassRegistry::getObject('Contact');
  2546. $Contact->validationErrors['text'] = array(true);
  2547. $this->Form->request->data['Contact']['text'] = 'test';
  2548. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2549. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2550. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2551. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2552. $expected = array(
  2553. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2554. );
  2555. $this->assertTags($result, $expected);
  2556. }
  2557. /**
  2558. * testDefaultValue method
  2559. *
  2560. * Test default value setting
  2561. *
  2562. * @return void
  2563. */
  2564. public function testDefaultValue() {
  2565. $this->Form->request->data['Model']['field'] = 'test';
  2566. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2567. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2568. unset($this->Form->request->data['Model']['field']);
  2569. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2570. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2571. }
  2572. /**
  2573. * testCheckboxDefaultValue method
  2574. *
  2575. * Test default value setting on checkbox() method
  2576. *
  2577. * @return void
  2578. */
  2579. public function testCheckboxDefaultValue() {
  2580. $this->Form->request->data['Model']['field'] = false;
  2581. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2582. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2583. unset($this->Form->request->data['Model']['field']);
  2584. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2585. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2586. $this->Form->request->data['Model']['field'] = true;
  2587. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2588. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2589. unset($this->Form->request->data['Model']['field']);
  2590. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2591. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2592. }
  2593. /**
  2594. * testError method
  2595. *
  2596. * Test field error generation
  2597. *
  2598. * @return void
  2599. */
  2600. public function testError() {
  2601. $Contact = ClassRegistry::getObject('Contact');
  2602. $Contact->validationErrors['field'] = array(1);
  2603. $result = $this->Form->error('Contact.field');
  2604. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2605. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2606. $this->assertEqual($result, 'Error in field Field');
  2607. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2608. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2609. $this->assertEqual($result, 'This field contains invalid input');
  2610. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2611. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2612. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2613. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2614. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2615. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2616. $this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
  2617. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2618. $this->assertEqual($result, '&lt;strong&gt;Badness!&lt;/strong&gt;');
  2619. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2620. $this->assertEqual($result, '<strong>Badness!</strong>');
  2621. $Contact->validationErrors['field'] = array("email");
  2622. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2623. $expected = array(
  2624. 'div' => array('class' => 'field-error'),
  2625. 'No good!',
  2626. '/div'
  2627. );
  2628. $this->assertTags($result, $expected);
  2629. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2630. $result = $this->Form->error('Contact.field', array(
  2631. 'notEmpty' => 'Cannot be empty',
  2632. 'email' => 'No good!'
  2633. ));
  2634. $expected = array(
  2635. 'div' => array('class' => 'error-message'),
  2636. 'ul' => array(),
  2637. '<li', 'Cannot be empty', '/li',
  2638. '<li', 'No good!', '/li',
  2639. '<li', 'Something else', '/li',
  2640. '/ul',
  2641. '/div'
  2642. );
  2643. $this->assertTags($result, $expected);
  2644. /** Testing error messages list options **/
  2645. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2646. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2647. $expected = array(
  2648. 'div' => array('class' => 'error-message'),
  2649. 'ol' => array(),
  2650. '<li', 'notEmpty', '/li',
  2651. '<li', 'email', '/li',
  2652. '/ol',
  2653. '/div'
  2654. );
  2655. $this->assertTags($result, $expected);
  2656. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2657. $expected = array(
  2658. 'div' => array('class' => 'error-message'),
  2659. 'ol' => array(),
  2660. '<li', 'notEmpty', '/li',
  2661. '<li', 'email', '/li',
  2662. '/ol',
  2663. '/div'
  2664. );
  2665. $this->assertTags($result, $expected);
  2666. $result = $this->Form->error('Contact.field', null, array(
  2667. 'listOptions' => array(
  2668. 'class' => 'ul-class',
  2669. 'itemOptions' => array(
  2670. 'class' => 'li-class'
  2671. )
  2672. )
  2673. ));
  2674. $expected = array(
  2675. 'div' => array('class' => 'error-message'),
  2676. 'ul' => array('class' => 'ul-class'),
  2677. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2678. array('li' => array('class' => 'li-class')), 'email', '/li',
  2679. '/ul',
  2680. '/div'
  2681. );
  2682. $this->assertTags($result, $expected);
  2683. }
  2684. /**
  2685. * test error options when using form->input();
  2686. *
  2687. * @return void
  2688. */
  2689. public function testInputErrorEscape() {
  2690. $this->Form->create('ValidateProfile');
  2691. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2692. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2693. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2694. $this->assertPattern('/required&lt;br&gt;/', $result);
  2695. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2696. $this->assertPattern('/required<br>/', $result);
  2697. }
  2698. /**
  2699. * testPassword method
  2700. *
  2701. * Test password element generation
  2702. *
  2703. * @return void
  2704. */
  2705. public function testPassword() {
  2706. $Contact = ClassRegistry::getObject('Contact');
  2707. $result = $this->Form->password('Contact.field');
  2708. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2709. $Contact->validationErrors['passwd'] = 1;
  2710. $this->Form->request->data['Contact']['passwd'] = 'test';
  2711. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2712. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2713. }
  2714. /**
  2715. * testRadio method
  2716. *
  2717. * Test radio element set generation
  2718. *
  2719. * @return void
  2720. */
  2721. public function testRadio() {
  2722. $result = $this->Form->radio('Model.field', array('option A'));
  2723. $expected = array(
  2724. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2725. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2726. 'label' => array('for' => 'ModelField0'),
  2727. 'option A',
  2728. '/label'
  2729. );
  2730. $this->assertTags($result, $expected);
  2731. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2732. $expected = array(
  2733. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2734. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2735. 'label' => array('for' => 'ModelField12'),
  2736. 'half',
  2737. '/label'
  2738. );
  2739. $this->assertTags($result, $expected);
  2740. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2741. $expected = array(
  2742. 'fieldset' => array(),
  2743. 'legend' => array(),
  2744. 'Field',
  2745. '/legend',
  2746. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2747. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2748. array('label' => array('for' => 'ModelField0')),
  2749. 'option A',
  2750. '/label',
  2751. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2752. array('label' => array('for' => 'ModelField1')),
  2753. 'option B',
  2754. '/label',
  2755. '/fieldset'
  2756. );
  2757. $this->assertTags($result, $expected);
  2758. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2759. $expected = array(
  2760. 'fieldset' => array(),
  2761. 'legend' => array(),
  2762. 'Field',
  2763. '/legend',
  2764. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2765. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2766. array('label' => array('for' => 'ModelField0')),
  2767. 'option A',
  2768. '/label',
  2769. 'br' => array(),
  2770. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2771. array('label' => array('for' => 'ModelField1')),
  2772. 'option B',
  2773. '/label',
  2774. '/fieldset'
  2775. );
  2776. $this->assertTags($result, $expected);
  2777. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2778. $expected = array(
  2779. 'fieldset' => array(),
  2780. 'legend' => array(),
  2781. 'Field',
  2782. '/legend',
  2783. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2784. array('label' => array('for' => 'ModelField1')),
  2785. 'Yes',
  2786. '/label',
  2787. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2788. array('label' => array('for' => 'ModelField0')),
  2789. 'No',
  2790. '/label',
  2791. '/fieldset'
  2792. );
  2793. $this->assertTags($result, $expected);
  2794. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2795. $expected = array(
  2796. 'fieldset' => array(),
  2797. 'legend' => array(),
  2798. 'Field',
  2799. '/legend',
  2800. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2801. array('label' => array('for' => 'ModelField1')),
  2802. 'Yes',
  2803. '/label',
  2804. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2805. array('label' => array('for' => 'ModelField0')),
  2806. 'No',
  2807. '/label',
  2808. '/fieldset'
  2809. );
  2810. $this->assertTags($result, $expected);
  2811. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2812. $expected = array(
  2813. 'fieldset' => array(),
  2814. 'legend' => array(),
  2815. 'Field',
  2816. '/legend',
  2817. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2818. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2819. array('label' => array('for' => 'ModelField1')),
  2820. 'Yes',
  2821. '/label',
  2822. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2823. array('label' => array('for' => 'ModelField0')),
  2824. 'No',
  2825. '/label',
  2826. '/fieldset'
  2827. );
  2828. $this->assertTags($result, $expected);
  2829. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  2830. $expected = array(
  2831. 'fieldset' => array(),
  2832. 'legend' => array(),
  2833. 'Field',
  2834. '/legend',
  2835. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2836. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2837. array('label' => array('for' => 'ModelField1')),
  2838. 'Yes',
  2839. '/label',
  2840. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2841. array('label' => array('for' => 'ModelField0')),
  2842. 'No',
  2843. '/label',
  2844. '/fieldset'
  2845. );
  2846. $this->assertTags($result, $expected);
  2847. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2848. $expected = array(
  2849. 'div' => array('class' => 'input radio'),
  2850. 'fieldset' => array(),
  2851. 'legend' => array(),
  2852. 'Legend title',
  2853. '/legend',
  2854. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2855. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2856. array('label' => array('for' => 'NewsletterSubscribe0')),
  2857. 'Unsubscribe',
  2858. '/label',
  2859. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2860. array('label' => array('for' => 'NewsletterSubscribe1')),
  2861. 'Subscribe',
  2862. '/label',
  2863. '/fieldset',
  2864. '/div'
  2865. );
  2866. $this->assertTags($result, $expected);
  2867. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2868. $expected = array(
  2869. 'div' => array('class' => 'input radio'),
  2870. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2871. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2872. array('label' => array('for' => 'NewsletterSubscribe0')),
  2873. 'Unsubscribe',
  2874. '/label',
  2875. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2876. array('label' => array('for' => 'NewsletterSubscribe1')),
  2877. 'Subscribe',
  2878. '/label',
  2879. '/div'
  2880. );
  2881. $this->assertTags($result, $expected);
  2882. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2883. $expected = array(
  2884. 'div' => array('class' => 'input radio'),
  2885. 'fieldset' => array(),
  2886. 'legend' => array(),
  2887. 'Legend title',
  2888. '/legend',
  2889. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2890. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2891. 'Unsubscribe',
  2892. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2893. 'Subscribe',
  2894. '/fieldset',
  2895. '/div'
  2896. );
  2897. $this->assertTags($result, $expected);
  2898. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2899. $expected = array(
  2900. 'div' => array('class' => 'input radio'),
  2901. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  2902. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2903. 'Unsubscribe',
  2904. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  2905. 'Subscribe',
  2906. '/div'
  2907. );
  2908. $this->assertTags($result, $expected);
  2909. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  2910. $expected = array(
  2911. 'div' => array('class' => 'input radio'),
  2912. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  2913. 'Unsubscribe',
  2914. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  2915. 'Subscribe',
  2916. '/div'
  2917. );
  2918. $this->assertTags($result, $expected);
  2919. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  2920. $expected = array(
  2921. 'fieldset' => array(),
  2922. 'legend' => array(),
  2923. 'Gender',
  2924. '/legend',
  2925. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  2926. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  2927. array('label' => array('for' => 'EmployeeGenderMale')),
  2928. 'Male',
  2929. '/label',
  2930. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  2931. array('label' => array('for' => 'EmployeeGenderFemale')),
  2932. 'Female',
  2933. '/label',
  2934. '/fieldset',
  2935. );
  2936. $this->assertTags($result, $expected);
  2937. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  2938. $expected = array(
  2939. 'fieldset' => array(),
  2940. 'legend' => array(),
  2941. 'Gender',
  2942. '/legend',
  2943. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  2944. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  2945. array('label' => array('for' => 'OfficerGenderMale')),
  2946. 'Male',
  2947. '/label',
  2948. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  2949. array('label' => array('for' => 'OfficerGenderFemale')),
  2950. 'Female',
  2951. '/label',
  2952. '/fieldset',
  2953. );
  2954. $this->assertTags($result, $expected);
  2955. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  2956. $expected = array(
  2957. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  2958. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  2959. 'label' => array('for' => 'Contact1Imrequired0'),
  2960. 'option A',
  2961. '/label'
  2962. );
  2963. $this->assertTags($result, $expected);
  2964. $result = $this->Form->radio('Model.1.field', array('option A'));
  2965. $expected = array(
  2966. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  2967. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  2968. 'label' => array('for' => 'Model1Field0'),
  2969. 'option A',
  2970. '/label'
  2971. );
  2972. $this->assertTags($result, $expected);
  2973. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  2974. $expected = array(
  2975. 'fieldset' => array(),
  2976. 'legend' => array(),
  2977. 'Field',
  2978. '/legend',
  2979. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  2980. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  2981. array('label' => array('for' => 'ModelField0')),
  2982. 'option A',
  2983. '/label',
  2984. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  2985. array('label' => array('for' => 'ModelField1')),
  2986. 'option B',
  2987. '/label',
  2988. '/fieldset'
  2989. );
  2990. $this->assertTags($result, $expected);
  2991. }
  2992. /**
  2993. * test disabled radio options
  2994. *
  2995. * @return void
  2996. */
  2997. public function testRadioDisabled() {
  2998. $result = $this->Form->radio(
  2999. 'Model.field',
  3000. array('option A', 'option B'),
  3001. array('disabled' => array('option A'), 'value' => 'option A')
  3002. );
  3003. $expected = array(
  3004. 'fieldset' => array(),
  3005. 'legend' => array(),
  3006. 'Field',
  3007. '/legend',
  3008. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3009. array('label' => array('for' => 'ModelField0')),
  3010. 'option A',
  3011. '/label',
  3012. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3013. array('label' => array('for' => 'ModelField1')),
  3014. 'option B',
  3015. '/label',
  3016. '/fieldset'
  3017. );
  3018. $this->assertTags($result, $expected);
  3019. }
  3020. /**
  3021. * test disabling the hidden input for radio buttons
  3022. *
  3023. * @return void
  3024. */
  3025. public function testRadioHiddenInputDisabling() {
  3026. $result = $this->Form->input('Model.1.field', array(
  3027. 'type' => 'radio',
  3028. 'options' => array('option A'),
  3029. 'hiddenField' => false
  3030. )
  3031. );
  3032. $expected = array(
  3033. 'div' => array('class' => 'input radio'),
  3034. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3035. 'label' => array('for' => 'Model1Field0'),
  3036. 'option A',
  3037. '/label',
  3038. '/div'
  3039. );
  3040. $this->assertTags($result, $expected);
  3041. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3042. $expected = array(
  3043. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3044. 'label' => array('for' => 'Model1Field0'),
  3045. 'option A',
  3046. '/label'
  3047. );
  3048. $this->assertTags($result, $expected);
  3049. }
  3050. /**
  3051. * testSelect method
  3052. *
  3053. * Test select element generation.
  3054. *
  3055. * @return void
  3056. */
  3057. public function testSelect() {
  3058. $result = $this->Form->select('Model.field', array());
  3059. $expected = array(
  3060. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3061. array('option' => array('value' => '')),
  3062. '/option',
  3063. '/select'
  3064. );
  3065. $this->assertTags($result, $expected);
  3066. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3067. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3068. $expected = array(
  3069. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3070. array('option' => array('value' => '')),
  3071. '/option',
  3072. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3073. 'good',
  3074. '/option',
  3075. array('option' => array('value' => 'other')),
  3076. 'bad',
  3077. '/option',
  3078. '/select'
  3079. );
  3080. $this->assertTags($result, $expected);
  3081. $this->Form->request->data = array();
  3082. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3083. $expected = array(
  3084. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3085. array('option' => array('value' => '')),
  3086. '/option',
  3087. array('option' => array('value' => 'value')),
  3088. 'good',
  3089. '/option',
  3090. array('option' => array('value' => 'other')),
  3091. 'bad',
  3092. '/option',
  3093. '/select'
  3094. );
  3095. $this->assertTags($result, $expected);
  3096. $result = $this->Form->select(
  3097. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3098. array('empty' => false)
  3099. );
  3100. $expected = array(
  3101. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3102. array('option' => array('value' => 'first')),
  3103. 'first &quot;html&quot; &lt;chars&gt;',
  3104. '/option',
  3105. array('option' => array('value' => 'second')),
  3106. 'value',
  3107. '/option',
  3108. '/select'
  3109. );
  3110. $this->assertTags($result, $expected);
  3111. $result = $this->Form->select(
  3112. 'Model.field',
  3113. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3114. array('escape' => false, 'empty' => false)
  3115. );
  3116. $expected = array(
  3117. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3118. array('option' => array('value' => 'first')),
  3119. 'first "html" <chars>',
  3120. '/option',
  3121. array('option' => array('value' => 'second')),
  3122. 'value',
  3123. '/option',
  3124. '/select'
  3125. );
  3126. $this->assertTags($result, $expected);
  3127. $options = array(
  3128. array('value' => 'first', 'name' => 'First'),
  3129. array('value' => 'first', 'name' => 'Another First'),
  3130. );
  3131. $result = $this->Form->select(
  3132. 'Model.field',
  3133. $options,
  3134. array('escape' => false, 'empty' => false)
  3135. );
  3136. $expected = array(
  3137. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3138. array('option' => array('value' => 'first')),
  3139. 'First',
  3140. '/option',
  3141. array('option' => array('value' => 'first')),
  3142. 'Another First',
  3143. '/option',
  3144. '/select'
  3145. );
  3146. $this->assertTags($result, $expected);
  3147. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3148. $result = $this->Form->select(
  3149. 'Model.contact_id',
  3150. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3151. array('escape' => false, 'empty' => 'pick something')
  3152. );
  3153. $expected = array(
  3154. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3155. array('option' => array('value' => '')), 'pick something', '/option',
  3156. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3157. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3158. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3159. '/select'
  3160. );
  3161. $this->assertTags($result, $expected);
  3162. }
  3163. /**
  3164. * test that select() with optiongroups listens to the escape param.
  3165. *
  3166. * @return void
  3167. */
  3168. public function testSelectOptionGroupEscaping() {
  3169. $options = array(
  3170. '>< Key' => array(
  3171. 1 => 'One',
  3172. 2 => 'Two'
  3173. )
  3174. );
  3175. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3176. $expected = array(
  3177. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3178. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3179. array('option' => array('value' => '1')), 'One', '/option',
  3180. array('option' => array('value' => '2')), 'Two', '/option',
  3181. '/optgroup',
  3182. '/select'
  3183. );
  3184. $this->assertTags($result, $expected);
  3185. $options = array(
  3186. '>< Key' => array(
  3187. 1 => 'One',
  3188. 2 => 'Two'
  3189. )
  3190. );
  3191. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3192. $expected = array(
  3193. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3194. 'optgroup' => array('label' => '>< Key'),
  3195. array('option' => array('value' => '1')), 'One', '/option',
  3196. array('option' => array('value' => '2')), 'Two', '/option',
  3197. '/optgroup',
  3198. '/select'
  3199. );
  3200. $this->assertTags($result, $expected);
  3201. }
  3202. /**
  3203. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3204. *
  3205. * @return void
  3206. */
  3207. public function testSelectWithNullAttributes() {
  3208. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3209. $expected = array(
  3210. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3211. array('option' => array('value' => '0')),
  3212. 'first',
  3213. '/option',
  3214. array('option' => array('value' => '1')),
  3215. 'second',
  3216. '/option',
  3217. '/select'
  3218. );
  3219. $this->assertTags($result, $expected);
  3220. }
  3221. /**
  3222. * testNestedSelect method
  3223. *
  3224. * test select element generation with optgroups
  3225. *
  3226. * @return void
  3227. */
  3228. public function testNestedSelect() {
  3229. $result = $this->Form->select(
  3230. 'Model.field',
  3231. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3232. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3233. )), array('empty' => false)
  3234. );
  3235. $expected = array(
  3236. 'select' => array('name' => 'data[Model][field]',
  3237. 'id' => 'ModelField'),
  3238. array('option' => array('value' => 1)),
  3239. 'One',
  3240. '/option',
  3241. array('option' => array('value' => 2)),
  3242. 'Two',
  3243. '/option',
  3244. array('optgroup' => array('label' => 'Three')),
  3245. array('option' => array('value' => 4)),
  3246. 'Four',
  3247. '/option',
  3248. array('option' => array('value' => 5)),
  3249. 'Five',
  3250. '/option',
  3251. '/optgroup',
  3252. '/select'
  3253. );
  3254. $this->assertTags($result, $expected);
  3255. $result = $this->Form->select(
  3256. 'Model.field',
  3257. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3258. array('showParents' => true, 'empty' => false)
  3259. );
  3260. $expected = array(
  3261. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3262. array('option' => array('value' => 1)),
  3263. 'One',
  3264. '/option',
  3265. array('option' => array('value' => 2)),
  3266. 'Two',
  3267. '/option',
  3268. array('optgroup' => array('label' => 'Three')),
  3269. array('option' => array('value' => 3)),
  3270. 'Three',
  3271. '/option',
  3272. array('option' => array('value' => 4)),
  3273. 'Four',
  3274. '/option',
  3275. '/optgroup',
  3276. '/select'
  3277. );
  3278. $this->assertTags($result, $expected);
  3279. }
  3280. /**
  3281. * testSelectMultiple method
  3282. *
  3283. * test generation of multiple select elements
  3284. *
  3285. * @return void
  3286. */
  3287. public function testSelectMultiple() {
  3288. $options = array('first', 'second', 'third');
  3289. $result = $this->Form->select(
  3290. 'Model.multi_field', $options, array('multiple' => true)
  3291. );
  3292. $expected = array(
  3293. 'input' => array(
  3294. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3295. ),
  3296. 'select' => array(
  3297. 'name' => 'data[Model][multi_field][]',
  3298. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3299. ),
  3300. array('option' => array('value' => '0')),
  3301. 'first',
  3302. '/option',
  3303. array('option' => array('value' => '1')),
  3304. 'second',
  3305. '/option',
  3306. array('option' => array('value' => '2')),
  3307. 'third',
  3308. '/option',
  3309. '/select'
  3310. );
  3311. $this->assertTags($result, $expected);
  3312. $result = $this->Form->select(
  3313. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3314. );
  3315. $expected = array(
  3316. 'input' => array(
  3317. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3318. ),
  3319. 'select' => array(
  3320. 'name' => 'data[Model][multi_field][]',
  3321. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3322. ),
  3323. array('option' => array('value' => '0')),
  3324. 'first',
  3325. '/option',
  3326. array('option' => array('value' => '1')),
  3327. 'second',
  3328. '/option',
  3329. array('option' => array('value' => '2')),
  3330. 'third',
  3331. '/option',
  3332. '/select'
  3333. );
  3334. $this->assertTags($result, $expected);
  3335. $result = $this->Form->select(
  3336. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3337. );
  3338. $expected = array(
  3339. 'input' => array(
  3340. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3341. ),
  3342. 'select' => array(
  3343. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3344. 'multiple' => 'multiple'
  3345. ),
  3346. array('option' => array('value' => '0', 'selected' => 'selected')),
  3347. 'first',
  3348. '/option',
  3349. array('option' => array('value' => '1', 'selected' => 'selected')),
  3350. 'second',
  3351. '/option',
  3352. array('option' => array('value' => '2')),
  3353. 'third',
  3354. '/option',
  3355. '/select'
  3356. );
  3357. $this->assertTags($result, $expected);
  3358. $result = $this->Form->select(
  3359. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3360. );
  3361. $expected = array(
  3362. 'select' => array(
  3363. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3364. ),
  3365. array('option' => array('value' => '0', 'selected' => 'selected')),
  3366. 'first',
  3367. '/option',
  3368. array('option' => array('value' => '1', 'selected' => 'selected')),
  3369. 'second',
  3370. '/option',
  3371. array('option' => array('value' => '2')),
  3372. 'third',
  3373. '/option',
  3374. '/select'
  3375. );
  3376. $this->assertTags($result, $expected);
  3377. }
  3378. /**
  3379. * test generation of habtm select boxes.
  3380. *
  3381. * @return void
  3382. */
  3383. public function testHabtmSelectBox() {
  3384. $this->View->viewVars['contactTags'] = array(
  3385. 1 => 'blue',
  3386. 2 => 'red',
  3387. 3 => 'green'
  3388. );
  3389. $this->Form->request->data = array(
  3390. 'Contact' => array(),
  3391. 'ContactTag' => array(
  3392. array(
  3393. 'id' => 1,
  3394. 'name' => 'blue'
  3395. ),
  3396. array(
  3397. 'id' => 3,
  3398. 'name' => 'green'
  3399. )
  3400. )
  3401. );
  3402. $this->Form->create('Contact');
  3403. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3404. $expected = array(
  3405. 'input' => array(
  3406. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3407. ),
  3408. 'select' => array(
  3409. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3410. 'multiple' => 'multiple'
  3411. ),
  3412. array('option' => array('value' => '1', 'selected' => 'selected')),
  3413. 'blue',
  3414. '/option',
  3415. array('option' => array('value' => '2')),
  3416. 'red',
  3417. '/option',
  3418. array('option' => array('value' => '3', 'selected' => 'selected')),
  3419. 'green',
  3420. '/option',
  3421. '/select'
  3422. );
  3423. $this->assertTags($result, $expected);
  3424. }
  3425. /**
  3426. * test generation of multi select elements in checkbox format
  3427. *
  3428. * @return void
  3429. */
  3430. public function testSelectMultipleCheckboxes() {
  3431. $result = $this->Form->select(
  3432. 'Model.multi_field',
  3433. array('first', 'second', 'third'),
  3434. array('multiple' => 'checkbox')
  3435. );
  3436. $expected = array(
  3437. 'input' => array(
  3438. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3439. ),
  3440. array('div' => array('class' => 'checkbox')),
  3441. array('input' => array(
  3442. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3443. 'value' => '0', 'id' => 'ModelMultiField0'
  3444. )),
  3445. array('label' => array('for' => 'ModelMultiField0')),
  3446. 'first',
  3447. '/label',
  3448. '/div',
  3449. array('div' => array('class' => 'checkbox')),
  3450. array('input' => array(
  3451. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3452. 'value' => '1', 'id' => 'ModelMultiField1'
  3453. )),
  3454. array('label' => array('for' => 'ModelMultiField1')),
  3455. 'second',
  3456. '/label',
  3457. '/div',
  3458. array('div' => array('class' => 'checkbox')),
  3459. array('input' => array(
  3460. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3461. 'value' => '2', 'id' => 'ModelMultiField2'
  3462. )),
  3463. array('label' => array('for' => 'ModelMultiField2')),
  3464. 'third',
  3465. '/label',
  3466. '/div'
  3467. );
  3468. $this->assertTags($result, $expected);
  3469. $result = $this->Form->select(
  3470. 'Model.multi_field',
  3471. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3472. array('multiple' => 'checkbox')
  3473. );
  3474. $expected = array(
  3475. 'input' => array(
  3476. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3477. ),
  3478. array('div' => array('class' => 'checkbox')),
  3479. array('input' => array(
  3480. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3481. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3482. )),
  3483. array('label' => array('for' => 'ModelMultiFieldA')),
  3484. 'first',
  3485. '/label',
  3486. '/div',
  3487. array('div' => array('class' => 'checkbox')),
  3488. array('input' => array(
  3489. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3490. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3491. )),
  3492. array('label' => array('for' => 'ModelMultiFieldB')),
  3493. 'second',
  3494. '/label',
  3495. '/div',
  3496. array('div' => array('class' => 'checkbox')),
  3497. array('input' => array(
  3498. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3499. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3500. )),
  3501. array('label' => array('for' => 'ModelMultiFieldC')),
  3502. 'third',
  3503. '/label',
  3504. '/div'
  3505. );
  3506. $this->assertTags($result, $expected);
  3507. $result = $this->Form->select(
  3508. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3509. );
  3510. $expected = array(
  3511. 'input' => array(
  3512. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3513. ),
  3514. array('div' => array('class' => 'checkbox')),
  3515. array('input' => array(
  3516. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3517. 'value' => '1', 'id' => 'ModelMultiField1'
  3518. )),
  3519. array('label' => array('for' => 'ModelMultiField1')),
  3520. 'first',
  3521. '/label',
  3522. '/div'
  3523. );
  3524. $this->assertTags($result, $expected);
  3525. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3526. $result = $this->Form->select(
  3527. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3528. );
  3529. $expected = array(
  3530. 'input' => array(
  3531. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3532. ),
  3533. array('div' => array('class' => 'checkbox')),
  3534. array('input' => array(
  3535. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3536. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3537. )),
  3538. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3539. 'first',
  3540. '/label',
  3541. '/div',
  3542. array('div' => array('class' => 'checkbox')),
  3543. array('input' => array(
  3544. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3545. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3546. )),
  3547. array('label' => array('for' => 'ModelTagsArray')),
  3548. 'Array',
  3549. '/label',
  3550. '/div'
  3551. );
  3552. $this->assertTags($result, $expected);
  3553. }
  3554. /**
  3555. * test multiple checkboxes with div styles.
  3556. *
  3557. * @return void
  3558. */
  3559. public function testSelectMultipleCheckboxDiv() {
  3560. $result = $this->Form->select(
  3561. 'Model.tags',
  3562. array('first', 'second'),
  3563. array('multiple' => 'checkbox', 'class' => 'my-class')
  3564. );
  3565. $expected = array(
  3566. 'input' => array(
  3567. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3568. ),
  3569. array('div' => array('class' => 'my-class')),
  3570. array('input' => array(
  3571. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3572. 'value' => '0', 'id' => 'ModelTags0'
  3573. )),
  3574. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3575. '/div',
  3576. array('div' => array('class' => 'my-class')),
  3577. array('input' => array(
  3578. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3579. 'value' => '1', 'id' => 'ModelTags1'
  3580. )),
  3581. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3582. '/div'
  3583. );
  3584. $this->assertTags($result, $expected);
  3585. $result = $this->Form->input('Model.tags', array(
  3586. 'options' => array('first', 'second'),
  3587. 'multiple' => 'checkbox',
  3588. 'class' => 'my-class',
  3589. 'div' => false,
  3590. 'label' => false
  3591. ));
  3592. $this->assertTags($result, $expected);
  3593. $Contact = ClassRegistry::getObject('Contact');
  3594. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3595. $result = $this->Form->input('Contact.tags', array(
  3596. 'options' => array('one'),
  3597. 'multiple' => 'checkbox',
  3598. 'label' => false,
  3599. 'div' => false
  3600. ));
  3601. $expected = array(
  3602. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3603. array('div' => array('class' => 'checkbox form-error')),
  3604. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3605. array('label' => array('for' => 'ContactTags0')),
  3606. 'one',
  3607. '/label',
  3608. '/div'
  3609. );
  3610. $this->assertTags($result, $expected);
  3611. $result = $this->Form->input('Contact.tags', array(
  3612. 'options' => array('one'),
  3613. 'multiple' => 'checkbox',
  3614. 'class' => 'mycheckbox',
  3615. 'label' => false,
  3616. 'div' => false
  3617. ));
  3618. $expected = array(
  3619. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3620. array('div' => array('class' => 'mycheckbox form-error')),
  3621. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3622. array('label' => array('for' => 'ContactTags0')),
  3623. 'one',
  3624. '/label',
  3625. '/div'
  3626. );
  3627. $this->assertTags($result, $expected);
  3628. }
  3629. /**
  3630. * Checks the security hash array generated for multiple-input checkbox elements
  3631. *
  3632. * @return void
  3633. */
  3634. public function testSelectMultipleCheckboxSecurity() {
  3635. $this->Form->request['_Token'] = array('key' => 'testKey');
  3636. $this->assertEqual($this->Form->fields, array());
  3637. $result = $this->Form->select(
  3638. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3639. array('multiple' => 'checkbox')
  3640. );
  3641. $this->assertEqual($this->Form->fields, array('Model.multi_field'));
  3642. $result = $this->Form->secure($this->Form->fields);
  3643. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3644. $this->assertPattern('/"' . $key . '"/', $result);
  3645. }
  3646. /**
  3647. * testInputMultipleCheckboxes method
  3648. *
  3649. * test input() resulting in multi select elements being generated.
  3650. *
  3651. * @return void
  3652. */
  3653. public function testInputMultipleCheckboxes() {
  3654. $result = $this->Form->input('Model.multi_field', array(
  3655. 'options' => array('first', 'second', 'third'),
  3656. 'multiple' => 'checkbox'
  3657. ));
  3658. $expected = array(
  3659. array('div' => array('class' => 'input select')),
  3660. array('label' => array('for' => 'ModelMultiField')),
  3661. 'Multi Field',
  3662. '/label',
  3663. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3664. array('div' => array('class' => 'checkbox')),
  3665. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3666. array('label' => array('for' => 'ModelMultiField0')),
  3667. 'first',
  3668. '/label',
  3669. '/div',
  3670. array('div' => array('class' => 'checkbox')),
  3671. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3672. array('label' => array('for' => 'ModelMultiField1')),
  3673. 'second',
  3674. '/label',
  3675. '/div',
  3676. array('div' => array('class' => 'checkbox')),
  3677. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3678. array('label' => array('for' => 'ModelMultiField2')),
  3679. 'third',
  3680. '/label',
  3681. '/div',
  3682. '/div'
  3683. );
  3684. $this->assertTags($result, $expected);
  3685. $result = $this->Form->input('Model.multi_field', array(
  3686. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3687. 'multiple' => 'checkbox'
  3688. ));
  3689. $expected = array(
  3690. array('div' => array('class' => 'input select')),
  3691. array('label' => array('for' => 'ModelMultiField')),
  3692. 'Multi Field',
  3693. '/label',
  3694. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3695. array('div' => array('class' => 'checkbox')),
  3696. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  3697. array('label' => array('for' => 'ModelMultiFieldA')),
  3698. 'first',
  3699. '/label',
  3700. '/div',
  3701. array('div' => array('class' => 'checkbox')),
  3702. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  3703. array('label' => array('for' => 'ModelMultiFieldB')),
  3704. 'second',
  3705. '/label',
  3706. '/div',
  3707. array('div' => array('class' => 'checkbox')),
  3708. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  3709. array('label' => array('for' => 'ModelMultiFieldC')),
  3710. 'third',
  3711. '/label',
  3712. '/div',
  3713. '/div'
  3714. );
  3715. $this->assertTags($result, $expected);
  3716. $result = $this->Form->input('Model.multi_field', array(
  3717. 'options' => array('1' => 'first'),
  3718. 'multiple' => 'checkbox',
  3719. 'label' => false,
  3720. 'div' => false
  3721. ));
  3722. $expected = array(
  3723. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3724. array('div' => array('class' => 'checkbox')),
  3725. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3726. array('label' => array('for' => 'ModelMultiField1')),
  3727. 'first',
  3728. '/label',
  3729. '/div'
  3730. );
  3731. $this->assertTags($result, $expected);
  3732. $result = $this->Form->input('Model.multi_field', array(
  3733. 'options' => array('2' => 'second'),
  3734. 'multiple' => 'checkbox',
  3735. 'label' => false,
  3736. 'div' => false
  3737. ));
  3738. $expected = array(
  3739. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  3740. array('div' => array('class' => 'checkbox')),
  3741. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  3742. array('label' => array('for' => 'ModelMultiField2')),
  3743. 'second',
  3744. '/label',
  3745. '/div'
  3746. );
  3747. $this->assertTags($result, $expected);
  3748. }
  3749. /**
  3750. * testSelectHiddenFieldOmission method
  3751. *
  3752. * test that select() with 'hiddenField' => false omits the hidden field
  3753. *
  3754. * @return void
  3755. */
  3756. public function testSelectHiddenFieldOmission() {
  3757. $result = $this->Form->select('Model.multi_field',
  3758. array('first', 'second'),
  3759. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  3760. );
  3761. $expected = array(
  3762. array('div' => array('class' => 'checkbox')),
  3763. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3764. array('label' => array('for' => 'ModelMultiField0')),
  3765. 'first',
  3766. '/label',
  3767. '/div',
  3768. array('div' => array('class' => 'checkbox')),
  3769. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3770. array('label' => array('for' => 'ModelMultiField1')),
  3771. 'second',
  3772. '/label',
  3773. '/div'
  3774. );
  3775. $this->assertTags($result, $expected);
  3776. $result = $this->Form->input('Model.multi_field', array(
  3777. 'options' => array('first', 'second'),
  3778. 'multiple' => 'checkbox',
  3779. 'hiddenField' => false
  3780. ));
  3781. $expected = array(
  3782. array('div' => array('class' => 'input select')),
  3783. array('label' => array('for' => 'ModelMultiField')),
  3784. 'Multi Field',
  3785. '/label',
  3786. array('div' => array('class' => 'checkbox')),
  3787. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  3788. array('label' => array('for' => 'ModelMultiField0')),
  3789. 'first',
  3790. '/label',
  3791. '/div',
  3792. array('div' => array('class' => 'checkbox')),
  3793. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  3794. array('label' => array('for' => 'ModelMultiField1')),
  3795. 'second',
  3796. '/label',
  3797. '/div',
  3798. '/div'
  3799. );
  3800. $this->assertTags($result, $expected);
  3801. }
  3802. /**
  3803. * test that select() with multiple = checkbox works with overriding name attribute.
  3804. *
  3805. * @return void
  3806. */
  3807. public function testSelectCheckboxMultipleOverrideName() {
  3808. $result = $this->Form->input('category', array(
  3809. 'type' => 'select',
  3810. 'multiple' => 'checkbox',
  3811. 'name' => 'data[fish]',
  3812. 'options' => array('1', '2'),
  3813. 'div' => false,
  3814. 'label' => false,
  3815. ));
  3816. $expected = array(
  3817. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  3818. array('div' => array('class' => 'checkbox')),
  3819. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  3820. array('label' => array('for' => 'Category0')), '1', '/label',
  3821. '/div',
  3822. array('div' => array('class' => 'checkbox')),
  3823. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  3824. array('label' => array('for' => 'Category1')), '2', '/label',
  3825. '/div'
  3826. );
  3827. $this->assertTags($result, $expected);
  3828. }
  3829. /**
  3830. * Test that 'id' overrides all the checkbox id's as well.
  3831. *
  3832. * @return void
  3833. */
  3834. public function testSelectCheckboxMultipleId() {
  3835. $result = $this->Form->select(
  3836. 'Model.multi_field',
  3837. array('first', 'second', 'third'),
  3838. array('multiple' => 'checkbox', 'id' => 'CustomId')
  3839. );
  3840. $expected = array(
  3841. 'input' => array(
  3842. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  3843. ),
  3844. array('div' => array('class' => 'checkbox')),
  3845. array('input' => array(
  3846. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3847. 'value' => '0', 'id' => 'CustomId0'
  3848. )),
  3849. array('label' => array('for' => 'CustomId0')),
  3850. 'first',
  3851. '/label',
  3852. '/div',
  3853. array('div' => array('class' => 'checkbox')),
  3854. array('input' => array(
  3855. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3856. 'value' => '1', 'id' => 'CustomId1'
  3857. )),
  3858. array('label' => array('for' => 'CustomId1')),
  3859. 'second',
  3860. '/label',
  3861. '/div',
  3862. array('div' => array('class' => 'checkbox')),
  3863. array('input' => array(
  3864. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3865. 'value' => '2', 'id' => 'CustomId2'
  3866. )),
  3867. array('label' => array('for' => 'CustomId2')),
  3868. 'third',
  3869. '/label',
  3870. '/div'
  3871. );
  3872. $this->assertTags($result, $expected);
  3873. }
  3874. /**
  3875. * testCheckbox method
  3876. *
  3877. * Test generation of checkboxes
  3878. *
  3879. * @return void
  3880. */
  3881. public function testCheckbox() {
  3882. $result = $this->Form->checkbox('Model.field');
  3883. $expected = array(
  3884. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3885. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  3886. );
  3887. $this->assertTags($result, $expected);
  3888. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  3889. $expected = array(
  3890. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  3891. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  3892. );
  3893. $this->assertTags($result, $expected);
  3894. $Contact = ClassRegistry::getObject('Contact');
  3895. $Contact->validationErrors['field'] = 1;
  3896. $this->Form->request->data['Contact']['field'] = 'myvalue';
  3897. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  3898. $expected = array(
  3899. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  3900. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  3901. );
  3902. $this->assertTags($result, $expected);
  3903. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  3904. $expected = array(
  3905. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  3906. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  3907. );
  3908. $this->assertTags($result, $expected);
  3909. $this->Form->request->data['Contact']['field'] = '';
  3910. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  3911. $expected = array(
  3912. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  3913. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  3914. );
  3915. $this->assertTags($result, $expected);
  3916. $Contact->validationErrors = array();
  3917. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  3918. $expected = array(
  3919. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  3920. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  3921. );
  3922. $this->assertTags($result, $expected);
  3923. $this->Form->request->data['Contact']['published'] = 1;
  3924. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  3925. $expected = array(
  3926. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  3927. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  3928. );
  3929. $this->assertTags($result, $expected);
  3930. $this->Form->request->data['Contact']['published'] = 0;
  3931. $result = $this->Form->checkbox('Contact.published', array('id'=>'theID'));
  3932. $expected = array(
  3933. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  3934. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  3935. );
  3936. $this->assertTags($result, $expected);
  3937. $result = $this->Form->checkbox('Model.CustomField.1.value');
  3938. $expected = array(
  3939. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  3940. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  3941. );
  3942. $this->assertTags($result, $expected);
  3943. $result = $this->Form->checkbox('CustomField.1.value');
  3944. $expected = array(
  3945. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  3946. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  3947. );
  3948. $this->assertTags($result, $expected);
  3949. }
  3950. /**
  3951. * test checkbox() with a custom name attribute
  3952. *
  3953. * @return void
  3954. */
  3955. public function testCheckboxCustomNameAttribute() {
  3956. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  3957. $expected = array(
  3958. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  3959. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  3960. );
  3961. $this->assertTags($result, $expected);
  3962. }
  3963. /**
  3964. * test the checked option for checkboxes.
  3965. *
  3966. * @return void
  3967. */
  3968. public function testCheckboxCheckedOption() {
  3969. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  3970. $expected = array(
  3971. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3972. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  3973. );
  3974. $this->assertTags($result, $expected);
  3975. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  3976. $expected = array(
  3977. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3978. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  3979. );
  3980. $this->assertTags($result, $expected);
  3981. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  3982. $expected = array(
  3983. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3984. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  3985. );
  3986. $this->assertTags($result, $expected);
  3987. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  3988. $expected = array(
  3989. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3990. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  3991. );
  3992. $this->assertTags($result, $expected);
  3993. $this->Form->request->data['Model']['field'] = 1;
  3994. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  3995. $expected = array(
  3996. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  3997. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  3998. );
  3999. $this->assertTags($result, $expected);
  4000. }
  4001. /**
  4002. * Test that disabled attribute works on both the checkbox and hidden input.
  4003. *
  4004. * @return void
  4005. */
  4006. public function testCheckboxDisabling() {
  4007. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4008. $expected = array(
  4009. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4010. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4011. );
  4012. $this->assertTags($result, $expected);
  4013. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4014. $expected = array(
  4015. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4016. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4017. );
  4018. $this->assertTags($result, $expected);
  4019. }
  4020. /**
  4021. * Test that the hidden input for checkboxes can be removed/omitted from the output.
  4022. *
  4023. * @return void
  4024. */
  4025. public function testCheckboxHiddenFieldOmission() {
  4026. $result = $this->Form->input('UserForm.something', array(
  4027. 'type' => 'checkbox',
  4028. 'hiddenField' => false
  4029. )
  4030. );
  4031. $expected = array(
  4032. 'div' => array('class' => 'input checkbox'),
  4033. array('input' => array(
  4034. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4035. 'value' => '1', 'id' => 'UserFormSomething'
  4036. )),
  4037. 'label' => array('for' => 'UserFormSomething'),
  4038. 'Something',
  4039. '/label',
  4040. '/div'
  4041. );
  4042. $this->assertTags($result, $expected);
  4043. }
  4044. /**
  4045. * testDateTime method
  4046. *
  4047. * Test generation of date/time select elements
  4048. *
  4049. * @return void
  4050. */
  4051. public function testDateTime() {
  4052. extract($this->dateRegex);
  4053. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4054. $now = strtotime('now');
  4055. $expected = array(
  4056. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4057. $daysRegex,
  4058. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4059. date('j', $now),
  4060. '/option',
  4061. '*/select',
  4062. '-',
  4063. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4064. $monthsRegex,
  4065. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4066. date('F', $now),
  4067. '/option',
  4068. '*/select',
  4069. '-',
  4070. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4071. $yearsRegex,
  4072. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4073. date('Y', $now),
  4074. '/option',
  4075. '*/select',
  4076. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4077. $hoursRegex,
  4078. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4079. date('g', $now),
  4080. '/option',
  4081. '*/select',
  4082. ':',
  4083. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4084. $minutesRegex,
  4085. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4086. date('i', $now),
  4087. '/option',
  4088. '*/select',
  4089. ' ',
  4090. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4091. $meridianRegex,
  4092. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4093. date('a', $now),
  4094. '/option',
  4095. '*/select'
  4096. );
  4097. $this->assertTags($result, $expected);
  4098. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4099. $expected = array(
  4100. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4101. $daysRegex,
  4102. array('option' => array('value' => '')),
  4103. '/option',
  4104. '*/select',
  4105. '-',
  4106. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4107. $monthsRegex,
  4108. array('option' => array('value' => '')),
  4109. '/option',
  4110. '*/select',
  4111. '-',
  4112. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4113. $yearsRegex,
  4114. array('option' => array('value' => '')),
  4115. '/option',
  4116. '*/select',
  4117. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4118. $hoursRegex,
  4119. array('option' => array('value' => '')),
  4120. '/option',
  4121. '*/select',
  4122. ':',
  4123. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4124. $minutesRegex,
  4125. array('option' => array('value' => '')),
  4126. '/option',
  4127. '*/select',
  4128. ' ',
  4129. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4130. $meridianRegex,
  4131. array('option' => array('value' => '')),
  4132. '/option',
  4133. '*/select'
  4134. );
  4135. $this->assertTags($result, $expected);
  4136. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4137. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4138. $expected = array(
  4139. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4140. $daysRegex,
  4141. array('option' => array('value' => '')),
  4142. '/option',
  4143. '*/select',
  4144. '-',
  4145. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4146. $monthsRegex,
  4147. array('option' => array('value' => '')),
  4148. '/option',
  4149. '*/select',
  4150. '-',
  4151. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4152. $yearsRegex,
  4153. array('option' => array('value' => '')),
  4154. '/option',
  4155. '*/select',
  4156. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4157. $hoursRegex,
  4158. array('option' => array('value' => '')),
  4159. '/option',
  4160. '*/select',
  4161. ':',
  4162. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4163. $minutesRegex,
  4164. array('option' => array('value' => '')),
  4165. '/option',
  4166. '*/select',
  4167. ' ',
  4168. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4169. $meridianRegex,
  4170. array('option' => array('value' => '')),
  4171. '/option',
  4172. '*/select'
  4173. );
  4174. $this->assertTags($result, $expected);
  4175. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4176. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4177. $expected = array(
  4178. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4179. $daysRegex,
  4180. array('option' => array('value' => '')),
  4181. '/option',
  4182. '*/select',
  4183. '-',
  4184. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4185. $monthsRegex,
  4186. array('option' => array('value' => '')),
  4187. '/option',
  4188. '*/select',
  4189. '-',
  4190. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4191. $yearsRegex,
  4192. array('option' => array('value' => '')),
  4193. '/option',
  4194. '*/select',
  4195. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4196. $hoursRegex,
  4197. array('option' => array('value' => '')),
  4198. '/option',
  4199. '*/select',
  4200. ':',
  4201. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4202. $minutesRegex,
  4203. array('option' => array('value' => '')),
  4204. '/option',
  4205. '*/select',
  4206. ' ',
  4207. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4208. $meridianRegex,
  4209. array('option' => array('value' => '')),
  4210. '/option',
  4211. '*/select'
  4212. );
  4213. $this->assertTags($result, $expected);
  4214. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4215. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4216. $expected = array(
  4217. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4218. $daysRegex,
  4219. array('option' => array('value' => '')),
  4220. '/option',
  4221. '*/select',
  4222. '-',
  4223. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4224. $monthsRegex,
  4225. array('option' => array('value' => '')),
  4226. '/option',
  4227. '*/select',
  4228. '-',
  4229. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4230. $yearsRegex,
  4231. array('option' => array('value' => '')),
  4232. '/option',
  4233. '*/select',
  4234. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4235. $hoursRegex,
  4236. array('option' => array('value' => '')),
  4237. '/option',
  4238. '*/select',
  4239. ':',
  4240. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4241. $minutesRegex,
  4242. array('option' => array('value' => '')),
  4243. '/option',
  4244. array('option' => array('value' => '00')),
  4245. '00',
  4246. '/option',
  4247. array('option' => array('value' => '05')),
  4248. '05',
  4249. '/option',
  4250. array('option' => array('value' => '10')),
  4251. '10',
  4252. '/option',
  4253. '*/select',
  4254. ' ',
  4255. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4256. $meridianRegex,
  4257. array('option' => array('value' => '')),
  4258. '/option',
  4259. '*/select'
  4260. );
  4261. $this->assertTags($result, $expected);
  4262. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4263. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4264. $expected = array(
  4265. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4266. $daysRegex,
  4267. array('option' => array('value' => '')),
  4268. '/option',
  4269. '*/select',
  4270. '-',
  4271. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4272. $monthsRegex,
  4273. array('option' => array('value' => '')),
  4274. '/option',
  4275. '*/select',
  4276. '-',
  4277. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4278. $yearsRegex,
  4279. array('option' => array('value' => '')),
  4280. '/option',
  4281. '*/select',
  4282. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4283. $hoursRegex,
  4284. array('option' => array('value' => '')),
  4285. '/option',
  4286. '*/select',
  4287. ':',
  4288. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4289. $minutesRegex,
  4290. array('option' => array('value' => '')),
  4291. '/option',
  4292. array('option' => array('value' => '00')),
  4293. '00',
  4294. '/option',
  4295. array('option' => array('value' => '05')),
  4296. '05',
  4297. '/option',
  4298. array('option' => array('value' => '10')),
  4299. '10',
  4300. '/option',
  4301. '*/select',
  4302. ' ',
  4303. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4304. $meridianRegex,
  4305. array('option' => array('value' => '')),
  4306. '/option',
  4307. '*/select'
  4308. );
  4309. $this->assertTags($result, $expected);
  4310. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4311. $this->Form->request->data['Contact']['data'] = null;
  4312. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4313. $expected = array(
  4314. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4315. $daysRegex,
  4316. array('option' => array('value' => '')),
  4317. '/option',
  4318. '*/select',
  4319. '-',
  4320. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4321. $monthsRegex,
  4322. array('option' => array('value' => '')),
  4323. '/option',
  4324. '*/select',
  4325. '-',
  4326. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4327. $yearsRegex,
  4328. array('option' => array('value' => '')),
  4329. '/option',
  4330. '*/select',
  4331. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4332. $hoursRegex,
  4333. array('option' => array('value' => '')),
  4334. '/option',
  4335. '*/select',
  4336. ':',
  4337. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4338. $minutesRegex,
  4339. array('option' => array('value' => '')),
  4340. '/option',
  4341. '*/select',
  4342. ' ',
  4343. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4344. $meridianRegex,
  4345. array('option' => array('value' => '')),
  4346. '/option',
  4347. '*/select'
  4348. );
  4349. $this->assertTags($result, $expected);
  4350. $this->assertNoPattern('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4351. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4352. $now = strtotime($this->Form->data['Model']['field']);
  4353. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4354. $expected = array(
  4355. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4356. $daysRegex,
  4357. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4358. date('j', $now),
  4359. '/option',
  4360. '*/select',
  4361. '-',
  4362. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4363. $monthsRegex,
  4364. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4365. date('F', $now),
  4366. '/option',
  4367. '*/select',
  4368. '-',
  4369. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4370. $yearsRegex,
  4371. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4372. date('Y', $now),
  4373. '/option',
  4374. '*/select',
  4375. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4376. $hoursRegex,
  4377. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4378. date('g', $now),
  4379. '/option',
  4380. '*/select',
  4381. ':',
  4382. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4383. $minutesRegex,
  4384. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4385. date('i', $now),
  4386. '/option',
  4387. '*/select',
  4388. ' ',
  4389. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4390. $meridianRegex,
  4391. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4392. date('a', $now),
  4393. '/option',
  4394. '*/select'
  4395. );
  4396. $this->assertTags($result, $expected);
  4397. $selected = strtotime('2008-10-26 12:33:00');
  4398. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4399. $this->assertPattern('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4400. $this->assertPattern('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4401. $this->assertPattern('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4402. $this->assertPattern('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4403. $this->assertPattern('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4404. $this->assertPattern('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4405. $this->Form->create('Contact');
  4406. $result = $this->Form->input('published');
  4407. $now = strtotime('now');
  4408. $expected = array(
  4409. 'div' => array('class' => 'input date'),
  4410. 'label' => array('for' => 'ContactPublishedMonth'),
  4411. 'Published',
  4412. '/label',
  4413. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4414. $monthsRegex,
  4415. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4416. date('F', $now),
  4417. '/option',
  4418. '*/select',
  4419. '-',
  4420. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4421. $daysRegex,
  4422. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4423. date('j', $now),
  4424. '/option',
  4425. '*/select',
  4426. '-',
  4427. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4428. $yearsRegex,
  4429. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4430. date('Y', $now),
  4431. '/option',
  4432. '*/select',
  4433. '/div'
  4434. );
  4435. $this->assertTags($result, $expected);
  4436. $result = $this->Form->input('published2', array('type' => 'date'));
  4437. $now = strtotime('now');
  4438. $expected = array(
  4439. 'div' => array('class' => 'input date'),
  4440. 'label' => array('for' => 'ContactPublished2Month'),
  4441. 'Published2',
  4442. '/label',
  4443. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4444. $monthsRegex,
  4445. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4446. date('F', $now),
  4447. '/option',
  4448. '*/select',
  4449. '-',
  4450. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4451. $daysRegex,
  4452. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4453. date('j', $now),
  4454. '/option',
  4455. '*/select',
  4456. '-',
  4457. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4458. $yearsRegex,
  4459. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4460. date('Y', $now),
  4461. '/option',
  4462. '*/select',
  4463. '/div'
  4464. );
  4465. $this->assertTags($result, $expected);
  4466. $this->Form->create('Contact');
  4467. $result = $this->Form->input('published', array('monthNames' => false));
  4468. $now = strtotime('now');
  4469. $expected = array(
  4470. 'div' => array('class' => 'input date'),
  4471. 'label' => array('for' => 'ContactPublishedMonth'),
  4472. 'Published',
  4473. '/label',
  4474. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4475. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4476. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4477. date('m', $now),
  4478. '/option',
  4479. '*/select',
  4480. '-',
  4481. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4482. $daysRegex,
  4483. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4484. date('j', $now),
  4485. '/option',
  4486. '*/select',
  4487. '-',
  4488. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4489. $yearsRegex,
  4490. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4491. date('Y', $now),
  4492. '/option',
  4493. '*/select',
  4494. '/div'
  4495. );
  4496. $this->assertTags($result, $expected);
  4497. $result = $this->Form->input('published', array('type' => 'time'));
  4498. $now = strtotime('now');
  4499. $expected = array(
  4500. 'div' => array('class' => 'input time'),
  4501. 'label' => array('for' => 'ContactPublishedHour'),
  4502. 'Published',
  4503. '/label',
  4504. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4505. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4506. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4507. date('g', $now),
  4508. '/option',
  4509. '*/select',
  4510. ':',
  4511. );
  4512. $this->assertTags($result, $expected);
  4513. $result = $this->Form->input('published', array(
  4514. 'timeFormat' => 24,
  4515. 'interval' => 5,
  4516. 'selected' => strtotime('2009-09-03 13:37:00'),
  4517. 'type' => 'datetime'
  4518. ));
  4519. $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4520. $this->assertPattern('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4521. $this->assertPattern('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4522. $this->assertPattern('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4523. $this->assertPattern('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4524. $this->assertNoErrors();
  4525. $this->Form->request->data['Contact'] = array(
  4526. 'date' => array(
  4527. 'day' => '',
  4528. 'month' => '',
  4529. 'year' => '',
  4530. 'hour' => '',
  4531. 'min' => '',
  4532. 'meridian' => ''
  4533. )
  4534. );
  4535. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4536. }
  4537. /**
  4538. * test that datetime() and default values work.
  4539. *
  4540. * @return void
  4541. */
  4542. public function testDatetimeWithDefault() {
  4543. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4544. $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4545. $this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4546. $this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4547. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4548. 'default' => '2009-06-01 11:15:30'
  4549. ));
  4550. $this->assertPattern('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4551. $this->assertPattern('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4552. $this->assertPattern('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4553. }
  4554. /**
  4555. * test that bogus non-date time data doesn't cause errors.
  4556. *
  4557. * @return void
  4558. */
  4559. public function testDateTimeWithBogusData() {
  4560. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4561. $this->assertNoPattern('/selected="selected">\d/', $result);
  4562. }
  4563. /**
  4564. * testFormDateTimeMulti method
  4565. *
  4566. * test multiple datetime element generation
  4567. *
  4568. * @return void
  4569. */
  4570. public function testFormDateTimeMulti() {
  4571. extract($this->dateRegex);
  4572. $result = $this->Form->dateTime('Contact.1.updated');
  4573. $expected = array(
  4574. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4575. $daysRegex,
  4576. array('option' => array('value' => '')),
  4577. '/option',
  4578. '*/select',
  4579. '-',
  4580. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4581. $monthsRegex,
  4582. array('option' => array('value' => '')),
  4583. '/option',
  4584. '*/select',
  4585. '-',
  4586. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4587. $yearsRegex,
  4588. array('option' => array('value' => '')),
  4589. '/option',
  4590. '*/select',
  4591. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4592. $hoursRegex,
  4593. array('option' => array('value' => '')),
  4594. '/option',
  4595. '*/select',
  4596. ':',
  4597. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4598. $minutesRegex,
  4599. array('option' => array('value' => '')),
  4600. '/option',
  4601. '*/select',
  4602. ' ',
  4603. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4604. $meridianRegex,
  4605. array('option' => array('value' => '')),
  4606. '/option',
  4607. '*/select'
  4608. );
  4609. $this->assertTags($result, $expected);
  4610. $result = $this->Form->dateTime('Contact.2.updated');
  4611. $expected = array(
  4612. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4613. $daysRegex,
  4614. array('option' => array('value' => '')),
  4615. '/option',
  4616. '*/select',
  4617. '-',
  4618. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4619. $monthsRegex,
  4620. array('option' => array('value' => '')),
  4621. '/option',
  4622. '*/select',
  4623. '-',
  4624. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4625. $yearsRegex,
  4626. array('option' => array('value' => '')),
  4627. '/option',
  4628. '*/select',
  4629. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4630. $hoursRegex,
  4631. array('option' => array('value' => '')),
  4632. '/option',
  4633. '*/select',
  4634. ':',
  4635. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4636. $minutesRegex,
  4637. array('option' => array('value' => '')),
  4638. '/option',
  4639. '*/select',
  4640. ' ',
  4641. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4642. $meridianRegex,
  4643. array('option' => array('value' => '')),
  4644. '/option',
  4645. '*/select'
  4646. );
  4647. $this->assertTags($result, $expected);
  4648. }
  4649. /**
  4650. * testMonth method
  4651. *
  4652. * @return void
  4653. */
  4654. public function testMonth() {
  4655. $result = $this->Form->month('Model.field');
  4656. $expected = array(
  4657. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4658. array('option' => array('value' => '')),
  4659. '/option',
  4660. array('option' => array('value' => '01')),
  4661. date('F', strtotime('2008-01-01 00:00:00')),
  4662. '/option',
  4663. array('option' => array('value' => '02')),
  4664. date('F', strtotime('2008-02-01 00:00:00')),
  4665. '/option',
  4666. '*/select',
  4667. );
  4668. $this->assertTags($result, $expected);
  4669. $result = $this->Form->month('Model.field', array('empty' => true));
  4670. $expected = array(
  4671. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4672. array('option' => array('value' => '')),
  4673. '/option',
  4674. array('option' => array('value' => '01')),
  4675. date('F', strtotime('2008-01-01 00:00:00')),
  4676. '/option',
  4677. array('option' => array('value' => '02')),
  4678. date('F', strtotime('2008-02-01 00:00:00')),
  4679. '/option',
  4680. '*/select',
  4681. );
  4682. $this->assertTags($result, $expected);
  4683. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4684. $expected = array(
  4685. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4686. array('option' => array('value' => '')),
  4687. '/option',
  4688. array('option' => array('value' => '01')),
  4689. '01',
  4690. '/option',
  4691. array('option' => array('value' => '02')),
  4692. '02',
  4693. '/option',
  4694. '*/select',
  4695. );
  4696. $this->assertTags($result, $expected);
  4697. $monthNames = array(
  4698. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  4699. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  4700. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  4701. $expected = array(
  4702. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4703. array('option' => array('value' => '')),
  4704. '/option',
  4705. array('option' => array('value' => '01')),
  4706. 'Jan',
  4707. '/option',
  4708. array('option' => array('value' => '02')),
  4709. 'Feb',
  4710. '/option',
  4711. '*/select',
  4712. );
  4713. $this->assertTags($result, $expected);
  4714. }
  4715. /**
  4716. * testDay method
  4717. *
  4718. * @return void
  4719. */
  4720. public function testDay() {
  4721. extract($this->dateRegex);
  4722. $result = $this->Form->day('Model.field', array('value' => false));
  4723. $expected = array(
  4724. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4725. array('option' => array('value' => '')),
  4726. '/option',
  4727. array('option' => array('value' => '01')),
  4728. '1',
  4729. '/option',
  4730. array('option' => array('value' => '02')),
  4731. '2',
  4732. '/option',
  4733. $daysRegex,
  4734. '/select',
  4735. );
  4736. $this->assertTags($result, $expected);
  4737. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4738. $result = $this->Form->day('Model.field');
  4739. $expected = array(
  4740. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4741. array('option' => array('value' => '')),
  4742. '/option',
  4743. array('option' => array('value' => '01')),
  4744. '1',
  4745. '/option',
  4746. array('option' => array('value' => '02')),
  4747. '2',
  4748. '/option',
  4749. $daysRegex,
  4750. array('option' => array('value' => '10', 'selected' => 'selected')),
  4751. '10',
  4752. '/option',
  4753. $daysRegex,
  4754. '/select',
  4755. );
  4756. $this->assertTags($result, $expected);
  4757. $this->Form->request->data['Model']['field'] = '';
  4758. $result = $this->Form->day('Model.field', array('value' => '10'));
  4759. $expected = array(
  4760. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4761. array('option' => array('value' => '')),
  4762. '/option',
  4763. array('option' => array('value' => '01')),
  4764. '1',
  4765. '/option',
  4766. array('option' => array('value' => '02')),
  4767. '2',
  4768. '/option',
  4769. $daysRegex,
  4770. array('option' => array('value' => '10', 'selected' => 'selected')),
  4771. '10',
  4772. '/option',
  4773. $daysRegex,
  4774. '/select',
  4775. );
  4776. $this->assertTags($result, $expected);
  4777. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  4778. $result = $this->Form->day('Model.field', array('value' => true));
  4779. $expected = array(
  4780. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4781. array('option' => array('value' => '')),
  4782. '/option',
  4783. array('option' => array('value' => '01')),
  4784. '1',
  4785. '/option',
  4786. array('option' => array('value' => '02')),
  4787. '2',
  4788. '/option',
  4789. $daysRegex,
  4790. array('option' => array('value' => '10', 'selected' => 'selected')),
  4791. '10',
  4792. '/option',
  4793. $daysRegex,
  4794. '/select',
  4795. );
  4796. $this->assertTags($result, $expected);
  4797. }
  4798. /**
  4799. * testMinute method
  4800. *
  4801. * @return void
  4802. */
  4803. public function testMinute() {
  4804. extract($this->dateRegex);
  4805. $result = $this->Form->minute('Model.field');
  4806. $expected = array(
  4807. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4808. array('option' => array('value' => '')),
  4809. '/option',
  4810. array('option' => array('value' => '00')),
  4811. '00',
  4812. '/option',
  4813. array('option' => array('value' => '01')),
  4814. '01',
  4815. '/option',
  4816. array('option' => array('value' => '02')),
  4817. '02',
  4818. '/option',
  4819. $minutesRegex,
  4820. '/select',
  4821. );
  4822. $this->assertTags($result, $expected);
  4823. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4824. $result = $this->Form->minute('Model.field');
  4825. $expected = array(
  4826. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4827. array('option' => array('value' => '')),
  4828. '/option',
  4829. array('option' => array('value' => '00')),
  4830. '00',
  4831. '/option',
  4832. array('option' => array('value' => '01')),
  4833. '01',
  4834. '/option',
  4835. array('option' => array('value' => '02')),
  4836. '02',
  4837. '/option',
  4838. $minutesRegex,
  4839. array('option' => array('value' => '12', 'selected' => 'selected')),
  4840. '12',
  4841. '/option',
  4842. $minutesRegex,
  4843. '/select',
  4844. );
  4845. $this->assertTags($result, $expected);
  4846. $this->Form->request->data['Model']['field'] = '';
  4847. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4848. $expected = array(
  4849. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4850. array('option' => array('value' => '')),
  4851. '/option',
  4852. array('option' => array('value' => '00')),
  4853. '00',
  4854. '/option',
  4855. array('option' => array('value' => '05')),
  4856. '05',
  4857. '/option',
  4858. array('option' => array('value' => '10')),
  4859. '10',
  4860. '/option',
  4861. $minutesRegex,
  4862. '/select',
  4863. );
  4864. $this->assertTags($result, $expected);
  4865. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  4866. $result = $this->Form->minute('Model.field', array('interval' => 5));
  4867. $expected = array(
  4868. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4869. array('option' => array('value' => '')),
  4870. '/option',
  4871. array('option' => array('value' => '00')),
  4872. '00',
  4873. '/option',
  4874. array('option' => array('value' => '05')),
  4875. '05',
  4876. '/option',
  4877. array('option' => array('value' => '10', 'selected' => 'selected')),
  4878. '10',
  4879. '/option',
  4880. $minutesRegex,
  4881. '/select',
  4882. );
  4883. $this->assertTags($result, $expected);
  4884. }
  4885. /**
  4886. * testHour method
  4887. *
  4888. * @return void
  4889. */
  4890. public function testHour() {
  4891. extract($this->dateRegex);
  4892. $result = $this->Form->hour('Model.field', false);
  4893. $expected = array(
  4894. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4895. array('option' => array('value' => '')),
  4896. '/option',
  4897. array('option' => array('value' => '01')),
  4898. '1',
  4899. '/option',
  4900. array('option' => array('value' => '02')),
  4901. '2',
  4902. '/option',
  4903. $hoursRegex,
  4904. '/select',
  4905. );
  4906. $this->assertTags($result, $expected);
  4907. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4908. $result = $this->Form->hour('Model.field', false);
  4909. $expected = array(
  4910. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4911. array('option' => array('value' => '')),
  4912. '/option',
  4913. array('option' => array('value' => '01')),
  4914. '1',
  4915. '/option',
  4916. array('option' => array('value' => '02')),
  4917. '2',
  4918. '/option',
  4919. $hoursRegex,
  4920. array('option' => array('value' => '12', 'selected' => 'selected')),
  4921. '12',
  4922. '/option',
  4923. '/select',
  4924. );
  4925. $this->assertTags($result, $expected);
  4926. $this->Form->request->data['Model']['field'] = '';
  4927. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  4928. $expected = array(
  4929. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4930. array('option' => array('value' => '')),
  4931. '/option',
  4932. array('option' => array('value' => '00')),
  4933. '0',
  4934. '/option',
  4935. array('option' => array('value' => '01')),
  4936. '1',
  4937. '/option',
  4938. array('option' => array('value' => '02')),
  4939. '2',
  4940. '/option',
  4941. $hoursRegex,
  4942. array('option' => array('value' => '23', 'selected' => 'selected')),
  4943. '23',
  4944. '/option',
  4945. '/select',
  4946. );
  4947. $this->assertTags($result, $expected);
  4948. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  4949. $result = $this->Form->hour('Model.field', true);
  4950. $expected = array(
  4951. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4952. array('option' => array('value' => '')),
  4953. '/option',
  4954. array('option' => array('value' => '00', 'selected' => 'selected')),
  4955. '0',
  4956. '/option',
  4957. array('option' => array('value' => '01')),
  4958. '1',
  4959. '/option',
  4960. array('option' => array('value' => '02')),
  4961. '2',
  4962. '/option',
  4963. $hoursRegex,
  4964. '/select',
  4965. );
  4966. $this->assertTags($result, $expected);
  4967. unset($this->Form->request->data['Model']['field']);
  4968. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  4969. $thisHour = date('H');
  4970. $optValue = date('G');
  4971. $this->assertPattern('/<option value="' . $thisHour . '" selected="selected">'. $optValue .'<\/option>/', $result);
  4972. }
  4973. /**
  4974. * testYear method
  4975. *
  4976. * @return void
  4977. */
  4978. public function testYear() {
  4979. $result = $this->Form->year('Model.field', 2006, 2007);
  4980. $expected = array(
  4981. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4982. array('option' => array('value' => '')),
  4983. '/option',
  4984. array('option' => array('value' => '2007')),
  4985. '2007',
  4986. '/option',
  4987. array('option' => array('value' => '2006')),
  4988. '2006',
  4989. '/option',
  4990. '/select',
  4991. );
  4992. $this->assertTags($result, $expected);
  4993. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  4994. $expected = array(
  4995. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4996. array('option' => array('value' => '')),
  4997. '/option',
  4998. array('option' => array('value' => '2006')),
  4999. '2006',
  5000. '/option',
  5001. array('option' => array('value' => '2007')),
  5002. '2007',
  5003. '/option',
  5004. '/select',
  5005. );
  5006. $this->assertTags($result, $expected);
  5007. $this->request->data['Contact']['published'] = '';
  5008. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5009. $expected = array(
  5010. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5011. array('option' => array('value' => '')),
  5012. '/option',
  5013. array('option' => array('value' => '2007')),
  5014. '2007',
  5015. '/option',
  5016. array('option' => array('value' => '2006')),
  5017. '2006',
  5018. '/option',
  5019. '/select',
  5020. );
  5021. $this->assertTags($result, $expected);
  5022. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5023. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5024. $expected = array(
  5025. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5026. array('option' => array('value' => '2007')),
  5027. '2007',
  5028. '/option',
  5029. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5030. '2006',
  5031. '/option',
  5032. '/select',
  5033. );
  5034. $this->assertTags($result, $expected);
  5035. $this->Form->request->data['Contact']['published'] = '';
  5036. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5037. $expected = array(
  5038. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5039. array('option' => array('value' => '')),
  5040. '/option',
  5041. array('option' => array('value' => '2007')),
  5042. '2007',
  5043. '/option',
  5044. array('option' => array('value' => '2006')),
  5045. '2006',
  5046. '/option',
  5047. '/select',
  5048. );
  5049. $this->assertTags($result, $expected);
  5050. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5051. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5052. $expected = array(
  5053. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5054. array('option' => array('value' => '2007')),
  5055. '2007',
  5056. '/option',
  5057. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5058. '2006',
  5059. '/option',
  5060. '/select',
  5061. );
  5062. $this->assertTags($result, $expected);
  5063. $this->Form->request->data['Contact']['published'] = '';
  5064. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5065. $expected = array(
  5066. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5067. array('option' => array('value' => '')),
  5068. '/option',
  5069. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5070. '2007',
  5071. '/option',
  5072. array('option' => array('value' => '2006')),
  5073. '2006',
  5074. '/option',
  5075. '/select',
  5076. );
  5077. $this->assertTags($result, $expected);
  5078. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5079. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5080. $expected = array(
  5081. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5082. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5083. '2007',
  5084. '/option',
  5085. array('option' => array('value' => '2006')),
  5086. '2006',
  5087. '/option',
  5088. '/select',
  5089. );
  5090. $this->assertTags($result, $expected);
  5091. $this->Form->request->data['Contact']['published'] = '';
  5092. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5093. $expected = array(
  5094. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5095. array('option' => array('value' => '2008')),
  5096. '2008',
  5097. '/option',
  5098. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5099. '2007',
  5100. '/option',
  5101. array('option' => array('value' => '2006')),
  5102. '2006',
  5103. '/option',
  5104. '/select',
  5105. );
  5106. $this->assertTags($result, $expected);
  5107. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5108. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5109. $expected = array(
  5110. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5111. array('option' => array('value' => '2008')),
  5112. '2008',
  5113. '/option',
  5114. array('option' => array('value' => '2007')),
  5115. '2007',
  5116. '/option',
  5117. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5118. '2006',
  5119. '/option',
  5120. '/select',
  5121. );
  5122. $this->assertTags($result, $expected);
  5123. $this->Form->request->data = array();
  5124. $this->Form->create('Contact');
  5125. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5126. $expected = array(
  5127. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5128. array('option' => array('value' => '2008')),
  5129. '2008',
  5130. '/option',
  5131. array('option' => array('value' => '2007')),
  5132. '2007',
  5133. '/option',
  5134. array('option' => array('value' => '2006')),
  5135. '2006',
  5136. '/option',
  5137. '/select',
  5138. );
  5139. $this->assertTags($result, $expected);
  5140. }
  5141. /**
  5142. * testTextArea method
  5143. *
  5144. * @return void
  5145. */
  5146. public function testTextArea() {
  5147. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5148. $result = $this->Form->textarea('Model.field');
  5149. $expected = array(
  5150. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5151. 'some test data',
  5152. '/textarea',
  5153. );
  5154. $this->assertTags($result, $expected);
  5155. $result = $this->Form->textarea('Model.tmp');
  5156. $expected = array(
  5157. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5158. '/textarea',
  5159. );
  5160. $this->assertTags($result, $expected);
  5161. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5162. $result = $this->Form->textarea('Model.field');
  5163. $expected = array(
  5164. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5165. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5166. '/textarea',
  5167. );
  5168. $this->assertTags($result, $expected);
  5169. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5170. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5171. $expected = array(
  5172. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5173. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5174. '/textarea',
  5175. );
  5176. $this->assertTags($result, $expected);
  5177. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5178. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5179. $expected = array(
  5180. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5181. '/textarea'
  5182. );
  5183. $this->assertTags($result, $expected);
  5184. }
  5185. /**
  5186. * testTextAreaWithStupidCharacters method
  5187. *
  5188. * test text area with non-ascii characters
  5189. *
  5190. * @return void
  5191. */
  5192. public function testTextAreaWithStupidCharacters() {
  5193. $this->loadFixtures('Post');
  5194. $result = $this->Form->input('Post.content', array(
  5195. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5196. ));
  5197. $expected = array(
  5198. 'div' => array('class' => 'input text'),
  5199. 'label' => array('for' => 'PostContent'),
  5200. 'Current Text',
  5201. '/label',
  5202. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5203. 'GREAT®',
  5204. '/textarea',
  5205. '/div'
  5206. );
  5207. $this->assertTags($result, $expected);
  5208. }
  5209. /**
  5210. * testHiddenField method
  5211. *
  5212. * @return void
  5213. */
  5214. public function testHiddenField() {
  5215. $Contact = ClassRegistry::getObject('Contact');
  5216. $Contact->validationErrors['field'] = 1;
  5217. $this->Form->request->data['Contact']['field'] = 'test';
  5218. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5219. $this->assertTags($result, array(
  5220. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5221. );
  5222. }
  5223. /**
  5224. * testFileUploadField method
  5225. *
  5226. * @return void
  5227. */
  5228. public function testFileUploadField() {
  5229. $result = $this->Form->file('Model.upload');
  5230. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5231. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5232. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5233. $expected = array(
  5234. 'div' => array('class' => 'input file'),
  5235. 'label' => array('for' => 'ModelUpload'),
  5236. 'Upload',
  5237. '/label',
  5238. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5239. '/div'
  5240. );
  5241. $this->assertTags($result, $expected);
  5242. }
  5243. /**
  5244. * test File upload input on a model not used in create();
  5245. *
  5246. * @return void
  5247. */
  5248. public function testFileUploadOnOtherModel() {
  5249. $this->Form->create('ValidateUser', array('type' => 'file'));
  5250. $result = $this->Form->file('ValidateProfile.city');
  5251. $expected = array(
  5252. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5253. );
  5254. $this->assertTags($result, $expected);
  5255. }
  5256. /**
  5257. * testButton method
  5258. *
  5259. * @return void
  5260. */
  5261. public function testButton() {
  5262. $result = $this->Form->button('Hi');
  5263. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5264. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5265. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5266. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5267. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5268. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5269. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5270. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5271. $this->assertNoPattern('/\&039/', $result);
  5272. }
  5273. /**
  5274. * Test that button() makes unlocked fields by default.
  5275. *
  5276. * @return void
  5277. */
  5278. public function testButtonUnlockedByDefault() {
  5279. $this->Form->request->params['_Token']['key'] = 'secured';
  5280. $this->Form->button('Save', array('name' => 'save'));
  5281. $this->Form->button('Clear');
  5282. $result = $this->Form->unlockField();
  5283. $this->assertEquals(array('save'), $result);
  5284. }
  5285. /**
  5286. * testPostButton method
  5287. *
  5288. * @return void
  5289. */
  5290. public function testPostButton() {
  5291. $result = $this->Form->postButton('Hi', '/controller/action');
  5292. $this->assertTags($result, array(
  5293. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8', 'style' => 'display:none;'),
  5294. 'div' => array('style' => 'display:none;'),
  5295. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5296. '/div',
  5297. 'button' => array('type' => 'submit'),
  5298. 'Hi',
  5299. '/button',
  5300. '/form'
  5301. ));
  5302. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5303. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5304. }
  5305. /**
  5306. * Test that postButton adds _Token fields.
  5307. *
  5308. * @return void
  5309. */
  5310. public function testSecurePostButton() {
  5311. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5312. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5313. $expected = array(
  5314. 'form' => array(
  5315. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5316. 'style' => 'display:none;'
  5317. ),
  5318. array('div' => array('style' => 'display:none;')),
  5319. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5320. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5321. '/div',
  5322. 'button' => array('type' => 'submit'),
  5323. 'Delete',
  5324. '/button',
  5325. array('div' => array('style' => 'display:none;')),
  5326. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5327. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5328. '/div',
  5329. '/form',
  5330. );
  5331. $this->assertTags($result, $expected);
  5332. }
  5333. /**
  5334. * testPostLink method
  5335. *
  5336. * @return void
  5337. */
  5338. public function testPostLink() {
  5339. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5340. $this->assertTags($result, array(
  5341. 'form' => array(
  5342. 'method' => 'post', 'action' => '/posts/delete/1',
  5343. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5344. ),
  5345. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5346. '/form',
  5347. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5348. 'Delete',
  5349. '/a'
  5350. ));
  5351. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5352. $this->assertTags($result, array(
  5353. 'form' => array(
  5354. 'method' => 'post', 'action' => '/posts/delete/1',
  5355. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5356. ),
  5357. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5358. '/form',
  5359. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5360. 'Delete',
  5361. '/a'
  5362. ));
  5363. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5364. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5365. }
  5366. /**
  5367. * Test that postLink adds _Token fields.
  5368. *
  5369. * @return void
  5370. */
  5371. public function testSecurePostLink() {
  5372. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5373. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5374. $expected = array(
  5375. 'form' => array(
  5376. 'method' => 'post', 'action' => '/posts/delete/1',
  5377. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5378. ),
  5379. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5380. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5381. 'div' => array('style' => 'display:none;'),
  5382. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5383. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5384. '/div',
  5385. '/form',
  5386. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5387. 'Delete',
  5388. '/a'
  5389. );
  5390. $this->assertTags($result, $expected);
  5391. }
  5392. /**
  5393. * testSubmitButton method
  5394. *
  5395. * @return void
  5396. */
  5397. public function testSubmitButton() {
  5398. $result = $this->Form->submit('');
  5399. $expected = array(
  5400. 'div' => array('class' => 'submit'),
  5401. 'input' => array('type' => 'submit', 'value' => ''),
  5402. '/div'
  5403. );
  5404. $this->assertTags($result, $expected);
  5405. $result = $this->Form->submit('Test Submit');
  5406. $expected = array(
  5407. 'div' => array('class' => 'submit'),
  5408. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5409. '/div'
  5410. );
  5411. $this->assertTags($result, $expected);
  5412. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5413. $expected = array(
  5414. 'span' => array('class' => 'submit'),
  5415. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5416. '/span'
  5417. );
  5418. $this->assertTags($result, $expected);
  5419. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5420. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5421. $this->assertTags($result, $expected);
  5422. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5423. $expected = array(
  5424. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5425. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5426. '/div'
  5427. );
  5428. $this->assertTags($result, $expected);
  5429. $result = $this->Form->submit('Next >');
  5430. $expected = array(
  5431. 'div' => array('class' => 'submit'),
  5432. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5433. '/div'
  5434. );
  5435. $this->assertTags($result, $expected);
  5436. $result = $this->Form->submit('Next >', array('escape' => false));
  5437. $expected = array(
  5438. 'div' => array('class' => 'submit'),
  5439. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5440. '/div'
  5441. );
  5442. $this->assertTags($result, $expected);
  5443. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5444. $expected = array(
  5445. 'div' => array('class' => 'submit'),
  5446. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5447. '/div'
  5448. );
  5449. $this->assertTags($result, $expected);
  5450. $before = '--before--';
  5451. $after = '--after--';
  5452. $result = $this->Form->submit('Test', array('before' => $before));
  5453. $expected = array(
  5454. 'div' => array('class' => 'submit'),
  5455. '--before--',
  5456. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5457. '/div'
  5458. );
  5459. $this->assertTags($result, $expected);
  5460. $result = $this->Form->submit('Test', array('after' => $after));
  5461. $expected = array(
  5462. 'div' => array('class' => 'submit'),
  5463. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5464. '--after--',
  5465. '/div'
  5466. );
  5467. $this->assertTags($result, $expected);
  5468. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5469. $expected = array(
  5470. 'div' => array('class' => 'submit'),
  5471. '--before--',
  5472. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5473. '--after--',
  5474. '/div'
  5475. );
  5476. $this->assertTags($result, $expected);
  5477. }
  5478. /**
  5479. * test image submit types.
  5480. *
  5481. * @return void
  5482. */
  5483. public function testSubmitImage() {
  5484. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5485. $expected = array(
  5486. 'div' => array('class' => 'submit'),
  5487. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5488. '/div'
  5489. );
  5490. $this->assertTags($result, $expected);
  5491. $result = $this->Form->submit('/relative/cake.power.gif');
  5492. $expected = array(
  5493. 'div' => array('class' => 'submit'),
  5494. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5495. '/div'
  5496. );
  5497. $this->assertTags($result, $expected);
  5498. $result = $this->Form->submit('cake.power.gif');
  5499. $expected = array(
  5500. 'div' => array('class' => 'submit'),
  5501. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5502. '/div'
  5503. );
  5504. $this->assertTags($result, $expected);
  5505. $result = $this->Form->submit('Not.an.image');
  5506. $expected = array(
  5507. 'div' => array('class' => 'submit'),
  5508. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5509. '/div'
  5510. );
  5511. $this->assertTags($result, $expected);
  5512. $after = '--after--';
  5513. $before = '--before--';
  5514. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5515. $expected = array(
  5516. 'div' => array('class' => 'submit'),
  5517. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5518. '--after--',
  5519. '/div'
  5520. );
  5521. $this->assertTags($result, $expected);
  5522. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5523. $expected = array(
  5524. 'div' => array('class' => 'submit'),
  5525. '--before--',
  5526. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5527. '/div'
  5528. );
  5529. $this->assertTags($result, $expected);
  5530. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5531. $expected = array(
  5532. 'div' => array('class' => 'submit'),
  5533. '--before--',
  5534. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5535. '--after--',
  5536. '/div'
  5537. );
  5538. $this->assertTags($result, $expected);
  5539. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5540. $expected = array(
  5541. 'div' => array('class' => 'submit'),
  5542. '--before--',
  5543. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5544. '--after--',
  5545. '/div'
  5546. );
  5547. $this->assertTags($result, $expected);
  5548. }
  5549. /**
  5550. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5551. * be submitted at a time.
  5552. *
  5553. * @return void
  5554. */
  5555. public function testSubmitUnlockedByDefault() {
  5556. $this->Form->request->params['_Token']['key'] = 'secured';
  5557. $this->Form->submit('Go go');
  5558. $this->Form->submit('Save', array('name' => 'save'));
  5559. $result = $this->Form->unlockField();
  5560. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5561. }
  5562. /**
  5563. * Test submit image with timestamps.
  5564. *
  5565. * @return void
  5566. */
  5567. function testSubmitImageTimestamp() {
  5568. Configure::write('Asset.timestamp', 'force');
  5569. $result = $this->Form->submit('cake.power.gif');
  5570. $expected = array(
  5571. 'div' => array('class' => 'submit'),
  5572. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5573. '/div'
  5574. );
  5575. $this->assertTags($result, $expected);
  5576. }
  5577. /**
  5578. * test the create() method
  5579. *
  5580. * @return void
  5581. */
  5582. public function testCreate() {
  5583. $result = $this->Form->create('Contact');
  5584. $encoding = strtolower(Configure::read('App.encoding'));
  5585. $expected = array(
  5586. 'form' => array(
  5587. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5588. 'accept-charset' => $encoding
  5589. ),
  5590. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5591. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5592. '/div'
  5593. );
  5594. $this->assertTags($result, $expected);
  5595. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5596. $expected = array('form' => array(
  5597. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5598. 'accept-charset' => $encoding
  5599. ));
  5600. $this->assertTags($result, $expected);
  5601. $result = $this->Form->create('Contact', array('type' => 'get'));
  5602. $expected = array('form' => array(
  5603. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5604. 'accept-charset' => $encoding
  5605. ));
  5606. $this->assertTags($result, $expected);
  5607. $result = $this->Form->create('Contact', array('type' => 'put'));
  5608. $expected = array(
  5609. 'form' => array(
  5610. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5611. 'accept-charset' => $encoding
  5612. ),
  5613. 'div' => array('style' => 'display:none;'),
  5614. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5615. '/div'
  5616. );
  5617. $this->assertTags($result, $expected);
  5618. $result = $this->Form->create('Contact', array('type' => 'file'));
  5619. $expected = array(
  5620. 'form' => array(
  5621. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5622. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5623. ),
  5624. 'div' => array('style' => 'display:none;'),
  5625. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5626. '/div'
  5627. );
  5628. $this->assertTags($result, $expected);
  5629. $this->Form->request->data['Contact']['id'] = 1;
  5630. $this->Form->request->here = '/contacts/edit/1';
  5631. $this->Form->request['action'] = 'edit';
  5632. $result = $this->Form->create('Contact');
  5633. $expected = array(
  5634. 'form' => array(
  5635. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5636. 'accept-charset' => $encoding
  5637. ),
  5638. 'div' => array('style' => 'display:none;'),
  5639. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5640. '/div'
  5641. );
  5642. $this->assertTags($result, $expected);
  5643. $this->Form->request->data['Contact']['id'] = 1;
  5644. $this->Form->request->here = '/contacts/edit/1';
  5645. $this->Form->request['action'] = 'edit';
  5646. $result = $this->Form->create('Contact', array('type' => 'file'));
  5647. $expected = array(
  5648. 'form' => array(
  5649. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5650. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5651. ),
  5652. 'div' => array('style' => 'display:none;'),
  5653. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5654. '/div'
  5655. );
  5656. $this->assertTags($result, $expected);
  5657. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5658. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5659. $expected = array(
  5660. 'form' => array(
  5661. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5662. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5663. ),
  5664. 'div' => array('style' => 'display:none;'),
  5665. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5666. '/div'
  5667. );
  5668. $this->assertTags($result, $expected);
  5669. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5670. $expected = array(
  5671. 'form' => array(
  5672. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5673. 'accept-charset' => $encoding
  5674. ),
  5675. 'div' => array('style' => 'display:none;'),
  5676. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5677. '/div'
  5678. );
  5679. $this->assertTags($result, $expected);
  5680. $this->Form->request['action'] = 'add';
  5681. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5682. $expected = array(
  5683. 'form' => array(
  5684. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  5685. 'accept-charset' => $encoding
  5686. ),
  5687. 'div' => array('style' => 'display:none;'),
  5688. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5689. '/div'
  5690. );
  5691. $this->assertTags($result, $expected);
  5692. $result = $this->Form->create('User', array('action' => 'login'));
  5693. $expected = array(
  5694. 'form' => array(
  5695. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  5696. 'accept-charset' => $encoding
  5697. ),
  5698. 'div' => array('style' => 'display:none;'),
  5699. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5700. '/div'
  5701. );
  5702. $this->assertTags($result, $expected);
  5703. $result = $this->Form->create('User', array('url' => '/users/login'));
  5704. $expected = array(
  5705. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  5706. 'div' => array('style' => 'display:none;'),
  5707. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5708. '/div'
  5709. );
  5710. $this->assertTags($result, $expected);
  5711. $this->Form->request['controller'] = 'pages';
  5712. $result = $this->Form->create('User', array('action' => 'signup'));
  5713. $expected = array(
  5714. 'form' => array(
  5715. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  5716. 'accept-charset' => $encoding
  5717. ),
  5718. 'div' => array('style' => 'display:none;'),
  5719. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5720. '/div'
  5721. );
  5722. $this->assertTags($result, $expected);
  5723. $this->Form->request->data = array();
  5724. $this->Form->request['controller'] = 'contacts';
  5725. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5726. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  5727. $expected = array(
  5728. 'form' => array(
  5729. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  5730. 'accept-charset' => 'utf-8'
  5731. ),
  5732. 'div' => array('style' => 'display:none;'),
  5733. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5734. '/div'
  5735. );
  5736. $this->assertTags($result, $expected);
  5737. }
  5738. /**
  5739. * Test the onsubmit option for create()
  5740. *
  5741. * @return void
  5742. */
  5743. public function testCreateOnSubmit() {
  5744. $this->Form->request->data = array();
  5745. $this->Form->request['controller'] = 'contacts';
  5746. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5747. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  5748. $expected = array(
  5749. 'form' => array(
  5750. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  5751. 'accept-charset' => 'utf-8'
  5752. ),
  5753. 'div' => array('style' => 'display:none;'),
  5754. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5755. '/div'
  5756. );
  5757. $this->assertTags($result, $expected);
  5758. $this->Form->request->data = array();
  5759. $this->Form->request['controller'] = 'contacts';
  5760. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  5761. $result = $this->Form->create(array(
  5762. 'url' => array('action' => 'index', 'param'),
  5763. 'default' => false,
  5764. 'onsubmit' => 'someFunction();'
  5765. ));
  5766. $expected = array(
  5767. 'form' => array(
  5768. 'id' => 'ContactAddForm', 'method' => 'post',
  5769. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  5770. 'action' => '/contacts/index/param',
  5771. 'accept-charset' => 'utf-8'
  5772. ),
  5773. 'div' => array('style' => 'display:none;'),
  5774. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5775. '/div'
  5776. );
  5777. $this->assertTags($result, $expected);
  5778. }
  5779. /**
  5780. * test create() with automatic url generation
  5781. *
  5782. * @return void
  5783. */
  5784. public function testCreateAutoUrl() {
  5785. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  5786. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  5787. $this->Form->request->base = '/base_url';
  5788. $result = $this->Form->create('Contact');
  5789. $expected = array(
  5790. 'form' => array(
  5791. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  5792. 'accept-charset' => 'utf-8'
  5793. ),
  5794. 'div' => array('style' => 'display:none;'),
  5795. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5796. '/div'
  5797. );
  5798. $this->assertTags($result, $expected);
  5799. $this->Form->request['action'] = 'delete';
  5800. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  5801. $this->Form->request->base = '/base_url';
  5802. $result = $this->Form->create('Contact');
  5803. $expected = array(
  5804. 'form' => array(
  5805. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  5806. 'accept-charset' => 'utf-8'
  5807. ),
  5808. 'div' => array('style' => 'display:none;'),
  5809. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5810. '/div'
  5811. );
  5812. $this->assertTags($result, $expected);
  5813. }
  5814. /**
  5815. * test create() with a custom route
  5816. *
  5817. * @return void
  5818. */
  5819. public function testCreateCustomRoute() {
  5820. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  5821. $encoding = strtolower(Configure::read('App.encoding'));
  5822. $result = $this->Form->create('User', array('action' => 'login'));
  5823. $expected = array(
  5824. 'form' => array(
  5825. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  5826. 'accept-charset' => $encoding
  5827. ),
  5828. 'div' => array('style' => 'display:none;'),
  5829. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5830. '/div'
  5831. );
  5832. $this->assertTags($result, $expected);
  5833. }
  5834. /**
  5835. * test that inputDefaults are stored and used.
  5836. *
  5837. * @return void
  5838. */
  5839. public function testCreateWithInputDefaults() {
  5840. $this->Form->create('User', array(
  5841. 'inputDefaults' => array('div' => false, 'label' => false)
  5842. ));
  5843. $result = $this->Form->input('username');
  5844. $expected = array(
  5845. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  5846. );
  5847. $this->assertTags($result, $expected);
  5848. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  5849. $expected = array(
  5850. 'div' => array('class' => 'input text'),
  5851. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  5852. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  5853. '/div'
  5854. );
  5855. $this->assertTags($result, $expected);
  5856. }
  5857. /**
  5858. * test automatic accept-charset overriding
  5859. *
  5860. * @return void
  5861. */
  5862. public function testCreateWithAcceptCharset() {
  5863. $result = $this->Form->create('UserForm', array(
  5864. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  5865. )
  5866. );
  5867. $expected = array(
  5868. 'form' => array(
  5869. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  5870. 'accept-charset' => 'iso-8859-1'
  5871. ),
  5872. 'div' => array('style' => 'display:none;'),
  5873. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5874. '/div'
  5875. );
  5876. $this->assertTags($result, $expected);
  5877. }
  5878. /**
  5879. * Test base form url when url param is passed with multiple parameters (&)
  5880. *
  5881. */
  5882. public function testCreateQuerystringrequest() {
  5883. $encoding = strtolower(Configure::read('App.encoding'));
  5884. $result = $this->Form->create('Contact', array(
  5885. 'type' => 'post',
  5886. 'escape' => false,
  5887. 'url' => array(
  5888. 'controller' => 'controller',
  5889. 'action' => 'action',
  5890. '?' => array('param1' => 'value1', 'param2' => 'value2')
  5891. )
  5892. ));
  5893. $expected = array(
  5894. 'form' => array(
  5895. 'id' => 'ContactAddForm',
  5896. 'method' => 'post',
  5897. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  5898. 'accept-charset' => $encoding
  5899. ),
  5900. 'div' => array('style' => 'display:none;'),
  5901. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5902. '/div'
  5903. );
  5904. $this->assertTags($result, $expected);
  5905. $result = $this->Form->create('Contact', array(
  5906. 'type' => 'post',
  5907. 'url' => array(
  5908. 'controller' => 'controller',
  5909. 'action' => 'action',
  5910. '?' => array('param1' => 'value1', 'param2' => 'value2')
  5911. )
  5912. ));
  5913. $expected = array(
  5914. 'form' => array(
  5915. 'id' => 'ContactAddForm',
  5916. 'method' => 'post',
  5917. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  5918. 'accept-charset' => $encoding
  5919. ),
  5920. 'div' => array('style' => 'display:none;'),
  5921. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5922. '/div'
  5923. );
  5924. $this->assertTags($result, $expected);
  5925. }
  5926. /**
  5927. * test that create() doesn't cause errors by multiple id's being in the primary key
  5928. * as could happen with multiple select or checkboxes.
  5929. *
  5930. * @return void
  5931. */
  5932. public function testCreateWithMultipleIdInData() {
  5933. $encoding = strtolower(Configure::read('App.encoding'));
  5934. $this->Form->request->data['Contact']['id'] = array(1, 2);
  5935. $result = $this->Form->create('Contact');
  5936. $expected = array(
  5937. 'form' => array(
  5938. 'id' => 'ContactAddForm',
  5939. 'method' => 'post',
  5940. 'action' => '/contacts/add',
  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. }
  5949. /**
  5950. * test that create() doesn't add in extra passed params.
  5951. *
  5952. * @return void
  5953. */
  5954. public function testCreatePassedArgs() {
  5955. $encoding = strtolower(Configure::read('App.encoding'));
  5956. $this->Form->request->data['Contact']['id'] = 1;
  5957. $result = $this->Form->create('Contact', array(
  5958. 'type' => 'post',
  5959. 'escape' => false,
  5960. 'url' => array(
  5961. 'action' => 'edit',
  5962. 'myparam'
  5963. )
  5964. ));
  5965. $expected = array(
  5966. 'form' => array(
  5967. 'id' => 'ContactAddForm',
  5968. 'method' => 'post',
  5969. 'action' => '/contacts/edit/myparam',
  5970. 'accept-charset' => $encoding
  5971. ),
  5972. 'div' => array('style' => 'display:none;'),
  5973. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. }
  5978. /**
  5979. * test creating a get form, and get form inputs.
  5980. *
  5981. * @return void
  5982. */
  5983. public function testGetFormCreate() {
  5984. $encoding = strtolower(Configure::read('App.encoding'));
  5985. $result = $this->Form->create('Contact', array('type' => 'get'));
  5986. $this->assertTags($result, array('form' => array(
  5987. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5988. 'accept-charset' => $encoding
  5989. )));
  5990. $result = $this->Form->text('Contact.name');
  5991. $this->assertTags($result, array('input' => array(
  5992. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  5993. )));
  5994. $result = $this->Form->password('password');
  5995. $this->assertTags($result, array('input' => array(
  5996. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  5997. )));
  5998. $this->assertNoPattern('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  5999. $result = $this->Form->text('user_form');
  6000. $this->assertTags($result, array('input' => array(
  6001. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6002. )));
  6003. }
  6004. /**
  6005. * test get form, and inputs when the model param is false
  6006. *
  6007. * @return void
  6008. */
  6009. public function testGetFormWithFalseModel() {
  6010. $encoding = strtolower(Configure::read('App.encoding'));
  6011. $this->Form->request['controller'] = 'contact_test';
  6012. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6013. $expected = array('form' => array(
  6014. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6015. 'accept-charset' => $encoding
  6016. ));
  6017. $this->assertTags($result, $expected);
  6018. $result = $this->Form->text('reason');
  6019. $expected = array(
  6020. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6021. );
  6022. $this->assertTags($result, $expected);
  6023. }
  6024. /**
  6025. * test that datetime() works with GET style forms.
  6026. *
  6027. * @return void
  6028. */
  6029. public function testDateTimeWithGetForms() {
  6030. extract($this->dateRegex);
  6031. $this->Form->create('Contact', array('type' => 'get'));
  6032. $result = $this->Form->datetime('created');
  6033. $this->assertPattern('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6034. $this->assertPattern('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6035. $this->assertPattern('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6036. $this->assertPattern('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6037. $this->assertPattern('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6038. $this->assertPattern('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6039. }
  6040. /**
  6041. * testEditFormWithData method
  6042. *
  6043. * test auto populating form elements from submitted data.
  6044. *
  6045. * @return void
  6046. */
  6047. public function testEditFormWithData() {
  6048. $this->Form->request->data = array('Person' => array(
  6049. 'id' => 1,
  6050. 'first_name' => 'Nate',
  6051. 'last_name' => 'Abele',
  6052. 'email' => 'nate@example.com'
  6053. ));
  6054. $this->Form->request->addParams(array(
  6055. 'models' => array('Person'),
  6056. 'controller' => 'people',
  6057. 'action' => 'add'
  6058. ));
  6059. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6060. $this->Form->create();
  6061. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6062. $expected = array(
  6063. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6064. 'select' => array(
  6065. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6066. ),
  6067. array('option' => array('value' => 1)), 'Nate', '/option',
  6068. array('option' => array('value' => 2)), 'Garrett', '/option',
  6069. array('option' => array('value' => 3)), 'Larry', '/option',
  6070. '/select'
  6071. );
  6072. $this->assertTags($result, $expected);
  6073. }
  6074. /**
  6075. * testFormMagicInput method
  6076. *
  6077. * @return void
  6078. */
  6079. public function testFormMagicInput() {
  6080. $encoding = strtolower(Configure::read('App.encoding'));
  6081. $result = $this->Form->create('Contact');
  6082. $expected = array(
  6083. 'form' => array(
  6084. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6085. 'accept-charset' => $encoding
  6086. ),
  6087. 'div' => array('style' => 'display:none;'),
  6088. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6089. '/div'
  6090. );
  6091. $this->assertTags($result, $expected);
  6092. $result = $this->Form->input('name');
  6093. $expected = array(
  6094. 'div' => array('class' => 'input text'),
  6095. 'label' => array('for' => 'ContactName'),
  6096. 'Name',
  6097. '/label',
  6098. 'input' => array(
  6099. 'type' => 'text', 'name' => 'data[Contact][name]',
  6100. 'id' => 'ContactName', 'maxlength' => '255'
  6101. ),
  6102. '/div'
  6103. );
  6104. $this->assertTags($result, $expected);
  6105. $result = $this->Form->input('non_existing_field_in_contact_model');
  6106. $expected = array(
  6107. 'div' => array('class' => 'input text'),
  6108. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6109. 'Non Existing Field In Contact Model',
  6110. '/label',
  6111. 'input' => array(
  6112. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6113. 'id' => 'ContactNonExistingFieldInContactModel'
  6114. ),
  6115. '/div'
  6116. );
  6117. $this->assertTags($result, $expected);
  6118. $result = $this->Form->input('Address.street');
  6119. $expected = array(
  6120. 'div' => array('class' => 'input text'),
  6121. 'label' => array('for' => 'AddressStreet'),
  6122. 'Street',
  6123. '/label',
  6124. 'input' => array(
  6125. 'type' => 'text', 'name' => 'data[Address][street]',
  6126. 'id' => 'AddressStreet'
  6127. ),
  6128. '/div'
  6129. );
  6130. $this->assertTags($result, $expected);
  6131. $result = $this->Form->input('Address.non_existing_field_in_model');
  6132. $expected = array(
  6133. 'div' => array('class' => 'input text'),
  6134. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6135. 'Non Existing Field In Model',
  6136. '/label',
  6137. 'input' => array(
  6138. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6139. 'id' => 'AddressNonExistingFieldInModel'
  6140. ),
  6141. '/div'
  6142. );
  6143. $this->assertTags($result, $expected);
  6144. $result = $this->Form->input('name', array('div' => false));
  6145. $expected = array(
  6146. 'label' => array('for' => 'ContactName'),
  6147. 'Name',
  6148. '/label',
  6149. 'input' => array(
  6150. 'type' => 'text', 'name' => 'data[Contact][name]',
  6151. 'id' => 'ContactName', 'maxlength' => '255'
  6152. )
  6153. );
  6154. $this->assertTags($result, $expected);
  6155. $result = $this->Form->input('Contact.non_existing');
  6156. $expected = array(
  6157. 'div' => array('class' => 'input text required'),
  6158. 'label' => array('for' => 'ContactNonExisting'),
  6159. 'Non Existing',
  6160. '/label',
  6161. 'input' => array(
  6162. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6163. 'id' => 'ContactNonExisting'
  6164. ),
  6165. '/div'
  6166. );
  6167. $this->assertTags($result, $expected);
  6168. $result = $this->Form->input('Contact.imrequired');
  6169. $expected = array(
  6170. 'div' => array('class' => 'input text required'),
  6171. 'label' => array('for' => 'ContactImrequired'),
  6172. 'Imrequired',
  6173. '/label',
  6174. 'input' => array(
  6175. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6176. 'id' => 'ContactImrequired'
  6177. ),
  6178. '/div'
  6179. );
  6180. $this->assertTags($result, $expected);
  6181. $result = $this->Form->input('Contact.imalsorequired');
  6182. $expected = array(
  6183. 'div' => array('class' => 'input text required'),
  6184. 'label' => array('for' => 'ContactImalsorequired'),
  6185. 'Imalsorequired',
  6186. '/label',
  6187. 'input' => array(
  6188. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6189. 'id' => 'ContactImalsorequired'
  6190. ),
  6191. '/div'
  6192. );
  6193. $this->assertTags($result, $expected);
  6194. $result = $this->Form->input('Contact.imrequiredtoo');
  6195. $expected = array(
  6196. 'div' => array('class' => 'input text required'),
  6197. 'label' => array('for' => 'ContactImrequiredtoo'),
  6198. 'Imrequiredtoo',
  6199. '/label',
  6200. 'input' => array(
  6201. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6202. 'id' => 'ContactImrequiredtoo'
  6203. ),
  6204. '/div'
  6205. );
  6206. $this->assertTags($result, $expected);
  6207. $result = $this->Form->input('Contact.required_one');
  6208. $expected = array(
  6209. 'div' => array('class' => 'input text required'),
  6210. 'label' => array('for' => 'ContactRequiredOne'),
  6211. 'Required One',
  6212. '/label',
  6213. 'input' => array(
  6214. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6215. 'id' => 'ContactRequiredOne'
  6216. ),
  6217. '/div'
  6218. );
  6219. $this->assertTags($result, $expected);
  6220. $result = $this->Form->input('Contact.imnotrequired');
  6221. $expected = array(
  6222. 'div' => array('class' => 'input text'),
  6223. 'label' => array('for' => 'ContactImnotrequired'),
  6224. 'Imnotrequired',
  6225. '/label',
  6226. 'input' => array(
  6227. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6228. 'id' => 'ContactImnotrequired'
  6229. ),
  6230. '/div'
  6231. );
  6232. $this->assertTags($result, $expected);
  6233. $result = $this->Form->input('Contact.imalsonotrequired');
  6234. $expected = array(
  6235. 'div' => array('class' => 'input text'),
  6236. 'label' => array('for' => 'ContactImalsonotrequired'),
  6237. 'Imalsonotrequired',
  6238. '/label',
  6239. 'input' => array(
  6240. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6241. 'id' => 'ContactImalsonotrequired'
  6242. ),
  6243. '/div'
  6244. );
  6245. $this->assertTags($result, $expected);
  6246. $result = $this->Form->input('Contact.imnotrequiredeither');
  6247. $expected = array(
  6248. 'div' => array('class' => 'input text'),
  6249. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6250. 'Imnotrequiredeither',
  6251. '/label',
  6252. 'input' => array(
  6253. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6254. 'id' => 'ContactImnotrequiredeither'
  6255. ),
  6256. '/div'
  6257. );
  6258. $this->assertTags($result, $expected);
  6259. extract($this->dateRegex);
  6260. $now = strtotime('now');
  6261. $result = $this->Form->input('Contact.published', array('div' => false));
  6262. $expected = array(
  6263. 'label' => array('for' => 'ContactPublishedMonth'),
  6264. 'Published',
  6265. '/label',
  6266. array('select' => array(
  6267. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6268. )),
  6269. $monthsRegex,
  6270. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6271. date('F', $now),
  6272. '/option',
  6273. '*/select',
  6274. '-',
  6275. array('select' => array(
  6276. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6277. )),
  6278. $daysRegex,
  6279. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6280. date('j', $now),
  6281. '/option',
  6282. '*/select',
  6283. '-',
  6284. array('select' => array(
  6285. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6286. )),
  6287. $yearsRegex,
  6288. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6289. date('Y', $now),
  6290. '*/select'
  6291. );
  6292. $this->assertTags($result, $expected);
  6293. $result = $this->Form->input('Contact.updated', array('div' => false));
  6294. $expected = array(
  6295. 'label' => array('for' => 'ContactUpdatedMonth'),
  6296. 'Updated',
  6297. '/label',
  6298. array('select' => array(
  6299. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6300. )),
  6301. $monthsRegex,
  6302. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6303. date('F', $now),
  6304. '/option',
  6305. '*/select',
  6306. '-',
  6307. array('select' => array(
  6308. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6309. )),
  6310. $daysRegex,
  6311. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6312. date('j', $now),
  6313. '/option',
  6314. '*/select',
  6315. '-',
  6316. array('select' => array(
  6317. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6318. )),
  6319. $yearsRegex,
  6320. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6321. date('Y', $now),
  6322. '*/select'
  6323. );
  6324. $this->assertTags($result, $expected);
  6325. $result = $this->Form->input('UserForm.stuff');
  6326. $expected = array(
  6327. 'div' => array('class' => 'input text'),
  6328. 'label' => array('for' => 'UserFormStuff'),
  6329. 'Stuff',
  6330. '/label',
  6331. 'input' => array(
  6332. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6333. 'id' => 'UserFormStuff', 'maxlength' => 10
  6334. ),
  6335. '/div'
  6336. );
  6337. $this->assertTags($result, $expected);
  6338. }
  6339. /**
  6340. * testForMagicInputNonExistingNorValidated method
  6341. *
  6342. * @return void
  6343. */
  6344. public function testForMagicInputNonExistingNorValidated() {
  6345. $encoding = strtolower(Configure::read('App.encoding'));
  6346. $result = $this->Form->create('Contact');
  6347. $expected = array(
  6348. 'form' => array(
  6349. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6350. 'accept-charset' => $encoding
  6351. ),
  6352. 'div' => array('style' => 'display:none;'),
  6353. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6354. '/div'
  6355. );
  6356. $this->assertTags($result, $expected);
  6357. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6358. $expected = array(
  6359. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6360. 'Non Existing Nor Validated',
  6361. '/label',
  6362. 'input' => array(
  6363. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6364. 'id' => 'ContactNonExistingNorValidated'
  6365. )
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6369. 'div' => false, 'value' => 'my value'
  6370. ));
  6371. $expected = array(
  6372. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6373. 'Non Existing Nor Validated',
  6374. '/label',
  6375. 'input' => array(
  6376. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6377. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6378. )
  6379. );
  6380. $this->assertTags($result, $expected);
  6381. $this->Form->request->data = array(
  6382. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6383. ));
  6384. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6385. $expected = array(
  6386. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6387. 'Non Existing Nor Validated',
  6388. '/label',
  6389. 'input' => array(
  6390. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6391. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6392. )
  6393. );
  6394. $this->assertTags($result, $expected);
  6395. }
  6396. /**
  6397. * testFormMagicInputLabel method
  6398. *
  6399. * @return void
  6400. */
  6401. public function testFormMagicInputLabel() {
  6402. $encoding = strtolower(Configure::read('App.encoding'));
  6403. $result = $this->Form->create('Contact');
  6404. $expected = array(
  6405. 'form' => array(
  6406. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6407. 'accept-charset' => $encoding
  6408. ),
  6409. 'div' => array('style' => 'display:none;'),
  6410. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6411. '/div'
  6412. );
  6413. $this->assertTags($result, $expected);
  6414. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6415. $this->assertTags($result, array('input' => array(
  6416. 'name' => 'data[Contact][name]', 'type' => 'text',
  6417. 'id' => 'ContactName', 'maxlength' => '255')
  6418. ));
  6419. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6420. $expected = array(
  6421. 'label' => array('for' => 'ContactName'),
  6422. 'My label',
  6423. '/label',
  6424. 'input' => array(
  6425. 'type' => 'text', 'name' => 'data[Contact][name]',
  6426. 'id' => 'ContactName', 'maxlength' => '255'
  6427. )
  6428. );
  6429. $this->assertTags($result, $expected);
  6430. $result = $this->Form->input('Contact.name', array(
  6431. 'div' => false, 'label' => array('class' => 'mandatory')
  6432. ));
  6433. $expected = array(
  6434. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6435. 'Name',
  6436. '/label',
  6437. 'input' => array(
  6438. 'type' => 'text', 'name' => 'data[Contact][name]',
  6439. 'id' => 'ContactName', 'maxlength' => '255'
  6440. )
  6441. );
  6442. $this->assertTags($result, $expected);
  6443. $result = $this->Form->input('Contact.name', array(
  6444. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6445. ));
  6446. $expected = array(
  6447. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6448. 'My label',
  6449. '/label',
  6450. 'input' => array(
  6451. 'type' => 'text', 'name' => 'data[Contact][name]',
  6452. 'id' => 'ContactName', 'maxlength' => '255'
  6453. )
  6454. );
  6455. $this->assertTags($result, $expected);
  6456. $result = $this->Form->input('Contact.name', array(
  6457. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6458. ));
  6459. $expected = array(
  6460. 'label' => array('for' => 'my_id'),
  6461. 'Name',
  6462. '/label',
  6463. 'input' => array(
  6464. 'type' => 'text', 'name' => 'data[Contact][name]',
  6465. 'id' => 'my_id', 'maxlength' => '255'
  6466. )
  6467. );
  6468. $this->assertTags($result, $expected);
  6469. $result = $this->Form->input('1.id');
  6470. $this->assertTags($result, array('input' => array(
  6471. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6472. 'id' => 'Contact1Id'
  6473. )));
  6474. $result = $this->Form->input("1.name");
  6475. $expected = array(
  6476. 'div' => array('class' => 'input text'),
  6477. 'label' => array('for' => 'Contact1Name'),
  6478. 'Name',
  6479. '/label',
  6480. 'input' => array(
  6481. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6482. 'id' => 'Contact1Name', 'maxlength' => '255'
  6483. ),
  6484. '/div'
  6485. );
  6486. $this->assertTags($result, $expected);
  6487. $result = $this->Form->input('Contact.1.id');
  6488. $this->assertTags($result, array(
  6489. 'input' => array(
  6490. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6491. 'id' => 'Contact1Id'
  6492. )
  6493. ));
  6494. $result = $this->Form->input("Model.1.name");
  6495. $expected = array(
  6496. 'div' => array('class' => 'input text'),
  6497. 'label' => array('for' => 'Model1Name'),
  6498. 'Name',
  6499. '/label',
  6500. 'input' => array(
  6501. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6502. 'id' => 'Model1Name'
  6503. ),
  6504. '/div'
  6505. );
  6506. $this->assertTags($result, $expected);
  6507. }
  6508. /**
  6509. * testFormEnd method
  6510. *
  6511. * @return void
  6512. */
  6513. public function testFormEnd() {
  6514. $this->assertEqual($this->Form->end(), '</form>');
  6515. $result = $this->Form->end('');
  6516. $expected = array(
  6517. 'div' => array('class' => 'submit'),
  6518. 'input' => array('type' => 'submit', 'value' => ''),
  6519. '/div',
  6520. '/form'
  6521. );
  6522. $this->assertTags($result, $expected);
  6523. $result = $this->Form->end(array('label' => ''));
  6524. $expected = array(
  6525. 'div' => array('class' => 'submit'),
  6526. 'input' => array('type' => 'submit', 'value' => ''),
  6527. '/div',
  6528. '/form'
  6529. );
  6530. $this->assertTags($result, $expected);
  6531. $result = $this->Form->end('save');
  6532. $expected = array(
  6533. 'div' => array('class' => 'submit'),
  6534. 'input' => array('type' => 'submit', 'value' => 'save'),
  6535. '/div',
  6536. '/form'
  6537. );
  6538. $this->assertTags($result, $expected);
  6539. $result = $this->Form->end(array('label' => 'save'));
  6540. $expected = array(
  6541. 'div' => array('class' => 'submit'),
  6542. 'input' => array('type' => 'submit', 'value' => 'save'),
  6543. '/div',
  6544. '/form'
  6545. );
  6546. $this->assertTags($result, $expected);
  6547. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6548. $expected = array(
  6549. 'div' => array('class' => 'submit'),
  6550. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6551. '/div',
  6552. '/form'
  6553. );
  6554. $this->assertTags($result, $expected);
  6555. $result = $this->Form->end(array('name' => 'Whatever'));
  6556. $expected = array(
  6557. 'div' => array('class' => 'submit'),
  6558. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6559. '/div',
  6560. '/form'
  6561. );
  6562. $this->assertTags($result, $expected);
  6563. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6564. $expected = array(
  6565. 'div' => array('class' => 'good'),
  6566. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6567. '/div',
  6568. '/form'
  6569. );
  6570. $this->assertTags($result, $expected);
  6571. $result = $this->Form->end(array(
  6572. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6573. ));
  6574. $expected = array(
  6575. 'div' => array('class' => 'good'),
  6576. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6577. '/div',
  6578. '/form'
  6579. );
  6580. $this->assertTags($result, $expected);
  6581. }
  6582. /**
  6583. * testMultipleFormWithIdFields method
  6584. *
  6585. * @return void
  6586. */
  6587. public function testMultipleFormWithIdFields() {
  6588. $this->Form->create('UserForm');
  6589. $result = $this->Form->input('id');
  6590. $this->assertTags($result, array('input' => array(
  6591. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6592. )));
  6593. $result = $this->Form->input('ValidateItem.id');
  6594. $this->assertTags($result, array('input' => array(
  6595. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6596. 'id' => 'ValidateItemId'
  6597. )));
  6598. $result = $this->Form->input('ValidateUser.id');
  6599. $this->assertTags($result, array('input' => array(
  6600. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6601. 'id' => 'ValidateUserId'
  6602. )));
  6603. }
  6604. /**
  6605. * testDbLessModel method
  6606. *
  6607. * @return void
  6608. */
  6609. public function testDbLessModel() {
  6610. $this->Form->create('TestMail');
  6611. $result = $this->Form->input('name');
  6612. $expected = array(
  6613. 'div' => array('class' => 'input text'),
  6614. 'label' => array('for' => 'TestMailName'),
  6615. 'Name',
  6616. '/label',
  6617. 'input' => array(
  6618. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6619. 'id' => 'TestMailName'
  6620. ),
  6621. '/div'
  6622. );
  6623. $this->assertTags($result, $expected);
  6624. ClassRegistry::init('TestMail');
  6625. $this->Form->create('TestMail');
  6626. $result = $this->Form->input('name');
  6627. $expected = array(
  6628. 'div' => array('class' => 'input text'),
  6629. 'label' => array('for' => 'TestMailName'),
  6630. 'Name',
  6631. '/label',
  6632. 'input' => array(
  6633. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6634. 'id' => 'TestMailName'
  6635. ),
  6636. '/div'
  6637. );
  6638. $this->assertTags($result, $expected);
  6639. }
  6640. /**
  6641. * testBrokenness method
  6642. *
  6643. * @return void
  6644. */
  6645. public function testBrokenness() {
  6646. /*
  6647. * #4 This test has two parents and four children. By default (as of r7117) both
  6648. * parents are show but the first parent is missing a child. This is the inconsistency
  6649. * in the default behaviour - one parent has all children, the other does not - dependent
  6650. * on the data values.
  6651. */
  6652. $result = $this->Form->select('Model.field', array(
  6653. 'Fred' => array(
  6654. 'freds_son_1' => 'Fred',
  6655. 'freds_son_2' => 'Freddie'
  6656. ),
  6657. 'Bert' => array(
  6658. 'berts_son_1' => 'Albert',
  6659. 'berts_son_2' => 'Bertie')
  6660. ),
  6661. array('showParents' => true, 'empty' => false)
  6662. );
  6663. $expected = array(
  6664. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6665. array('optgroup' => array('label' => 'Fred')),
  6666. array('option' => array('value' => 'freds_son_1')),
  6667. 'Fred',
  6668. '/option',
  6669. array('option' => array('value' => 'freds_son_2')),
  6670. 'Freddie',
  6671. '/option',
  6672. '/optgroup',
  6673. array('optgroup' => array('label' => 'Bert')),
  6674. array('option' => array('value' => 'berts_son_1')),
  6675. 'Albert',
  6676. '/option',
  6677. array('option' => array('value' => 'berts_son_2')),
  6678. 'Bertie',
  6679. '/option',
  6680. '/optgroup',
  6681. '/select'
  6682. );
  6683. $this->assertTags($result, $expected);
  6684. /*
  6685. * #2 This is structurally identical to the test above (#1) - only the parent name has
  6686. * changed, so we should expect the same select list data, just with a different name
  6687. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  6688. * This is where data corruption can occur, because when a select value is missing from
  6689. * a list a form will substitute the first value in the list - without the user knowing.
  6690. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  6691. * affect the availability of 3 => 'Three' as a valid option.
  6692. */
  6693. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  6694. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  6695. ));
  6696. $result = $this->Form->select(
  6697. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  6698. );
  6699. $expected = array(
  6700. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  6701. array('option' => array('value' => 1)),
  6702. 'One',
  6703. '/option',
  6704. array('option' => array('value' => 2)),
  6705. 'Two',
  6706. '/option',
  6707. array('optgroup' => array('label' => 'Three')),
  6708. array('option' => array('value' => 3)),
  6709. 'Three',
  6710. '/option',
  6711. array('option' => array('value' => 4)),
  6712. 'Four',
  6713. '/option',
  6714. array('option' => array('value' => 5)),
  6715. 'Five',
  6716. '/option',
  6717. '/optgroup',
  6718. '/select'
  6719. );
  6720. $this->assertTags($result, $expected);
  6721. }
  6722. /**
  6723. * Test the generation of fields for a multi record form.
  6724. *
  6725. * @return void
  6726. */
  6727. public function testMultiRecordForm() {
  6728. $this->Form->create('ValidateProfile');
  6729. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  6730. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  6731. $expected = array(
  6732. 'div' => array('class' => 'input textarea'),
  6733. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  6734. 'Name',
  6735. '/label',
  6736. 'textarea' => array(
  6737. 'id' => 'ValidateProfile1ValidateItem2Name',
  6738. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  6739. 'cols' => 30,
  6740. 'rows' => 6
  6741. ),
  6742. 'Value',
  6743. '/textarea',
  6744. '/div'
  6745. );
  6746. $this->assertTags($result, $expected);
  6747. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  6748. $expected = array(
  6749. 'div' => array('class' => 'input date'),
  6750. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  6751. 'Created',
  6752. '/label',
  6753. array('select' => array(
  6754. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  6755. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  6756. )
  6757. ),
  6758. array('option' => array('value' => '')), '/option',
  6759. $this->dateRegex['monthsRegex'],
  6760. '/select', '-',
  6761. array('select' => array(
  6762. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  6763. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  6764. )
  6765. ),
  6766. array('option' => array('value' => '')), '/option',
  6767. $this->dateRegex['daysRegex'],
  6768. '/select', '-',
  6769. array('select' => array(
  6770. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  6771. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  6772. )
  6773. ),
  6774. array('option' => array('value' => '')), '/option',
  6775. $this->dateRegex['yearsRegex'],
  6776. '/select',
  6777. '/div'
  6778. );
  6779. $this->assertTags($result, $expected);
  6780. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  6781. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  6782. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  6783. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  6784. $expected = array(
  6785. 'div' => array('class' => 'input select error'),
  6786. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  6787. 'Profile',
  6788. '/label',
  6789. 'select' => array(
  6790. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  6791. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  6792. 'class' => 'form-error'
  6793. ),
  6794. '/select',
  6795. array('div' => array('class' => 'error-message')),
  6796. 'Error',
  6797. '/div',
  6798. '/div'
  6799. );
  6800. $this->assertTags($result, $expected);
  6801. }
  6802. /**
  6803. * test the correct display of multi-record form validation errors.
  6804. *
  6805. * @return void
  6806. */
  6807. public function testMultiRecordFormValidationErrors() {
  6808. $this->Form->create('ValidateProfile');
  6809. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  6810. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  6811. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  6812. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  6813. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  6814. $result = $this->Form->error('ValidateProfile.2.city');
  6815. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6816. $result = $this->Form->error('2.city');
  6817. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  6818. }
  6819. /**
  6820. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  6821. *
  6822. * @return void
  6823. */
  6824. public function testInputTemplate() {
  6825. $result = $this->Form->input('Contact.email', array(
  6826. 'type' => 'text', 'format' => array('input')
  6827. ));
  6828. $expected = array(
  6829. 'div' => array('class' => 'input text'),
  6830. 'input' => array(
  6831. 'type' => 'text', 'name' => 'data[Contact][email]',
  6832. 'id' => 'ContactEmail'
  6833. ),
  6834. '/div'
  6835. );
  6836. $this->assertTags($result, $expected);
  6837. $result = $this->Form->input('Contact.email', array(
  6838. 'type' => 'text', 'format' => array('input', 'label'),
  6839. 'label' => '<em>Email (required)</em>'
  6840. ));
  6841. $expected = array(
  6842. 'div' => array('class' => 'input text'),
  6843. array('input' => array(
  6844. 'type' => 'text', 'name' => 'data[Contact][email]',
  6845. 'id' => 'ContactEmail'
  6846. )),
  6847. 'label' => array('for' => 'ContactEmail'),
  6848. 'em' => array(),
  6849. 'Email (required)',
  6850. '/em',
  6851. '/label',
  6852. '/div'
  6853. );
  6854. $this->assertTags($result, $expected);
  6855. $result = $this->Form->input('Contact.email', array(
  6856. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  6857. 'between' => '<div>Something in the middle</div>',
  6858. 'after' => '<span>Some text at the end</span>'
  6859. ));
  6860. $expected = array(
  6861. 'div' => array('class' => 'input text'),
  6862. array('input' => array(
  6863. 'type' => 'text', 'name' => 'data[Contact][email]',
  6864. 'id' => 'ContactEmail'
  6865. )),
  6866. array('div' => array()),
  6867. 'Something in the middle',
  6868. '/div',
  6869. 'label' => array('for' => 'ContactEmail'),
  6870. 'Email',
  6871. '/label',
  6872. 'span' => array(),
  6873. 'Some text at the end',
  6874. '/span',
  6875. '/div'
  6876. );
  6877. $this->assertTags($result, $expected);
  6878. }
  6879. /**
  6880. * test that some html5 inputs + FormHelper::__call() work
  6881. *
  6882. * @return void
  6883. */
  6884. public function testHtml5Inputs() {
  6885. $result = $this->Form->email('User.email');
  6886. $expected = array(
  6887. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  6888. );
  6889. $this->assertTags($result, $expected);
  6890. $result = $this->Form->search('User.query');
  6891. $expected = array(
  6892. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  6893. );
  6894. $this->assertTags($result, $expected);
  6895. $result = $this->Form->search('User.query', array('value' => 'test'));
  6896. $expected = array(
  6897. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  6898. );
  6899. $this->assertTags($result, $expected);
  6900. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  6901. $expected = array(
  6902. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  6903. );
  6904. $this->assertTags($result, $expected);
  6905. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  6906. $expected = array(
  6907. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  6908. );
  6909. $this->assertTags($result, $expected);
  6910. }
  6911. /**
  6912. *
  6913. * @expectedException CakeException
  6914. * @return void
  6915. */
  6916. public function testHtml5InputException() {
  6917. $this->Form->email();
  6918. }
  6919. /**
  6920. * Tests that a model can be loaded from the model names passed in the request object
  6921. *
  6922. * @return void
  6923. */
  6924. public function testIntrospectModelFromRequest() {
  6925. $this->loadFixtures('Post');
  6926. App::build(array(
  6927. 'plugins' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  6928. ));
  6929. CakePlugin::load('TestPlugin');
  6930. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  6931. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  6932. $this->Form->create('TestPluginPost');
  6933. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  6934. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  6935. CakePlugin::unload();
  6936. App::build();
  6937. }
  6938. /**
  6939. * Tests that it is possible to set the validation errors directly in the helper for a field
  6940. *
  6941. * @return void
  6942. */
  6943. public function testCustomValidationErrors() {
  6944. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  6945. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  6946. $this->assertEquals('Badness!', $result);
  6947. }
  6948. }