FormHelperTest.php 258 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170
  1. <?php
  2. /**
  3. * FormHelperTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc.
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc.
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @package Cake.Test.Case.View.Helper
  16. * @since CakePHP(tm) v 1.2.0.4206
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('ClassRegistry', 'Utility');
  20. App::uses('Controller', 'Controller');
  21. App::uses('View', 'View');
  22. App::uses('Model', 'Model');
  23. App::uses('Security', 'Utility');
  24. App::uses('CakeRequest', 'Network');
  25. App::uses('HtmlHelper', 'View/Helper');
  26. App::uses('FormHelper', 'View/Helper');
  27. App::uses('Router', 'Routing');
  28. /**
  29. * ContactTestController class
  30. *
  31. * @package cake
  32. * @package Cake.Test.Case.View.Helper
  33. */
  34. class ContactTestController extends Controller {
  35. /**
  36. * name property
  37. *
  38. * @var string 'ContactTest'
  39. */
  40. public $name = 'ContactTest';
  41. /**
  42. * uses property
  43. *
  44. * @var mixed null
  45. */
  46. public $uses = null;
  47. }
  48. /**
  49. * Contact class
  50. *
  51. * @package cake
  52. * @package Cake.Test.Case.View.Helper
  53. */
  54. class Contact extends CakeTestModel {
  55. /**
  56. * primaryKey property
  57. *
  58. * @var string 'id'
  59. */
  60. public $primaryKey = 'id';
  61. /**
  62. * useTable property
  63. *
  64. * @var bool false
  65. */
  66. public $useTable = false;
  67. /**
  68. * name property
  69. *
  70. * @var string 'Contact'
  71. */
  72. public $name = 'Contact';
  73. /**
  74. * Default schema
  75. *
  76. * @var array
  77. */
  78. protected $_schema = array(
  79. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  80. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  81. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  82. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  83. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  84. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  85. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  86. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  87. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  88. );
  89. /**
  90. * validate property
  91. *
  92. * @var array
  93. */
  94. public $validate = array(
  95. 'non_existing' => array(),
  96. 'idontexist' => array(),
  97. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  98. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  99. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  100. 'imrequiredonboth' => array(
  101. 'required' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  102. 'check' => array('rule' => 'alphaNumeric')
  103. ),
  104. 'string_required' => 'notEmpty',
  105. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  106. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  107. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  108. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  109. 'imalsonotrequired' => array(
  110. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  111. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  112. ),
  113. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  114. );
  115. /**
  116. * schema method
  117. *
  118. * @return void
  119. */
  120. public function setSchema($schema) {
  121. $this->_schema = $schema;
  122. }
  123. /**
  124. * hasAndBelongsToMany property
  125. *
  126. * @var array
  127. */
  128. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  129. /**
  130. * hasAndBelongsToMany property
  131. *
  132. * @var array
  133. */
  134. public $belongsTo = array('User' => array('className' => 'UserForm'));
  135. }
  136. /**
  137. * ContactTagsContact class
  138. *
  139. * @package cake
  140. * @package Cake.Test.Case.View.Helper
  141. */
  142. class ContactTagsContact extends CakeTestModel {
  143. /**
  144. * useTable property
  145. *
  146. * @var bool false
  147. */
  148. public $useTable = false;
  149. /**
  150. * name property
  151. *
  152. * @var string 'Contact'
  153. */
  154. public $name = 'ContactTagsContact';
  155. /**
  156. * Default schema
  157. *
  158. * @var array
  159. */
  160. protected $_schema = array(
  161. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  162. 'contact_tag_id' => array(
  163. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  164. )
  165. );
  166. /**
  167. * schema method
  168. *
  169. * @return void
  170. */
  171. public function setSchema($schema) {
  172. $this->_schema = $schema;
  173. }
  174. }
  175. /**
  176. * ContactNonStandardPk class
  177. *
  178. * @package cake
  179. * @package Cake.Test.Case.View.Helper
  180. */
  181. class ContactNonStandardPk extends Contact {
  182. /**
  183. * primaryKey property
  184. *
  185. * @var string 'pk'
  186. */
  187. public $primaryKey = 'pk';
  188. /**
  189. * name property
  190. *
  191. * @var string 'ContactNonStandardPk'
  192. */
  193. public $name = 'ContactNonStandardPk';
  194. /**
  195. * schema method
  196. *
  197. * @return void
  198. */
  199. public function schema($field = false) {
  200. $this->_schema = parent::schema();
  201. $this->_schema['pk'] = $this->_schema['id'];
  202. unset($this->_schema['id']);
  203. return $this->_schema;
  204. }
  205. }
  206. /**
  207. * ContactTag class
  208. *
  209. * @package cake
  210. * @package Cake.Test.Case.View.Helper
  211. */
  212. class ContactTag extends Model {
  213. /**
  214. * useTable property
  215. *
  216. * @var bool false
  217. */
  218. public $useTable = false;
  219. /**
  220. * schema definition
  221. *
  222. * @var array
  223. */
  224. protected $_schema = array(
  225. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  226. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  227. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  228. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  229. );
  230. }
  231. /**
  232. * UserForm class
  233. *
  234. * @package cake
  235. * @package Cake.Test.Case.View.Helper
  236. */
  237. class UserForm extends CakeTestModel {
  238. /**
  239. * useTable property
  240. *
  241. * @var bool false
  242. */
  243. public $useTable = false;
  244. /**
  245. * primaryKey property
  246. *
  247. * @var string 'id'
  248. */
  249. public $primaryKey = 'id';
  250. /**
  251. * name property
  252. *
  253. * @var string 'UserForm'
  254. */
  255. public $name = 'UserForm';
  256. /**
  257. * hasMany property
  258. *
  259. * @var array
  260. */
  261. public $hasMany = array(
  262. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  263. ));
  264. /**
  265. * schema definition
  266. *
  267. * @var array
  268. */
  269. protected $_schema = array(
  270. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  271. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  272. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  273. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  274. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  275. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  276. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  277. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  278. );
  279. }
  280. /**
  281. * OpenidUrl class
  282. *
  283. * @package cake
  284. * @package Cake.Test.Case.View.Helper
  285. */
  286. class OpenidUrl extends CakeTestModel {
  287. /**
  288. * useTable property
  289. *
  290. * @var bool false
  291. */
  292. public $useTable = false;
  293. /**
  294. * primaryKey property
  295. *
  296. * @var string 'id'
  297. */
  298. public $primaryKey = 'id';
  299. /**
  300. * name property
  301. *
  302. * @var string 'OpenidUrl'
  303. */
  304. public $name = 'OpenidUrl';
  305. /**
  306. * belongsTo property
  307. *
  308. * @var array
  309. */
  310. public $belongsTo = array('UserForm' => array(
  311. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  312. ));
  313. /**
  314. * validate property
  315. *
  316. * @var array
  317. */
  318. public $validate = array('openid_not_registered' => array());
  319. /**
  320. * schema method
  321. *
  322. * @var array
  323. */
  324. protected $_schema = array(
  325. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  326. 'user_form_id' => array(
  327. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  328. ),
  329. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  330. );
  331. /**
  332. * beforeValidate method
  333. *
  334. * @return void
  335. */
  336. public function beforeValidate($options = array()) {
  337. $this->invalidate('openid_not_registered');
  338. return true;
  339. }
  340. }
  341. /**
  342. * ValidateUser class
  343. *
  344. * @package cake
  345. * @package Cake.Test.Case.View.Helper
  346. */
  347. class ValidateUser extends CakeTestModel {
  348. /**
  349. * primaryKey property
  350. *
  351. * @var string 'id'
  352. */
  353. public $primaryKey = 'id';
  354. /**
  355. * useTable property
  356. *
  357. * @var bool false
  358. */
  359. public $useTable = false;
  360. /**
  361. * name property
  362. *
  363. * @var string 'ValidateUser'
  364. */
  365. public $name = 'ValidateUser';
  366. /**
  367. * hasOne property
  368. *
  369. * @var array
  370. */
  371. public $hasOne = array('ValidateProfile' => array(
  372. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  373. ));
  374. /**
  375. * schema method
  376. *
  377. * @var array
  378. */
  379. protected $_schema = array(
  380. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  381. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  382. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  383. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  384. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  385. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  386. );
  387. /**
  388. * beforeValidate method
  389. *
  390. * @return void
  391. */
  392. public function beforeValidate($options = array()) {
  393. $this->invalidate('email');
  394. return false;
  395. }
  396. }
  397. /**
  398. * ValidateProfile class
  399. *
  400. * @package cake
  401. * @package Cake.Test.Case.View.Helper
  402. */
  403. class ValidateProfile extends CakeTestModel {
  404. /**
  405. * primaryKey property
  406. *
  407. * @var string 'id'
  408. */
  409. public $primaryKey = 'id';
  410. /**
  411. * useTable property
  412. *
  413. * @var bool false
  414. */
  415. public $useTable = false;
  416. /**
  417. * schema property
  418. *
  419. * @var array
  420. */
  421. protected $_schema = array(
  422. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  423. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  424. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  425. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  426. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  427. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  428. );
  429. /**
  430. * name property
  431. *
  432. * @var string 'ValidateProfile'
  433. */
  434. public $name = 'ValidateProfile';
  435. /**
  436. * hasOne property
  437. *
  438. * @var array
  439. */
  440. public $hasOne = array('ValidateItem' => array(
  441. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  442. ));
  443. /**
  444. * belongsTo property
  445. *
  446. * @var array
  447. */
  448. public $belongsTo = array('ValidateUser' => array(
  449. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  450. ));
  451. /**
  452. * beforeValidate method
  453. *
  454. * @return void
  455. */
  456. public function beforeValidate($options = array()) {
  457. $this->invalidate('full_name');
  458. $this->invalidate('city');
  459. return false;
  460. }
  461. }
  462. /**
  463. * ValidateItem class
  464. *
  465. * @package cake
  466. * @package Cake.Test.Case.View.Helper
  467. */
  468. class ValidateItem extends CakeTestModel {
  469. /**
  470. * primaryKey property
  471. *
  472. * @var string 'id'
  473. */
  474. public $primaryKey = 'id';
  475. /**
  476. * useTable property
  477. *
  478. * @var bool false
  479. */
  480. public $useTable = false;
  481. /**
  482. * name property
  483. *
  484. * @var string 'ValidateItem'
  485. */
  486. public $name = 'ValidateItem';
  487. /**
  488. * schema property
  489. *
  490. * @var array
  491. */
  492. protected $_schema = array(
  493. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  494. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  495. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  496. 'description' => array(
  497. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  498. ),
  499. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  500. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  501. );
  502. /**
  503. * belongsTo property
  504. *
  505. * @var array
  506. */
  507. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  508. /**
  509. * beforeValidate method
  510. *
  511. * @return void
  512. */
  513. public function beforeValidate($options = array()) {
  514. $this->invalidate('description');
  515. return false;
  516. }
  517. }
  518. /**
  519. * TestMail class
  520. *
  521. * @package cake
  522. * @package Cake.Test.Case.View.Helper
  523. */
  524. class TestMail extends CakeTestModel {
  525. /**
  526. * primaryKey property
  527. *
  528. * @var string 'id'
  529. */
  530. public $primaryKey = 'id';
  531. /**
  532. * useTable property
  533. *
  534. * @var bool false
  535. */
  536. public $useTable = false;
  537. /**
  538. * name property
  539. *
  540. * @var string 'TestMail'
  541. */
  542. public $name = 'TestMail';
  543. }
  544. /**
  545. * FormHelperTest class
  546. *
  547. * @package cake
  548. * @subpackage Cake.Test.Case.View.Helper
  549. * @property FormHelper $Form
  550. */
  551. class FormHelperTest extends CakeTestCase {
  552. /**
  553. * Fixtures to be used
  554. *
  555. * @var array
  556. */
  557. public $fixtures = array('core.post');
  558. /**
  559. * Do not load the fixtures by default
  560. *
  561. * @var boolean
  562. */
  563. public $autoFixtures = false;
  564. /**
  565. * setUp method
  566. *
  567. * @return void
  568. */
  569. public function setUp() {
  570. parent::setUp();
  571. Configure::write('App.base', '');
  572. $this->Controller = new ContactTestController();
  573. $this->View = new View($this->Controller);
  574. $this->Form = new FormHelper($this->View);
  575. $this->Form->Html = new HtmlHelper($this->View);
  576. $this->Form->request = new CakeRequest('contacts/add', false);
  577. $this->Form->request->here = '/contacts/add';
  578. $this->Form->request['action'] = 'add';
  579. $this->Form->request->webroot = '';
  580. $this->Form->request->base = '';
  581. ClassRegistry::addObject('Contact', new Contact());
  582. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  583. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  584. ClassRegistry::addObject('User', new UserForm());
  585. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  586. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  587. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  588. $this->oldSalt = Configure::read('Security.salt');
  589. $this->dateRegex = array(
  590. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  591. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  592. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  593. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  594. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  595. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  596. );
  597. Configure::write('Security.salt', 'foo!');
  598. }
  599. /**
  600. * tearDown method
  601. *
  602. * @return void
  603. */
  604. public function tearDown() {
  605. parent::tearDown();
  606. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  607. Configure::write('Security.salt', $this->oldSalt);
  608. }
  609. /**
  610. * testFormCreateWithSecurity method
  611. *
  612. * Test form->create() with security key.
  613. *
  614. * @return void
  615. */
  616. public function testCreateWithSecurity() {
  617. $this->Form->request['_Token'] = array('key' => 'testKey');
  618. $encoding = strtolower(Configure::read('App.encoding'));
  619. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  620. $expected = array(
  621. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  622. 'div' => array('style' => 'display:none;'),
  623. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  624. array('input' => array(
  625. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  626. )),
  627. '/div'
  628. );
  629. $this->assertTags($result, $expected);
  630. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  631. $expected['form']['id'] = 'MyForm';
  632. $this->assertTags($result, $expected);
  633. }
  634. /**
  635. * test that create() clears the fields property so it starts fresh
  636. *
  637. * @return void
  638. */
  639. public function testCreateClearingFields() {
  640. $this->Form->fields = array('model_id');
  641. $this->Form->create('Contact');
  642. $this->assertEquals(array(), $this->Form->fields);
  643. }
  644. /**
  645. * Tests form hash generation with model-less data
  646. *
  647. * @return void
  648. */
  649. public function testValidateHashNoModel() {
  650. $this->Form->request['_Token'] = array('key' => 'foo');
  651. $result = $this->Form->secure(array('anything'));
  652. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  653. }
  654. /**
  655. * Tests that models with identical field names get resolved properly
  656. *
  657. * @return void
  658. */
  659. public function testDuplicateFieldNameResolution() {
  660. $result = $this->Form->create('ValidateUser');
  661. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  662. $result = $this->Form->input('ValidateItem.name');
  663. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  664. $result = $this->Form->input('ValidateUser.name');
  665. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  666. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  667. $this->assertRegExp('/type="text"/', $result);
  668. $result = $this->Form->input('ValidateItem.name');
  669. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  670. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  671. $this->assertRegExp('/<textarea/', $result);
  672. $result = $this->Form->input('name');
  673. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  674. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  675. $this->assertRegExp('/type="text"/', $result);
  676. }
  677. /**
  678. * Tests that hidden fields generated for checkboxes don't get locked
  679. *
  680. * @return void
  681. */
  682. public function testNoCheckboxLocking() {
  683. $this->Form->request['_Token'] = array('key' => 'foo');
  684. $this->assertSame(array(), $this->Form->fields);
  685. $this->Form->checkbox('check', array('value' => '1'));
  686. $this->assertSame($this->Form->fields, array('check'));
  687. }
  688. /**
  689. * testFormSecurityFields method
  690. *
  691. * Test generation of secure form hash generation.
  692. *
  693. * @return void
  694. */
  695. public function testFormSecurityFields() {
  696. $key = 'testKey';
  697. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  698. $this->Form->request['_Token'] = array('key' => $key);
  699. $result = $this->Form->secure($fields);
  700. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  701. $hash .= ':' . 'Model.valid';
  702. $hash = urlencode($hash);
  703. $expected = array(
  704. 'div' => array('style' => 'display:none;'),
  705. array('input' => array(
  706. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  707. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  708. )),
  709. array('input' => array(
  710. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  711. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  712. )),
  713. '/div'
  714. );
  715. $this->assertTags($result, $expected);
  716. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  717. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  718. $result = $this->Form->secure($fields);
  719. $expected = array(
  720. 'div' => array('class' => 'hidden'),
  721. array('input' => array(
  722. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  723. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  724. )),
  725. array('input' => array(
  726. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  727. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  728. )),
  729. '/div'
  730. );
  731. $this->assertTags($result, $expected);
  732. }
  733. /**
  734. * Tests correct generation of number fields for double and float fields
  735. *
  736. * @return void
  737. */
  738. public function testTextFieldGenerationForFloats() {
  739. $model = ClassRegistry::getObject('Contact');
  740. $model->setSchema(array('foo' => array(
  741. 'type' => 'float',
  742. 'null' => false,
  743. 'default' => null,
  744. 'length' => null
  745. )));
  746. $this->Form->create('Contact');
  747. $result = $this->Form->input('foo');
  748. $expected = array(
  749. 'div' => array('class' => 'input number'),
  750. 'label' => array('for' => 'ContactFoo'),
  751. 'Foo',
  752. '/label',
  753. array('input' => array(
  754. 'type' => 'number',
  755. 'name' => 'data[Contact][foo]',
  756. 'id' => 'ContactFoo',
  757. 'step' => 'any'
  758. )),
  759. '/div'
  760. );
  761. $this->assertTags($result, $expected);
  762. $result = $this->Form->input('foo', array('step' => 0.5));
  763. $expected = array(
  764. 'div' => array('class' => 'input number'),
  765. 'label' => array('for' => 'ContactFoo'),
  766. 'Foo',
  767. '/label',
  768. array('input' => array(
  769. 'type' => 'number',
  770. 'name' => 'data[Contact][foo]',
  771. 'id' => 'ContactFoo',
  772. 'step' => '0.5'
  773. )),
  774. '/div'
  775. );
  776. $this->assertTags($result, $expected);
  777. }
  778. /**
  779. * Tests correct generation of number fields for integer fields
  780. *
  781. * @access public
  782. * @return void
  783. */
  784. public function testTextFieldTypeNumberGenerationForIntegers() {
  785. $model = ClassRegistry::getObject('Contact');
  786. $model->setSchema(array('foo' => array(
  787. 'type' => 'integer',
  788. 'null' => false,
  789. 'default' => null,
  790. 'length' => null
  791. )));
  792. $this->Form->create('Contact');
  793. $result = $this->Form->input('foo');
  794. $expected = array(
  795. 'div' => array('class' => 'input number'),
  796. 'label' => array('for' => 'ContactFoo'),
  797. 'Foo',
  798. '/label',
  799. array('input' => array(
  800. 'type' => 'number', 'name' => 'data[Contact][foo]',
  801. 'id' => 'ContactFoo'
  802. )),
  803. '/div'
  804. );
  805. $this->assertTags($result, $expected);
  806. }
  807. /**
  808. * testFormSecurityMultipleFields method
  809. *
  810. * Test secure() with multiple row form. Ensure hash is correct.
  811. *
  812. * @return void
  813. */
  814. public function testFormSecurityMultipleFields() {
  815. $key = 'testKey';
  816. $fields = array(
  817. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  818. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  819. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  820. );
  821. $this->Form->request['_Token'] = array('key' => $key);
  822. $result = $this->Form->secure($fields);
  823. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  824. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  825. $expected = array(
  826. 'div' => array('style' => 'display:none;'),
  827. array('input' => array(
  828. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  829. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  830. )),
  831. array('input' => array(
  832. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  833. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  834. )),
  835. '/div'
  836. );
  837. $this->assertTags($result, $expected);
  838. }
  839. /**
  840. * testFormSecurityMultipleSubmitButtons
  841. *
  842. * test form submit generation and ensure that _Token is only created on end()
  843. *
  844. * @return void
  845. */
  846. public function testFormSecurityMultipleSubmitButtons() {
  847. $key = 'testKey';
  848. $this->Form->request['_Token'] = array('key' => $key);
  849. $this->Form->create('Addresses');
  850. $this->Form->input('Address.title');
  851. $this->Form->input('Address.first_name');
  852. $result = $this->Form->submit('Save', array('name' => 'save'));
  853. $expected = array(
  854. 'div' => array('class' => 'submit'),
  855. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  856. '/div',
  857. );
  858. $this->assertTags($result, $expected);
  859. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  860. $expected = array(
  861. 'div' => array('class' => 'submit'),
  862. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  863. '/div',
  864. );
  865. $this->assertTags($result, $expected);
  866. $result = $this->Form->end(null);
  867. $expected = array(
  868. 'div' => array('style' => 'display:none;'),
  869. array('input' => array(
  870. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  871. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  872. )),
  873. array('input' => array(
  874. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  875. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  876. )),
  877. '/div'
  878. );
  879. $this->assertTags($result, $expected);
  880. }
  881. /**
  882. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  883. *
  884. * @return void
  885. */
  886. public function testSecurityButtonNestedNamed() {
  887. $key = 'testKey';
  888. $this->Form->request['_Token'] = array('key' => $key);
  889. $this->Form->create('Addresses');
  890. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  891. $result = $this->Form->unlockField();
  892. $this->assertEquals(array('Address.button'), $result);
  893. }
  894. /**
  895. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  896. *
  897. * @return void
  898. */
  899. public function testSecuritySubmitNestedNamed() {
  900. $key = 'testKey';
  901. $this->Form->request['_Token'] = array('key' => $key);
  902. $this->Form->create('Addresses');
  903. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  904. $result = $this->Form->unlockField();
  905. $this->assertEquals(array('Address.button'), $result);
  906. }
  907. /**
  908. * Test that the correct fields are unlocked for image submits with no names.
  909. *
  910. * @return void
  911. */
  912. public function testSecuritySubmitImageNoName() {
  913. $key = 'testKey';
  914. $this->Form->request['_Token'] = array('key' => $key);
  915. $this->Form->create('User');
  916. $result = $this->Form->submit('save.png');
  917. $expected = array(
  918. 'div' => array('class' => 'submit'),
  919. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  920. '/div'
  921. );
  922. $this->assertTags($result, $expected);
  923. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  924. }
  925. /**
  926. * Test that the correct fields are unlocked for image submits with names.
  927. *
  928. * @return void
  929. */
  930. public function testSecuritySubmitImageName() {
  931. $key = 'testKey';
  932. $this->Form->request['_Token'] = array('key' => $key);
  933. $this->Form->create('User');
  934. $result = $this->Form->submit('save.png', array('name' => 'test'));
  935. $expected = array(
  936. 'div' => array('class' => 'submit'),
  937. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  938. '/div'
  939. );
  940. $this->assertTags($result, $expected);
  941. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  942. }
  943. /**
  944. * testFormSecurityMultipleInputFields method
  945. *
  946. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  947. *
  948. * @return void
  949. */
  950. public function testFormSecurityMultipleInputFields() {
  951. $key = 'testKey';
  952. $this->Form->request['_Token'] = array('key' => $key);
  953. $this->Form->create('Addresses');
  954. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  955. $this->Form->input('Addresses.0.title');
  956. $this->Form->input('Addresses.0.first_name');
  957. $this->Form->input('Addresses.0.last_name');
  958. $this->Form->input('Addresses.0.address');
  959. $this->Form->input('Addresses.0.city');
  960. $this->Form->input('Addresses.0.phone');
  961. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  962. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  963. $this->Form->input('Addresses.1.title');
  964. $this->Form->input('Addresses.1.first_name');
  965. $this->Form->input('Addresses.1.last_name');
  966. $this->Form->input('Addresses.1.address');
  967. $this->Form->input('Addresses.1.city');
  968. $this->Form->input('Addresses.1.phone');
  969. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  970. $result = $this->Form->secure($this->Form->fields);
  971. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  972. $expected = array(
  973. 'div' => array('style' => 'display:none;'),
  974. array('input' => array(
  975. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  976. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  977. )),
  978. array('input' => array(
  979. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  980. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  981. )),
  982. '/div'
  983. );
  984. $this->assertTags($result, $expected);
  985. }
  986. /**
  987. * Test form security with Model.field.0 style inputs
  988. *
  989. * @return void
  990. */
  991. public function testFormSecurityArrayFields() {
  992. $key = 'testKey';
  993. $this->Form->request->params['_Token']['key'] = $key;
  994. $this->Form->create('Address');
  995. $this->Form->input('Address.primary.1');
  996. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  997. $this->Form->input('Address.secondary.1.0');
  998. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  999. }
  1000. /**
  1001. * testFormSecurityMultipleInputDisabledFields method
  1002. *
  1003. * test secure form generation with multiple records and disabled fields.
  1004. *
  1005. * @return void
  1006. */
  1007. public function testFormSecurityMultipleInputDisabledFields() {
  1008. $key = 'testKey';
  1009. $this->Form->request->params['_Token'] = array(
  1010. 'key' => $key,
  1011. 'unlockedFields' => array('first_name', 'address')
  1012. );
  1013. $this->Form->create();
  1014. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  1015. $this->Form->input('Addresses.0.title');
  1016. $this->Form->input('Addresses.0.first_name');
  1017. $this->Form->input('Addresses.0.last_name');
  1018. $this->Form->input('Addresses.0.address');
  1019. $this->Form->input('Addresses.0.city');
  1020. $this->Form->input('Addresses.0.phone');
  1021. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  1022. $this->Form->input('Addresses.1.title');
  1023. $this->Form->input('Addresses.1.first_name');
  1024. $this->Form->input('Addresses.1.last_name');
  1025. $this->Form->input('Addresses.1.address');
  1026. $this->Form->input('Addresses.1.city');
  1027. $this->Form->input('Addresses.1.phone');
  1028. $result = $this->Form->secure($this->Form->fields);
  1029. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  1030. $expected = array(
  1031. 'div' => array('style' => 'display:none;'),
  1032. array('input' => array(
  1033. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1034. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1035. )),
  1036. array('input' => array(
  1037. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1038. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1039. )),
  1040. '/div'
  1041. );
  1042. $this->assertTags($result, $expected);
  1043. }
  1044. /**
  1045. * testFormSecurityInputDisabledFields method
  1046. *
  1047. * Test single record form with disabled fields.
  1048. *
  1049. * @return void
  1050. */
  1051. public function testFormSecurityInputUnlockedFields() {
  1052. $key = 'testKey';
  1053. $this->Form->request['_Token'] = array(
  1054. 'key' => $key,
  1055. 'unlockedFields' => array('first_name', 'address')
  1056. );
  1057. $this->Form->create();
  1058. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  1059. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1060. $this->Form->input('Addresses.title');
  1061. $this->Form->input('Addresses.first_name');
  1062. $this->Form->input('Addresses.last_name');
  1063. $this->Form->input('Addresses.address');
  1064. $this->Form->input('Addresses.city');
  1065. $this->Form->input('Addresses.phone');
  1066. $result = $this->Form->fields;
  1067. $expected = array(
  1068. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  1069. 'Addresses.city', 'Addresses.phone'
  1070. );
  1071. $this->assertEquals($expected, $result);
  1072. $result = $this->Form->secure($expected);
  1073. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  1074. $expected = array(
  1075. 'div' => array('style' => 'display:none;'),
  1076. array('input' => array(
  1077. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1078. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1079. )),
  1080. array('input' => array(
  1081. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1082. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  1083. )),
  1084. '/div'
  1085. );
  1086. $this->assertTags($result, $expected);
  1087. }
  1088. /**
  1089. * test securing inputs with custom name attributes.
  1090. *
  1091. * @return void
  1092. */
  1093. public function testFormSecureWithCustomNameAttribute() {
  1094. $this->Form->request->params['_Token']['key'] = 'testKey';
  1095. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1096. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1097. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1098. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1099. }
  1100. /**
  1101. * testFormSecuredInput method
  1102. *
  1103. * Test generation of entire secure form, assertions made on input() output.
  1104. *
  1105. * @return void
  1106. */
  1107. public function testFormSecuredInput() {
  1108. $this->Form->request['_Token'] = array('key' => 'testKey');
  1109. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1110. $encoding = strtolower(Configure::read('App.encoding'));
  1111. $expected = array(
  1112. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1113. 'div' => array('style' => 'display:none;'),
  1114. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1115. array('input' => array(
  1116. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1117. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1118. )),
  1119. '/div'
  1120. );
  1121. $this->assertTags($result, $expected);
  1122. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1123. $expected = array(
  1124. 'div' => array('class' => 'input text'),
  1125. 'label' => array('for' => 'UserFormPublished'),
  1126. 'Published',
  1127. '/label',
  1128. array('input' => array(
  1129. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1130. 'id' => 'UserFormPublished'
  1131. )),
  1132. '/div'
  1133. );
  1134. $this->assertTags($result, $expected);
  1135. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1136. $expected = array(
  1137. 'div' => array('class' => 'input text'),
  1138. 'label' => array('for' => 'UserFormOther'),
  1139. 'Other',
  1140. '/label',
  1141. array('input' => array(
  1142. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1143. 'id' => 'UserFormOther'
  1144. )),
  1145. '/div'
  1146. );
  1147. $this->assertTags($result, $expected);
  1148. $result = $this->Form->hidden('UserForm.stuff');
  1149. $expected = array('input' => array(
  1150. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1151. 'id' => 'UserFormStuff'
  1152. ));
  1153. $this->assertTags($result, $expected);
  1154. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1155. $expected = array('input' => array(
  1156. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1157. 'value' => '0', 'id' => 'UserFormHidden'
  1158. ));
  1159. $this->assertTags($result, $expected);
  1160. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1161. $expected = array(
  1162. 'div' => array('class' => 'input checkbox'),
  1163. array('input' => array(
  1164. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1165. 'value' => '0', 'id' => 'UserFormSomething_'
  1166. )),
  1167. array('input' => array(
  1168. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1169. 'value' => '1', 'id' => 'UserFormSomething'
  1170. )),
  1171. 'label' => array('for' => 'UserFormSomething'),
  1172. 'Something',
  1173. '/label',
  1174. '/div'
  1175. );
  1176. $this->assertTags($result, $expected);
  1177. $result = $this->Form->fields;
  1178. $expected = array(
  1179. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1180. 'UserForm.hidden' => '0', 'UserForm.something'
  1181. );
  1182. $this->assertEquals($expected, $result);
  1183. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1184. $result = $this->Form->secure($this->Form->fields);
  1185. $expected = array(
  1186. 'div' => array('style' => 'display:none;'),
  1187. array('input' => array(
  1188. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1189. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1190. )),
  1191. array('input' => array(
  1192. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1193. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1194. )),
  1195. '/div'
  1196. );
  1197. $this->assertTags($result, $expected);
  1198. }
  1199. /**
  1200. * Tests that the correct keys are added to the field hash index
  1201. *
  1202. * @return void
  1203. */
  1204. public function testFormSecuredFileInput() {
  1205. $this->Form->request['_Token'] = array('key' => 'testKey');
  1206. $this->assertEquals(array(), $this->Form->fields);
  1207. $result = $this->Form->file('Attachment.file');
  1208. $expected = array(
  1209. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1210. 'Attachment.file.error', 'Attachment.file.size'
  1211. );
  1212. $this->assertEquals($expected, $this->Form->fields);
  1213. }
  1214. /**
  1215. * test that multiple selects keys are added to field hash
  1216. *
  1217. * @return void
  1218. */
  1219. public function testFormSecuredMultipleSelect() {
  1220. $this->Form->request['_Token'] = array('key' => 'testKey');
  1221. $this->assertEquals(array(), $this->Form->fields);
  1222. $options = array('1' => 'one', '2' => 'two');
  1223. $this->Form->select('Model.select', $options);
  1224. $expected = array('Model.select');
  1225. $this->assertEquals($expected, $this->Form->fields);
  1226. $this->Form->fields = array();
  1227. $this->Form->select('Model.select', $options, array('multiple' => true));
  1228. $this->assertEquals($expected, $this->Form->fields);
  1229. }
  1230. /**
  1231. * testFormSecuredRadio method
  1232. *
  1233. * @return void
  1234. */
  1235. public function testFormSecuredRadio() {
  1236. $this->Form->request['_Token'] = array('key' => 'testKey');
  1237. $this->assertEquals(array(), $this->Form->fields);
  1238. $options = array('1' => 'option1', '2' => 'option2');
  1239. $this->Form->radio('Test.test', $options);
  1240. $expected = array('Test.test');
  1241. $this->assertEquals($expected, $this->Form->fields);
  1242. }
  1243. /**
  1244. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1245. * hashing.
  1246. *
  1247. * @return void
  1248. */
  1249. public function testFormSecuredAndDisabled() {
  1250. $this->Form->request['_Token'] = array('key' => 'testKey');
  1251. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1252. $this->Form->text('Model.text', array('disabled' => true));
  1253. $this->Form->password('Model.text', array('disabled' => true));
  1254. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1255. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1256. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1257. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1258. $this->Form->month('Model.month', array('disabled' => true));
  1259. $this->Form->day('Model.day', array('disabled' => true));
  1260. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1261. $this->Form->minute('Model.minute', array('disabled' => true));
  1262. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1263. $expected = array(
  1264. 'Model.radio' => ''
  1265. );
  1266. $this->assertEquals($expected, $this->Form->fields);
  1267. }
  1268. /**
  1269. * testDisableSecurityUsingForm method
  1270. *
  1271. * @return void
  1272. */
  1273. public function testDisableSecurityUsingForm() {
  1274. $this->Form->request['_Token'] = array(
  1275. 'key' => 'testKey',
  1276. 'disabledFields' => array()
  1277. );
  1278. $this->Form->create();
  1279. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1280. $this->Form->input('Addresses.title');
  1281. $this->Form->input('Addresses.first_name', array('secure' => false));
  1282. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1283. $this->Form->input('Addresses.zip', array(
  1284. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1285. ));
  1286. $result = $this->Form->fields;
  1287. $expected = array(
  1288. 'Addresses.id' => '123456', 'Addresses.title',
  1289. );
  1290. $this->assertEquals($expected, $result);
  1291. }
  1292. /**
  1293. * test disableField
  1294. *
  1295. * @return void
  1296. */
  1297. public function testUnlockFieldAddsToList() {
  1298. $this->Form->request['_Token'] = array(
  1299. 'key' => 'testKey',
  1300. 'unlockedFields' => array()
  1301. );
  1302. $this->Form->create('Contact');
  1303. $this->Form->unlockField('Contact.name');
  1304. $this->Form->text('Contact.name');
  1305. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1306. $this->assertEquals(array(), $this->Form->fields);
  1307. }
  1308. /**
  1309. * test unlockField removing from fields array.
  1310. *
  1311. * @return void
  1312. */
  1313. public function testUnlockFieldRemovingFromFields() {
  1314. $this->Form->request['_Token'] = array(
  1315. 'key' => 'testKey',
  1316. 'unlockedFields' => array()
  1317. );
  1318. $this->Form->create('Contact');
  1319. $this->Form->hidden('Contact.id', array('value' => 1));
  1320. $this->Form->text('Contact.name');
  1321. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1322. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1323. $this->Form->unlockField('Contact.name');
  1324. $this->Form->unlockField('Contact.id');
  1325. $this->assertEquals(array(), $this->Form->fields);
  1326. }
  1327. /**
  1328. * testTagIsInvalid method
  1329. *
  1330. * @return void
  1331. */
  1332. public function testTagIsInvalid() {
  1333. $Contact = ClassRegistry::getObject('Contact');
  1334. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1335. $this->Form->setEntity('Contact.0.email');
  1336. $result = $this->Form->tagIsInvalid();
  1337. $expected = array('Please provide an email');
  1338. $this->assertEquals($expected, $result);
  1339. $this->Form->setEntity('Contact.1.email');
  1340. $result = $this->Form->tagIsInvalid();
  1341. $expected = false;
  1342. $this->assertSame($expected, $result);
  1343. $this->Form->setEntity('Contact.0.name');
  1344. $result = $this->Form->tagIsInvalid();
  1345. $expected = false;
  1346. $this->assertSame($expected, $result);
  1347. }
  1348. /**
  1349. * testPasswordValidation method
  1350. *
  1351. * test validation errors on password input.
  1352. *
  1353. * @return void
  1354. */
  1355. public function testPasswordValidation() {
  1356. $Contact = ClassRegistry::getObject('Contact');
  1357. $Contact->validationErrors['password'] = array('Please provide a password');
  1358. $result = $this->Form->input('Contact.password');
  1359. $expected = array(
  1360. 'div' => array('class' => 'input password error'),
  1361. 'label' => array('for' => 'ContactPassword'),
  1362. 'Password',
  1363. '/label',
  1364. 'input' => array(
  1365. 'type' => 'password', 'name' => 'data[Contact][password]',
  1366. 'id' => 'ContactPassword', 'class' => 'form-error'
  1367. ),
  1368. array('div' => array('class' => 'error-message')),
  1369. 'Please provide a password',
  1370. '/div',
  1371. '/div'
  1372. );
  1373. $this->assertTags($result, $expected);
  1374. }
  1375. /**
  1376. * testEmptyErrorValidation method
  1377. *
  1378. * test validation error div when validation message is an empty string
  1379. *
  1380. * @access public
  1381. * @return void
  1382. */
  1383. public function testEmptyErrorValidation() {
  1384. $this->Form->validationErrors['Contact']['password'] = '';
  1385. $result = $this->Form->input('Contact.password');
  1386. $expected = array(
  1387. 'div' => array('class' => 'input password error'),
  1388. 'label' => array('for' => 'ContactPassword'),
  1389. 'Password',
  1390. '/label',
  1391. 'input' => array(
  1392. 'type' => 'password', 'name' => 'data[Contact][password]',
  1393. 'id' => 'ContactPassword', 'class' => 'form-error'
  1394. ),
  1395. array('div' => array('class' => 'error-message')),
  1396. array(),
  1397. '/div',
  1398. '/div'
  1399. );
  1400. $this->assertTags($result, $expected);
  1401. }
  1402. /**
  1403. * testEmptyInputErrorValidation method
  1404. *
  1405. * test validation error div when validation message is overridden by an empty string when calling input()
  1406. *
  1407. * @access public
  1408. * @return void
  1409. */
  1410. public function testEmptyInputErrorValidation() {
  1411. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1412. $result = $this->Form->input('Contact.password', array('error' => ''));
  1413. $expected = array(
  1414. 'div' => array('class' => 'input password error'),
  1415. 'label' => array('for' => 'ContactPassword'),
  1416. 'Password',
  1417. '/label',
  1418. 'input' => array(
  1419. 'type' => 'password', 'name' => 'data[Contact][password]',
  1420. 'id' => 'ContactPassword', 'class' => 'form-error'
  1421. ),
  1422. array('div' => array('class' => 'error-message')),
  1423. array(),
  1424. '/div',
  1425. '/div'
  1426. );
  1427. $this->assertTags($result, $expected);
  1428. }
  1429. /**
  1430. * testFormValidationAssociated method
  1431. *
  1432. * test display of form errors in conjunction with model::validates.
  1433. *
  1434. * @return void
  1435. */
  1436. public function testFormValidationAssociated() {
  1437. $this->UserForm = ClassRegistry::getObject('UserForm');
  1438. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1439. $data = array(
  1440. 'UserForm' => array('name' => 'user'),
  1441. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1442. );
  1443. $result = $this->UserForm->OpenidUrl->create($data);
  1444. $this->assertFalse(empty($result));
  1445. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1446. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1447. $encoding = strtolower(Configure::read('App.encoding'));
  1448. $expected = array(
  1449. 'form' => array(
  1450. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1451. 'accept-charset' => $encoding
  1452. ),
  1453. 'div' => array('style' => 'display:none;'),
  1454. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1455. '/div'
  1456. );
  1457. $this->assertTags($result, $expected);
  1458. $result = $this->Form->error(
  1459. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1460. );
  1461. $this->assertEquals('Error, not registered', $result);
  1462. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1463. }
  1464. /**
  1465. * testFormValidationAssociatedFirstLevel method
  1466. *
  1467. * test form error display with associated model.
  1468. *
  1469. * @return void
  1470. */
  1471. public function testFormValidationAssociatedFirstLevel() {
  1472. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1473. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1474. $data = array(
  1475. 'ValidateUser' => array('name' => 'mariano'),
  1476. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1477. );
  1478. $result = $this->ValidateUser->create($data);
  1479. $this->assertFalse(empty($result));
  1480. $this->assertFalse($this->ValidateUser->validates());
  1481. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1482. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1483. $encoding = strtolower(Configure::read('App.encoding'));
  1484. $expected = array(
  1485. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1486. 'div' => array('style' => 'display:none;'),
  1487. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1488. '/div'
  1489. );
  1490. $this->assertTags($result, $expected);
  1491. $result = $this->Form->error(
  1492. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1493. );
  1494. $this->assertEquals('Invalid email', $result);
  1495. $result = $this->Form->error(
  1496. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1497. );
  1498. $this->assertEquals('Invalid name', $result);
  1499. $result = $this->Form->error(
  1500. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1501. );
  1502. $this->assertEquals('Invalid city', $result);
  1503. unset($this->ValidateUser->ValidateProfile);
  1504. unset($this->ValidateUser);
  1505. }
  1506. /**
  1507. * testFormValidationAssociatedSecondLevel method
  1508. *
  1509. * test form error display with associated model.
  1510. *
  1511. * @return void
  1512. */
  1513. public function testFormValidationAssociatedSecondLevel() {
  1514. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1515. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1516. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1517. $data = array(
  1518. 'ValidateUser' => array('name' => 'mariano'),
  1519. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1520. 'ValidateItem' => array('name' => 'Item')
  1521. );
  1522. $result = $this->ValidateUser->create($data);
  1523. $this->assertFalse(empty($result));
  1524. $this->assertFalse($this->ValidateUser->validates());
  1525. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1526. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1527. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1528. $encoding = strtolower(Configure::read('App.encoding'));
  1529. $expected = array(
  1530. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1531. 'div' => array('style' => 'display:none;'),
  1532. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1533. '/div'
  1534. );
  1535. $this->assertTags($result, $expected);
  1536. $result = $this->Form->error(
  1537. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1538. );
  1539. $this->assertEquals('Invalid email', $result);
  1540. $result = $this->Form->error(
  1541. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1542. );
  1543. $this->assertEquals('Invalid name', $result);
  1544. $result = $this->Form->error(
  1545. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1546. );
  1547. $result = $this->Form->error(
  1548. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1549. );
  1550. $this->assertEquals('Invalid description', $result);
  1551. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1552. unset($this->ValidateUser->ValidateProfile);
  1553. unset($this->ValidateUser);
  1554. }
  1555. /**
  1556. * testFormValidationMultiRecord method
  1557. *
  1558. * test form error display with multiple records.
  1559. *
  1560. * @return void
  1561. */
  1562. public function testFormValidationMultiRecord() {
  1563. $Contact = ClassRegistry::getObject('Contact');
  1564. $Contact->validationErrors[2] = array(
  1565. 'name' => array('This field cannot be left blank')
  1566. );
  1567. $result = $this->Form->input('Contact.2.name');
  1568. $expected = array(
  1569. 'div' => array('class' => 'input text error'),
  1570. 'label' => array('for' => 'Contact2Name'),
  1571. 'Name',
  1572. '/label',
  1573. 'input' => array(
  1574. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1575. 'class' => 'form-error', 'maxlength' => 255
  1576. ),
  1577. array('div' => array('class' => 'error-message')),
  1578. 'This field cannot be left blank',
  1579. '/div',
  1580. '/div'
  1581. );
  1582. $this->assertTags($result, $expected);
  1583. }
  1584. /**
  1585. * testMultipleInputValidation method
  1586. *
  1587. * test multiple record form validation error display.
  1588. *
  1589. * @return void
  1590. */
  1591. public function testMultipleInputValidation() {
  1592. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1593. $Address->validationErrors[0] = array(
  1594. 'title' => array('This field cannot be empty'),
  1595. 'first_name' => array('This field cannot be empty')
  1596. );
  1597. $Address->validationErrors[1] = array(
  1598. 'last_name' => array('You must have a last name')
  1599. );
  1600. $this->Form->create();
  1601. $result = $this->Form->input('Address.0.title');
  1602. $expected = array(
  1603. 'div' => array('class'),
  1604. 'label' => array('for'),
  1605. 'preg:/[^<]+/',
  1606. '/label',
  1607. 'input' => array(
  1608. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1609. ),
  1610. array('div' => array('class' => 'error-message')),
  1611. 'This field cannot be empty',
  1612. '/div',
  1613. '/div'
  1614. );
  1615. $this->assertTags($result, $expected);
  1616. $result = $this->Form->input('Address.0.first_name');
  1617. $expected = array(
  1618. 'div' => array('class'),
  1619. 'label' => array('for'),
  1620. 'preg:/[^<]+/',
  1621. '/label',
  1622. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1623. array('div' => array('class' => 'error-message')),
  1624. 'This field cannot be empty',
  1625. '/div',
  1626. '/div'
  1627. );
  1628. $this->assertTags($result, $expected);
  1629. $result = $this->Form->input('Address.0.last_name');
  1630. $expected = array(
  1631. 'div' => array('class'),
  1632. 'label' => array('for'),
  1633. 'preg:/[^<]+/',
  1634. '/label',
  1635. 'input' => array('type' => 'text', 'name', 'id'),
  1636. '/div'
  1637. );
  1638. $this->assertTags($result, $expected);
  1639. $result = $this->Form->input('Address.1.last_name');
  1640. $expected = array(
  1641. 'div' => array('class'),
  1642. 'label' => array('for'),
  1643. 'preg:/[^<]+/',
  1644. '/label',
  1645. 'input' => array(
  1646. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1647. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1648. ),
  1649. array('div' => array('class' => 'error-message')),
  1650. 'You must have a last name',
  1651. '/div',
  1652. '/div'
  1653. );
  1654. $this->assertTags($result, $expected);
  1655. }
  1656. /**
  1657. * testInput method
  1658. *
  1659. * Test various incarnations of input().
  1660. *
  1661. * @return void
  1662. */
  1663. public function testInput() {
  1664. $result = $this->Form->input('ValidateUser.balance');
  1665. $expected = array(
  1666. 'div' => array('class'),
  1667. 'label' => array('for'),
  1668. 'Balance',
  1669. '/label',
  1670. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1671. '/div',
  1672. );
  1673. $this->assertTags($result, $expected);
  1674. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1675. $expected = array(
  1676. 'div' => array('class' => 'input text'),
  1677. 'label' => array('for' => 'custom'),
  1678. 'Email',
  1679. '/label',
  1680. array('input' => array(
  1681. 'type' => 'text', 'name' => 'data[Contact][email]',
  1682. 'id' => 'custom', 'maxlength' => 255
  1683. )),
  1684. '/div'
  1685. );
  1686. $this->assertTags($result, $expected);
  1687. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1688. $expected = array(
  1689. '<div',
  1690. 'label' => array('for' => 'ContactEmail'),
  1691. 'Email',
  1692. '/label',
  1693. array('input' => array(
  1694. 'type' => 'text', 'name' => 'data[Contact][email]',
  1695. 'id' => 'ContactEmail', 'maxlength' => 255
  1696. )),
  1697. '/div'
  1698. );
  1699. $this->assertTags($result, $expected);
  1700. $result = $this->Form->hidden('Contact.idontexist');
  1701. $expected = array('input' => array(
  1702. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1703. 'id' => 'ContactIdontexist'
  1704. ));
  1705. $this->assertTags($result, $expected);
  1706. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1707. $expected = array(
  1708. 'div' => array('class' => 'input text'),
  1709. 'label' => array('for' => 'ContactEmail'),
  1710. 'Email',
  1711. '/label',
  1712. array('input' => array(
  1713. 'type' => 'text', 'name' => 'data[Contact][email]',
  1714. 'id' => 'ContactEmail'
  1715. )),
  1716. '/div'
  1717. );
  1718. $this->assertTags($result, $expected);
  1719. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1720. $expected = array(
  1721. 'div' => array('class' => 'input text'),
  1722. 'label' => array('for' => 'Contact5Email'),
  1723. 'Email',
  1724. '/label',
  1725. array('input' => array(
  1726. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1727. 'id' => 'Contact5Email'
  1728. )),
  1729. '/div'
  1730. );
  1731. $this->assertTags($result, $expected);
  1732. $result = $this->Form->input('Contact.password');
  1733. $expected = array(
  1734. 'div' => array('class' => 'input password'),
  1735. 'label' => array('for' => 'ContactPassword'),
  1736. 'Password',
  1737. '/label',
  1738. array('input' => array(
  1739. 'type' => 'password', 'name' => 'data[Contact][password]',
  1740. 'id' => 'ContactPassword'
  1741. )),
  1742. '/div'
  1743. );
  1744. $this->assertTags($result, $expected);
  1745. $result = $this->Form->input('Contact.email', array(
  1746. 'type' => 'file', 'class' => 'textbox'
  1747. ));
  1748. $expected = array(
  1749. 'div' => array('class' => 'input file'),
  1750. 'label' => array('for' => 'ContactEmail'),
  1751. 'Email',
  1752. '/label',
  1753. array('input' => array(
  1754. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1755. 'id' => 'ContactEmail'
  1756. )),
  1757. '/div'
  1758. );
  1759. $this->assertTags($result, $expected);
  1760. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1761. $result = $this->Form->input('Contact.phone');
  1762. $expected = array(
  1763. 'div' => array('class' => 'input text'),
  1764. 'label' => array('for' => 'ContactPhone'),
  1765. 'Phone',
  1766. '/label',
  1767. array('input' => array(
  1768. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1769. 'value' => 'Hello &amp; World &gt; weird chars',
  1770. 'id' => 'ContactPhone', 'maxlength' => 255
  1771. )),
  1772. '/div'
  1773. );
  1774. $this->assertTags($result, $expected);
  1775. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1776. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1777. $expected = array(
  1778. 'div' => array('class' => 'input text'),
  1779. 'label' => array('for' => 'myId'),
  1780. 'Field',
  1781. '/label',
  1782. 'input' => array(
  1783. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1784. 'value' => 'My value', 'id' => 'myId'
  1785. ),
  1786. '/div'
  1787. );
  1788. $this->assertTags($result, $expected);
  1789. unset($this->Form->request->data);
  1790. $Contact = ClassRegistry::getObject('Contact');
  1791. $Contact->validationErrors['field'] = array('Badness!');
  1792. $result = $this->Form->input('Contact.field');
  1793. $expected = array(
  1794. 'div' => array('class' => 'input text error'),
  1795. 'label' => array('for' => 'ContactField'),
  1796. 'Field',
  1797. '/label',
  1798. 'input' => array(
  1799. 'type' => 'text', 'name' => 'data[Contact][field]',
  1800. 'id' => 'ContactField', 'class' => 'form-error'
  1801. ),
  1802. array('div' => array('class' => 'error-message')),
  1803. 'Badness!',
  1804. '/div',
  1805. '/div'
  1806. );
  1807. $this->assertTags($result, $expected);
  1808. $result = $this->Form->input('Contact.field', array(
  1809. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1810. ));
  1811. $expected = array(
  1812. 'label' => array('for' => 'ContactField'),
  1813. 'Field',
  1814. '/label',
  1815. 'input' => array(
  1816. 'type' => 'text', 'name' => 'data[Contact][field]',
  1817. 'id' => 'ContactField', 'class' => 'form-error'
  1818. ),
  1819. array('span' => array('class' => 'error-message')),
  1820. 'Badness!',
  1821. '/span'
  1822. );
  1823. $this->assertTags($result, $expected);
  1824. $result = $this->Form->input('Contact.field', array(
  1825. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1826. ));
  1827. $expected = array(
  1828. 'div' => array('class' => 'input text error'),
  1829. 'label' => array('for' => 'ContactField'),
  1830. 'Field',
  1831. '/label',
  1832. 'input' => array(
  1833. 'type' => 'text', 'name' => 'data[Contact][field]',
  1834. 'id' => 'ContactField', 'class' => 'form-error'
  1835. ),
  1836. array('div' => array('class' => 'error')),
  1837. 'Badness!',
  1838. '/div'
  1839. );
  1840. $this->assertTags($result, $expected);
  1841. $result = $this->Form->input('Contact.field', array(
  1842. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1843. ));
  1844. $expected = array(
  1845. 'span' => array('class' => 'input text error'),
  1846. 'label' => array('for' => 'ContactField'),
  1847. 'Field',
  1848. '/label',
  1849. 'input' => array(
  1850. 'type' => 'text', 'name' => 'data[Contact][field]',
  1851. 'id' => 'ContactField', 'class' => 'form-error'
  1852. ),
  1853. 'Badness!',
  1854. '/span'
  1855. );
  1856. $this->assertTags($result, $expected);
  1857. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1858. $expected = array(
  1859. 'div' => array('class' => 'input text error'),
  1860. 'label' => array('for' => 'ContactField'),
  1861. 'Field',
  1862. '/label',
  1863. 'input' => array(
  1864. 'type' => 'text', 'name' => 'data[Contact][field]',
  1865. 'id' => 'ContactField', 'class' => 'form-error'
  1866. ),
  1867. 'A message to you, Rudy',
  1868. array('div' => array('class' => 'error-message')),
  1869. 'Badness!',
  1870. '/div',
  1871. '/div'
  1872. );
  1873. $this->assertTags($result, $expected);
  1874. $this->Form->setEntity(null);
  1875. $this->Form->setEntity('Contact.field');
  1876. $result = $this->Form->input('Contact.field', array(
  1877. 'after' => 'A message to you, Rudy', 'error' => false
  1878. ));
  1879. $expected = array(
  1880. 'div' => array('class' => 'input text'),
  1881. 'label' => array('for' => 'ContactField'),
  1882. 'Field',
  1883. '/label',
  1884. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1885. 'A message to you, Rudy',
  1886. '/div'
  1887. );
  1888. $this->assertTags($result, $expected);
  1889. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1890. $expected = array(
  1891. 'div' => array('class' => 'input text'),
  1892. 'label' => array('for' => 'ObjectField'),
  1893. 'Field',
  1894. '/label',
  1895. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1896. 'A message to you, Rudy',
  1897. '/div'
  1898. );
  1899. $this->assertTags($result, $expected);
  1900. $Contact->validationErrors['field'] = array('minLength');
  1901. $result = $this->Form->input('Contact.field', array(
  1902. 'error' => array(
  1903. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1904. 'maxLength' => 'login too large'
  1905. )
  1906. ));
  1907. $expected = array(
  1908. 'div' => array('class' => 'input text error'),
  1909. 'label' => array('for' => 'ContactField'),
  1910. 'Field',
  1911. '/label',
  1912. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1913. array('div' => array('class' => 'error-message')),
  1914. 'Le login doit contenir au moins 2 caractères',
  1915. '/div',
  1916. '/div'
  1917. );
  1918. $this->assertTags($result, $expected);
  1919. $Contact->validationErrors['field'] = array('maxLength');
  1920. $result = $this->Form->input('Contact.field', array(
  1921. 'error' => array(
  1922. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1923. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1924. 'maxLength' => 'login too large',
  1925. )
  1926. ));
  1927. $expected = array(
  1928. 'div' => array('class' => 'input text error'),
  1929. 'label' => array('for' => 'ContactField'),
  1930. 'Field',
  1931. '/label',
  1932. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1933. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1934. 'login too large',
  1935. '/span',
  1936. '/div'
  1937. );
  1938. $this->assertTags($result, $expected);
  1939. }
  1940. /**
  1941. * test input() with checkbox creation
  1942. *
  1943. * @return void
  1944. */
  1945. public function testInputCheckbox() {
  1946. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1947. $expected = array(
  1948. 'div' => array('class' => 'input checkbox'),
  1949. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1950. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1951. '/div'
  1952. );
  1953. $this->assertTags($result, $expected);
  1954. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1955. $expected = array(
  1956. 'div' => array('class' => 'input checkbox'),
  1957. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1958. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1959. '/div'
  1960. );
  1961. $this->assertTags($result, $expected);
  1962. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1963. $expected = array(
  1964. 'div' => array('class' => 'input checkbox'),
  1965. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1966. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1967. '/div'
  1968. );
  1969. $this->assertTags($result, $expected);
  1970. }
  1971. /**
  1972. * test form->input() with time types.
  1973. *
  1974. */
  1975. public function testInputTime() {
  1976. extract($this->dateRegex);
  1977. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1978. $result = explode(':', $result);
  1979. $this->assertRegExp('/option value="23"/', $result[0]);
  1980. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1981. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1982. $result = explode(':', $result);
  1983. $this->assertRegExp('/option value="23"/', $result[0]);
  1984. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1985. $result = $this->Form->input('Model.field', array(
  1986. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1987. ));
  1988. $result = explode(':', $result);
  1989. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1990. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1991. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1992. $result = $this->Form->input('Model.field', array(
  1993. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1994. ));
  1995. $result = explode(':', $result);
  1996. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1997. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1998. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1999. $result = $this->Form->input('prueba', array(
  2000. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  2001. 'maxYear' => date('Y') + 1 ,'interval' => 15
  2002. ));
  2003. $result = explode(':', $result);
  2004. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2005. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2006. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2007. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2008. $result = $this->Form->input('Random.start_time', array(
  2009. 'type' => 'time',
  2010. 'selected' => '18:15'
  2011. ));
  2012. $this->assertRegExp('#<option value="06"[^>]*>6</option>#', $result);
  2013. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result);
  2014. $this->assertRegExp('#<option value="pm"[^>]*>pm</option>#', $result);
  2015. }
  2016. /**
  2017. * test form->input() with datetime, date and time types
  2018. *
  2019. * @return void
  2020. */
  2021. public function testInputDatetime() {
  2022. extract($this->dateRegex);
  2023. $result = $this->Form->input('prueba', array(
  2024. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2025. 'maxYear' => date('Y') + 1, 'interval' => 15
  2026. ));
  2027. $result = explode(':', $result);
  2028. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2029. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2030. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2031. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2032. //related to ticket #5013
  2033. $result = $this->Form->input('Contact.date', array(
  2034. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2035. ));
  2036. $this->assertRegExp('/class="customClass"/', $result);
  2037. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2038. $result = $this->Form->input('Contact.date', array(
  2039. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2040. ));
  2041. $this->assertRegExp('/id="customIdDay"/', $result);
  2042. $this->assertRegExp('/id="customIdMonth"/', $result);
  2043. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2044. $result = $this->Form->input('Model.field', array(
  2045. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2046. ));
  2047. $this->assertRegExp('/id="customIDDay"/', $result);
  2048. $this->assertRegExp('/id="customIDHour"/', $result);
  2049. $result = explode('</select><select', $result);
  2050. $result = explode(':', $result[1]);
  2051. $this->assertRegExp('/option value="23"/', $result[0]);
  2052. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2053. $result = $this->Form->input('Model.field', array(
  2054. 'type' => 'datetime', 'timeFormat' => 12
  2055. ));
  2056. $result = explode('</select><select', $result);
  2057. $result = explode(':', $result[1]);
  2058. $this->assertRegExp('/option value="12"/', $result[0]);
  2059. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2060. $this->Form->request->data = array('Contact' => array('created' => null));
  2061. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2062. $expected = array(
  2063. 'div' => array('class' => 'input date'),
  2064. 'label' => array('for' => 'ContactCreatedMonth'),
  2065. 'Created',
  2066. '/label',
  2067. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2068. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2069. $monthsRegex,
  2070. '/select', '-',
  2071. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2072. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2073. $daysRegex,
  2074. '/select', '-',
  2075. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2076. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2077. $yearsRegex,
  2078. '/select',
  2079. '/div'
  2080. );
  2081. $this->assertTags($result, $expected);
  2082. $this->Form->request->data = array('Contact' => array('created' => null));
  2083. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2084. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2085. $this->Form->request->data = array('Contact' => array('created' => null));
  2086. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2087. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2088. $result = $this->Form->input('Contact.created', array(
  2089. 'type' => 'date',
  2090. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2091. 'timeFormat' => 'NONE'
  2092. ));
  2093. $this->assertRegExp('/for\="created-month"/', $result);
  2094. }
  2095. /**
  2096. * Test generating checkboxes in a loop.
  2097. *
  2098. * @return void
  2099. */
  2100. public function testInputCheckboxesInLoop() {
  2101. for ($i = 1; $i < 5; $i++) {
  2102. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2103. $expected = array(
  2104. 'div' => array('class' => 'input checkbox'),
  2105. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2106. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2107. 'label' => array('for' => "Contact{$i}Email"),
  2108. 'Email',
  2109. '/label',
  2110. '/div'
  2111. );
  2112. $this->assertTags($result, $expected);
  2113. }
  2114. }
  2115. /**
  2116. * Test generating checkboxes with disabled elements.
  2117. *
  2118. * @return void
  2119. */
  2120. public function testInputCheckboxWithDisabledElements() {
  2121. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2122. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2123. $expected = array(
  2124. array('div' => array('class' => 'input select')),
  2125. array('label' => array('for' => "ContactMultiple")),
  2126. 'Multiple',
  2127. '/label',
  2128. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2129. array('div' => array('class' => 'checkbox')),
  2130. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2131. array('label' => array('for' => "ContactMultiple1")),
  2132. 'One',
  2133. '/label',
  2134. '/div',
  2135. array('div' => array('class' => 'checkbox')),
  2136. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2137. array('label' => array('for' => "ContactMultiple2")),
  2138. 'Two',
  2139. '/label',
  2140. '/div',
  2141. array('div' => array('class' => 'checkbox')),
  2142. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2143. array('label' => array('for' => "ContactMultiple3")),
  2144. 'Three',
  2145. '/label',
  2146. '/div',
  2147. '/div'
  2148. );
  2149. $this->assertTags($result, $expected);
  2150. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2151. $this->assertTags($result, $expected);
  2152. $disabled = array('2', 3);
  2153. $expected = array(
  2154. array('div' => array('class' => 'input select')),
  2155. array('label' => array('for' => "ContactMultiple")),
  2156. 'Multiple',
  2157. '/label',
  2158. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2159. array('div' => array('class' => 'checkbox')),
  2160. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2161. array('label' => array('for' => "ContactMultiple1")),
  2162. 'One',
  2163. '/label',
  2164. '/div',
  2165. array('div' => array('class' => 'checkbox')),
  2166. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2167. array('label' => array('for' => "ContactMultiple2")),
  2168. 'Two',
  2169. '/label',
  2170. '/div',
  2171. array('div' => array('class' => 'checkbox')),
  2172. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2173. array('label' => array('for' => "ContactMultiple3")),
  2174. 'Three',
  2175. '/label',
  2176. '/div',
  2177. '/div'
  2178. );
  2179. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2180. $this->assertTags($result, $expected);
  2181. }
  2182. /**
  2183. * test input name with leading integer, ensure attributes are generated correctly.
  2184. *
  2185. * @return void
  2186. */
  2187. public function testInputWithLeadingInteger() {
  2188. $result = $this->Form->text('0.Node.title');
  2189. $expected = array(
  2190. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2191. );
  2192. $this->assertTags($result, $expected);
  2193. }
  2194. /**
  2195. * test form->input() with select type inputs.
  2196. *
  2197. * @return void
  2198. */
  2199. public function testInputSelectType() {
  2200. $result = $this->Form->input('email', array(
  2201. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2202. );
  2203. $expected = array(
  2204. 'div' => array('class' => 'input select'),
  2205. 'label' => array('for' => 'email'),
  2206. 'Email',
  2207. '/label',
  2208. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2209. array('option' => array('value' => '')),
  2210. '/option',
  2211. array('option' => array('value' => 'è')),
  2212. 'Firést',
  2213. '/option',
  2214. array('option' => array('value' => 'é')),
  2215. 'Secoènd',
  2216. '/option',
  2217. '/select',
  2218. '/div'
  2219. );
  2220. $this->assertTags($result, $expected);
  2221. $result = $this->Form->input('email', array(
  2222. 'options' => array('First', 'Second'), 'empty' => true)
  2223. );
  2224. $expected = array(
  2225. 'div' => array('class' => 'input select'),
  2226. 'label' => array('for' => 'email'),
  2227. 'Email',
  2228. '/label',
  2229. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2230. array('option' => array('value' => '')),
  2231. '/option',
  2232. array('option' => array('value' => '0')),
  2233. 'First',
  2234. '/option',
  2235. array('option' => array('value' => '1')),
  2236. 'Second',
  2237. '/option',
  2238. '/select',
  2239. '/div'
  2240. );
  2241. $this->assertTags($result, $expected);
  2242. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2243. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2244. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2245. $expected = array(
  2246. 'div' => array('class' => 'input select'),
  2247. 'label' => array('for' => 'ModelUserId'),
  2248. 'User',
  2249. '/label',
  2250. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2251. array('option' => array('value' => '')),
  2252. '/option',
  2253. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2254. 'good',
  2255. '/option',
  2256. array('option' => array('value' => 'other')),
  2257. 'bad',
  2258. '/option',
  2259. '/select',
  2260. '/div'
  2261. );
  2262. $this->assertTags($result, $expected);
  2263. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2264. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2265. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2266. $expected = array(
  2267. 'div' => array('class' => 'input select'),
  2268. 'label' => array('for' => 'ThingUserId'),
  2269. 'User',
  2270. '/label',
  2271. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2272. array('option' => array('value' => '')),
  2273. 'Some Empty',
  2274. '/option',
  2275. array('option' => array('value' => 'value')),
  2276. 'good',
  2277. '/option',
  2278. array('option' => array('value' => 'other')),
  2279. 'bad',
  2280. '/option',
  2281. '/select',
  2282. '/div'
  2283. );
  2284. $this->assertTags($result, $expected);
  2285. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2286. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2287. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2288. $expected = array(
  2289. 'div' => array('class' => 'input select'),
  2290. 'label' => array('for' => 'ThingUserId'),
  2291. 'User',
  2292. '/label',
  2293. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2294. array('option' => array('value' => '')),
  2295. 'Some Empty',
  2296. '/option',
  2297. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2298. 'good',
  2299. '/option',
  2300. array('option' => array('value' => 'other')),
  2301. 'bad',
  2302. '/option',
  2303. '/select',
  2304. '/div'
  2305. );
  2306. $this->assertTags($result, $expected);
  2307. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2308. $this->Form->request->data = array('User' => array('User' => array('value')));
  2309. $result = $this->Form->input('User.User', array('empty' => true));
  2310. $expected = array(
  2311. 'div' => array('class' => 'input select'),
  2312. 'label' => array('for' => 'UserUser'),
  2313. 'User',
  2314. '/label',
  2315. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2316. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2317. array('option' => array('value' => '')),
  2318. '/option',
  2319. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2320. 'good',
  2321. '/option',
  2322. array('option' => array('value' => 'other')),
  2323. 'bad',
  2324. '/option',
  2325. '/select',
  2326. '/div'
  2327. );
  2328. $this->assertTags($result, $expected);
  2329. $this->Form->data = array();
  2330. $result = $this->Form->input('Publisher.id', array(
  2331. 'label' => 'Publisher',
  2332. 'type' => 'select',
  2333. 'multiple' => 'checkbox',
  2334. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2335. ));
  2336. $expected = array(
  2337. array('div' => array('class' => 'input select')),
  2338. array('label' => array('for' => 'PublisherId')),
  2339. 'Publisher',
  2340. '/label',
  2341. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2342. array('div' => array('class' => 'checkbox')),
  2343. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2344. array('label' => array('for' => 'PublisherIdValue1')),
  2345. 'Label 1',
  2346. '/label',
  2347. '/div',
  2348. array('div' => array('class' => 'checkbox')),
  2349. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2350. array('label' => array('for' => 'PublisherIdValue2')),
  2351. 'Label 2',
  2352. '/label',
  2353. '/div',
  2354. '/div'
  2355. );
  2356. $this->assertTags($result, $expected);
  2357. }
  2358. /**
  2359. * test that input() and a non standard primary key makes a hidden input by default.
  2360. *
  2361. * @return void
  2362. */
  2363. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2364. $this->Form->create('User');
  2365. $this->Form->fieldset = array(
  2366. 'User' => array(
  2367. 'fields' => array(
  2368. 'model_id' => array('type' => 'integer')
  2369. ),
  2370. 'validates' => array(),
  2371. 'key' => 'model_id'
  2372. )
  2373. );
  2374. $result = $this->Form->input('model_id');
  2375. $expected = array(
  2376. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2377. );
  2378. $this->assertTags($result, $expected);
  2379. }
  2380. /**
  2381. * test that overriding the magic select type widget is possible
  2382. *
  2383. * @return void
  2384. */
  2385. public function testInputOverridingMagicSelectType() {
  2386. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2387. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2388. $expected = array(
  2389. 'div' => array('class' => 'input text'),
  2390. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2391. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2392. '/div'
  2393. );
  2394. $this->assertTags($result, $expected);
  2395. //Check that magic types still work for plural/singular vars
  2396. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2397. $result = $this->Form->input('Model.type');
  2398. $expected = array(
  2399. 'div' => array('class' => 'input select'),
  2400. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2401. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2402. array('option' => array('value' => 'value')), 'good', '/option',
  2403. array('option' => array('value' => 'other')), 'bad', '/option',
  2404. '/select',
  2405. '/div'
  2406. );
  2407. $this->assertTags($result, $expected);
  2408. }
  2409. /**
  2410. * Test that magic input() selects can easily be converted into radio types without error.
  2411. *
  2412. * @return void
  2413. */
  2414. public function testInputMagicSelectChangeToRadio() {
  2415. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2416. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2417. $this->assertRegExp('/input type="radio"/', $result);
  2418. }
  2419. /**
  2420. * fields with the same name as the model should work.
  2421. *
  2422. * @return void
  2423. */
  2424. public function testInputWithMatchingFieldAndModelName() {
  2425. $this->Form->create('User');
  2426. $this->Form->fieldset = array(
  2427. 'User' => array(
  2428. 'fields' => array(
  2429. 'User' => array('type' => 'text')
  2430. ),
  2431. 'validates' => array(),
  2432. 'key' => 'id'
  2433. )
  2434. );
  2435. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2436. $result = $this->Form->input('User', array('type' => 'text'));
  2437. $expected = array(
  2438. 'div' => array('class' => 'input text'),
  2439. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2440. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2441. '/div'
  2442. );
  2443. $this->assertTags($result, $expected);
  2444. }
  2445. /**
  2446. * testFormInputs method
  2447. *
  2448. * test correct results from form::inputs().
  2449. *
  2450. * @return void
  2451. */
  2452. public function testFormInputs() {
  2453. $this->Form->create('Contact');
  2454. $result = $this->Form->inputs('The Legend');
  2455. $expected = array(
  2456. '<fieldset',
  2457. '<legend',
  2458. 'The Legend',
  2459. '/legend',
  2460. '*/fieldset',
  2461. );
  2462. $this->assertTags($result, $expected);
  2463. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2464. $expected = array(
  2465. 'fieldset' => array('class' => 'classy-stuff'),
  2466. '<legend',
  2467. 'Field of Dreams',
  2468. '/legend',
  2469. '*/fieldset'
  2470. );
  2471. $this->assertTags($result, $expected);
  2472. $this->Form->create('Contact');
  2473. $this->Form->request['prefix'] = 'admin';
  2474. $this->Form->request['action'] = 'admin_edit';
  2475. $result = $this->Form->inputs();
  2476. $expected = array(
  2477. '<fieldset',
  2478. '<legend',
  2479. 'Edit Contact',
  2480. '/legend',
  2481. '*/fieldset',
  2482. );
  2483. $this->assertTags($result, $expected);
  2484. $this->Form->create('Contact');
  2485. $result = $this->Form->inputs(false);
  2486. $expected = array(
  2487. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2488. array('div' => array('class' => 'input text')),
  2489. '*/div',
  2490. array('div' => array('class' => 'input text')),
  2491. '*/div',
  2492. array('div' => array('class' => 'input text')),
  2493. '*/div',
  2494. array('div' => array('class' => 'input password')),
  2495. '*/div',
  2496. array('div' => array('class' => 'input date')),
  2497. '*/div',
  2498. array('div' => array('class' => 'input date')),
  2499. '*/div',
  2500. array('div' => array('class' => 'input datetime')),
  2501. '*/div',
  2502. array('div' => array('class' => 'input number')),
  2503. '*/div',
  2504. array('div' => array('class' => 'input select')),
  2505. '*/div',
  2506. );
  2507. $this->assertTags($result, $expected);
  2508. $this->Form->create('Contact');
  2509. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2510. $expected = array(
  2511. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2512. array('div' => array('class' => 'input text')),
  2513. '*/div',
  2514. array('div' => array('class' => 'input text')),
  2515. '*/div',
  2516. array('div' => array('class' => 'input text')),
  2517. '*/div',
  2518. array('div' => array('class' => 'input password')),
  2519. '*/div',
  2520. array('div' => array('class' => 'input date')),
  2521. '*/div',
  2522. array('div' => array('class' => 'input date')),
  2523. '*/div',
  2524. array('div' => array('class' => 'input datetime')),
  2525. '*/div',
  2526. array('div' => array('class' => 'input number')),
  2527. '*/div',
  2528. array('div' => array('class' => 'input select')),
  2529. '*/div',
  2530. );
  2531. $this->assertTags($result, $expected);
  2532. $this->Form->create('Contact');
  2533. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2534. $expected = array(
  2535. 'fieldset' => array(),
  2536. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2537. array('div' => array('class' => 'input text')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input text')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input text')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input password')),
  2544. '*/div',
  2545. array('div' => array('class' => 'input date')),
  2546. '*/div',
  2547. array('div' => array('class' => 'input date')),
  2548. '*/div',
  2549. array('div' => array('class' => 'input datetime')),
  2550. '*/div',
  2551. array('div' => array('class' => 'input number')),
  2552. '*/div',
  2553. array('div' => array('class' => 'input select')),
  2554. '*/div',
  2555. '/fieldset'
  2556. );
  2557. $this->assertTags($result, $expected);
  2558. $this->Form->create('Contact');
  2559. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2560. $expected = array(
  2561. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2562. array('div' => array('class' => 'input text')),
  2563. '*/div',
  2564. array('div' => array('class' => 'input text')),
  2565. '*/div',
  2566. array('div' => array('class' => 'input text')),
  2567. '*/div',
  2568. array('div' => array('class' => 'input password')),
  2569. '*/div',
  2570. array('div' => array('class' => 'input date')),
  2571. '*/div',
  2572. array('div' => array('class' => 'input date')),
  2573. '*/div',
  2574. array('div' => array('class' => 'input datetime')),
  2575. '*/div',
  2576. array('div' => array('class' => 'input number')),
  2577. '*/div',
  2578. array('div' => array('class' => 'input select')),
  2579. '*/div',
  2580. );
  2581. $this->assertTags($result, $expected);
  2582. $this->Form->create('Contact');
  2583. $result = $this->Form->inputs('Hello');
  2584. $expected = array(
  2585. 'fieldset' => array(),
  2586. 'legend' => array(),
  2587. 'Hello',
  2588. '/legend',
  2589. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2590. array('div' => array('class' => 'input text')),
  2591. '*/div',
  2592. array('div' => array('class' => 'input text')),
  2593. '*/div',
  2594. array('div' => array('class' => 'input text')),
  2595. '*/div',
  2596. array('div' => array('class' => 'input password')),
  2597. '*/div',
  2598. array('div' => array('class' => 'input date')),
  2599. '*/div',
  2600. array('div' => array('class' => 'input date')),
  2601. '*/div',
  2602. array('div' => array('class' => 'input datetime')),
  2603. '*/div',
  2604. array('div' => array('class' => 'input number')),
  2605. '*/div',
  2606. array('div' => array('class' => 'input select')),
  2607. '*/div',
  2608. '/fieldset'
  2609. );
  2610. $this->assertTags($result, $expected);
  2611. $this->Form->create('Contact');
  2612. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2613. $expected = array(
  2614. 'fieldset' => array(),
  2615. 'legend' => array(),
  2616. 'Hello',
  2617. '/legend',
  2618. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2619. array('div' => array('class' => 'input text')),
  2620. '*/div',
  2621. array('div' => array('class' => 'input text')),
  2622. '*/div',
  2623. array('div' => array('class' => 'input text')),
  2624. '*/div',
  2625. array('div' => array('class' => 'input password')),
  2626. '*/div',
  2627. array('div' => array('class' => 'input date')),
  2628. '*/div',
  2629. array('div' => array('class' => 'input date')),
  2630. '*/div',
  2631. array('div' => array('class' => 'input datetime')),
  2632. '*/div',
  2633. array('div' => array('class' => 'input number')),
  2634. '*/div',
  2635. array('div' => array('class' => 'input select')),
  2636. '*/div',
  2637. '/fieldset'
  2638. );
  2639. $this->assertTags($result, $expected);
  2640. }
  2641. /**
  2642. * testSelectAsCheckbox method
  2643. *
  2644. * test multi-select widget with checkbox formatting.
  2645. *
  2646. * @return void
  2647. */
  2648. public function testSelectAsCheckbox() {
  2649. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2650. $expected = array(
  2651. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2652. array('div' => array('class' => 'checkbox')),
  2653. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2654. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2655. 'first',
  2656. '/label',
  2657. '/div',
  2658. array('div' => array('class' => 'checkbox')),
  2659. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2660. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2661. 'second',
  2662. '/label',
  2663. '/div',
  2664. array('div' => array('class' => 'checkbox')),
  2665. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2666. array('label' => array('for' => 'ModelMultiField2')),
  2667. 'third',
  2668. '/label',
  2669. '/div',
  2670. );
  2671. $this->assertTags($result, $expected);
  2672. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2673. $expected = array(
  2674. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2675. array('div' => array('class' => 'checkbox')),
  2676. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2677. array('label' => array('for' => 'ModelMultiField12')),
  2678. 'half',
  2679. '/label',
  2680. '/div',
  2681. );
  2682. $this->assertTags($result, $expected);
  2683. }
  2684. /**
  2685. * testLabel method
  2686. *
  2687. * test label generation.
  2688. *
  2689. * @return void
  2690. */
  2691. public function testLabel() {
  2692. $this->Form->text('Person.name');
  2693. $result = $this->Form->label();
  2694. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2695. $this->Form->text('Person.name');
  2696. $result = $this->Form->label();
  2697. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2698. $result = $this->Form->label('Person.first_name');
  2699. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2700. $result = $this->Form->label('Person.first_name', 'Your first name');
  2701. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2702. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2703. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2704. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2705. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2706. $result = $this->Form->label('Person.first_name', '');
  2707. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2708. $result = $this->Form->label('Person.2.name', '');
  2709. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2710. }
  2711. /**
  2712. * testTextbox method
  2713. *
  2714. * test textbox element generation
  2715. *
  2716. * @return void
  2717. */
  2718. public function testTextbox() {
  2719. $result = $this->Form->text('Model.field');
  2720. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2721. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2722. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2723. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2724. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2725. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2726. $result = $this->Form->text('Model.text');
  2727. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2728. $Contact = ClassRegistry::getObject('Contact');
  2729. $Contact->validationErrors['text'] = array(true);
  2730. $this->Form->request->data['Contact']['text'] = 'test';
  2731. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2732. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2733. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2734. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2735. $expected = array(
  2736. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2737. );
  2738. $this->assertTags($result, $expected);
  2739. }
  2740. /**
  2741. * testDefaultValue method
  2742. *
  2743. * Test default value setting
  2744. *
  2745. * @return void
  2746. */
  2747. public function testDefaultValue() {
  2748. $this->Form->request->data['Model']['field'] = 'test';
  2749. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2750. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2751. unset($this->Form->request->data['Model']['field']);
  2752. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2753. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2754. }
  2755. /**
  2756. * testCheckboxDefaultValue method
  2757. *
  2758. * Test default value setting on checkbox() method
  2759. *
  2760. * @return void
  2761. */
  2762. public function testCheckboxDefaultValue() {
  2763. $this->Form->request->data['Model']['field'] = false;
  2764. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2765. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2766. unset($this->Form->request->data['Model']['field']);
  2767. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2768. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2769. $this->Form->request->data['Model']['field'] = true;
  2770. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2771. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2772. unset($this->Form->request->data['Model']['field']);
  2773. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2774. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2775. }
  2776. /**
  2777. * testError method
  2778. *
  2779. * Test field error generation
  2780. *
  2781. * @return void
  2782. */
  2783. public function testError() {
  2784. $Contact = ClassRegistry::getObject('Contact');
  2785. $Contact->validationErrors['field'] = array(1);
  2786. $result = $this->Form->error('Contact.field');
  2787. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2788. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2789. $this->assertEquals('Error in field Field', $result);
  2790. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2791. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2792. $this->assertEquals('This field contains invalid input', $result);
  2793. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2794. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2795. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2796. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2797. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2798. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2799. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2800. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2801. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2802. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2803. $this->assertEquals('<strong>Badness!</strong>', $result);
  2804. $Contact->validationErrors['field'] = array("email");
  2805. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2806. $expected = array(
  2807. 'div' => array('class' => 'field-error'),
  2808. 'No good!',
  2809. '/div'
  2810. );
  2811. $this->assertTags($result, $expected);
  2812. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2813. $result = $this->Form->error('Contact.field', array(
  2814. 'notEmpty' => 'Cannot be empty',
  2815. 'email' => 'No good!'
  2816. ));
  2817. $expected = array(
  2818. 'div' => array('class' => 'error-message'),
  2819. 'ul' => array(),
  2820. '<li', 'Cannot be empty', '/li',
  2821. '<li', 'No good!', '/li',
  2822. '<li', 'Something else', '/li',
  2823. '/ul',
  2824. '/div'
  2825. );
  2826. $this->assertTags($result, $expected);
  2827. /** Testing error messages list options **/
  2828. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2829. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2830. $expected = array(
  2831. 'div' => array('class' => 'error-message'),
  2832. 'ol' => array(),
  2833. '<li', 'notEmpty', '/li',
  2834. '<li', 'email', '/li',
  2835. '/ol',
  2836. '/div'
  2837. );
  2838. $this->assertTags($result, $expected);
  2839. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2840. $expected = array(
  2841. 'div' => array('class' => 'error-message'),
  2842. 'ol' => array(),
  2843. '<li', 'notEmpty', '/li',
  2844. '<li', 'email', '/li',
  2845. '/ol',
  2846. '/div'
  2847. );
  2848. $this->assertTags($result, $expected);
  2849. $result = $this->Form->error('Contact.field', null, array(
  2850. 'listOptions' => array(
  2851. 'class' => 'ul-class',
  2852. 'itemOptions' => array(
  2853. 'class' => 'li-class'
  2854. )
  2855. )
  2856. ));
  2857. $expected = array(
  2858. 'div' => array('class' => 'error-message'),
  2859. 'ul' => array('class' => 'ul-class'),
  2860. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2861. array('li' => array('class' => 'li-class')), 'email', '/li',
  2862. '/ul',
  2863. '/div'
  2864. );
  2865. $this->assertTags($result, $expected);
  2866. }
  2867. /**
  2868. * test error options when using form->input();
  2869. *
  2870. * @return void
  2871. */
  2872. public function testInputErrorEscape() {
  2873. $this->Form->create('ValidateProfile');
  2874. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2875. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2876. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2877. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2878. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2879. $this->assertRegExp('/required<br>/', $result);
  2880. }
  2881. /**
  2882. * testPassword method
  2883. *
  2884. * Test password element generation
  2885. *
  2886. * @return void
  2887. */
  2888. public function testPassword() {
  2889. $Contact = ClassRegistry::getObject('Contact');
  2890. $result = $this->Form->password('Contact.field');
  2891. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2892. $Contact->validationErrors['passwd'] = 1;
  2893. $this->Form->request->data['Contact']['passwd'] = 'test';
  2894. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2895. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2896. }
  2897. /**
  2898. * testRadio method
  2899. *
  2900. * Test radio element set generation
  2901. *
  2902. * @return void
  2903. */
  2904. public function testRadio() {
  2905. $result = $this->Form->radio('Model.field', array('option A'));
  2906. $expected = array(
  2907. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2908. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2909. 'label' => array('for' => 'ModelField0'),
  2910. 'option A',
  2911. '/label'
  2912. );
  2913. $this->assertTags($result, $expected);
  2914. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2915. $expected = array(
  2916. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2917. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2918. 'label' => array('for' => 'ModelField12'),
  2919. 'half',
  2920. '/label'
  2921. );
  2922. $this->assertTags($result, $expected);
  2923. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2924. $expected = array(
  2925. 'fieldset' => array(),
  2926. 'legend' => array(),
  2927. 'Field',
  2928. '/legend',
  2929. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2930. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2931. array('label' => array('for' => 'ModelField0')),
  2932. 'option A',
  2933. '/label',
  2934. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2935. array('label' => array('for' => 'ModelField1')),
  2936. 'option B',
  2937. '/label',
  2938. '/fieldset'
  2939. );
  2940. $this->assertTags($result, $expected);
  2941. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  2942. $expected = array(
  2943. 'fieldset' => array(),
  2944. 'legend' => array(),
  2945. 'Field',
  2946. '/legend',
  2947. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2948. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2949. array('label' => array('for' => 'ModelField0')),
  2950. 'option A',
  2951. '/label',
  2952. 'br' => array(),
  2953. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2954. array('label' => array('for' => 'ModelField1')),
  2955. 'option B',
  2956. '/label',
  2957. '/fieldset'
  2958. );
  2959. $this->assertTags($result, $expected);
  2960. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  2961. $expected = array(
  2962. 'fieldset' => array(),
  2963. 'legend' => array(),
  2964. 'Field',
  2965. '/legend',
  2966. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'checked' => 'checked')),
  2967. array('label' => array('for' => 'ModelField1')),
  2968. 'Yes',
  2969. '/label',
  2970. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2971. array('label' => array('for' => 'ModelField0')),
  2972. 'No',
  2973. '/label',
  2974. '/fieldset'
  2975. );
  2976. $this->assertTags($result, $expected);
  2977. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  2978. $expected = array(
  2979. 'fieldset' => array(),
  2980. 'legend' => array(),
  2981. 'Field',
  2982. '/legend',
  2983. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  2984. array('label' => array('for' => 'ModelField1')),
  2985. 'Yes',
  2986. '/label',
  2987. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  2988. array('label' => array('for' => 'ModelField0')),
  2989. 'No',
  2990. '/label',
  2991. '/fieldset'
  2992. );
  2993. $this->assertTags($result, $expected);
  2994. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  2995. $expected = array(
  2996. 'fieldset' => array(),
  2997. 'legend' => array(),
  2998. 'Field',
  2999. '/legend',
  3000. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3001. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3002. array('label' => array('for' => 'ModelField1')),
  3003. 'Yes',
  3004. '/label',
  3005. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3006. array('label' => array('for' => 'ModelField0')),
  3007. 'No',
  3008. '/label',
  3009. '/fieldset'
  3010. );
  3011. $this->assertTags($result, $expected);
  3012. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3013. $expected = array(
  3014. 'fieldset' => array(),
  3015. 'legend' => array(),
  3016. 'Field',
  3017. '/legend',
  3018. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3019. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3020. array('label' => array('for' => 'ModelField1')),
  3021. 'Yes',
  3022. '/label',
  3023. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3024. array('label' => array('for' => 'ModelField0')),
  3025. 'No',
  3026. '/label',
  3027. '/fieldset'
  3028. );
  3029. $this->assertTags($result, $expected);
  3030. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3031. $expected = array(
  3032. 'div' => array('class' => 'input radio'),
  3033. 'fieldset' => array(),
  3034. 'legend' => array(),
  3035. 'Legend title',
  3036. '/legend',
  3037. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3038. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3039. array('label' => array('for' => 'NewsletterSubscribe0')),
  3040. 'Unsubscribe',
  3041. '/label',
  3042. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3043. array('label' => array('for' => 'NewsletterSubscribe1')),
  3044. 'Subscribe',
  3045. '/label',
  3046. '/fieldset',
  3047. '/div'
  3048. );
  3049. $this->assertTags($result, $expected);
  3050. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3051. $expected = array(
  3052. 'div' => array('class' => 'input radio'),
  3053. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3054. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3055. array('label' => array('for' => 'NewsletterSubscribe0')),
  3056. 'Unsubscribe',
  3057. '/label',
  3058. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3059. array('label' => array('for' => 'NewsletterSubscribe1')),
  3060. 'Subscribe',
  3061. '/label',
  3062. '/div'
  3063. );
  3064. $this->assertTags($result, $expected);
  3065. $result = $this->Form->input('Newsletter.subscribe', array('legend' => 'Legend title', 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3066. $expected = array(
  3067. 'div' => array('class' => 'input radio'),
  3068. 'fieldset' => array(),
  3069. 'legend' => array(),
  3070. 'Legend title',
  3071. '/legend',
  3072. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3073. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3074. 'Unsubscribe',
  3075. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3076. 'Subscribe',
  3077. '/fieldset',
  3078. '/div'
  3079. );
  3080. $this->assertTags($result, $expected);
  3081. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3082. $expected = array(
  3083. 'div' => array('class' => 'input radio'),
  3084. 'input' => array('type' => 'hidden', 'name' => 'data[Newsletter][subscribe]', 'value' => '', 'id' => 'NewsletterSubscribe_'),
  3085. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3086. 'Unsubscribe',
  3087. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1')),
  3088. 'Subscribe',
  3089. '/div'
  3090. );
  3091. $this->assertTags($result, $expected);
  3092. $result = $this->Form->input('Newsletter.subscribe', array('legend' => false, 'label' => false, 'type' => 'radio', 'value' => '1', 'options' => array('0' => 'Unsubscribe', '1' => 'Subscribe')));
  3093. $expected = array(
  3094. 'div' => array('class' => 'input radio'),
  3095. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '0', 'id' => 'NewsletterSubscribe0')),
  3096. 'Unsubscribe',
  3097. array('input' => array('type' => 'radio', 'name' => 'data[Newsletter][subscribe]', 'value' => '1', 'id' => 'NewsletterSubscribe1', 'checked' => 'checked')),
  3098. 'Subscribe',
  3099. '/div'
  3100. );
  3101. $this->assertTags($result, $expected);
  3102. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3103. $expected = array(
  3104. 'fieldset' => array(),
  3105. 'legend' => array(),
  3106. 'Gender',
  3107. '/legend',
  3108. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3109. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3110. array('label' => array('for' => 'EmployeeGenderMale')),
  3111. 'Male',
  3112. '/label',
  3113. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3114. array('label' => array('for' => 'EmployeeGenderFemale')),
  3115. 'Female',
  3116. '/label',
  3117. '/fieldset',
  3118. );
  3119. $this->assertTags($result, $expected);
  3120. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3121. $expected = array(
  3122. 'fieldset' => array(),
  3123. 'legend' => array(),
  3124. 'Gender',
  3125. '/legend',
  3126. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3127. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3128. array('label' => array('for' => 'OfficerGenderMale')),
  3129. 'Male',
  3130. '/label',
  3131. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3132. array('label' => array('for' => 'OfficerGenderFemale')),
  3133. 'Female',
  3134. '/label',
  3135. '/fieldset',
  3136. );
  3137. $this->assertTags($result, $expected);
  3138. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3139. $expected = array(
  3140. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3141. array('input' => array('type' => 'radio', 'name' => 'data[Contact][1][imrequired]', 'value' => '0', 'id' => 'Contact1Imrequired0')),
  3142. 'label' => array('for' => 'Contact1Imrequired0'),
  3143. 'option A',
  3144. '/label'
  3145. );
  3146. $this->assertTags($result, $expected);
  3147. $result = $this->Form->radio('Model.1.field', array('option A'));
  3148. $expected = array(
  3149. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3150. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3151. 'label' => array('for' => 'Model1Field0'),
  3152. 'option A',
  3153. '/label'
  3154. );
  3155. $this->assertTags($result, $expected);
  3156. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3157. $expected = array(
  3158. 'fieldset' => array(),
  3159. 'legend' => array(),
  3160. 'Field',
  3161. '/legend',
  3162. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3163. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3164. array('label' => array('for' => 'ModelField0')),
  3165. 'option A',
  3166. '/label',
  3167. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3168. array('label' => array('for' => 'ModelField1')),
  3169. 'option B',
  3170. '/label',
  3171. '/fieldset'
  3172. );
  3173. $this->assertTags($result, $expected);
  3174. $result = $this->Form->radio(
  3175. 'Model.field',
  3176. array('option A', 'option B'),
  3177. array('between' => 'I am between')
  3178. );
  3179. $expected = array(
  3180. 'fieldset' => array(),
  3181. 'legend' => array(),
  3182. 'Field',
  3183. '/legend',
  3184. 'I am between',
  3185. 'input' => array(
  3186. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3187. 'value' => '', 'id' => 'ModelField_'
  3188. ),
  3189. array('input' => array(
  3190. 'type' => 'radio', 'name' => 'data[Model][field]',
  3191. 'value' => '0', 'id' => 'ModelField0'
  3192. )),
  3193. array('label' => array('for' => 'ModelField0')),
  3194. 'option A',
  3195. '/label',
  3196. array('input' => array(
  3197. 'type' => 'radio', 'name' => 'data[Model][field]',
  3198. 'value' => '1', 'id' => 'ModelField1'
  3199. )),
  3200. array('label' => array('for' => 'ModelField1')),
  3201. 'option B',
  3202. '/label',
  3203. '/fieldset'
  3204. );
  3205. $this->assertTags($result, $expected);
  3206. }
  3207. /**
  3208. * test disabled radio options
  3209. *
  3210. * @return void
  3211. */
  3212. public function testRadioDisabled() {
  3213. $result = $this->Form->radio(
  3214. 'Model.field',
  3215. array('option A', 'option B'),
  3216. array('disabled' => array('option A'), 'value' => 'option A')
  3217. );
  3218. $expected = array(
  3219. 'fieldset' => array(),
  3220. 'legend' => array(),
  3221. 'Field',
  3222. '/legend',
  3223. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3224. array('label' => array('for' => 'ModelField0')),
  3225. 'option A',
  3226. '/label',
  3227. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3228. array('label' => array('for' => 'ModelField1')),
  3229. 'option B',
  3230. '/label',
  3231. '/fieldset'
  3232. );
  3233. $this->assertTags($result, $expected);
  3234. $result = $this->Form->radio(
  3235. 'Model.field',
  3236. array('option A', 'option B'),
  3237. array('disabled' => true, 'value' => 'option A')
  3238. );
  3239. $expected = array(
  3240. 'fieldset' => array(),
  3241. 'legend' => array(),
  3242. 'Field',
  3243. '/legend',
  3244. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3245. array('label' => array('for' => 'ModelField0')),
  3246. 'option A',
  3247. '/label',
  3248. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3249. array('label' => array('for' => 'ModelField1')),
  3250. 'option B',
  3251. '/label',
  3252. '/fieldset'
  3253. );
  3254. $this->assertTags($result, $expected);
  3255. $result = $this->Form->radio(
  3256. 'Model.field',
  3257. array('option A', 'option B'),
  3258. array('disabled' => 'disabled', 'value' => 'option A')
  3259. );
  3260. $expected = array(
  3261. 'fieldset' => array(),
  3262. 'legend' => array(),
  3263. 'Field',
  3264. '/legend',
  3265. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3266. array('label' => array('for' => 'ModelField0')),
  3267. 'option A',
  3268. '/label',
  3269. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3270. array('label' => array('for' => 'ModelField1')),
  3271. 'option B',
  3272. '/label',
  3273. '/fieldset'
  3274. );
  3275. $this->assertTags($result, $expected);
  3276. }
  3277. /**
  3278. * test disabling the hidden input for radio buttons
  3279. *
  3280. * @return void
  3281. */
  3282. public function testRadioHiddenInputDisabling() {
  3283. $result = $this->Form->input('Model.1.field', array(
  3284. 'type' => 'radio',
  3285. 'options' => array('option A'),
  3286. 'hiddenField' => false
  3287. )
  3288. );
  3289. $expected = array(
  3290. 'div' => array('class' => 'input radio'),
  3291. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3292. 'label' => array('for' => 'Model1Field0'),
  3293. 'option A',
  3294. '/label',
  3295. '/div'
  3296. );
  3297. $this->assertTags($result, $expected);
  3298. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3299. $expected = array(
  3300. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3301. 'label' => array('for' => 'Model1Field0'),
  3302. 'option A',
  3303. '/label'
  3304. );
  3305. $this->assertTags($result, $expected);
  3306. }
  3307. /**
  3308. * test adding an empty option for radio buttons
  3309. *
  3310. * @return void
  3311. */
  3312. public function testRadioAddEmptyOption() {
  3313. $result = $this->Form->input('Model.1.field', array(
  3314. 'type' => 'radio',
  3315. 'options' => array('option A'),
  3316. 'empty' => true,
  3317. 'hiddenField' => false
  3318. ));
  3319. $expected = array(
  3320. 'div' => array('class' => 'input radio'),
  3321. 'fieldset' => array(),
  3322. 'legend' => array(),
  3323. 'Field',
  3324. '/legend',
  3325. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3326. array('label' => array('for' => 'Model1Field')),
  3327. __('empty'),
  3328. '/label',
  3329. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3330. array('label' => array('for' => 'Model1Field0')),
  3331. 'option A',
  3332. '/label',
  3333. '/fieldset',
  3334. '/div'
  3335. );
  3336. $this->assertTags($result, $expected);
  3337. $result = $this->Form->input('Model.1.field', array(
  3338. 'type' => 'radio',
  3339. 'options' => array('option A'),
  3340. 'empty' => 'CustomEmptyLabel',
  3341. 'hiddenField' => false
  3342. ));
  3343. $expected = array(
  3344. 'div' => array('class' => 'input radio'),
  3345. 'fieldset' => array(),
  3346. 'legend' => array(),
  3347. 'Field',
  3348. '/legend',
  3349. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3350. array('label' => array('for' => 'Model1Field')),
  3351. 'CustomEmptyLabel',
  3352. '/label',
  3353. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3354. array('label' => array('for' => 'Model1Field0')),
  3355. 'option A',
  3356. '/label',
  3357. '/fieldset',
  3358. '/div'
  3359. );
  3360. $this->assertTags($result, $expected);
  3361. $result = $this->Form->input('Model.1.field', array(
  3362. 'type' => 'radio',
  3363. 'options' => array('option A'),
  3364. 'empty' => false,
  3365. 'hiddenField' => false
  3366. ));
  3367. $this->assertTextNotContains('"Model1Field"', $result);
  3368. }
  3369. /**
  3370. * testSelect method
  3371. *
  3372. * Test select element generation.
  3373. *
  3374. * @return void
  3375. */
  3376. public function testSelect() {
  3377. $result = $this->Form->select('Model.field', array());
  3378. $expected = array(
  3379. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3380. array('option' => array('value' => '')),
  3381. '/option',
  3382. '/select'
  3383. );
  3384. $this->assertTags($result, $expected);
  3385. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3386. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3387. $expected = array(
  3388. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3389. array('option' => array('value' => '')),
  3390. '/option',
  3391. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3392. 'good',
  3393. '/option',
  3394. array('option' => array('value' => 'other')),
  3395. 'bad',
  3396. '/option',
  3397. '/select'
  3398. );
  3399. $this->assertTags($result, $expected);
  3400. $this->Form->request->data = array();
  3401. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3402. $expected = array(
  3403. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3404. array('option' => array('value' => '')),
  3405. '/option',
  3406. array('option' => array('value' => 'value')),
  3407. 'good',
  3408. '/option',
  3409. array('option' => array('value' => 'other')),
  3410. 'bad',
  3411. '/option',
  3412. '/select'
  3413. );
  3414. $this->assertTags($result, $expected);
  3415. $result = $this->Form->select(
  3416. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3417. array('empty' => false)
  3418. );
  3419. $expected = array(
  3420. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3421. array('option' => array('value' => 'first')),
  3422. 'first &quot;html&quot; &lt;chars&gt;',
  3423. '/option',
  3424. array('option' => array('value' => 'second')),
  3425. 'value',
  3426. '/option',
  3427. '/select'
  3428. );
  3429. $this->assertTags($result, $expected);
  3430. $result = $this->Form->select(
  3431. 'Model.field',
  3432. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3433. array('escape' => false, 'empty' => false)
  3434. );
  3435. $expected = array(
  3436. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3437. array('option' => array('value' => 'first')),
  3438. 'first "html" <chars>',
  3439. '/option',
  3440. array('option' => array('value' => 'second')),
  3441. 'value',
  3442. '/option',
  3443. '/select'
  3444. );
  3445. $this->assertTags($result, $expected);
  3446. $options = array(
  3447. array('value' => 'first', 'name' => 'First'),
  3448. array('value' => 'first', 'name' => 'Another First'),
  3449. );
  3450. $result = $this->Form->select(
  3451. 'Model.field',
  3452. $options,
  3453. array('escape' => false, 'empty' => false)
  3454. );
  3455. $expected = array(
  3456. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3457. array('option' => array('value' => 'first')),
  3458. 'First',
  3459. '/option',
  3460. array('option' => array('value' => 'first')),
  3461. 'Another First',
  3462. '/option',
  3463. '/select'
  3464. );
  3465. $this->assertTags($result, $expected);
  3466. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3467. $result = $this->Form->select(
  3468. 'Model.contact_id',
  3469. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3470. array('escape' => false, 'empty' => 'pick something')
  3471. );
  3472. $expected = array(
  3473. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3474. array('option' => array('value' => '')), 'pick something', '/option',
  3475. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3476. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3477. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3478. '/select'
  3479. );
  3480. $this->assertTags($result, $expected);
  3481. $this->Form->request->data['Model']['field'] = 0;
  3482. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3483. $expected = array(
  3484. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3485. array('option' => array('value' => '')), '/option',
  3486. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3487. array('option' => array('value' => '1')), 'Yes', '/option',
  3488. '/select'
  3489. );
  3490. $this->assertTags($result, $expected);
  3491. }
  3492. /**
  3493. * test that select() with optiongroups listens to the escape param.
  3494. *
  3495. * @return void
  3496. */
  3497. public function testSelectOptionGroupEscaping() {
  3498. $options = array(
  3499. '>< Key' => array(
  3500. 1 => 'One',
  3501. 2 => 'Two'
  3502. )
  3503. );
  3504. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3505. $expected = array(
  3506. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3507. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3508. array('option' => array('value' => '1')), 'One', '/option',
  3509. array('option' => array('value' => '2')), 'Two', '/option',
  3510. '/optgroup',
  3511. '/select'
  3512. );
  3513. $this->assertTags($result, $expected);
  3514. $options = array(
  3515. '>< Key' => array(
  3516. 1 => 'One',
  3517. 2 => 'Two'
  3518. )
  3519. );
  3520. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3521. $expected = array(
  3522. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3523. 'optgroup' => array('label' => '>< Key'),
  3524. array('option' => array('value' => '1')), 'One', '/option',
  3525. array('option' => array('value' => '2')), 'Two', '/option',
  3526. '/optgroup',
  3527. '/select'
  3528. );
  3529. $this->assertTags($result, $expected);
  3530. }
  3531. /**
  3532. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3533. *
  3534. * @return void
  3535. */
  3536. public function testSelectWithNullAttributes() {
  3537. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3538. $expected = array(
  3539. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3540. array('option' => array('value' => '0')),
  3541. 'first',
  3542. '/option',
  3543. array('option' => array('value' => '1')),
  3544. 'second',
  3545. '/option',
  3546. '/select'
  3547. );
  3548. $this->assertTags($result, $expected);
  3549. }
  3550. /**
  3551. * testNestedSelect method
  3552. *
  3553. * test select element generation with optgroups
  3554. *
  3555. * @return void
  3556. */
  3557. public function testNestedSelect() {
  3558. $result = $this->Form->select(
  3559. 'Model.field',
  3560. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3561. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3562. )), array('empty' => false)
  3563. );
  3564. $expected = array(
  3565. 'select' => array('name' => 'data[Model][field]',
  3566. 'id' => 'ModelField'),
  3567. array('option' => array('value' => 1)),
  3568. 'One',
  3569. '/option',
  3570. array('option' => array('value' => 2)),
  3571. 'Two',
  3572. '/option',
  3573. array('optgroup' => array('label' => 'Three')),
  3574. array('option' => array('value' => 4)),
  3575. 'Four',
  3576. '/option',
  3577. array('option' => array('value' => 5)),
  3578. 'Five',
  3579. '/option',
  3580. '/optgroup',
  3581. '/select'
  3582. );
  3583. $this->assertTags($result, $expected);
  3584. $result = $this->Form->select(
  3585. 'Model.field',
  3586. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3587. array('showParents' => true, 'empty' => false)
  3588. );
  3589. $expected = array(
  3590. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3591. array('option' => array('value' => 1)),
  3592. 'One',
  3593. '/option',
  3594. array('option' => array('value' => 2)),
  3595. 'Two',
  3596. '/option',
  3597. array('optgroup' => array('label' => 'Three')),
  3598. array('option' => array('value' => 3)),
  3599. 'Three',
  3600. '/option',
  3601. array('option' => array('value' => 4)),
  3602. 'Four',
  3603. '/option',
  3604. '/optgroup',
  3605. '/select'
  3606. );
  3607. $this->assertTags($result, $expected);
  3608. }
  3609. /**
  3610. * testSelectMultiple method
  3611. *
  3612. * test generation of multiple select elements
  3613. *
  3614. * @return void
  3615. */
  3616. public function testSelectMultiple() {
  3617. $options = array('first', 'second', 'third');
  3618. $result = $this->Form->select(
  3619. 'Model.multi_field', $options, array('multiple' => true)
  3620. );
  3621. $expected = array(
  3622. 'input' => array(
  3623. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3624. ),
  3625. 'select' => array(
  3626. 'name' => 'data[Model][multi_field][]',
  3627. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3628. ),
  3629. array('option' => array('value' => '0')),
  3630. 'first',
  3631. '/option',
  3632. array('option' => array('value' => '1')),
  3633. 'second',
  3634. '/option',
  3635. array('option' => array('value' => '2')),
  3636. 'third',
  3637. '/option',
  3638. '/select'
  3639. );
  3640. $this->assertTags($result, $expected);
  3641. $result = $this->Form->select(
  3642. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3643. );
  3644. $expected = array(
  3645. 'input' => array(
  3646. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3647. ),
  3648. 'select' => array(
  3649. 'name' => 'data[Model][multi_field][]',
  3650. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3651. ),
  3652. array('option' => array('value' => '0')),
  3653. 'first',
  3654. '/option',
  3655. array('option' => array('value' => '1')),
  3656. 'second',
  3657. '/option',
  3658. array('option' => array('value' => '2')),
  3659. 'third',
  3660. '/option',
  3661. '/select'
  3662. );
  3663. $this->assertTags($result, $expected);
  3664. $result = $this->Form->select(
  3665. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3666. );
  3667. $expected = array(
  3668. 'input' => array(
  3669. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3670. ),
  3671. 'select' => array(
  3672. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3673. 'multiple' => 'multiple'
  3674. ),
  3675. array('option' => array('value' => '0', 'selected' => 'selected')),
  3676. 'first',
  3677. '/option',
  3678. array('option' => array('value' => '1', 'selected' => 'selected')),
  3679. 'second',
  3680. '/option',
  3681. array('option' => array('value' => '2')),
  3682. 'third',
  3683. '/option',
  3684. '/select'
  3685. );
  3686. $this->assertTags($result, $expected);
  3687. $result = $this->Form->select(
  3688. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3689. );
  3690. $expected = array(
  3691. 'select' => array(
  3692. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3693. ),
  3694. array('option' => array('value' => '0', 'selected' => 'selected')),
  3695. 'first',
  3696. '/option',
  3697. array('option' => array('value' => '1', 'selected' => 'selected')),
  3698. 'second',
  3699. '/option',
  3700. array('option' => array('value' => '2')),
  3701. 'third',
  3702. '/option',
  3703. '/select'
  3704. );
  3705. $this->assertTags($result, $expected);
  3706. }
  3707. /**
  3708. * test generation of habtm select boxes.
  3709. *
  3710. * @return void
  3711. */
  3712. public function testHabtmSelectBox() {
  3713. $this->View->viewVars['contactTags'] = array(
  3714. 1 => 'blue',
  3715. 2 => 'red',
  3716. 3 => 'green'
  3717. );
  3718. $this->Form->request->data = array(
  3719. 'Contact' => array(),
  3720. 'ContactTag' => array(
  3721. array(
  3722. 'id' => 1,
  3723. 'name' => 'blue'
  3724. ),
  3725. array(
  3726. 'id' => 3,
  3727. 'name' => 'green'
  3728. )
  3729. )
  3730. );
  3731. $this->Form->create('Contact');
  3732. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3733. $expected = array(
  3734. 'input' => array(
  3735. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3736. ),
  3737. 'select' => array(
  3738. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3739. 'multiple' => 'multiple'
  3740. ),
  3741. array('option' => array('value' => '1', 'selected' => 'selected')),
  3742. 'blue',
  3743. '/option',
  3744. array('option' => array('value' => '2')),
  3745. 'red',
  3746. '/option',
  3747. array('option' => array('value' => '3', 'selected' => 'selected')),
  3748. 'green',
  3749. '/option',
  3750. '/select'
  3751. );
  3752. $this->assertTags($result, $expected);
  3753. }
  3754. /**
  3755. * test generation of multi select elements in checkbox format
  3756. *
  3757. * @return void
  3758. */
  3759. public function testSelectMultipleCheckboxes() {
  3760. $result = $this->Form->select(
  3761. 'Model.multi_field',
  3762. array('first', 'second', 'third'),
  3763. array('multiple' => 'checkbox')
  3764. );
  3765. $expected = array(
  3766. 'input' => array(
  3767. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3768. ),
  3769. array('div' => array('class' => 'checkbox')),
  3770. array('input' => array(
  3771. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3772. 'value' => '0', 'id' => 'ModelMultiField0'
  3773. )),
  3774. array('label' => array('for' => 'ModelMultiField0')),
  3775. 'first',
  3776. '/label',
  3777. '/div',
  3778. array('div' => array('class' => 'checkbox')),
  3779. array('input' => array(
  3780. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3781. 'value' => '1', 'id' => 'ModelMultiField1'
  3782. )),
  3783. array('label' => array('for' => 'ModelMultiField1')),
  3784. 'second',
  3785. '/label',
  3786. '/div',
  3787. array('div' => array('class' => 'checkbox')),
  3788. array('input' => array(
  3789. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3790. 'value' => '2', 'id' => 'ModelMultiField2'
  3791. )),
  3792. array('label' => array('for' => 'ModelMultiField2')),
  3793. 'third',
  3794. '/label',
  3795. '/div'
  3796. );
  3797. $this->assertTags($result, $expected);
  3798. $result = $this->Form->select(
  3799. 'Model.multi_field',
  3800. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3801. array('multiple' => 'checkbox')
  3802. );
  3803. $expected = array(
  3804. 'input' => array(
  3805. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3806. ),
  3807. array('div' => array('class' => 'checkbox')),
  3808. array('input' => array(
  3809. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3810. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3811. )),
  3812. array('label' => array('for' => 'ModelMultiFieldA')),
  3813. 'first',
  3814. '/label',
  3815. '/div',
  3816. array('div' => array('class' => 'checkbox')),
  3817. array('input' => array(
  3818. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3819. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3820. )),
  3821. array('label' => array('for' => 'ModelMultiFieldB')),
  3822. 'second',
  3823. '/label',
  3824. '/div',
  3825. array('div' => array('class' => 'checkbox')),
  3826. array('input' => array(
  3827. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3828. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3829. )),
  3830. array('label' => array('for' => 'ModelMultiFieldC')),
  3831. 'third',
  3832. '/label',
  3833. '/div'
  3834. );
  3835. $this->assertTags($result, $expected);
  3836. $result = $this->Form->select(
  3837. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3838. );
  3839. $expected = array(
  3840. 'input' => array(
  3841. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3842. ),
  3843. array('div' => array('class' => 'checkbox')),
  3844. array('input' => array(
  3845. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3846. 'value' => '1', 'id' => 'ModelMultiField1'
  3847. )),
  3848. array('label' => array('for' => 'ModelMultiField1')),
  3849. 'first',
  3850. '/label',
  3851. '/div'
  3852. );
  3853. $this->assertTags($result, $expected);
  3854. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3855. $result = $this->Form->select(
  3856. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3857. );
  3858. $expected = array(
  3859. 'input' => array(
  3860. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3861. ),
  3862. array('div' => array('class' => 'checkbox')),
  3863. array('input' => array(
  3864. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3865. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3866. )),
  3867. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3868. 'first',
  3869. '/label',
  3870. '/div',
  3871. array('div' => array('class' => 'checkbox')),
  3872. array('input' => array(
  3873. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3874. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3875. )),
  3876. array('label' => array('for' => 'ModelTagsArray')),
  3877. 'Array',
  3878. '/label',
  3879. '/div'
  3880. );
  3881. $this->assertTags($result, $expected);
  3882. }
  3883. /**
  3884. * test multiple checkboxes with div styles.
  3885. *
  3886. * @return void
  3887. */
  3888. public function testSelectMultipleCheckboxDiv() {
  3889. $result = $this->Form->select(
  3890. 'Model.tags',
  3891. array('first', 'second'),
  3892. array('multiple' => 'checkbox', 'class' => 'my-class')
  3893. );
  3894. $expected = array(
  3895. 'input' => array(
  3896. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3897. ),
  3898. array('div' => array('class' => 'my-class')),
  3899. array('input' => array(
  3900. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3901. 'value' => '0', 'id' => 'ModelTags0'
  3902. )),
  3903. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  3904. '/div',
  3905. array('div' => array('class' => 'my-class')),
  3906. array('input' => array(
  3907. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3908. 'value' => '1', 'id' => 'ModelTags1'
  3909. )),
  3910. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  3911. '/div'
  3912. );
  3913. $this->assertTags($result, $expected);
  3914. $result = $this->Form->input('Model.tags', array(
  3915. 'options' => array('first', 'second'),
  3916. 'multiple' => 'checkbox',
  3917. 'class' => 'my-class',
  3918. 'div' => false,
  3919. 'label' => false
  3920. ));
  3921. $this->assertTags($result, $expected);
  3922. $Contact = ClassRegistry::getObject('Contact');
  3923. $Contact->validationErrors['tags'] = 'Select atleast one option';
  3924. $result = $this->Form->input('Contact.tags', array(
  3925. 'options' => array('one'),
  3926. 'multiple' => 'checkbox',
  3927. 'label' => false,
  3928. 'div' => false
  3929. ));
  3930. $expected = array(
  3931. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3932. array('div' => array('class' => 'checkbox form-error')),
  3933. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3934. array('label' => array('for' => 'ContactTags0')),
  3935. 'one',
  3936. '/label',
  3937. '/div'
  3938. );
  3939. $this->assertTags($result, $expected);
  3940. $result = $this->Form->input('Contact.tags', array(
  3941. 'options' => array('one'),
  3942. 'multiple' => 'checkbox',
  3943. 'class' => 'mycheckbox',
  3944. 'label' => false,
  3945. 'div' => false
  3946. ));
  3947. $expected = array(
  3948. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  3949. array('div' => array('class' => 'mycheckbox form-error')),
  3950. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  3951. array('label' => array('for' => 'ContactTags0')),
  3952. 'one',
  3953. '/label',
  3954. '/div'
  3955. );
  3956. $this->assertTags($result, $expected);
  3957. }
  3958. /**
  3959. * Checks the security hash array generated for multiple-input checkbox elements
  3960. *
  3961. * @return void
  3962. */
  3963. public function testSelectMultipleCheckboxSecurity() {
  3964. $this->Form->request['_Token'] = array('key' => 'testKey');
  3965. $this->assertEquals(array(), $this->Form->fields);
  3966. $result = $this->Form->select(
  3967. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  3968. array('multiple' => 'checkbox')
  3969. );
  3970. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  3971. $result = $this->Form->secure($this->Form->fields);
  3972. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  3973. $this->assertRegExp('/"' . $key . '"/', $result);
  3974. }
  3975. /**
  3976. * When a select box has no options it should not be added to the fields list
  3977. * as it always fail post validation.
  3978. *
  3979. * @return void
  3980. */
  3981. public function testSelectNoSecureWithNoOptions() {
  3982. $this->Form->request['_Token'] = array('key' => 'testkey');
  3983. $this->assertEquals(array(), $this->Form->fields);
  3984. $this->Form->select(
  3985. 'Model.select',
  3986. array()
  3987. );
  3988. $this->assertEquals(array(), $this->Form->fields);
  3989. $this->Form->select(
  3990. 'Model.select',
  3991. array(),
  3992. array('empty' => true)
  3993. );
  3994. $this->assertEquals(array('Model.select'), $this->Form->fields);
  3995. }
  3996. /**
  3997. * testInputMultipleCheckboxes method
  3998. *
  3999. * test input() resulting in multi select elements being generated.
  4000. *
  4001. * @return void
  4002. */
  4003. public function testInputMultipleCheckboxes() {
  4004. $result = $this->Form->input('Model.multi_field', array(
  4005. 'options' => array('first', 'second', 'third'),
  4006. 'multiple' => 'checkbox'
  4007. ));
  4008. $expected = array(
  4009. array('div' => array('class' => 'input select')),
  4010. array('label' => array('for' => 'ModelMultiField')),
  4011. 'Multi Field',
  4012. '/label',
  4013. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4014. array('div' => array('class' => 'checkbox')),
  4015. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4016. array('label' => array('for' => 'ModelMultiField0')),
  4017. 'first',
  4018. '/label',
  4019. '/div',
  4020. array('div' => array('class' => 'checkbox')),
  4021. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4022. array('label' => array('for' => 'ModelMultiField1')),
  4023. 'second',
  4024. '/label',
  4025. '/div',
  4026. array('div' => array('class' => 'checkbox')),
  4027. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4028. array('label' => array('for' => 'ModelMultiField2')),
  4029. 'third',
  4030. '/label',
  4031. '/div',
  4032. '/div'
  4033. );
  4034. $this->assertTags($result, $expected);
  4035. $result = $this->Form->input('Model.multi_field', array(
  4036. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4037. 'multiple' => 'checkbox'
  4038. ));
  4039. $expected = array(
  4040. array('div' => array('class' => 'input select')),
  4041. array('label' => array('for' => 'ModelMultiField')),
  4042. 'Multi Field',
  4043. '/label',
  4044. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4045. array('div' => array('class' => 'checkbox')),
  4046. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4047. array('label' => array('for' => 'ModelMultiFieldA')),
  4048. 'first',
  4049. '/label',
  4050. '/div',
  4051. array('div' => array('class' => 'checkbox')),
  4052. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4053. array('label' => array('for' => 'ModelMultiFieldB')),
  4054. 'second',
  4055. '/label',
  4056. '/div',
  4057. array('div' => array('class' => 'checkbox')),
  4058. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4059. array('label' => array('for' => 'ModelMultiFieldC')),
  4060. 'third',
  4061. '/label',
  4062. '/div',
  4063. '/div'
  4064. );
  4065. $this->assertTags($result, $expected);
  4066. $result = $this->Form->input('Model.multi_field', array(
  4067. 'options' => array('1' => 'first'),
  4068. 'multiple' => 'checkbox',
  4069. 'label' => false,
  4070. 'div' => false
  4071. ));
  4072. $expected = array(
  4073. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4074. array('div' => array('class' => 'checkbox')),
  4075. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4076. array('label' => array('for' => 'ModelMultiField1')),
  4077. 'first',
  4078. '/label',
  4079. '/div'
  4080. );
  4081. $this->assertTags($result, $expected);
  4082. $result = $this->Form->input('Model.multi_field', array(
  4083. 'options' => array('2' => 'second'),
  4084. 'multiple' => 'checkbox',
  4085. 'label' => false,
  4086. 'div' => false
  4087. ));
  4088. $expected = array(
  4089. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4090. array('div' => array('class' => 'checkbox')),
  4091. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4092. array('label' => array('for' => 'ModelMultiField2')),
  4093. 'second',
  4094. '/label',
  4095. '/div'
  4096. );
  4097. $this->assertTags($result, $expected);
  4098. }
  4099. /**
  4100. * testSelectHiddenFieldOmission method
  4101. *
  4102. * test that select() with 'hiddenField' => false omits the hidden field
  4103. *
  4104. * @return void
  4105. */
  4106. public function testSelectHiddenFieldOmission() {
  4107. $result = $this->Form->select('Model.multi_field',
  4108. array('first', 'second'),
  4109. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4110. );
  4111. $expected = array(
  4112. array('div' => array('class' => 'checkbox')),
  4113. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4114. array('label' => array('for' => 'ModelMultiField0')),
  4115. 'first',
  4116. '/label',
  4117. '/div',
  4118. array('div' => array('class' => 'checkbox')),
  4119. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4120. array('label' => array('for' => 'ModelMultiField1')),
  4121. 'second',
  4122. '/label',
  4123. '/div'
  4124. );
  4125. $this->assertTags($result, $expected);
  4126. $result = $this->Form->input('Model.multi_field', array(
  4127. 'options' => array('first', 'second'),
  4128. 'multiple' => 'checkbox',
  4129. 'hiddenField' => false
  4130. ));
  4131. $expected = array(
  4132. array('div' => array('class' => 'input select')),
  4133. array('label' => array('for' => 'ModelMultiField')),
  4134. 'Multi Field',
  4135. '/label',
  4136. array('div' => array('class' => 'checkbox')),
  4137. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4138. array('label' => array('for' => 'ModelMultiField0')),
  4139. 'first',
  4140. '/label',
  4141. '/div',
  4142. array('div' => array('class' => 'checkbox')),
  4143. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4144. array('label' => array('for' => 'ModelMultiField1')),
  4145. 'second',
  4146. '/label',
  4147. '/div',
  4148. '/div'
  4149. );
  4150. $this->assertTags($result, $expected);
  4151. }
  4152. /**
  4153. * test that select() with multiple = checkbox works with overriding name attribute.
  4154. *
  4155. * @return void
  4156. */
  4157. public function testSelectCheckboxMultipleOverrideName() {
  4158. $result = $this->Form->input('category', array(
  4159. 'type' => 'select',
  4160. 'multiple' => 'checkbox',
  4161. 'name' => 'data[fish]',
  4162. 'options' => array('1', '2'),
  4163. 'div' => false,
  4164. 'label' => false,
  4165. ));
  4166. $expected = array(
  4167. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4168. array('div' => array('class' => 'checkbox')),
  4169. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4170. array('label' => array('for' => 'Category0')), '1', '/label',
  4171. '/div',
  4172. array('div' => array('class' => 'checkbox')),
  4173. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4174. array('label' => array('for' => 'Category1')), '2', '/label',
  4175. '/div'
  4176. );
  4177. $this->assertTags($result, $expected);
  4178. }
  4179. /**
  4180. * Test that 'id' overrides all the checkbox id's as well.
  4181. *
  4182. * @return void
  4183. */
  4184. public function testSelectCheckboxMultipleId() {
  4185. $result = $this->Form->select(
  4186. 'Model.multi_field',
  4187. array('first', 'second', 'third'),
  4188. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4189. );
  4190. $expected = array(
  4191. 'input' => array(
  4192. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4193. ),
  4194. array('div' => array('class' => 'checkbox')),
  4195. array('input' => array(
  4196. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4197. 'value' => '0', 'id' => 'CustomId0'
  4198. )),
  4199. array('label' => array('for' => 'CustomId0')),
  4200. 'first',
  4201. '/label',
  4202. '/div',
  4203. array('div' => array('class' => 'checkbox')),
  4204. array('input' => array(
  4205. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4206. 'value' => '1', 'id' => 'CustomId1'
  4207. )),
  4208. array('label' => array('for' => 'CustomId1')),
  4209. 'second',
  4210. '/label',
  4211. '/div',
  4212. array('div' => array('class' => 'checkbox')),
  4213. array('input' => array(
  4214. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4215. 'value' => '2', 'id' => 'CustomId2'
  4216. )),
  4217. array('label' => array('for' => 'CustomId2')),
  4218. 'third',
  4219. '/label',
  4220. '/div'
  4221. );
  4222. $this->assertTags($result, $expected);
  4223. }
  4224. /**
  4225. * testCheckbox method
  4226. *
  4227. * Test generation of checkboxes
  4228. *
  4229. * @return void
  4230. */
  4231. public function testCheckbox() {
  4232. $result = $this->Form->checkbox('Model.field');
  4233. $expected = array(
  4234. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4235. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4236. );
  4237. $this->assertTags($result, $expected);
  4238. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4239. $expected = array(
  4240. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4241. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4242. );
  4243. $this->assertTags($result, $expected);
  4244. $Contact = ClassRegistry::getObject('Contact');
  4245. $Contact->validationErrors['field'] = 1;
  4246. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4247. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4248. $expected = array(
  4249. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4250. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4251. );
  4252. $this->assertTags($result, $expected);
  4253. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4254. $expected = array(
  4255. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4256. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4257. );
  4258. $this->assertTags($result, $expected);
  4259. $this->Form->request->data['Contact']['field'] = '';
  4260. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4261. $expected = array(
  4262. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4263. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4264. );
  4265. $this->assertTags($result, $expected);
  4266. $Contact->validationErrors = array();
  4267. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4268. $expected = array(
  4269. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4270. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4271. );
  4272. $this->assertTags($result, $expected);
  4273. $this->Form->request->data['Contact']['published'] = 1;
  4274. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4275. $expected = array(
  4276. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4277. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4278. );
  4279. $this->assertTags($result, $expected);
  4280. $this->Form->request->data['Contact']['published'] = 0;
  4281. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4282. $expected = array(
  4283. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4284. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4285. );
  4286. $this->assertTags($result, $expected);
  4287. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4288. $expected = array(
  4289. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4290. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4291. );
  4292. $this->assertTags($result, $expected);
  4293. $result = $this->Form->checkbox('CustomField.1.value');
  4294. $expected = array(
  4295. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4296. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4297. );
  4298. $this->assertTags($result, $expected);
  4299. }
  4300. /**
  4301. * test checkbox() with a custom name attribute
  4302. *
  4303. * @return void
  4304. */
  4305. public function testCheckboxCustomNameAttribute() {
  4306. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4307. $expected = array(
  4308. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4309. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4310. );
  4311. $this->assertTags($result, $expected);
  4312. }
  4313. /**
  4314. * test the checked option for checkboxes.
  4315. *
  4316. * @return void
  4317. */
  4318. public function testCheckboxCheckedOption() {
  4319. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4320. $expected = array(
  4321. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4322. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4323. );
  4324. $this->assertTags($result, $expected);
  4325. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4326. $expected = array(
  4327. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4328. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4329. );
  4330. $this->assertTags($result, $expected);
  4331. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4332. $expected = array(
  4333. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4334. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4335. );
  4336. $this->assertTags($result, $expected);
  4337. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4338. $expected = array(
  4339. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4340. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4341. );
  4342. $this->assertTags($result, $expected);
  4343. $this->Form->request->data['Model']['field'] = 1;
  4344. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4345. $expected = array(
  4346. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4347. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4348. );
  4349. $this->assertTags($result, $expected);
  4350. }
  4351. /**
  4352. * Test that disabled attribute works on both the checkbox and hidden input.
  4353. *
  4354. * @return void
  4355. */
  4356. public function testCheckboxDisabling() {
  4357. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4358. $expected = array(
  4359. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4360. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4361. );
  4362. $this->assertTags($result, $expected);
  4363. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4364. $expected = array(
  4365. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4366. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4367. );
  4368. $this->assertTags($result, $expected);
  4369. }
  4370. /**
  4371. * Test that the hidden input for checkboxes can be omitted or set to a
  4372. * specific value.
  4373. *
  4374. * @return void
  4375. */
  4376. public function testCheckboxHiddenField() {
  4377. $result = $this->Form->input('UserForm.something', array(
  4378. 'type' => 'checkbox',
  4379. 'hiddenField' => false
  4380. ));
  4381. $expected = array(
  4382. 'div' => array('class' => 'input checkbox'),
  4383. array('input' => array(
  4384. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4385. 'value' => '1', 'id' => 'UserFormSomething'
  4386. )),
  4387. 'label' => array('for' => 'UserFormSomething'),
  4388. 'Something',
  4389. '/label',
  4390. '/div'
  4391. );
  4392. $this->assertTags($result, $expected);
  4393. $result = $this->Form->input('UserForm.something', array(
  4394. 'type' => 'checkbox',
  4395. 'value' => 'Y',
  4396. 'hiddenField' => 'N',
  4397. ));
  4398. $expected = array(
  4399. 'div' => array('class' => 'input checkbox'),
  4400. array('input' => array(
  4401. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4402. 'value' => 'N', 'id' => 'UserFormSomething_'
  4403. )),
  4404. array('input' => array(
  4405. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4406. 'value' => 'Y', 'id' => 'UserFormSomething'
  4407. )),
  4408. 'label' => array('for' => 'UserFormSomething'),
  4409. 'Something',
  4410. '/label',
  4411. '/div'
  4412. );
  4413. $this->assertTags($result, $expected);
  4414. }
  4415. /**
  4416. * testDateTime method
  4417. *
  4418. * Test generation of date/time select elements
  4419. *
  4420. * @return void
  4421. */
  4422. public function testDateTime() {
  4423. extract($this->dateRegex);
  4424. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4425. $now = strtotime('now');
  4426. $expected = array(
  4427. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4428. $daysRegex,
  4429. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4430. date('j', $now),
  4431. '/option',
  4432. '*/select',
  4433. '-',
  4434. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4435. $monthsRegex,
  4436. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4437. date('F', $now),
  4438. '/option',
  4439. '*/select',
  4440. '-',
  4441. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4442. $yearsRegex,
  4443. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4444. date('Y', $now),
  4445. '/option',
  4446. '*/select',
  4447. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4448. $hoursRegex,
  4449. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4450. date('g', $now),
  4451. '/option',
  4452. '*/select',
  4453. ':',
  4454. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4455. $minutesRegex,
  4456. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4457. date('i', $now),
  4458. '/option',
  4459. '*/select',
  4460. ' ',
  4461. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4462. $meridianRegex,
  4463. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4464. date('a', $now),
  4465. '/option',
  4466. '*/select'
  4467. );
  4468. $this->assertTags($result, $expected);
  4469. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4470. $expected = array(
  4471. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4472. $daysRegex,
  4473. array('option' => array('value' => '')),
  4474. '/option',
  4475. '*/select',
  4476. '-',
  4477. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4478. $monthsRegex,
  4479. array('option' => array('value' => '')),
  4480. '/option',
  4481. '*/select',
  4482. '-',
  4483. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4484. $yearsRegex,
  4485. array('option' => array('value' => '')),
  4486. '/option',
  4487. '*/select',
  4488. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4489. $hoursRegex,
  4490. array('option' => array('value' => '')),
  4491. '/option',
  4492. '*/select',
  4493. ':',
  4494. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4495. $minutesRegex,
  4496. array('option' => array('value' => '')),
  4497. '/option',
  4498. '*/select',
  4499. ' ',
  4500. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4501. $meridianRegex,
  4502. array('option' => array('value' => '')),
  4503. '/option',
  4504. '*/select'
  4505. );
  4506. $this->assertTags($result, $expected);
  4507. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4508. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4509. $this->assertTags($result, $expected);
  4510. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4511. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4512. $this->assertTags($result, $expected);
  4513. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4514. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4515. $expected = array(
  4516. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4517. $daysRegex,
  4518. array('option' => array('value' => '')),
  4519. '/option',
  4520. '*/select',
  4521. '-',
  4522. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4523. $monthsRegex,
  4524. array('option' => array('value' => '')),
  4525. '/option',
  4526. '*/select',
  4527. '-',
  4528. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4529. $yearsRegex,
  4530. array('option' => array('value' => '')),
  4531. '/option',
  4532. '*/select',
  4533. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4534. $hoursRegex,
  4535. array('option' => array('value' => '')),
  4536. '/option',
  4537. '*/select',
  4538. ':',
  4539. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4540. $minutesRegex,
  4541. array('option' => array('value' => '')),
  4542. '/option',
  4543. array('option' => array('value' => '00')),
  4544. '00',
  4545. '/option',
  4546. array('option' => array('value' => '05')),
  4547. '05',
  4548. '/option',
  4549. array('option' => array('value' => '10')),
  4550. '10',
  4551. '/option',
  4552. '*/select',
  4553. ' ',
  4554. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4555. $meridianRegex,
  4556. array('option' => array('value' => '')),
  4557. '/option',
  4558. '*/select'
  4559. );
  4560. $this->assertTags($result, $expected);
  4561. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4562. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4563. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4564. $this->Form->request->data['Contact']['data'] = null;
  4565. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4566. $expected = array(
  4567. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4568. $daysRegex,
  4569. array('option' => array('value' => '')),
  4570. '/option',
  4571. '*/select',
  4572. '-',
  4573. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4574. $monthsRegex,
  4575. array('option' => array('value' => '')),
  4576. '/option',
  4577. '*/select',
  4578. '-',
  4579. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4580. $yearsRegex,
  4581. array('option' => array('value' => '')),
  4582. '/option',
  4583. '*/select',
  4584. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4585. $hoursRegex,
  4586. array('option' => array('value' => '')),
  4587. '/option',
  4588. '*/select',
  4589. ':',
  4590. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4591. $minutesRegex,
  4592. array('option' => array('value' => '')),
  4593. '/option',
  4594. '*/select',
  4595. ' ',
  4596. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4597. $meridianRegex,
  4598. array('option' => array('value' => '')),
  4599. '/option',
  4600. '*/select'
  4601. );
  4602. $this->assertTags($result, $expected);
  4603. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4604. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4605. $now = strtotime($this->Form->data['Model']['field']);
  4606. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4607. $expected = array(
  4608. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4609. $daysRegex,
  4610. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4611. date('j', $now),
  4612. '/option',
  4613. '*/select',
  4614. '-',
  4615. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4616. $monthsRegex,
  4617. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4618. date('F', $now),
  4619. '/option',
  4620. '*/select',
  4621. '-',
  4622. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4623. $yearsRegex,
  4624. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4625. date('Y', $now),
  4626. '/option',
  4627. '*/select',
  4628. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4629. $hoursRegex,
  4630. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4631. date('g', $now),
  4632. '/option',
  4633. '*/select',
  4634. ':',
  4635. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4636. $minutesRegex,
  4637. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4638. date('i', $now),
  4639. '/option',
  4640. '*/select',
  4641. ' ',
  4642. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4643. $meridianRegex,
  4644. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4645. date('a', $now),
  4646. '/option',
  4647. '*/select'
  4648. );
  4649. $this->assertTags($result, $expected);
  4650. $selected = strtotime('2008-10-26 12:33:00');
  4651. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4652. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4653. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4654. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4655. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4656. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4657. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4658. $this->Form->create('Contact');
  4659. $result = $this->Form->input('published');
  4660. $now = strtotime('now');
  4661. $expected = array(
  4662. 'div' => array('class' => 'input date'),
  4663. 'label' => array('for' => 'ContactPublishedMonth'),
  4664. 'Published',
  4665. '/label',
  4666. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4667. $monthsRegex,
  4668. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4669. date('F', $now),
  4670. '/option',
  4671. '*/select',
  4672. '-',
  4673. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4674. $daysRegex,
  4675. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4676. date('j', $now),
  4677. '/option',
  4678. '*/select',
  4679. '-',
  4680. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4681. $yearsRegex,
  4682. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4683. date('Y', $now),
  4684. '/option',
  4685. '*/select',
  4686. '/div'
  4687. );
  4688. $this->assertTags($result, $expected);
  4689. $result = $this->Form->input('published2', array('type' => 'date'));
  4690. $now = strtotime('now');
  4691. $expected = array(
  4692. 'div' => array('class' => 'input date'),
  4693. 'label' => array('for' => 'ContactPublished2Month'),
  4694. 'Published2',
  4695. '/label',
  4696. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4697. $monthsRegex,
  4698. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4699. date('F', $now),
  4700. '/option',
  4701. '*/select',
  4702. '-',
  4703. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4704. $daysRegex,
  4705. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4706. date('j', $now),
  4707. '/option',
  4708. '*/select',
  4709. '-',
  4710. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4711. $yearsRegex,
  4712. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4713. date('Y', $now),
  4714. '/option',
  4715. '*/select',
  4716. '/div'
  4717. );
  4718. $this->assertTags($result, $expected);
  4719. $this->Form->create('Contact');
  4720. $result = $this->Form->input('published', array('monthNames' => false));
  4721. $now = strtotime('now');
  4722. $expected = array(
  4723. 'div' => array('class' => 'input date'),
  4724. 'label' => array('for' => 'ContactPublishedMonth'),
  4725. 'Published',
  4726. '/label',
  4727. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4728. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4729. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4730. date('m', $now),
  4731. '/option',
  4732. '*/select',
  4733. '-',
  4734. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4735. $daysRegex,
  4736. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4737. date('j', $now),
  4738. '/option',
  4739. '*/select',
  4740. '-',
  4741. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4742. $yearsRegex,
  4743. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4744. date('Y', $now),
  4745. '/option',
  4746. '*/select',
  4747. '/div'
  4748. );
  4749. $this->assertTags($result, $expected);
  4750. $result = $this->Form->input('published', array('type' => 'time'));
  4751. $now = strtotime('now');
  4752. $expected = array(
  4753. 'div' => array('class' => 'input time'),
  4754. 'label' => array('for' => 'ContactPublishedHour'),
  4755. 'Published',
  4756. '/label',
  4757. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4758. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4759. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4760. date('g', $now),
  4761. '/option',
  4762. '*/select',
  4763. ':',
  4764. );
  4765. $this->assertTags($result, $expected);
  4766. $result = $this->Form->input('published', array(
  4767. 'timeFormat' => 24,
  4768. 'interval' => 5,
  4769. 'selected' => strtotime('2009-09-03 13:37:00'),
  4770. 'type' => 'datetime'
  4771. ));
  4772. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4773. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4774. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4775. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4776. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4777. $this->assertNoErrors();
  4778. $this->Form->request->data['Contact'] = array(
  4779. 'date' => array(
  4780. 'day' => '',
  4781. 'month' => '',
  4782. 'year' => '',
  4783. 'hour' => '',
  4784. 'min' => '',
  4785. 'meridian' => ''
  4786. )
  4787. );
  4788. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4789. }
  4790. /**
  4791. * test that datetime() and default values work.
  4792. *
  4793. * @return void
  4794. */
  4795. public function testDatetimeWithDefault() {
  4796. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4797. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4798. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4799. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4800. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4801. 'default' => '2009-06-01 11:15:30'
  4802. ));
  4803. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4804. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4805. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4806. }
  4807. /**
  4808. * test that bogus non-date time data doesn't cause errors.
  4809. *
  4810. * @return void
  4811. */
  4812. public function testDateTimeWithBogusData() {
  4813. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4814. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4815. }
  4816. /**
  4817. * testDateTimeEmptyAsArray
  4818. *
  4819. * @return void
  4820. */
  4821. public function testDateTimeEmptyAsArray() {
  4822. $result = $this->Form->dateTime('Contact.date',
  4823. 'DMY',
  4824. '12',
  4825. array(
  4826. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4827. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4828. )
  4829. )
  4830. );
  4831. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4832. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4833. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4834. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4835. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4836. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4837. $result = $this->Form->dateTime('Contact.date',
  4838. 'DMY',
  4839. '12',
  4840. array(
  4841. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4842. )
  4843. );
  4844. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4845. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4846. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4847. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4848. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4849. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4850. }
  4851. /**
  4852. * testFormDateTimeMulti method
  4853. *
  4854. * test multiple datetime element generation
  4855. *
  4856. * @return void
  4857. */
  4858. public function testFormDateTimeMulti() {
  4859. extract($this->dateRegex);
  4860. $result = $this->Form->dateTime('Contact.1.updated');
  4861. $expected = array(
  4862. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4863. $daysRegex,
  4864. array('option' => array('value' => '')),
  4865. '/option',
  4866. '*/select',
  4867. '-',
  4868. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4869. $monthsRegex,
  4870. array('option' => array('value' => '')),
  4871. '/option',
  4872. '*/select',
  4873. '-',
  4874. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4875. $yearsRegex,
  4876. array('option' => array('value' => '')),
  4877. '/option',
  4878. '*/select',
  4879. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  4880. $hoursRegex,
  4881. array('option' => array('value' => '')),
  4882. '/option',
  4883. '*/select',
  4884. ':',
  4885. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  4886. $minutesRegex,
  4887. array('option' => array('value' => '')),
  4888. '/option',
  4889. '*/select',
  4890. ' ',
  4891. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  4892. $meridianRegex,
  4893. array('option' => array('value' => '')),
  4894. '/option',
  4895. '*/select'
  4896. );
  4897. $this->assertTags($result, $expected);
  4898. $result = $this->Form->dateTime('Contact.2.updated');
  4899. $expected = array(
  4900. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  4901. $daysRegex,
  4902. array('option' => array('value' => '')),
  4903. '/option',
  4904. '*/select',
  4905. '-',
  4906. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  4907. $monthsRegex,
  4908. array('option' => array('value' => '')),
  4909. '/option',
  4910. '*/select',
  4911. '-',
  4912. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  4913. $yearsRegex,
  4914. array('option' => array('value' => '')),
  4915. '/option',
  4916. '*/select',
  4917. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  4918. $hoursRegex,
  4919. array('option' => array('value' => '')),
  4920. '/option',
  4921. '*/select',
  4922. ':',
  4923. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  4924. $minutesRegex,
  4925. array('option' => array('value' => '')),
  4926. '/option',
  4927. '*/select',
  4928. ' ',
  4929. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  4930. $meridianRegex,
  4931. array('option' => array('value' => '')),
  4932. '/option',
  4933. '*/select'
  4934. );
  4935. $this->assertTags($result, $expected);
  4936. }
  4937. /**
  4938. * When changing the date format, the label should always focus the first select box when
  4939. * clicked.
  4940. *
  4941. * @return void
  4942. */
  4943. public function testDateTimeLabelIdMatchesFirstInput() {
  4944. $result = $this->Form->input('Model.date', array('type' => 'date'));
  4945. $this->assertContains('label for="ModelDateMonth"', $result);
  4946. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  4947. $this->assertContains('label for="ModelDateDay"', $result);
  4948. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  4949. $this->assertContains('label for="ModelDateYear"', $result);
  4950. }
  4951. /**
  4952. * testMonth method
  4953. *
  4954. * @return void
  4955. */
  4956. public function testMonth() {
  4957. $result = $this->Form->month('Model.field');
  4958. $expected = array(
  4959. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4960. array('option' => array('value' => '')),
  4961. '/option',
  4962. array('option' => array('value' => '01')),
  4963. date('F', strtotime('2008-01-01 00:00:00')),
  4964. '/option',
  4965. array('option' => array('value' => '02')),
  4966. date('F', strtotime('2008-02-01 00:00:00')),
  4967. '/option',
  4968. '*/select',
  4969. );
  4970. $this->assertTags($result, $expected);
  4971. $result = $this->Form->month('Model.field', array('empty' => true));
  4972. $expected = array(
  4973. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4974. array('option' => array('value' => '')),
  4975. '/option',
  4976. array('option' => array('value' => '01')),
  4977. date('F', strtotime('2008-01-01 00:00:00')),
  4978. '/option',
  4979. array('option' => array('value' => '02')),
  4980. date('F', strtotime('2008-02-01 00:00:00')),
  4981. '/option',
  4982. '*/select',
  4983. );
  4984. $this->assertTags($result, $expected);
  4985. $result = $this->Form->month('Model.field', array('monthNames' => false));
  4986. $expected = array(
  4987. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4988. array('option' => array('value' => '')),
  4989. '/option',
  4990. array('option' => array('value' => '01')),
  4991. '01',
  4992. '/option',
  4993. array('option' => array('value' => '02')),
  4994. '02',
  4995. '/option',
  4996. '*/select',
  4997. );
  4998. $this->assertTags($result, $expected);
  4999. $monthNames = array(
  5000. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5001. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5002. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5003. $expected = array(
  5004. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5005. array('option' => array('value' => '')),
  5006. '/option',
  5007. array('option' => array('value' => '01')),
  5008. 'Jan',
  5009. '/option',
  5010. array('option' => array('value' => '02')),
  5011. 'Feb',
  5012. '/option',
  5013. '*/select',
  5014. );
  5015. $this->assertTags($result, $expected);
  5016. }
  5017. /**
  5018. * testDay method
  5019. *
  5020. * @return void
  5021. */
  5022. public function testDay() {
  5023. extract($this->dateRegex);
  5024. $result = $this->Form->day('Model.field', array('value' => false));
  5025. $expected = array(
  5026. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5027. array('option' => array('value' => '')),
  5028. '/option',
  5029. array('option' => array('value' => '01')),
  5030. '1',
  5031. '/option',
  5032. array('option' => array('value' => '02')),
  5033. '2',
  5034. '/option',
  5035. $daysRegex,
  5036. '/select',
  5037. );
  5038. $this->assertTags($result, $expected);
  5039. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5040. $result = $this->Form->day('Model.field');
  5041. $expected = array(
  5042. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5043. array('option' => array('value' => '')),
  5044. '/option',
  5045. array('option' => array('value' => '01')),
  5046. '1',
  5047. '/option',
  5048. array('option' => array('value' => '02')),
  5049. '2',
  5050. '/option',
  5051. $daysRegex,
  5052. array('option' => array('value' => '10', 'selected' => 'selected')),
  5053. '10',
  5054. '/option',
  5055. $daysRegex,
  5056. '/select',
  5057. );
  5058. $this->assertTags($result, $expected);
  5059. $this->Form->request->data['Model']['field'] = '';
  5060. $result = $this->Form->day('Model.field', array('value' => '10'));
  5061. $expected = array(
  5062. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5063. array('option' => array('value' => '')),
  5064. '/option',
  5065. array('option' => array('value' => '01')),
  5066. '1',
  5067. '/option',
  5068. array('option' => array('value' => '02')),
  5069. '2',
  5070. '/option',
  5071. $daysRegex,
  5072. array('option' => array('value' => '10', 'selected' => 'selected')),
  5073. '10',
  5074. '/option',
  5075. $daysRegex,
  5076. '/select',
  5077. );
  5078. $this->assertTags($result, $expected);
  5079. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5080. $result = $this->Form->day('Model.field', array('value' => true));
  5081. $expected = array(
  5082. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5083. array('option' => array('value' => '')),
  5084. '/option',
  5085. array('option' => array('value' => '01')),
  5086. '1',
  5087. '/option',
  5088. array('option' => array('value' => '02')),
  5089. '2',
  5090. '/option',
  5091. $daysRegex,
  5092. array('option' => array('value' => '10', 'selected' => 'selected')),
  5093. '10',
  5094. '/option',
  5095. $daysRegex,
  5096. '/select',
  5097. );
  5098. $this->assertTags($result, $expected);
  5099. }
  5100. /**
  5101. * testMinute method
  5102. *
  5103. * @return void
  5104. */
  5105. public function testMinute() {
  5106. extract($this->dateRegex);
  5107. $result = $this->Form->minute('Model.field');
  5108. $expected = array(
  5109. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5110. array('option' => array('value' => '')),
  5111. '/option',
  5112. array('option' => array('value' => '00')),
  5113. '00',
  5114. '/option',
  5115. array('option' => array('value' => '01')),
  5116. '01',
  5117. '/option',
  5118. array('option' => array('value' => '02')),
  5119. '02',
  5120. '/option',
  5121. $minutesRegex,
  5122. '/select',
  5123. );
  5124. $this->assertTags($result, $expected);
  5125. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5126. $result = $this->Form->minute('Model.field');
  5127. $expected = array(
  5128. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5129. array('option' => array('value' => '')),
  5130. '/option',
  5131. array('option' => array('value' => '00')),
  5132. '00',
  5133. '/option',
  5134. array('option' => array('value' => '01')),
  5135. '01',
  5136. '/option',
  5137. array('option' => array('value' => '02')),
  5138. '02',
  5139. '/option',
  5140. $minutesRegex,
  5141. array('option' => array('value' => '12', 'selected' => 'selected')),
  5142. '12',
  5143. '/option',
  5144. $minutesRegex,
  5145. '/select',
  5146. );
  5147. $this->assertTags($result, $expected);
  5148. $this->Form->request->data['Model']['field'] = '';
  5149. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5150. $expected = array(
  5151. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5152. array('option' => array('value' => '')),
  5153. '/option',
  5154. array('option' => array('value' => '00')),
  5155. '00',
  5156. '/option',
  5157. array('option' => array('value' => '05')),
  5158. '05',
  5159. '/option',
  5160. array('option' => array('value' => '10')),
  5161. '10',
  5162. '/option',
  5163. $minutesRegex,
  5164. '/select',
  5165. );
  5166. $this->assertTags($result, $expected);
  5167. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5168. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5169. $expected = array(
  5170. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5171. array('option' => array('value' => '')),
  5172. '/option',
  5173. array('option' => array('value' => '00')),
  5174. '00',
  5175. '/option',
  5176. array('option' => array('value' => '05')),
  5177. '05',
  5178. '/option',
  5179. array('option' => array('value' => '10', 'selected' => 'selected')),
  5180. '10',
  5181. '/option',
  5182. $minutesRegex,
  5183. '/select',
  5184. );
  5185. $this->assertTags($result, $expected);
  5186. }
  5187. /**
  5188. * testHour method
  5189. *
  5190. * @return void
  5191. */
  5192. public function testHour() {
  5193. extract($this->dateRegex);
  5194. $result = $this->Form->hour('Model.field', false);
  5195. $expected = array(
  5196. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5197. array('option' => array('value' => '')),
  5198. '/option',
  5199. array('option' => array('value' => '01')),
  5200. '1',
  5201. '/option',
  5202. array('option' => array('value' => '02')),
  5203. '2',
  5204. '/option',
  5205. $hoursRegex,
  5206. '/select',
  5207. );
  5208. $this->assertTags($result, $expected);
  5209. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5210. $result = $this->Form->hour('Model.field', false);
  5211. $expected = array(
  5212. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5213. array('option' => array('value' => '')),
  5214. '/option',
  5215. array('option' => array('value' => '01')),
  5216. '1',
  5217. '/option',
  5218. array('option' => array('value' => '02')),
  5219. '2',
  5220. '/option',
  5221. $hoursRegex,
  5222. array('option' => array('value' => '12', 'selected' => 'selected')),
  5223. '12',
  5224. '/option',
  5225. '/select',
  5226. );
  5227. $this->assertTags($result, $expected);
  5228. $this->Form->request->data['Model']['field'] = '';
  5229. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5230. $expected = array(
  5231. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5232. array('option' => array('value' => '')),
  5233. '/option',
  5234. array('option' => array('value' => '00')),
  5235. '0',
  5236. '/option',
  5237. array('option' => array('value' => '01')),
  5238. '1',
  5239. '/option',
  5240. array('option' => array('value' => '02')),
  5241. '2',
  5242. '/option',
  5243. $hoursRegex,
  5244. array('option' => array('value' => '23', 'selected' => 'selected')),
  5245. '23',
  5246. '/option',
  5247. '/select',
  5248. );
  5249. $this->assertTags($result, $expected);
  5250. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5251. $result = $this->Form->hour('Model.field', true);
  5252. $expected = array(
  5253. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5254. array('option' => array('value' => '')),
  5255. '/option',
  5256. array('option' => array('value' => '00', 'selected' => 'selected')),
  5257. '0',
  5258. '/option',
  5259. array('option' => array('value' => '01')),
  5260. '1',
  5261. '/option',
  5262. array('option' => array('value' => '02')),
  5263. '2',
  5264. '/option',
  5265. $hoursRegex,
  5266. '/select',
  5267. );
  5268. $this->assertTags($result, $expected);
  5269. unset($this->Form->request->data['Model']['field']);
  5270. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5271. $thisHour = date('H');
  5272. $optValue = date('G');
  5273. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5274. }
  5275. /**
  5276. * testYear method
  5277. *
  5278. * @return void
  5279. */
  5280. public function testYear() {
  5281. $result = $this->Form->year('Model.field', 2006, 2007);
  5282. $expected = array(
  5283. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5284. array('option' => array('value' => '')),
  5285. '/option',
  5286. array('option' => array('value' => '2007')),
  5287. '2007',
  5288. '/option',
  5289. array('option' => array('value' => '2006')),
  5290. '2006',
  5291. '/option',
  5292. '/select',
  5293. );
  5294. $this->assertTags($result, $expected);
  5295. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5296. $expected = array(
  5297. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5298. array('option' => array('value' => '')),
  5299. '/option',
  5300. array('option' => array('value' => '2006')),
  5301. '2006',
  5302. '/option',
  5303. array('option' => array('value' => '2007')),
  5304. '2007',
  5305. '/option',
  5306. '/select',
  5307. );
  5308. $this->assertTags($result, $expected);
  5309. $this->request->data['Contact']['published'] = '';
  5310. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5311. $expected = array(
  5312. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5313. array('option' => array('value' => '')),
  5314. '/option',
  5315. array('option' => array('value' => '2007')),
  5316. '2007',
  5317. '/option',
  5318. array('option' => array('value' => '2006')),
  5319. '2006',
  5320. '/option',
  5321. '/select',
  5322. );
  5323. $this->assertTags($result, $expected);
  5324. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5325. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5326. $expected = array(
  5327. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5328. array('option' => array('value' => '2007')),
  5329. '2007',
  5330. '/option',
  5331. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5332. '2006',
  5333. '/option',
  5334. '/select',
  5335. );
  5336. $this->assertTags($result, $expected);
  5337. $this->Form->request->data['Contact']['published'] = '';
  5338. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5339. $expected = array(
  5340. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5341. array('option' => array('value' => '')),
  5342. '/option',
  5343. array('option' => array('value' => '2007')),
  5344. '2007',
  5345. '/option',
  5346. array('option' => array('value' => '2006')),
  5347. '2006',
  5348. '/option',
  5349. '/select',
  5350. );
  5351. $this->assertTags($result, $expected);
  5352. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5353. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5354. $expected = array(
  5355. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5356. array('option' => array('value' => '2007')),
  5357. '2007',
  5358. '/option',
  5359. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5360. '2006',
  5361. '/option',
  5362. '/select',
  5363. );
  5364. $this->assertTags($result, $expected);
  5365. $this->Form->request->data['Contact']['published'] = '';
  5366. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5367. $expected = array(
  5368. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5369. array('option' => array('value' => '')),
  5370. '/option',
  5371. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5372. '2007',
  5373. '/option',
  5374. array('option' => array('value' => '2006')),
  5375. '2006',
  5376. '/option',
  5377. '/select',
  5378. );
  5379. $this->assertTags($result, $expected);
  5380. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5381. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5382. $expected = array(
  5383. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5384. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5385. '2007',
  5386. '/option',
  5387. array('option' => array('value' => '2006')),
  5388. '2006',
  5389. '/option',
  5390. '/select',
  5391. );
  5392. $this->assertTags($result, $expected);
  5393. $this->Form->request->data['Contact']['published'] = '';
  5394. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5395. $expected = array(
  5396. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5397. array('option' => array('value' => '2008')),
  5398. '2008',
  5399. '/option',
  5400. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5401. '2007',
  5402. '/option',
  5403. array('option' => array('value' => '2006')),
  5404. '2006',
  5405. '/option',
  5406. '/select',
  5407. );
  5408. $this->assertTags($result, $expected);
  5409. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5410. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5411. $expected = array(
  5412. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5413. array('option' => array('value' => '2008')),
  5414. '2008',
  5415. '/option',
  5416. array('option' => array('value' => '2007')),
  5417. '2007',
  5418. '/option',
  5419. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5420. '2006',
  5421. '/option',
  5422. '/select',
  5423. );
  5424. $this->assertTags($result, $expected);
  5425. $this->Form->request->data = array();
  5426. $this->Form->create('Contact');
  5427. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5428. $expected = array(
  5429. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5430. array('option' => array('value' => '2008')),
  5431. '2008',
  5432. '/option',
  5433. array('option' => array('value' => '2007')),
  5434. '2007',
  5435. '/option',
  5436. array('option' => array('value' => '2006')),
  5437. '2006',
  5438. '/option',
  5439. '/select',
  5440. );
  5441. $this->assertTags($result, $expected);
  5442. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5443. $this->assertContains('data[Contact][published][year]', $result);
  5444. }
  5445. /**
  5446. * testTextArea method
  5447. *
  5448. * @return void
  5449. */
  5450. public function testTextArea() {
  5451. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5452. $result = $this->Form->textarea('Model.field');
  5453. $expected = array(
  5454. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5455. 'some test data',
  5456. '/textarea',
  5457. );
  5458. $this->assertTags($result, $expected);
  5459. $result = $this->Form->textarea('Model.tmp');
  5460. $expected = array(
  5461. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5462. '/textarea',
  5463. );
  5464. $this->assertTags($result, $expected);
  5465. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5466. $result = $this->Form->textarea('Model.field');
  5467. $expected = array(
  5468. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5469. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5470. '/textarea',
  5471. );
  5472. $this->assertTags($result, $expected);
  5473. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5474. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5475. $expected = array(
  5476. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5477. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5478. '/textarea',
  5479. );
  5480. $this->assertTags($result, $expected);
  5481. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5482. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5483. $expected = array(
  5484. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5485. '/textarea'
  5486. );
  5487. $this->assertTags($result, $expected);
  5488. }
  5489. /**
  5490. * testTextAreaWithStupidCharacters method
  5491. *
  5492. * test text area with non-ascii characters
  5493. *
  5494. * @return void
  5495. */
  5496. public function testTextAreaWithStupidCharacters() {
  5497. $this->loadFixtures('Post');
  5498. $result = $this->Form->input('Post.content', array(
  5499. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5500. ));
  5501. $expected = array(
  5502. 'div' => array('class' => 'input text'),
  5503. 'label' => array('for' => 'PostContent'),
  5504. 'Current Text',
  5505. '/label',
  5506. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5507. 'GREAT®',
  5508. '/textarea',
  5509. '/div'
  5510. );
  5511. $this->assertTags($result, $expected);
  5512. }
  5513. /**
  5514. * testHiddenField method
  5515. *
  5516. * @return void
  5517. */
  5518. public function testHiddenField() {
  5519. $Contact = ClassRegistry::getObject('Contact');
  5520. $Contact->validationErrors['field'] = 1;
  5521. $this->Form->request->data['Contact']['field'] = 'test';
  5522. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5523. $this->assertTags($result, array(
  5524. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5525. );
  5526. }
  5527. /**
  5528. * testFileUploadField method
  5529. *
  5530. * @return void
  5531. */
  5532. public function testFileUploadField() {
  5533. $result = $this->Form->file('Model.upload');
  5534. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5535. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5536. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5537. $expected = array(
  5538. 'div' => array('class' => 'input file'),
  5539. 'label' => array('for' => 'ModelUpload'),
  5540. 'Upload',
  5541. '/label',
  5542. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5543. '/div'
  5544. );
  5545. $this->assertTags($result, $expected);
  5546. }
  5547. /**
  5548. * test File upload input on a model not used in create();
  5549. *
  5550. * @return void
  5551. */
  5552. public function testFileUploadOnOtherModel() {
  5553. $this->Form->create('ValidateUser', array('type' => 'file'));
  5554. $result = $this->Form->file('ValidateProfile.city');
  5555. $expected = array(
  5556. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5557. );
  5558. $this->assertTags($result, $expected);
  5559. }
  5560. /**
  5561. * testButton method
  5562. *
  5563. * @return void
  5564. */
  5565. public function testButton() {
  5566. $result = $this->Form->button('Hi');
  5567. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5568. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5569. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5570. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5571. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5572. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5573. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5574. $result = $this->Form->button('No type', array('type' => false));
  5575. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5576. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5577. $this->assertNotRegExp('/\&039/', $result);
  5578. }
  5579. /**
  5580. * Test that button() makes unlocked fields by default.
  5581. *
  5582. * @return void
  5583. */
  5584. public function testButtonUnlockedByDefault() {
  5585. $this->Form->request->params['_Token']['key'] = 'secured';
  5586. $this->Form->button('Save', array('name' => 'save'));
  5587. $this->Form->button('Clear');
  5588. $result = $this->Form->unlockField();
  5589. $this->assertEquals(array('save'), $result);
  5590. }
  5591. /**
  5592. * testPostButton method
  5593. *
  5594. * @return void
  5595. */
  5596. public function testPostButton() {
  5597. $result = $this->Form->postButton('Hi', '/controller/action');
  5598. $this->assertTags($result, array(
  5599. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5600. 'div' => array('style' => 'display:none;'),
  5601. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5602. '/div',
  5603. 'button' => array('type' => 'submit'),
  5604. 'Hi',
  5605. '/button',
  5606. '/form'
  5607. ));
  5608. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5609. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5610. }
  5611. /**
  5612. * Test that postButton adds _Token fields.
  5613. *
  5614. * @return void
  5615. */
  5616. public function testSecurePostButton() {
  5617. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5618. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5619. $expected = array(
  5620. 'form' => array(
  5621. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5622. ),
  5623. array('div' => array('style' => 'display:none;')),
  5624. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5625. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5626. '/div',
  5627. 'button' => array('type' => 'submit'),
  5628. 'Delete',
  5629. '/button',
  5630. array('div' => array('style' => 'display:none;')),
  5631. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5632. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5633. '/div',
  5634. '/form',
  5635. );
  5636. $this->assertTags($result, $expected);
  5637. }
  5638. /**
  5639. * testPostLink method
  5640. *
  5641. * @return void
  5642. */
  5643. public function testPostLink() {
  5644. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5645. $this->assertTags($result, array(
  5646. 'form' => array(
  5647. 'method' => 'post', 'action' => '/posts/delete/1',
  5648. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5649. ),
  5650. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5651. '/form',
  5652. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5653. 'Delete',
  5654. '/a'
  5655. ));
  5656. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5657. $this->assertTags($result, array(
  5658. 'form' => array(
  5659. 'method' => 'post', 'action' => '/posts/delete/1',
  5660. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5661. ),
  5662. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5663. '/form',
  5664. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5665. 'Delete',
  5666. '/a'
  5667. ));
  5668. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5669. $this->assertTags($result, array(
  5670. 'form' => array(
  5671. 'method' => 'post', 'action' => '/posts/delete/1',
  5672. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5673. ),
  5674. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5675. '/form',
  5676. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5677. 'Delete',
  5678. '/a'
  5679. ));
  5680. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5681. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5682. }
  5683. /**
  5684. * Test that postLink adds _Token fields.
  5685. *
  5686. * @return void
  5687. */
  5688. public function testSecurePostLink() {
  5689. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5690. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5691. $expected = array(
  5692. 'form' => array(
  5693. 'method' => 'post', 'action' => '/posts/delete/1',
  5694. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5695. ),
  5696. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5697. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5698. 'div' => array('style' => 'display:none;'),
  5699. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5700. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5701. '/div',
  5702. '/form',
  5703. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5704. 'Delete',
  5705. '/a'
  5706. );
  5707. $this->assertTags($result, $expected);
  5708. }
  5709. /**
  5710. * testSubmitButton method
  5711. *
  5712. * @return void
  5713. */
  5714. public function testSubmitButton() {
  5715. $result = $this->Form->submit('');
  5716. $expected = array(
  5717. 'div' => array('class' => 'submit'),
  5718. 'input' => array('type' => 'submit', 'value' => ''),
  5719. '/div'
  5720. );
  5721. $this->assertTags($result, $expected);
  5722. $result = $this->Form->submit('Test Submit');
  5723. $expected = array(
  5724. 'div' => array('class' => 'submit'),
  5725. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5726. '/div'
  5727. );
  5728. $this->assertTags($result, $expected);
  5729. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5730. $expected = array(
  5731. 'span' => array('class' => 'submit'),
  5732. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5733. '/span'
  5734. );
  5735. $this->assertTags($result, $expected);
  5736. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5737. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5738. $this->assertTags($result, $expected);
  5739. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5740. $expected = array(
  5741. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5742. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5743. '/div'
  5744. );
  5745. $this->assertTags($result, $expected);
  5746. $result = $this->Form->submit('Next >');
  5747. $expected = array(
  5748. 'div' => array('class' => 'submit'),
  5749. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5750. '/div'
  5751. );
  5752. $this->assertTags($result, $expected);
  5753. $result = $this->Form->submit('Next >', array('escape' => false));
  5754. $expected = array(
  5755. 'div' => array('class' => 'submit'),
  5756. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5757. '/div'
  5758. );
  5759. $this->assertTags($result, $expected);
  5760. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5761. $expected = array(
  5762. 'div' => array('class' => 'submit'),
  5763. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5764. '/div'
  5765. );
  5766. $this->assertTags($result, $expected);
  5767. $before = '--before--';
  5768. $after = '--after--';
  5769. $result = $this->Form->submit('Test', array('before' => $before));
  5770. $expected = array(
  5771. 'div' => array('class' => 'submit'),
  5772. '--before--',
  5773. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5774. '/div'
  5775. );
  5776. $this->assertTags($result, $expected);
  5777. $result = $this->Form->submit('Test', array('after' => $after));
  5778. $expected = array(
  5779. 'div' => array('class' => 'submit'),
  5780. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5781. '--after--',
  5782. '/div'
  5783. );
  5784. $this->assertTags($result, $expected);
  5785. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5786. $expected = array(
  5787. 'div' => array('class' => 'submit'),
  5788. '--before--',
  5789. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5790. '--after--',
  5791. '/div'
  5792. );
  5793. $this->assertTags($result, $expected);
  5794. }
  5795. /**
  5796. * test image submit types.
  5797. *
  5798. * @return void
  5799. */
  5800. public function testSubmitImage() {
  5801. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5802. $expected = array(
  5803. 'div' => array('class' => 'submit'),
  5804. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5805. '/div'
  5806. );
  5807. $this->assertTags($result, $expected);
  5808. $result = $this->Form->submit('/relative/cake.power.gif');
  5809. $expected = array(
  5810. 'div' => array('class' => 'submit'),
  5811. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5812. '/div'
  5813. );
  5814. $this->assertTags($result, $expected);
  5815. $result = $this->Form->submit('cake.power.gif');
  5816. $expected = array(
  5817. 'div' => array('class' => 'submit'),
  5818. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5819. '/div'
  5820. );
  5821. $this->assertTags($result, $expected);
  5822. $result = $this->Form->submit('Not.an.image');
  5823. $expected = array(
  5824. 'div' => array('class' => 'submit'),
  5825. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5826. '/div'
  5827. );
  5828. $this->assertTags($result, $expected);
  5829. $after = '--after--';
  5830. $before = '--before--';
  5831. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5832. $expected = array(
  5833. 'div' => array('class' => 'submit'),
  5834. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5835. '--after--',
  5836. '/div'
  5837. );
  5838. $this->assertTags($result, $expected);
  5839. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5840. $expected = array(
  5841. 'div' => array('class' => 'submit'),
  5842. '--before--',
  5843. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5844. '/div'
  5845. );
  5846. $this->assertTags($result, $expected);
  5847. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5848. $expected = array(
  5849. 'div' => array('class' => 'submit'),
  5850. '--before--',
  5851. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5852. '--after--',
  5853. '/div'
  5854. );
  5855. $this->assertTags($result, $expected);
  5856. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5857. $expected = array(
  5858. 'div' => array('class' => 'submit'),
  5859. '--before--',
  5860. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5861. '--after--',
  5862. '/div'
  5863. );
  5864. $this->assertTags($result, $expected);
  5865. }
  5866. /**
  5867. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5868. * be submitted at a time.
  5869. *
  5870. * @return void
  5871. */
  5872. public function testSubmitUnlockedByDefault() {
  5873. $this->Form->request->params['_Token']['key'] = 'secured';
  5874. $this->Form->submit('Go go');
  5875. $this->Form->submit('Save', array('name' => 'save'));
  5876. $result = $this->Form->unlockField();
  5877. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5878. }
  5879. /**
  5880. * Test submit image with timestamps.
  5881. *
  5882. * @return void
  5883. */
  5884. public function testSubmitImageTimestamp() {
  5885. Configure::write('Asset.timestamp', 'force');
  5886. $result = $this->Form->submit('cake.power.gif');
  5887. $expected = array(
  5888. 'div' => array('class' => 'submit'),
  5889. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  5890. '/div'
  5891. );
  5892. $this->assertTags($result, $expected);
  5893. }
  5894. /**
  5895. * test the create() method
  5896. *
  5897. * @return void
  5898. */
  5899. public function testCreate() {
  5900. $result = $this->Form->create('Contact');
  5901. $encoding = strtolower(Configure::read('App.encoding'));
  5902. $expected = array(
  5903. 'form' => array(
  5904. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5905. 'accept-charset' => $encoding
  5906. ),
  5907. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  5908. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5909. '/div'
  5910. );
  5911. $this->assertTags($result, $expected);
  5912. $result = $this->Form->create('Contact', array('type' => 'GET'));
  5913. $expected = array('form' => array(
  5914. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5915. 'accept-charset' => $encoding
  5916. ));
  5917. $this->assertTags($result, $expected);
  5918. $result = $this->Form->create('Contact', array('type' => 'get'));
  5919. $expected = array('form' => array(
  5920. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  5921. 'accept-charset' => $encoding
  5922. ));
  5923. $this->assertTags($result, $expected);
  5924. $result = $this->Form->create('Contact', array('type' => 'put'));
  5925. $expected = array(
  5926. 'form' => array(
  5927. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5928. 'accept-charset' => $encoding
  5929. ),
  5930. 'div' => array('style' => 'display:none;'),
  5931. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5932. '/div'
  5933. );
  5934. $this->assertTags($result, $expected);
  5935. $result = $this->Form->create('Contact', array('type' => 'file'));
  5936. $expected = array(
  5937. 'form' => array(
  5938. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  5939. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5940. ),
  5941. 'div' => array('style' => 'display:none;'),
  5942. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5943. '/div'
  5944. );
  5945. $this->assertTags($result, $expected);
  5946. $this->Form->request->data['Contact']['id'] = 1;
  5947. $this->Form->request->here = '/contacts/edit/1';
  5948. $this->Form->request['action'] = 'edit';
  5949. $result = $this->Form->create('Contact');
  5950. $expected = array(
  5951. 'form' => array(
  5952. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5953. 'accept-charset' => $encoding
  5954. ),
  5955. 'div' => array('style' => 'display:none;'),
  5956. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5957. '/div'
  5958. );
  5959. $this->assertTags($result, $expected);
  5960. $this->Form->request->data['Contact']['id'] = 1;
  5961. $this->Form->request->here = '/contacts/edit/1';
  5962. $this->Form->request['action'] = 'edit';
  5963. $result = $this->Form->create('Contact', array('type' => 'file'));
  5964. $expected = array(
  5965. 'form' => array(
  5966. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  5967. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  5968. ),
  5969. 'div' => array('style' => 'display:none;'),
  5970. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5971. '/div'
  5972. );
  5973. $this->assertTags($result, $expected);
  5974. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  5975. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  5976. $expected = array(
  5977. 'form' => array(
  5978. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  5979. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  5980. ),
  5981. 'div' => array('style' => 'display:none;'),
  5982. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5983. '/div'
  5984. );
  5985. $this->assertTags($result, $expected);
  5986. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  5987. $expected = array(
  5988. 'form' => array(
  5989. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  5990. 'accept-charset' => $encoding
  5991. ),
  5992. 'div' => array('style' => 'display:none;'),
  5993. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  5994. '/div'
  5995. );
  5996. $this->assertTags($result, $expected);
  5997. $this->Form->request['action'] = 'add';
  5998. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  5999. $expected = array(
  6000. 'form' => array(
  6001. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6002. 'accept-charset' => $encoding
  6003. ),
  6004. 'div' => array('style' => 'display:none;'),
  6005. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6006. '/div'
  6007. );
  6008. $this->assertTags($result, $expected);
  6009. $result = $this->Form->create('User', array('action' => 'login'));
  6010. $expected = array(
  6011. 'form' => array(
  6012. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6013. 'accept-charset' => $encoding
  6014. ),
  6015. 'div' => array('style' => 'display:none;'),
  6016. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6017. '/div'
  6018. );
  6019. $this->assertTags($result, $expected);
  6020. $result = $this->Form->create('User', array('url' => '/users/login'));
  6021. $expected = array(
  6022. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6023. 'div' => array('style' => 'display:none;'),
  6024. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6025. '/div'
  6026. );
  6027. $this->assertTags($result, $expected);
  6028. $this->Form->request['controller'] = 'pages';
  6029. $result = $this->Form->create('User', array('action' => 'signup'));
  6030. $expected = array(
  6031. 'form' => array(
  6032. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6033. 'accept-charset' => $encoding
  6034. ),
  6035. 'div' => array('style' => 'display:none;'),
  6036. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6037. '/div'
  6038. );
  6039. $this->assertTags($result, $expected);
  6040. $this->Form->request->data = array();
  6041. $this->Form->request['controller'] = 'contacts';
  6042. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6043. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6044. $expected = array(
  6045. 'form' => array(
  6046. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6047. 'accept-charset' => 'utf-8'
  6048. ),
  6049. 'div' => array('style' => 'display:none;'),
  6050. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6051. '/div'
  6052. );
  6053. $this->assertTags($result, $expected);
  6054. }
  6055. /**
  6056. * Test the onsubmit option for create()
  6057. *
  6058. * @return void
  6059. */
  6060. public function testCreateOnSubmit() {
  6061. $this->Form->request->data = array();
  6062. $this->Form->request['controller'] = 'contacts';
  6063. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6064. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6065. $expected = array(
  6066. 'form' => array(
  6067. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6068. 'accept-charset' => 'utf-8'
  6069. ),
  6070. 'div' => array('style' => 'display:none;'),
  6071. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6072. '/div'
  6073. );
  6074. $this->assertTags($result, $expected);
  6075. $this->Form->request->data = array();
  6076. $this->Form->request['controller'] = 'contacts';
  6077. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6078. $result = $this->Form->create(array(
  6079. 'url' => array('action' => 'index', 'param'),
  6080. 'default' => false,
  6081. 'onsubmit' => 'someFunction();'
  6082. ));
  6083. $expected = array(
  6084. 'form' => array(
  6085. 'id' => 'ContactAddForm', 'method' => 'post',
  6086. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6087. 'action' => '/contacts/index/param',
  6088. 'accept-charset' => 'utf-8'
  6089. ),
  6090. 'div' => array('style' => 'display:none;'),
  6091. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6092. '/div'
  6093. );
  6094. $this->assertTags($result, $expected);
  6095. }
  6096. /**
  6097. * test create() with automatic url generation
  6098. *
  6099. * @return void
  6100. */
  6101. public function testCreateAutoUrl() {
  6102. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6103. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6104. $this->Form->request->base = '/base_url';
  6105. $result = $this->Form->create('Contact');
  6106. $expected = array(
  6107. 'form' => array(
  6108. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6109. 'accept-charset' => 'utf-8'
  6110. ),
  6111. 'div' => array('style' => 'display:none;'),
  6112. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6113. '/div'
  6114. );
  6115. $this->assertTags($result, $expected);
  6116. $this->Form->request['action'] = 'delete';
  6117. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6118. $this->Form->request->base = '/base_url';
  6119. $result = $this->Form->create('Contact');
  6120. $expected = array(
  6121. 'form' => array(
  6122. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6123. 'accept-charset' => 'utf-8'
  6124. ),
  6125. 'div' => array('style' => 'display:none;'),
  6126. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6127. '/div'
  6128. );
  6129. $this->assertTags($result, $expected);
  6130. }
  6131. /**
  6132. * test create() with a custom route
  6133. *
  6134. * @return void
  6135. */
  6136. public function testCreateCustomRoute() {
  6137. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6138. $encoding = strtolower(Configure::read('App.encoding'));
  6139. $result = $this->Form->create('User', array('action' => 'login'));
  6140. $expected = array(
  6141. 'form' => array(
  6142. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6143. 'accept-charset' => $encoding
  6144. ),
  6145. 'div' => array('style' => 'display:none;'),
  6146. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6147. '/div'
  6148. );
  6149. $this->assertTags($result, $expected);
  6150. }
  6151. /**
  6152. * test that inputDefaults are stored and used.
  6153. *
  6154. * @return void
  6155. */
  6156. public function testCreateWithInputDefaults() {
  6157. $this->Form->create('User', array(
  6158. 'inputDefaults' => array(
  6159. 'div' => false,
  6160. 'label' => false,
  6161. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6162. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6163. )
  6164. ));
  6165. $result = $this->Form->input('username');
  6166. $expected = array(
  6167. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6168. );
  6169. $this->assertTags($result, $expected);
  6170. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6171. $expected = array(
  6172. 'div' => array('class' => 'input text'),
  6173. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6174. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6175. '/div'
  6176. );
  6177. $this->assertTags($result, $expected);
  6178. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6179. $expected = array(
  6180. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6181. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6182. );
  6183. $this->assertTags($result, $expected);
  6184. $this->Form->create('User', array(
  6185. 'inputDefaults' => array(
  6186. 'div' => false,
  6187. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6188. )
  6189. ));
  6190. $result = $this->Form->input('username', array('div' => true));
  6191. $expected = array(
  6192. 'div' => array('class' => 'input text'),
  6193. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6194. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6195. '/div'
  6196. );
  6197. $this->assertTags($result, $expected);
  6198. }
  6199. /**
  6200. * test automatic accept-charset overriding
  6201. *
  6202. * @return void
  6203. */
  6204. public function testCreateWithAcceptCharset() {
  6205. $result = $this->Form->create('UserForm', array(
  6206. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6207. )
  6208. );
  6209. $expected = array(
  6210. 'form' => array(
  6211. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6212. 'accept-charset' => 'iso-8859-1'
  6213. ),
  6214. 'div' => array('style' => 'display:none;'),
  6215. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6216. '/div'
  6217. );
  6218. $this->assertTags($result, $expected);
  6219. }
  6220. /**
  6221. * Test base form url when url param is passed with multiple parameters (&)
  6222. *
  6223. */
  6224. public function testCreateQuerystringrequest() {
  6225. $encoding = strtolower(Configure::read('App.encoding'));
  6226. $result = $this->Form->create('Contact', array(
  6227. 'type' => 'post',
  6228. 'escape' => false,
  6229. 'url' => array(
  6230. 'controller' => 'controller',
  6231. 'action' => 'action',
  6232. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6233. )
  6234. ));
  6235. $expected = array(
  6236. 'form' => array(
  6237. 'id' => 'ContactAddForm',
  6238. 'method' => 'post',
  6239. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6240. 'accept-charset' => $encoding
  6241. ),
  6242. 'div' => array('style' => 'display:none;'),
  6243. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6244. '/div'
  6245. );
  6246. $this->assertTags($result, $expected);
  6247. $result = $this->Form->create('Contact', array(
  6248. 'type' => 'post',
  6249. 'url' => array(
  6250. 'controller' => 'controller',
  6251. 'action' => 'action',
  6252. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6253. )
  6254. ));
  6255. $expected = array(
  6256. 'form' => array(
  6257. 'id' => 'ContactAddForm',
  6258. 'method' => 'post',
  6259. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6260. 'accept-charset' => $encoding
  6261. ),
  6262. 'div' => array('style' => 'display:none;'),
  6263. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6264. '/div'
  6265. );
  6266. $this->assertTags($result, $expected);
  6267. }
  6268. /**
  6269. * test that create() doesn't cause errors by multiple id's being in the primary key
  6270. * as could happen with multiple select or checkboxes.
  6271. *
  6272. * @return void
  6273. */
  6274. public function testCreateWithMultipleIdInData() {
  6275. $encoding = strtolower(Configure::read('App.encoding'));
  6276. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6277. $result = $this->Form->create('Contact');
  6278. $expected = array(
  6279. 'form' => array(
  6280. 'id' => 'ContactAddForm',
  6281. 'method' => 'post',
  6282. 'action' => '/contacts/add',
  6283. 'accept-charset' => $encoding
  6284. ),
  6285. 'div' => array('style' => 'display:none;'),
  6286. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6287. '/div'
  6288. );
  6289. $this->assertTags($result, $expected);
  6290. }
  6291. /**
  6292. * test that create() doesn't add in extra passed params.
  6293. *
  6294. * @return void
  6295. */
  6296. public function testCreatePassedArgs() {
  6297. $encoding = strtolower(Configure::read('App.encoding'));
  6298. $this->Form->request->data['Contact']['id'] = 1;
  6299. $result = $this->Form->create('Contact', array(
  6300. 'type' => 'post',
  6301. 'escape' => false,
  6302. 'url' => array(
  6303. 'action' => 'edit',
  6304. 'myparam'
  6305. )
  6306. ));
  6307. $expected = array(
  6308. 'form' => array(
  6309. 'id' => 'ContactAddForm',
  6310. 'method' => 'post',
  6311. 'action' => '/contacts/edit/myparam',
  6312. 'accept-charset' => $encoding
  6313. ),
  6314. 'div' => array('style' => 'display:none;'),
  6315. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6316. '/div'
  6317. );
  6318. $this->assertTags($result, $expected);
  6319. }
  6320. /**
  6321. * test creating a get form, and get form inputs.
  6322. *
  6323. * @return void
  6324. */
  6325. public function testGetFormCreate() {
  6326. $encoding = strtolower(Configure::read('App.encoding'));
  6327. $result = $this->Form->create('Contact', array('type' => 'get'));
  6328. $this->assertTags($result, array('form' => array(
  6329. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6330. 'accept-charset' => $encoding
  6331. )));
  6332. $result = $this->Form->text('Contact.name');
  6333. $this->assertTags($result, array('input' => array(
  6334. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6335. )));
  6336. $result = $this->Form->password('password');
  6337. $this->assertTags($result, array('input' => array(
  6338. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6339. )));
  6340. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6341. $result = $this->Form->text('user_form');
  6342. $this->assertTags($result, array('input' => array(
  6343. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6344. )));
  6345. }
  6346. /**
  6347. * test get form, and inputs when the model param is false
  6348. *
  6349. * @return void
  6350. */
  6351. public function testGetFormWithFalseModel() {
  6352. $encoding = strtolower(Configure::read('App.encoding'));
  6353. $this->Form->request['controller'] = 'contact_test';
  6354. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6355. $expected = array('form' => array(
  6356. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6357. 'accept-charset' => $encoding
  6358. ));
  6359. $this->assertTags($result, $expected);
  6360. $result = $this->Form->text('reason');
  6361. $expected = array(
  6362. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6363. );
  6364. $this->assertTags($result, $expected);
  6365. }
  6366. /**
  6367. * test that datetime() works with GET style forms.
  6368. *
  6369. * @return void
  6370. */
  6371. public function testDateTimeWithGetForms() {
  6372. extract($this->dateRegex);
  6373. $this->Form->create('Contact', array('type' => 'get'));
  6374. $result = $this->Form->datetime('created');
  6375. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6376. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6377. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6378. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6379. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6380. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6381. }
  6382. /**
  6383. * testEditFormWithData method
  6384. *
  6385. * test auto populating form elements from submitted data.
  6386. *
  6387. * @return void
  6388. */
  6389. public function testEditFormWithData() {
  6390. $this->Form->request->data = array('Person' => array(
  6391. 'id' => 1,
  6392. 'first_name' => 'Nate',
  6393. 'last_name' => 'Abele',
  6394. 'email' => 'nate@example.com'
  6395. ));
  6396. $this->Form->request->addParams(array(
  6397. 'models' => array('Person'),
  6398. 'controller' => 'people',
  6399. 'action' => 'add'
  6400. ));
  6401. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6402. $this->Form->create();
  6403. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6404. $expected = array(
  6405. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6406. 'select' => array(
  6407. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6408. ),
  6409. array('option' => array('value' => 1)), 'Nate', '/option',
  6410. array('option' => array('value' => 2)), 'Garrett', '/option',
  6411. array('option' => array('value' => 3)), 'Larry', '/option',
  6412. '/select'
  6413. );
  6414. $this->assertTags($result, $expected);
  6415. }
  6416. /**
  6417. * Test that required fields are created for various types of validation.
  6418. *
  6419. * @return void
  6420. */
  6421. public function testFormInputRequiredDetection() {
  6422. $this->Form->create('Contact');
  6423. $result = $this->Form->input('Contact.non_existing');
  6424. $expected = array(
  6425. 'div' => array('class' => 'input text'),
  6426. 'label' => array('for' => 'ContactNonExisting'),
  6427. 'Non Existing',
  6428. '/label',
  6429. 'input' => array(
  6430. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6431. 'id' => 'ContactNonExisting'
  6432. ),
  6433. '/div'
  6434. );
  6435. $this->assertTags($result, $expected);
  6436. $result = $this->Form->input('Contact.imrequired');
  6437. $expected = array(
  6438. 'div' => array('class' => 'input text required'),
  6439. 'label' => array('for' => 'ContactImrequired'),
  6440. 'Imrequired',
  6441. '/label',
  6442. 'input' => array(
  6443. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6444. 'id' => 'ContactImrequired'
  6445. ),
  6446. '/div'
  6447. );
  6448. $this->assertTags($result, $expected);
  6449. $result = $this->Form->input('Contact.imalsorequired');
  6450. $expected = array(
  6451. 'div' => array('class' => 'input text required'),
  6452. 'label' => array('for' => 'ContactImalsorequired'),
  6453. 'Imalsorequired',
  6454. '/label',
  6455. 'input' => array(
  6456. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6457. 'id' => 'ContactImalsorequired'
  6458. ),
  6459. '/div'
  6460. );
  6461. $this->assertTags($result, $expected);
  6462. $result = $this->Form->input('Contact.imrequiredtoo');
  6463. $expected = array(
  6464. 'div' => array('class' => 'input text required'),
  6465. 'label' => array('for' => 'ContactImrequiredtoo'),
  6466. 'Imrequiredtoo',
  6467. '/label',
  6468. 'input' => array(
  6469. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6470. 'id' => 'ContactImrequiredtoo'
  6471. ),
  6472. '/div'
  6473. );
  6474. $this->assertTags($result, $expected);
  6475. $result = $this->Form->input('Contact.required_one');
  6476. $expected = array(
  6477. 'div' => array('class' => 'input text required'),
  6478. 'label' => array('for' => 'ContactRequiredOne'),
  6479. 'Required One',
  6480. '/label',
  6481. 'input' => array(
  6482. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6483. 'id' => 'ContactRequiredOne'
  6484. ),
  6485. '/div'
  6486. );
  6487. $this->assertTags($result, $expected);
  6488. $result = $this->Form->input('Contact.string_required');
  6489. $expected = array(
  6490. 'div' => array('class' => 'input text required'),
  6491. 'label' => array('for' => 'ContactStringRequired'),
  6492. 'String Required',
  6493. '/label',
  6494. 'input' => array(
  6495. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6496. 'id' => 'ContactStringRequired'
  6497. ),
  6498. '/div'
  6499. );
  6500. $this->assertTags($result, $expected);
  6501. $result = $this->Form->input('Contact.imnotrequired');
  6502. $expected = array(
  6503. 'div' => array('class' => 'input text'),
  6504. 'label' => array('for' => 'ContactImnotrequired'),
  6505. 'Imnotrequired',
  6506. '/label',
  6507. 'input' => array(
  6508. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6509. 'id' => 'ContactImnotrequired'
  6510. ),
  6511. '/div'
  6512. );
  6513. $this->assertTags($result, $expected);
  6514. $result = $this->Form->input('Contact.imalsonotrequired');
  6515. $expected = array(
  6516. 'div' => array('class' => 'input text'),
  6517. 'label' => array('for' => 'ContactImalsonotrequired'),
  6518. 'Imalsonotrequired',
  6519. '/label',
  6520. 'input' => array(
  6521. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6522. 'id' => 'ContactImalsonotrequired'
  6523. ),
  6524. '/div'
  6525. );
  6526. $this->assertTags($result, $expected);
  6527. $result = $this->Form->input('Contact.imnotrequiredeither');
  6528. $expected = array(
  6529. 'div' => array('class' => 'input text'),
  6530. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6531. 'Imnotrequiredeither',
  6532. '/label',
  6533. 'input' => array(
  6534. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6535. 'id' => 'ContactImnotrequiredeither'
  6536. ),
  6537. '/div'
  6538. );
  6539. $this->assertTags($result, $expected);
  6540. }
  6541. /**
  6542. * testFormMagicInput method
  6543. *
  6544. * @return void
  6545. */
  6546. public function testFormMagicInput() {
  6547. $encoding = strtolower(Configure::read('App.encoding'));
  6548. $result = $this->Form->create('Contact');
  6549. $expected = array(
  6550. 'form' => array(
  6551. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6552. 'accept-charset' => $encoding
  6553. ),
  6554. 'div' => array('style' => 'display:none;'),
  6555. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6556. '/div'
  6557. );
  6558. $this->assertTags($result, $expected);
  6559. $result = $this->Form->input('name');
  6560. $expected = array(
  6561. 'div' => array('class' => 'input text'),
  6562. 'label' => array('for' => 'ContactName'),
  6563. 'Name',
  6564. '/label',
  6565. 'input' => array(
  6566. 'type' => 'text', 'name' => 'data[Contact][name]',
  6567. 'id' => 'ContactName', 'maxlength' => '255'
  6568. ),
  6569. '/div'
  6570. );
  6571. $this->assertTags($result, $expected);
  6572. $result = $this->Form->input('non_existing_field_in_contact_model');
  6573. $expected = array(
  6574. 'div' => array('class' => 'input text'),
  6575. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6576. 'Non Existing Field In Contact Model',
  6577. '/label',
  6578. 'input' => array(
  6579. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6580. 'id' => 'ContactNonExistingFieldInContactModel'
  6581. ),
  6582. '/div'
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. $result = $this->Form->input('Address.street');
  6586. $expected = array(
  6587. 'div' => array('class' => 'input text'),
  6588. 'label' => array('for' => 'AddressStreet'),
  6589. 'Street',
  6590. '/label',
  6591. 'input' => array(
  6592. 'type' => 'text', 'name' => 'data[Address][street]',
  6593. 'id' => 'AddressStreet'
  6594. ),
  6595. '/div'
  6596. );
  6597. $this->assertTags($result, $expected);
  6598. $result = $this->Form->input('Address.non_existing_field_in_model');
  6599. $expected = array(
  6600. 'div' => array('class' => 'input text'),
  6601. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6602. 'Non Existing Field In Model',
  6603. '/label',
  6604. 'input' => array(
  6605. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6606. 'id' => 'AddressNonExistingFieldInModel'
  6607. ),
  6608. '/div'
  6609. );
  6610. $this->assertTags($result, $expected);
  6611. $result = $this->Form->input('name', array('div' => false));
  6612. $expected = array(
  6613. 'label' => array('for' => 'ContactName'),
  6614. 'Name',
  6615. '/label',
  6616. 'input' => array(
  6617. 'type' => 'text', 'name' => 'data[Contact][name]',
  6618. 'id' => 'ContactName', 'maxlength' => '255'
  6619. )
  6620. );
  6621. $this->assertTags($result, $expected);
  6622. extract($this->dateRegex);
  6623. $now = strtotime('now');
  6624. $result = $this->Form->input('Contact.published', array('div' => false));
  6625. $expected = array(
  6626. 'label' => array('for' => 'ContactPublishedMonth'),
  6627. 'Published',
  6628. '/label',
  6629. array('select' => array(
  6630. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6631. )),
  6632. $monthsRegex,
  6633. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6634. date('F', $now),
  6635. '/option',
  6636. '*/select',
  6637. '-',
  6638. array('select' => array(
  6639. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6640. )),
  6641. $daysRegex,
  6642. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6643. date('j', $now),
  6644. '/option',
  6645. '*/select',
  6646. '-',
  6647. array('select' => array(
  6648. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6649. )),
  6650. $yearsRegex,
  6651. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6652. date('Y', $now),
  6653. '*/select'
  6654. );
  6655. $this->assertTags($result, $expected);
  6656. $result = $this->Form->input('Contact.updated', array('div' => false));
  6657. $expected = array(
  6658. 'label' => array('for' => 'ContactUpdatedMonth'),
  6659. 'Updated',
  6660. '/label',
  6661. array('select' => array(
  6662. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6663. )),
  6664. $monthsRegex,
  6665. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6666. date('F', $now),
  6667. '/option',
  6668. '*/select',
  6669. '-',
  6670. array('select' => array(
  6671. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6672. )),
  6673. $daysRegex,
  6674. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6675. date('j', $now),
  6676. '/option',
  6677. '*/select',
  6678. '-',
  6679. array('select' => array(
  6680. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6681. )),
  6682. $yearsRegex,
  6683. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6684. date('Y', $now),
  6685. '*/select'
  6686. );
  6687. $this->assertTags($result, $expected);
  6688. $result = $this->Form->input('UserForm.stuff');
  6689. $expected = array(
  6690. 'div' => array('class' => 'input text'),
  6691. 'label' => array('for' => 'UserFormStuff'),
  6692. 'Stuff',
  6693. '/label',
  6694. 'input' => array(
  6695. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6696. 'id' => 'UserFormStuff', 'maxlength' => 10
  6697. ),
  6698. '/div'
  6699. );
  6700. $this->assertTags($result, $expected);
  6701. }
  6702. /**
  6703. * testForMagicInputNonExistingNorValidated method
  6704. *
  6705. * @return void
  6706. */
  6707. public function testForMagicInputNonExistingNorValidated() {
  6708. $encoding = strtolower(Configure::read('App.encoding'));
  6709. $result = $this->Form->create('Contact');
  6710. $expected = array(
  6711. 'form' => array(
  6712. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6713. 'accept-charset' => $encoding
  6714. ),
  6715. 'div' => array('style' => 'display:none;'),
  6716. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6717. '/div'
  6718. );
  6719. $this->assertTags($result, $expected);
  6720. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6721. $expected = array(
  6722. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6723. 'Non Existing Nor Validated',
  6724. '/label',
  6725. 'input' => array(
  6726. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6727. 'id' => 'ContactNonExistingNorValidated'
  6728. )
  6729. );
  6730. $this->assertTags($result, $expected);
  6731. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6732. 'div' => false, 'value' => 'my value'
  6733. ));
  6734. $expected = array(
  6735. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6736. 'Non Existing Nor Validated',
  6737. '/label',
  6738. 'input' => array(
  6739. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6740. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6741. )
  6742. );
  6743. $this->assertTags($result, $expected);
  6744. $this->Form->request->data = array(
  6745. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6746. ));
  6747. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6748. $expected = array(
  6749. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6750. 'Non Existing Nor Validated',
  6751. '/label',
  6752. 'input' => array(
  6753. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6754. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6755. )
  6756. );
  6757. $this->assertTags($result, $expected);
  6758. }
  6759. /**
  6760. * testFormMagicInputLabel method
  6761. *
  6762. * @return void
  6763. */
  6764. public function testFormMagicInputLabel() {
  6765. $encoding = strtolower(Configure::read('App.encoding'));
  6766. $result = $this->Form->create('Contact');
  6767. $expected = array(
  6768. 'form' => array(
  6769. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6770. 'accept-charset' => $encoding
  6771. ),
  6772. 'div' => array('style' => 'display:none;'),
  6773. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6774. '/div'
  6775. );
  6776. $this->assertTags($result, $expected);
  6777. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6778. $this->assertTags($result, array('input' => array(
  6779. 'name' => 'data[Contact][name]', 'type' => 'text',
  6780. 'id' => 'ContactName', 'maxlength' => '255')
  6781. ));
  6782. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6783. $expected = array(
  6784. 'label' => array('for' => 'ContactName'),
  6785. 'My label',
  6786. '/label',
  6787. 'input' => array(
  6788. 'type' => 'text', 'name' => 'data[Contact][name]',
  6789. 'id' => 'ContactName', 'maxlength' => '255'
  6790. )
  6791. );
  6792. $this->assertTags($result, $expected);
  6793. $result = $this->Form->input('Contact.name', array(
  6794. 'div' => false, 'label' => array('class' => 'mandatory')
  6795. ));
  6796. $expected = array(
  6797. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6798. 'Name',
  6799. '/label',
  6800. 'input' => array(
  6801. 'type' => 'text', 'name' => 'data[Contact][name]',
  6802. 'id' => 'ContactName', 'maxlength' => '255'
  6803. )
  6804. );
  6805. $this->assertTags($result, $expected);
  6806. $result = $this->Form->input('Contact.name', array(
  6807. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6808. ));
  6809. $expected = array(
  6810. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6811. 'My label',
  6812. '/label',
  6813. 'input' => array(
  6814. 'type' => 'text', 'name' => 'data[Contact][name]',
  6815. 'id' => 'ContactName', 'maxlength' => '255'
  6816. )
  6817. );
  6818. $this->assertTags($result, $expected);
  6819. $result = $this->Form->input('Contact.name', array(
  6820. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6821. ));
  6822. $expected = array(
  6823. 'label' => array('for' => 'my_id'),
  6824. 'Name',
  6825. '/label',
  6826. 'input' => array(
  6827. 'type' => 'text', 'name' => 'data[Contact][name]',
  6828. 'id' => 'my_id', 'maxlength' => '255'
  6829. )
  6830. );
  6831. $this->assertTags($result, $expected);
  6832. $result = $this->Form->input('1.id');
  6833. $this->assertTags($result, array('input' => array(
  6834. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6835. 'id' => 'Contact1Id'
  6836. )));
  6837. $result = $this->Form->input("1.name");
  6838. $expected = array(
  6839. 'div' => array('class' => 'input text'),
  6840. 'label' => array('for' => 'Contact1Name'),
  6841. 'Name',
  6842. '/label',
  6843. 'input' => array(
  6844. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  6845. 'id' => 'Contact1Name', 'maxlength' => '255'
  6846. ),
  6847. '/div'
  6848. );
  6849. $this->assertTags($result, $expected);
  6850. $result = $this->Form->input('Contact.1.id');
  6851. $this->assertTags($result, array(
  6852. 'input' => array(
  6853. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  6854. 'id' => 'Contact1Id'
  6855. )
  6856. ));
  6857. $result = $this->Form->input("Model.1.name");
  6858. $expected = array(
  6859. 'div' => array('class' => 'input text'),
  6860. 'label' => array('for' => 'Model1Name'),
  6861. 'Name',
  6862. '/label',
  6863. 'input' => array(
  6864. 'type' => 'text', 'name' => 'data[Model][1][name]',
  6865. 'id' => 'Model1Name'
  6866. ),
  6867. '/div'
  6868. );
  6869. $this->assertTags($result, $expected);
  6870. }
  6871. /**
  6872. * testFormEnd method
  6873. *
  6874. * @return void
  6875. */
  6876. public function testFormEnd() {
  6877. $this->assertEquals('</form>', $this->Form->end());
  6878. $result = $this->Form->end('');
  6879. $expected = array(
  6880. 'div' => array('class' => 'submit'),
  6881. 'input' => array('type' => 'submit', 'value' => ''),
  6882. '/div',
  6883. '/form'
  6884. );
  6885. $this->assertTags($result, $expected);
  6886. $result = $this->Form->end(array('label' => ''));
  6887. $expected = array(
  6888. 'div' => array('class' => 'submit'),
  6889. 'input' => array('type' => 'submit', 'value' => ''),
  6890. '/div',
  6891. '/form'
  6892. );
  6893. $this->assertTags($result, $expected);
  6894. $result = $this->Form->end('save');
  6895. $expected = array(
  6896. 'div' => array('class' => 'submit'),
  6897. 'input' => array('type' => 'submit', 'value' => 'save'),
  6898. '/div',
  6899. '/form'
  6900. );
  6901. $this->assertTags($result, $expected);
  6902. $result = $this->Form->end(array('label' => 'save'));
  6903. $expected = array(
  6904. 'div' => array('class' => 'submit'),
  6905. 'input' => array('type' => 'submit', 'value' => 'save'),
  6906. '/div',
  6907. '/form'
  6908. );
  6909. $this->assertTags($result, $expected);
  6910. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  6911. $expected = array(
  6912. 'div' => array('class' => 'submit'),
  6913. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6914. '/div',
  6915. '/form'
  6916. );
  6917. $this->assertTags($result, $expected);
  6918. $result = $this->Form->end(array('name' => 'Whatever'));
  6919. $expected = array(
  6920. 'div' => array('class' => 'submit'),
  6921. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  6922. '/div',
  6923. '/form'
  6924. );
  6925. $this->assertTags($result, $expected);
  6926. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  6927. $expected = array(
  6928. 'div' => array('class' => 'good'),
  6929. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6930. '/div',
  6931. '/form'
  6932. );
  6933. $this->assertTags($result, $expected);
  6934. $result = $this->Form->end(array(
  6935. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  6936. ));
  6937. $expected = array(
  6938. 'div' => array('class' => 'good'),
  6939. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  6940. '/div',
  6941. '/form'
  6942. );
  6943. $this->assertTags($result, $expected);
  6944. }
  6945. /**
  6946. * testMultipleFormWithIdFields method
  6947. *
  6948. * @return void
  6949. */
  6950. public function testMultipleFormWithIdFields() {
  6951. $this->Form->create('UserForm');
  6952. $result = $this->Form->input('id');
  6953. $this->assertTags($result, array('input' => array(
  6954. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  6955. )));
  6956. $result = $this->Form->input('ValidateItem.id');
  6957. $this->assertTags($result, array('input' => array(
  6958. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  6959. 'id' => 'ValidateItemId'
  6960. )));
  6961. $result = $this->Form->input('ValidateUser.id');
  6962. $this->assertTags($result, array('input' => array(
  6963. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  6964. 'id' => 'ValidateUserId'
  6965. )));
  6966. }
  6967. /**
  6968. * testDbLessModel method
  6969. *
  6970. * @return void
  6971. */
  6972. public function testDbLessModel() {
  6973. $this->Form->create('TestMail');
  6974. $result = $this->Form->input('name');
  6975. $expected = array(
  6976. 'div' => array('class' => 'input text'),
  6977. 'label' => array('for' => 'TestMailName'),
  6978. 'Name',
  6979. '/label',
  6980. 'input' => array(
  6981. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6982. 'id' => 'TestMailName'
  6983. ),
  6984. '/div'
  6985. );
  6986. $this->assertTags($result, $expected);
  6987. ClassRegistry::init('TestMail');
  6988. $this->Form->create('TestMail');
  6989. $result = $this->Form->input('name');
  6990. $expected = array(
  6991. 'div' => array('class' => 'input text'),
  6992. 'label' => array('for' => 'TestMailName'),
  6993. 'Name',
  6994. '/label',
  6995. 'input' => array(
  6996. 'name' => 'data[TestMail][name]', 'type' => 'text',
  6997. 'id' => 'TestMailName'
  6998. ),
  6999. '/div'
  7000. );
  7001. $this->assertTags($result, $expected);
  7002. }
  7003. /**
  7004. * testBrokenness method
  7005. *
  7006. * @return void
  7007. */
  7008. public function testBrokenness() {
  7009. /*
  7010. * #4 This test has two parents and four children. By default (as of r7117) both
  7011. * parents are show but the first parent is missing a child. This is the inconsistency
  7012. * in the default behaviour - one parent has all children, the other does not - dependent
  7013. * on the data values.
  7014. */
  7015. $result = $this->Form->select('Model.field', array(
  7016. 'Fred' => array(
  7017. 'freds_son_1' => 'Fred',
  7018. 'freds_son_2' => 'Freddie'
  7019. ),
  7020. 'Bert' => array(
  7021. 'berts_son_1' => 'Albert',
  7022. 'berts_son_2' => 'Bertie')
  7023. ),
  7024. array('showParents' => true, 'empty' => false)
  7025. );
  7026. $expected = array(
  7027. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7028. array('optgroup' => array('label' => 'Fred')),
  7029. array('option' => array('value' => 'freds_son_1')),
  7030. 'Fred',
  7031. '/option',
  7032. array('option' => array('value' => 'freds_son_2')),
  7033. 'Freddie',
  7034. '/option',
  7035. '/optgroup',
  7036. array('optgroup' => array('label' => 'Bert')),
  7037. array('option' => array('value' => 'berts_son_1')),
  7038. 'Albert',
  7039. '/option',
  7040. array('option' => array('value' => 'berts_son_2')),
  7041. 'Bertie',
  7042. '/option',
  7043. '/optgroup',
  7044. '/select'
  7045. );
  7046. $this->assertTags($result, $expected);
  7047. /*
  7048. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7049. * changed, so we should expect the same select list data, just with a different name
  7050. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7051. * This is where data corruption can occur, because when a select value is missing from
  7052. * a list a form will substitute the first value in the list - without the user knowing.
  7053. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7054. * affect the availability of 3 => 'Three' as a valid option.
  7055. */
  7056. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7057. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7058. ));
  7059. $result = $this->Form->select(
  7060. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7061. );
  7062. $expected = array(
  7063. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7064. array('option' => array('value' => 1)),
  7065. 'One',
  7066. '/option',
  7067. array('option' => array('value' => 2)),
  7068. 'Two',
  7069. '/option',
  7070. array('optgroup' => array('label' => 'Three')),
  7071. array('option' => array('value' => 3)),
  7072. 'Three',
  7073. '/option',
  7074. array('option' => array('value' => 4)),
  7075. 'Four',
  7076. '/option',
  7077. array('option' => array('value' => 5)),
  7078. 'Five',
  7079. '/option',
  7080. '/optgroup',
  7081. '/select'
  7082. );
  7083. $this->assertTags($result, $expected);
  7084. }
  7085. /**
  7086. * Test the generation of fields for a multi record form.
  7087. *
  7088. * @return void
  7089. */
  7090. public function testMultiRecordForm() {
  7091. $this->Form->create('ValidateProfile');
  7092. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7093. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7094. $expected = array(
  7095. 'div' => array('class' => 'input textarea'),
  7096. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7097. 'Name',
  7098. '/label',
  7099. 'textarea' => array(
  7100. 'id' => 'ValidateProfile1ValidateItem2Name',
  7101. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7102. 'cols' => 30,
  7103. 'rows' => 6
  7104. ),
  7105. 'Value',
  7106. '/textarea',
  7107. '/div'
  7108. );
  7109. $this->assertTags($result, $expected);
  7110. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7111. $expected = array(
  7112. 'div' => array('class' => 'input date'),
  7113. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7114. 'Created',
  7115. '/label',
  7116. array('select' => array(
  7117. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7118. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7119. )
  7120. ),
  7121. array('option' => array('value' => '')), '/option',
  7122. $this->dateRegex['monthsRegex'],
  7123. '/select', '-',
  7124. array('select' => array(
  7125. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7126. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7127. )
  7128. ),
  7129. array('option' => array('value' => '')), '/option',
  7130. $this->dateRegex['daysRegex'],
  7131. '/select', '-',
  7132. array('select' => array(
  7133. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7134. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7135. )
  7136. ),
  7137. array('option' => array('value' => '')), '/option',
  7138. $this->dateRegex['yearsRegex'],
  7139. '/select',
  7140. '/div'
  7141. );
  7142. $this->assertTags($result, $expected);
  7143. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7144. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7145. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7146. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7147. $expected = array(
  7148. 'div' => array('class' => 'input select error'),
  7149. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7150. 'Profile',
  7151. '/label',
  7152. 'select' => array(
  7153. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7154. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7155. 'class' => 'form-error'
  7156. ),
  7157. '/select',
  7158. array('div' => array('class' => 'error-message')),
  7159. 'Error',
  7160. '/div',
  7161. '/div'
  7162. );
  7163. $this->assertTags($result, $expected);
  7164. }
  7165. /**
  7166. * test the correct display of multi-record form validation errors.
  7167. *
  7168. * @return void
  7169. */
  7170. public function testMultiRecordFormValidationErrors() {
  7171. $this->Form->create('ValidateProfile');
  7172. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7173. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7174. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7175. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7176. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7177. $result = $this->Form->error('ValidateProfile.2.city');
  7178. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7179. $result = $this->Form->error('2.city');
  7180. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7181. }
  7182. /**
  7183. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7184. *
  7185. * @return void
  7186. */
  7187. public function testInputTemplate() {
  7188. $result = $this->Form->input('Contact.email', array(
  7189. 'type' => 'text', 'format' => array('input')
  7190. ));
  7191. $expected = array(
  7192. 'div' => array('class' => 'input text'),
  7193. 'input' => array(
  7194. 'type' => 'text', 'name' => 'data[Contact][email]',
  7195. 'id' => 'ContactEmail'
  7196. ),
  7197. '/div'
  7198. );
  7199. $this->assertTags($result, $expected);
  7200. $result = $this->Form->input('Contact.email', array(
  7201. 'type' => 'text', 'format' => array('input', 'label'),
  7202. 'label' => '<em>Email (required)</em>'
  7203. ));
  7204. $expected = array(
  7205. 'div' => array('class' => 'input text'),
  7206. array('input' => array(
  7207. 'type' => 'text', 'name' => 'data[Contact][email]',
  7208. 'id' => 'ContactEmail'
  7209. )),
  7210. 'label' => array('for' => 'ContactEmail'),
  7211. 'em' => array(),
  7212. 'Email (required)',
  7213. '/em',
  7214. '/label',
  7215. '/div'
  7216. );
  7217. $this->assertTags($result, $expected);
  7218. $result = $this->Form->input('Contact.email', array(
  7219. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7220. 'between' => '<div>Something in the middle</div>',
  7221. 'after' => '<span>Some text at the end</span>'
  7222. ));
  7223. $expected = array(
  7224. 'div' => array('class' => 'input text'),
  7225. array('input' => array(
  7226. 'type' => 'text', 'name' => 'data[Contact][email]',
  7227. 'id' => 'ContactEmail'
  7228. )),
  7229. array('div' => array()),
  7230. 'Something in the middle',
  7231. '/div',
  7232. 'label' => array('for' => 'ContactEmail'),
  7233. 'Email',
  7234. '/label',
  7235. 'span' => array(),
  7236. 'Some text at the end',
  7237. '/span',
  7238. '/div'
  7239. );
  7240. $this->assertTags($result, $expected);
  7241. $result = $this->Form->input('Contact.method', array(
  7242. 'type' => 'radio',
  7243. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7244. 'between' => 'I am between',
  7245. ));
  7246. $expected = array(
  7247. 'div' => array('class' => 'input radio'),
  7248. 'fieldset' => array(),
  7249. 'legend' => array(),
  7250. 'Method',
  7251. '/legend',
  7252. 'I am between',
  7253. 'input' => array(
  7254. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7255. 'value' => '', 'id' => 'ContactMethod_'
  7256. ),
  7257. array('input' => array(
  7258. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7259. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7260. )),
  7261. array('label' => array('for' => 'ContactMethodEmail')),
  7262. 'Email',
  7263. '/label',
  7264. array('input' => array(
  7265. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7266. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7267. )),
  7268. array('label' => array('for' => 'ContactMethodPigeon')),
  7269. 'Pigeon',
  7270. '/label',
  7271. '/fieldset',
  7272. '/div',
  7273. );
  7274. $this->assertTags($result, $expected);
  7275. }
  7276. /**
  7277. * test that some html5 inputs + FormHelper::__call() work
  7278. *
  7279. * @return void
  7280. */
  7281. public function testHtml5Inputs() {
  7282. $result = $this->Form->email('User.email');
  7283. $expected = array(
  7284. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7285. );
  7286. $this->assertTags($result, $expected);
  7287. $result = $this->Form->search('User.query');
  7288. $expected = array(
  7289. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7290. );
  7291. $this->assertTags($result, $expected);
  7292. $result = $this->Form->search('User.query', array('value' => 'test'));
  7293. $expected = array(
  7294. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7295. );
  7296. $this->assertTags($result, $expected);
  7297. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7298. $expected = array(
  7299. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7300. );
  7301. $this->assertTags($result, $expected);
  7302. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7303. $expected = array(
  7304. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7305. );
  7306. $this->assertTags($result, $expected);
  7307. }
  7308. /**
  7309. *
  7310. * @expectedException CakeException
  7311. * @return void
  7312. */
  7313. public function testHtml5InputException() {
  7314. $this->Form->email();
  7315. }
  7316. /**
  7317. * Tests that a model can be loaded from the model names passed in the request object
  7318. *
  7319. * @return void
  7320. */
  7321. public function testIntrospectModelFromRequest() {
  7322. $this->loadFixtures('Post');
  7323. App::build(array(
  7324. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7325. ));
  7326. CakePlugin::load('TestPlugin');
  7327. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7328. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7329. $this->Form->create('TestPluginPost');
  7330. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7331. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7332. CakePlugin::unload();
  7333. App::build();
  7334. }
  7335. /**
  7336. * Tests that it is possible to set the validation errors directly in the helper for a field
  7337. *
  7338. * @return void
  7339. */
  7340. public function testCustomValidationErrors() {
  7341. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7342. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7343. $this->assertEquals('Badness!', $result);
  7344. }
  7345. /**
  7346. * Tests that the 'on' key validates as expected on create
  7347. *
  7348. * @return void
  7349. */
  7350. public function testRequiredOnCreate() {
  7351. $this->Form->create('Contact');
  7352. $result = $this->Form->input('Contact.imrequiredonupdate');
  7353. $expected = array(
  7354. 'div' => array('class' => 'input text'),
  7355. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7356. 'Imrequiredonupdate',
  7357. '/label',
  7358. 'input' => array(
  7359. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7360. 'id' => 'ContactImrequiredonupdate'
  7361. ),
  7362. '/div'
  7363. );
  7364. $this->assertTags($result, $expected);
  7365. $result = $this->Form->input('Contact.imrequiredoncreate');
  7366. $expected = array(
  7367. 'div' => array('class' => 'input text required'),
  7368. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7369. 'Imrequiredoncreate',
  7370. '/label',
  7371. 'input' => array(
  7372. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7373. 'id' => 'ContactImrequiredoncreate'
  7374. ),
  7375. '/div'
  7376. );
  7377. $this->assertTags($result, $expected);
  7378. $result = $this->Form->input('Contact.imrequiredonboth');
  7379. $expected = array(
  7380. 'div' => array('class' => 'input text required'),
  7381. 'label' => array('for' => 'ContactImrequiredonboth'),
  7382. 'Imrequiredonboth',
  7383. '/label',
  7384. 'input' => array(
  7385. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7386. 'id' => 'ContactImrequiredonboth'
  7387. ),
  7388. '/div'
  7389. );
  7390. $this->assertTags($result, $expected);
  7391. $result = $this->Form->input('Contact.imrequired');
  7392. $expected = array(
  7393. 'div' => array('class' => 'input text required'),
  7394. 'label' => array('for' => 'ContactImrequired'),
  7395. 'Imrequired',
  7396. '/label',
  7397. 'input' => array(
  7398. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7399. 'id' => 'ContactImrequired'
  7400. ),
  7401. '/div'
  7402. );
  7403. $this->assertTags($result, $expected);
  7404. }
  7405. /**
  7406. * Tests that the 'on' key validates as expected on update
  7407. *
  7408. * @return void
  7409. */
  7410. public function testRequiredOnUpdate() {
  7411. $this->Form->request->data['Contact']['id'] = 1;
  7412. $this->Form->create('Contact');
  7413. $result = $this->Form->input('Contact.imrequiredonupdate');
  7414. $expected = array(
  7415. 'div' => array('class' => 'input text required'),
  7416. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7417. 'Imrequiredonupdate',
  7418. '/label',
  7419. 'input' => array(
  7420. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7421. 'id' => 'ContactImrequiredonupdate'
  7422. ),
  7423. '/div'
  7424. );
  7425. $this->assertTags($result, $expected);
  7426. $result = $this->Form->input('Contact.imrequiredoncreate');
  7427. $expected = array(
  7428. 'div' => array('class' => 'input text'),
  7429. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7430. 'Imrequiredoncreate',
  7431. '/label',
  7432. 'input' => array(
  7433. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7434. 'id' => 'ContactImrequiredoncreate'
  7435. ),
  7436. '/div'
  7437. );
  7438. $this->assertTags($result, $expected);
  7439. $result = $this->Form->input('Contact.imrequiredonboth');
  7440. $expected = array(
  7441. 'div' => array('class' => 'input text required'),
  7442. 'label' => array('for' => 'ContactImrequiredonboth'),
  7443. 'Imrequiredonboth',
  7444. '/label',
  7445. 'input' => array(
  7446. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7447. 'id' => 'ContactImrequiredonboth'
  7448. ),
  7449. '/div'
  7450. );
  7451. $this->assertTags($result, $expected);
  7452. $result = $this->Form->input('Contact.imrequired');
  7453. $expected = array(
  7454. 'div' => array('class' => 'input text required'),
  7455. 'label' => array('for' => 'ContactImrequired'),
  7456. 'Imrequired',
  7457. '/label',
  7458. 'input' => array(
  7459. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7460. 'id' => 'ContactImrequired'
  7461. ),
  7462. '/div'
  7463. );
  7464. $this->assertTags($result, $expected);
  7465. }
  7466. /**
  7467. * Test inputDefaults setter and getter
  7468. *
  7469. * @return void
  7470. */
  7471. public function testInputDefaults() {
  7472. $this->Form->create('Contact');
  7473. $this->Form->inputDefaults(array(
  7474. 'label' => false,
  7475. 'div' => array(
  7476. 'style' => 'color: #000;'
  7477. )
  7478. ));
  7479. $result = $this->Form->input('Contact.field1');
  7480. $expected = array(
  7481. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7482. 'input' => array(
  7483. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7484. 'id' => 'ContactField1'
  7485. ),
  7486. '/div'
  7487. );
  7488. $this->assertTags($result, $expected);
  7489. $this->Form->inputDefaults(array(
  7490. 'div' => false,
  7491. 'label' => 'Label',
  7492. ));
  7493. $result = $this->Form->input('Contact.field1');
  7494. $expected = array(
  7495. 'label' => array('for' => 'ContactField1'),
  7496. 'Label',
  7497. '/label',
  7498. 'input' => array(
  7499. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7500. 'id' => 'ContactField1'
  7501. ),
  7502. );
  7503. $this->assertTags($result, $expected);
  7504. $this->Form->inputDefaults(array(
  7505. 'label' => false,
  7506. ), true);
  7507. $result = $this->Form->input('Contact.field1');
  7508. $expected = array(
  7509. 'input' => array(
  7510. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7511. 'id' => 'ContactField1'
  7512. ),
  7513. );
  7514. $this->assertTags($result, $expected);
  7515. $result = $this->Form->inputDefaults();
  7516. $expected = array(
  7517. 'div' => false,
  7518. 'label' => false,
  7519. );
  7520. $this->assertEqual($result, $expected);
  7521. }
  7522. }