FormHelperTest.php 235 KB

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