FormHelperTest.php 265 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368
  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. * uses property
  37. *
  38. * @var mixed null
  39. */
  40. public $uses = null;
  41. }
  42. /**
  43. * Contact class
  44. *
  45. * @package cake
  46. * @package Cake.Test.Case.View.Helper
  47. */
  48. class Contact extends CakeTestModel {
  49. /**
  50. * useTable property
  51. *
  52. * @var bool false
  53. */
  54. public $useTable = false;
  55. /**
  56. * Default schema
  57. *
  58. * @var array
  59. */
  60. protected $_schema = array(
  61. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  62. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  63. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  64. 'phone' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  65. 'password' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  66. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  67. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  68. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null),
  69. 'age' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => null)
  70. );
  71. /**
  72. * validate property
  73. *
  74. * @var array
  75. */
  76. public $validate = array(
  77. 'non_existing' => array(),
  78. 'idontexist' => array(),
  79. 'imrequired' => array('rule' => array('between', 5, 30), 'allowEmpty' => false),
  80. 'imrequiredonupdate' => array('notEmpty' => array('rule' => 'alphaNumeric', 'on' => 'update')),
  81. 'imrequiredoncreate' => array('required' => array('rule' => 'alphaNumeric', 'on' => 'create')),
  82. 'imrequiredonboth' => array(
  83. 'required' => array('rule' => 'alphaNumeric'),
  84. ),
  85. 'string_required' => 'notEmpty',
  86. 'imalsorequired' => array('rule' => 'alphaNumeric', 'allowEmpty' => false),
  87. 'imrequiredtoo' => array('rule' => 'notEmpty'),
  88. 'required_one' => array('required' => array('rule' => array('notEmpty'))),
  89. 'imnotrequired' => array('required' => false, 'rule' => 'alphaNumeric', 'allowEmpty' => true),
  90. 'imalsonotrequired' => array(
  91. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  92. 'between' => array('rule' => array('between', 5, 30)),
  93. ),
  94. 'imalsonotrequired2' => array(
  95. 'alpha' => array('rule' => 'alphaNumeric', 'allowEmpty' => true),
  96. 'between' => array('rule' => array('between', 5, 30), 'allowEmpty' => true),
  97. ),
  98. 'imnotrequiredeither' => array('required' => true, 'rule' => array('between', 5, 30), 'allowEmpty' => true),
  99. 'iamrequiredalways' => array(
  100. 'email' => array('rule' => 'email'),
  101. 'rule_on_create' => array('rule' => array('maxLength', 50), 'on' => 'create'),
  102. 'rule_on_update' => array('rule' => array('between', 1, 50), 'on' => 'update'),
  103. ),
  104. 'boolean_field' => array('rule' => 'boolean')
  105. );
  106. /**
  107. * schema method
  108. *
  109. * @return void
  110. */
  111. public function setSchema($schema) {
  112. $this->_schema = $schema;
  113. }
  114. /**
  115. * hasAndBelongsToMany property
  116. *
  117. * @var array
  118. */
  119. public $hasAndBelongsToMany = array('ContactTag' => array('with' => 'ContactTagsContact'));
  120. /**
  121. * hasAndBelongsToMany property
  122. *
  123. * @var array
  124. */
  125. public $belongsTo = array('User' => array('className' => 'UserForm'));
  126. }
  127. /**
  128. * ContactTagsContact class
  129. *
  130. * @package cake
  131. * @package Cake.Test.Case.View.Helper
  132. */
  133. class ContactTagsContact extends CakeTestModel {
  134. /**
  135. * useTable property
  136. *
  137. * @var bool false
  138. */
  139. public $useTable = false;
  140. /**
  141. * Default schema
  142. *
  143. * @var array
  144. */
  145. protected $_schema = array(
  146. 'contact_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  147. 'contact_tag_id' => array(
  148. 'type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'
  149. )
  150. );
  151. /**
  152. * schema method
  153. *
  154. * @return void
  155. */
  156. public function setSchema($schema) {
  157. $this->_schema = $schema;
  158. }
  159. }
  160. /**
  161. * ContactNonStandardPk class
  162. *
  163. * @package cake
  164. * @package Cake.Test.Case.View.Helper
  165. */
  166. class ContactNonStandardPk extends Contact {
  167. /**
  168. * primaryKey property
  169. *
  170. * @var string 'pk'
  171. */
  172. public $primaryKey = 'pk';
  173. /**
  174. * schema method
  175. *
  176. * @return void
  177. */
  178. public function schema($field = false) {
  179. $this->_schema = parent::schema();
  180. $this->_schema['pk'] = $this->_schema['id'];
  181. unset($this->_schema['id']);
  182. return $this->_schema;
  183. }
  184. }
  185. /**
  186. * ContactTag class
  187. *
  188. * @package cake
  189. * @package Cake.Test.Case.View.Helper
  190. */
  191. class ContactTag extends Model {
  192. /**
  193. * useTable property
  194. *
  195. * @var bool false
  196. */
  197. public $useTable = false;
  198. /**
  199. * schema definition
  200. *
  201. * @var array
  202. */
  203. protected $_schema = array(
  204. 'id' => array('type' => 'integer', 'null' => false, 'default' => '', 'length' => '8'),
  205. 'name' => array('type' => 'string', 'null' => false, 'default' => '', 'length' => '255'),
  206. 'created' => array('type' => 'date', 'null' => true, 'default' => '', 'length' => ''),
  207. 'modified' => array('type' => 'datetime', 'null' => true, 'default' => '', 'length' => null)
  208. );
  209. }
  210. /**
  211. * UserForm class
  212. *
  213. * @package cake
  214. * @package Cake.Test.Case.View.Helper
  215. */
  216. class UserForm extends CakeTestModel {
  217. /**
  218. * useTable property
  219. *
  220. * @var bool false
  221. */
  222. public $useTable = false;
  223. /**
  224. * hasMany property
  225. *
  226. * @var array
  227. */
  228. public $hasMany = array(
  229. 'OpenidUrl' => array('className' => 'OpenidUrl', 'foreignKey' => 'user_form_id'
  230. ));
  231. /**
  232. * schema definition
  233. *
  234. * @var array
  235. */
  236. protected $_schema = array(
  237. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  238. 'published' => array('type' => 'date', 'null' => true, 'default' => null, 'length' => null),
  239. 'other' => array('type' => 'text', 'null' => true, 'default' => null, 'length' => null),
  240. 'stuff' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 10),
  241. 'something' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 255),
  242. 'active' => array('type' => 'boolean', 'null' => false, 'default' => false),
  243. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  244. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  245. );
  246. }
  247. /**
  248. * OpenidUrl class
  249. *
  250. * @package cake
  251. * @package Cake.Test.Case.View.Helper
  252. */
  253. class OpenidUrl extends CakeTestModel {
  254. /**
  255. * useTable property
  256. *
  257. * @var bool false
  258. */
  259. public $useTable = false;
  260. /**
  261. * belongsTo property
  262. *
  263. * @var array
  264. */
  265. public $belongsTo = array('UserForm' => array(
  266. 'className' => 'UserForm', 'foreignKey' => 'user_form_id'
  267. ));
  268. /**
  269. * validate property
  270. *
  271. * @var array
  272. */
  273. public $validate = array('openid_not_registered' => array());
  274. /**
  275. * schema method
  276. *
  277. * @var array
  278. */
  279. protected $_schema = array(
  280. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  281. 'user_form_id' => array(
  282. 'type' => 'user_form_id', 'null' => '', 'default' => '', 'length' => '8'
  283. ),
  284. 'url' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  285. );
  286. /**
  287. * beforeValidate method
  288. *
  289. * @return void
  290. */
  291. public function beforeValidate($options = array()) {
  292. $this->invalidate('openid_not_registered');
  293. return true;
  294. }
  295. }
  296. /**
  297. * ValidateUser class
  298. *
  299. * @package cake
  300. * @package Cake.Test.Case.View.Helper
  301. */
  302. class ValidateUser extends CakeTestModel {
  303. /**
  304. * useTable property
  305. *
  306. * @var bool false
  307. */
  308. public $useTable = false;
  309. /**
  310. * hasOne property
  311. *
  312. * @var array
  313. */
  314. public $hasOne = array('ValidateProfile' => array(
  315. 'className' => 'ValidateProfile', 'foreignKey' => 'user_id'
  316. ));
  317. /**
  318. * schema method
  319. *
  320. * @var array
  321. */
  322. protected $_schema = array(
  323. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  324. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  325. 'email' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  326. 'balance' => array('type' => 'float', 'null' => false, 'length' => '5,2'),
  327. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  328. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  329. );
  330. /**
  331. * beforeValidate method
  332. *
  333. * @return void
  334. */
  335. public function beforeValidate($options = array()) {
  336. $this->invalidate('email');
  337. return false;
  338. }
  339. }
  340. /**
  341. * ValidateProfile class
  342. *
  343. * @package cake
  344. * @package Cake.Test.Case.View.Helper
  345. */
  346. class ValidateProfile extends CakeTestModel {
  347. /**
  348. * useTable property
  349. *
  350. * @var bool false
  351. */
  352. public $useTable = false;
  353. /**
  354. * schema property
  355. *
  356. * @var array
  357. */
  358. protected $_schema = array(
  359. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  360. 'user_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  361. 'full_name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  362. 'city' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  363. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  364. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  365. );
  366. /**
  367. * hasOne property
  368. *
  369. * @var array
  370. */
  371. public $hasOne = array('ValidateItem' => array(
  372. 'className' => 'ValidateItem', 'foreignKey' => 'profile_id'
  373. ));
  374. /**
  375. * belongsTo property
  376. *
  377. * @var array
  378. */
  379. public $belongsTo = array('ValidateUser' => array(
  380. 'className' => 'ValidateUser', 'foreignKey' => 'user_id'
  381. ));
  382. /**
  383. * beforeValidate method
  384. *
  385. * @return void
  386. */
  387. public function beforeValidate($options = array()) {
  388. $this->invalidate('full_name');
  389. $this->invalidate('city');
  390. return false;
  391. }
  392. }
  393. /**
  394. * ValidateItem class
  395. *
  396. * @package cake
  397. * @package Cake.Test.Case.View.Helper
  398. */
  399. class ValidateItem extends CakeTestModel {
  400. /**
  401. * useTable property
  402. *
  403. * @var bool false
  404. */
  405. public $useTable = false;
  406. /**
  407. * schema property
  408. *
  409. * @var array
  410. */
  411. protected $_schema = array(
  412. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  413. 'profile_id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  414. 'name' => array('type' => 'text', 'null' => '', 'default' => '', 'length' => '255'),
  415. 'description' => array(
  416. 'type' => 'string', 'null' => '', 'default' => '', 'length' => '255'
  417. ),
  418. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  419. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  420. );
  421. /**
  422. * belongsTo property
  423. *
  424. * @var array
  425. */
  426. public $belongsTo = array('ValidateProfile' => array('foreignKey' => 'profile_id'));
  427. /**
  428. * beforeValidate method
  429. *
  430. * @return void
  431. */
  432. public function beforeValidate($options = array()) {
  433. $this->invalidate('description');
  434. return false;
  435. }
  436. }
  437. /**
  438. * TestMail class
  439. *
  440. * @package cake
  441. * @package Cake.Test.Case.View.Helper
  442. */
  443. class TestMail extends CakeTestModel {
  444. /**
  445. * useTable property
  446. *
  447. * @var bool false
  448. */
  449. public $useTable = false;
  450. }
  451. /**
  452. * FormHelperTest class
  453. *
  454. * @package cake
  455. * @subpackage Cake.Test.Case.View.Helper
  456. * @property FormHelper $Form
  457. */
  458. class FormHelperTest extends CakeTestCase {
  459. /**
  460. * Fixtures to be used
  461. *
  462. * @var array
  463. */
  464. public $fixtures = array('core.post');
  465. /**
  466. * Do not load the fixtures by default
  467. *
  468. * @var boolean
  469. */
  470. public $autoFixtures = false;
  471. /**
  472. * setUp method
  473. *
  474. * @return void
  475. */
  476. public function setUp() {
  477. parent::setUp();
  478. Configure::write('App.base', '');
  479. $this->Controller = new ContactTestController();
  480. $this->View = new View($this->Controller);
  481. $this->Form = new FormHelper($this->View);
  482. $this->Form->Html = new HtmlHelper($this->View);
  483. $this->Form->request = new CakeRequest('contacts/add', false);
  484. $this->Form->request->here = '/contacts/add';
  485. $this->Form->request['action'] = 'add';
  486. $this->Form->request->webroot = '';
  487. $this->Form->request->base = '';
  488. ClassRegistry::addObject('Contact', new Contact());
  489. ClassRegistry::addObject('ContactNonStandardPk', new ContactNonStandardPk());
  490. ClassRegistry::addObject('OpenidUrl', new OpenidUrl());
  491. ClassRegistry::addObject('User', new UserForm());
  492. ClassRegistry::addObject('ValidateItem', new ValidateItem());
  493. ClassRegistry::addObject('ValidateUser', new ValidateUser());
  494. ClassRegistry::addObject('ValidateProfile', new ValidateProfile());
  495. $this->oldSalt = Configure::read('Security.salt');
  496. $this->dateRegex = array(
  497. 'daysRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  498. 'monthsRegex' => 'preg:/(?:<option value="[\d]+">[\w]+<\/option>[\r\n]*)*/',
  499. 'yearsRegex' => 'preg:/(?:<option value="([\d]+)">\\1<\/option>[\r\n]*)*/',
  500. 'hoursRegex' => 'preg:/(?:<option value="0?([\d]+)">\\1<\/option>[\r\n]*)*/',
  501. 'minutesRegex' => 'preg:/(?:<option value="([\d]+)">0?\\1<\/option>[\r\n]*)*/',
  502. 'meridianRegex' => 'preg:/(?:<option value="(am|pm)">\\1<\/option>[\r\n]*)*/',
  503. );
  504. Configure::write('Security.salt', 'foo!');
  505. }
  506. /**
  507. * tearDown method
  508. *
  509. * @return void
  510. */
  511. public function tearDown() {
  512. parent::tearDown();
  513. unset($this->Form->Html, $this->Form, $this->Controller, $this->View);
  514. Configure::write('Security.salt', $this->oldSalt);
  515. }
  516. /**
  517. * testFormCreateWithSecurity method
  518. *
  519. * Test form->create() with security key.
  520. *
  521. * @return void
  522. */
  523. public function testCreateWithSecurity() {
  524. $this->Form->request['_Token'] = array('key' => 'testKey');
  525. $encoding = strtolower(Configure::read('App.encoding'));
  526. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  527. $expected = array(
  528. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  529. 'div' => array('style' => 'display:none;'),
  530. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  531. array('input' => array(
  532. 'type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testKey', 'id'
  533. )),
  534. '/div'
  535. );
  536. $this->assertTags($result, $expected);
  537. $result = $this->Form->create('Contact', array('url' => '/contacts/add', 'id' => 'MyForm'));
  538. $expected['form']['id'] = 'MyForm';
  539. $this->assertTags($result, $expected);
  540. }
  541. /**
  542. * test that create() clears the fields property so it starts fresh
  543. *
  544. * @return void
  545. */
  546. public function testCreateClearingFields() {
  547. $this->Form->fields = array('model_id');
  548. $this->Form->create('Contact');
  549. $this->assertEquals(array(), $this->Form->fields);
  550. }
  551. /**
  552. * Tests form hash generation with model-less data
  553. *
  554. * @return void
  555. */
  556. public function testValidateHashNoModel() {
  557. $this->Form->request['_Token'] = array('key' => 'foo');
  558. $result = $this->Form->secure(array('anything'));
  559. $this->assertRegExp('/540ac9c60d323c22bafe997b72c0790f39a8bdef/', $result);
  560. }
  561. /**
  562. * Tests that models with identical field names get resolved properly
  563. *
  564. * @return void
  565. */
  566. public function testDuplicateFieldNameResolution() {
  567. $result = $this->Form->create('ValidateUser');
  568. $this->assertEquals(array('ValidateUser'), $this->Form->entity());
  569. $result = $this->Form->input('ValidateItem.name');
  570. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  571. $result = $this->Form->input('ValidateUser.name');
  572. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  573. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  574. $this->assertRegExp('/type="text"/', $result);
  575. $result = $this->Form->input('ValidateItem.name');
  576. $this->assertEquals(array('ValidateItem', 'name'), $this->Form->entity());
  577. $this->assertRegExp('/name="data\[ValidateItem\]\[name\]"/', $result);
  578. $this->assertRegExp('/<textarea/', $result);
  579. $result = $this->Form->input('name');
  580. $this->assertEquals(array('ValidateUser', 'name'), $this->Form->entity());
  581. $this->assertRegExp('/name="data\[ValidateUser\]\[name\]"/', $result);
  582. $this->assertRegExp('/type="text"/', $result);
  583. }
  584. /**
  585. * Tests that hidden fields generated for checkboxes don't get locked
  586. *
  587. * @return void
  588. */
  589. public function testNoCheckboxLocking() {
  590. $this->Form->request['_Token'] = array('key' => 'foo');
  591. $this->assertSame(array(), $this->Form->fields);
  592. $this->Form->checkbox('check', array('value' => '1'));
  593. $this->assertSame($this->Form->fields, array('check'));
  594. }
  595. /**
  596. * testFormSecurityFields method
  597. *
  598. * Test generation of secure form hash generation.
  599. *
  600. * @return void
  601. */
  602. public function testFormSecurityFields() {
  603. $key = 'testKey';
  604. $fields = array('Model.password', 'Model.username', 'Model.valid' => '0');
  605. $this->Form->request['_Token'] = array('key' => $key);
  606. $result = $this->Form->secure($fields);
  607. $hash = Security::hash(serialize($fields) . Configure::read('Security.salt'));
  608. $hash .= ':' . 'Model.valid';
  609. $hash = urlencode($hash);
  610. $expected = array(
  611. 'div' => array('style' => 'display:none;'),
  612. array('input' => array(
  613. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  614. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  615. )),
  616. array('input' => array(
  617. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  618. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  619. )),
  620. '/div'
  621. );
  622. $this->assertTags($result, $expected);
  623. $path = CAKE . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  624. $this->Form->Html->loadConfig('htmlhelper_tags', $path);
  625. $result = $this->Form->secure($fields);
  626. $expected = array(
  627. 'div' => array('class' => 'hidden'),
  628. array('input' => array(
  629. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  630. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  631. )),
  632. array('input' => array(
  633. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  634. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  635. )),
  636. '/div'
  637. );
  638. $this->assertTags($result, $expected);
  639. }
  640. /**
  641. * Tests correct generation of number fields for double and float fields
  642. *
  643. * @return void
  644. */
  645. public function testTextFieldGenerationForFloats() {
  646. $model = ClassRegistry::getObject('Contact');
  647. $model->setSchema(array('foo' => array(
  648. 'type' => 'float',
  649. 'null' => false,
  650. 'default' => null,
  651. 'length' => null
  652. )));
  653. $this->Form->create('Contact');
  654. $result = $this->Form->input('foo');
  655. $expected = array(
  656. 'div' => array('class' => 'input number'),
  657. 'label' => array('for' => 'ContactFoo'),
  658. 'Foo',
  659. '/label',
  660. array('input' => array(
  661. 'type' => 'number',
  662. 'name' => 'data[Contact][foo]',
  663. 'id' => 'ContactFoo',
  664. 'step' => 'any'
  665. )),
  666. '/div'
  667. );
  668. $this->assertTags($result, $expected);
  669. $result = $this->Form->input('foo', array('step' => 0.5));
  670. $expected = array(
  671. 'div' => array('class' => 'input number'),
  672. 'label' => array('for' => 'ContactFoo'),
  673. 'Foo',
  674. '/label',
  675. array('input' => array(
  676. 'type' => 'number',
  677. 'name' => 'data[Contact][foo]',
  678. 'id' => 'ContactFoo',
  679. 'step' => '0.5'
  680. )),
  681. '/div'
  682. );
  683. $this->assertTags($result, $expected);
  684. }
  685. /**
  686. * Tests correct generation of number fields for integer fields
  687. *
  688. * @access public
  689. * @return void
  690. */
  691. public function testTextFieldTypeNumberGenerationForIntegers() {
  692. $model = ClassRegistry::getObject('Contact');
  693. $model->setSchema(array('foo' => array(
  694. 'type' => 'integer',
  695. 'null' => false,
  696. 'default' => null,
  697. 'length' => null
  698. )));
  699. $this->Form->create('Contact');
  700. $result = $this->Form->input('foo');
  701. $expected = array(
  702. 'div' => array('class' => 'input number'),
  703. 'label' => array('for' => 'ContactFoo'),
  704. 'Foo',
  705. '/label',
  706. array('input' => array(
  707. 'type' => 'number', 'name' => 'data[Contact][foo]',
  708. 'id' => 'ContactFoo'
  709. )),
  710. '/div'
  711. );
  712. $this->assertTags($result, $expected);
  713. }
  714. /**
  715. * testFormSecurityMultipleFields method
  716. *
  717. * Test secure() with multiple row form. Ensure hash is correct.
  718. *
  719. * @return void
  720. */
  721. public function testFormSecurityMultipleFields() {
  722. $key = 'testKey';
  723. $fields = array(
  724. 'Model.0.password', 'Model.0.username', 'Model.0.hidden' => 'value',
  725. 'Model.0.valid' => '0', 'Model.1.password', 'Model.1.username',
  726. 'Model.1.hidden' => 'value', 'Model.1.valid' => '0'
  727. );
  728. $this->Form->request['_Token'] = array('key' => $key);
  729. $result = $this->Form->secure($fields);
  730. $hash = '51e3b55a6edd82020b3f29c9ae200e14bbeb7ee5%3AModel.0.hidden%7CModel.0.valid';
  731. $hash .= '%7CModel.1.hidden%7CModel.1.valid';
  732. $expected = array(
  733. 'div' => array('style' => 'display:none;'),
  734. array('input' => array(
  735. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  736. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  737. )),
  738. array('input' => array(
  739. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  740. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  741. )),
  742. '/div'
  743. );
  744. $this->assertTags($result, $expected);
  745. }
  746. /**
  747. * testFormSecurityMultipleSubmitButtons
  748. *
  749. * test form submit generation and ensure that _Token is only created on end()
  750. *
  751. * @return void
  752. */
  753. public function testFormSecurityMultipleSubmitButtons() {
  754. $key = 'testKey';
  755. $this->Form->request['_Token'] = array('key' => $key);
  756. $this->Form->create('Addresses');
  757. $this->Form->input('Address.title');
  758. $this->Form->input('Address.first_name');
  759. $result = $this->Form->submit('Save', array('name' => 'save'));
  760. $expected = array(
  761. 'div' => array('class' => 'submit'),
  762. 'input' => array('type' => 'submit', 'name' => 'save', 'value' => 'Save'),
  763. '/div',
  764. );
  765. $this->assertTags($result, $expected);
  766. $result = $this->Form->submit('Cancel', array('name' => 'cancel'));
  767. $expected = array(
  768. 'div' => array('class' => 'submit'),
  769. 'input' => array('type' => 'submit', 'name' => 'cancel', 'value' => 'Cancel'),
  770. '/div',
  771. );
  772. $this->assertTags($result, $expected);
  773. $result = $this->Form->end(null);
  774. $expected = array(
  775. 'div' => array('style' => 'display:none;'),
  776. array('input' => array(
  777. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  778. 'value' => 'preg:/.+/', 'id' => 'preg:/TokenFields\d+/'
  779. )),
  780. array('input' => array(
  781. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  782. 'value' => 'cancel%7Csave', 'id' => 'preg:/TokenUnlocked\d+/'
  783. )),
  784. '/div'
  785. );
  786. $this->assertTags($result, $expected);
  787. }
  788. /**
  789. * Test that buttons created with foo[bar] name attributes are unlocked correctly.
  790. *
  791. * @return void
  792. */
  793. public function testSecurityButtonNestedNamed() {
  794. $key = 'testKey';
  795. $this->Form->request['_Token'] = array('key' => $key);
  796. $this->Form->create('Addresses');
  797. $this->Form->button('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  798. $result = $this->Form->unlockField();
  799. $this->assertEquals(array('Address.button'), $result);
  800. }
  801. /**
  802. * Test that submit inputs created with foo[bar] name attributes are unlocked correctly.
  803. *
  804. * @return void
  805. */
  806. public function testSecuritySubmitNestedNamed() {
  807. $key = 'testKey';
  808. $this->Form->request['_Token'] = array('key' => $key);
  809. $this->Form->create('Addresses');
  810. $this->Form->submit('Test', array('type' => 'submit', 'name' => 'Address[button]'));
  811. $result = $this->Form->unlockField();
  812. $this->assertEquals(array('Address.button'), $result);
  813. }
  814. /**
  815. * Test that the correct fields are unlocked for image submits with no names.
  816. *
  817. * @return void
  818. */
  819. public function testSecuritySubmitImageNoName() {
  820. $key = 'testKey';
  821. $this->Form->request['_Token'] = array('key' => $key);
  822. $this->Form->create('User');
  823. $result = $this->Form->submit('save.png');
  824. $expected = array(
  825. 'div' => array('class' => 'submit'),
  826. 'input' => array('type' => 'image', 'src' => 'img/save.png'),
  827. '/div'
  828. );
  829. $this->assertTags($result, $expected);
  830. $this->assertEquals(array('x', 'y'), $this->Form->unlockField());
  831. }
  832. /**
  833. * Test that the correct fields are unlocked for image submits with names.
  834. *
  835. * @return void
  836. */
  837. public function testSecuritySubmitImageName() {
  838. $key = 'testKey';
  839. $this->Form->request['_Token'] = array('key' => $key);
  840. $this->Form->create('User');
  841. $result = $this->Form->submit('save.png', array('name' => 'test'));
  842. $expected = array(
  843. 'div' => array('class' => 'submit'),
  844. 'input' => array('type' => 'image', 'name' => 'test', 'src' => 'img/save.png'),
  845. '/div'
  846. );
  847. $this->assertTags($result, $expected);
  848. $this->assertEquals(array('test', 'test_x', 'test_y'), $this->Form->unlockField());
  849. }
  850. /**
  851. * testFormSecurityMultipleInputFields method
  852. *
  853. * Test secure form creation with multiple row creation. Checks hidden, text, checkbox field types
  854. *
  855. * @return void
  856. */
  857. public function testFormSecurityMultipleInputFields() {
  858. $key = 'testKey';
  859. $this->Form->request['_Token'] = array('key' => $key);
  860. $this->Form->create('Addresses');
  861. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  862. $this->Form->input('Addresses.0.title');
  863. $this->Form->input('Addresses.0.first_name');
  864. $this->Form->input('Addresses.0.last_name');
  865. $this->Form->input('Addresses.0.address');
  866. $this->Form->input('Addresses.0.city');
  867. $this->Form->input('Addresses.0.phone');
  868. $this->Form->input('Addresses.0.primary', array('type' => 'checkbox'));
  869. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  870. $this->Form->input('Addresses.1.title');
  871. $this->Form->input('Addresses.1.first_name');
  872. $this->Form->input('Addresses.1.last_name');
  873. $this->Form->input('Addresses.1.address');
  874. $this->Form->input('Addresses.1.city');
  875. $this->Form->input('Addresses.1.phone');
  876. $this->Form->input('Addresses.1.primary', array('type' => 'checkbox'));
  877. $result = $this->Form->secure($this->Form->fields);
  878. $hash = 'c9118120e680a7201b543f562e5301006ccfcbe2%3AAddresses.0.id%7CAddresses.1.id';
  879. $expected = array(
  880. 'div' => array('style' => 'display:none;'),
  881. array('input' => array(
  882. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  883. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  884. )),
  885. array('input' => array(
  886. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  887. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  888. )),
  889. '/div'
  890. );
  891. $this->assertTags($result, $expected);
  892. }
  893. /**
  894. * Test form security with Model.field.0 style inputs
  895. *
  896. * @return void
  897. */
  898. public function testFormSecurityArrayFields() {
  899. $key = 'testKey';
  900. $this->Form->request->params['_Token']['key'] = $key;
  901. $this->Form->create('Address');
  902. $this->Form->input('Address.primary.1');
  903. $this->assertEquals('Address.primary', $this->Form->fields[0]);
  904. $this->Form->input('Address.secondary.1.0');
  905. $this->assertEquals('Address.secondary', $this->Form->fields[1]);
  906. }
  907. /**
  908. * testFormSecurityMultipleInputDisabledFields method
  909. *
  910. * test secure form generation with multiple records and disabled fields.
  911. *
  912. * @return void
  913. */
  914. public function testFormSecurityMultipleInputDisabledFields() {
  915. $key = 'testKey';
  916. $this->Form->request->params['_Token'] = array(
  917. 'key' => $key,
  918. 'unlockedFields' => array('first_name', 'address')
  919. );
  920. $this->Form->create();
  921. $this->Form->hidden('Addresses.0.id', array('value' => '123456'));
  922. $this->Form->input('Addresses.0.title');
  923. $this->Form->input('Addresses.0.first_name');
  924. $this->Form->input('Addresses.0.last_name');
  925. $this->Form->input('Addresses.0.address');
  926. $this->Form->input('Addresses.0.city');
  927. $this->Form->input('Addresses.0.phone');
  928. $this->Form->hidden('Addresses.1.id', array('value' => '654321'));
  929. $this->Form->input('Addresses.1.title');
  930. $this->Form->input('Addresses.1.first_name');
  931. $this->Form->input('Addresses.1.last_name');
  932. $this->Form->input('Addresses.1.address');
  933. $this->Form->input('Addresses.1.city');
  934. $this->Form->input('Addresses.1.phone');
  935. $result = $this->Form->secure($this->Form->fields);
  936. $hash = '629b6536dcece48aa41a117045628ce602ccbbb2%3AAddresses.0.id%7CAddresses.1.id';
  937. $expected = array(
  938. 'div' => array('style' => 'display:none;'),
  939. array('input' => array(
  940. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  941. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  942. )),
  943. array('input' => array(
  944. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  945. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  946. )),
  947. '/div'
  948. );
  949. $this->assertTags($result, $expected);
  950. }
  951. /**
  952. * testFormSecurityInputDisabledFields method
  953. *
  954. * Test single record form with disabled fields.
  955. *
  956. * @return void
  957. */
  958. public function testFormSecurityInputUnlockedFields() {
  959. $key = 'testKey';
  960. $this->Form->request['_Token'] = array(
  961. 'key' => $key,
  962. 'unlockedFields' => array('first_name', 'address')
  963. );
  964. $this->Form->create();
  965. $this->assertEquals($this->Form->request['_Token']['unlockedFields'], $this->Form->unlockField());
  966. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  967. $this->Form->input('Addresses.title');
  968. $this->Form->input('Addresses.first_name');
  969. $this->Form->input('Addresses.last_name');
  970. $this->Form->input('Addresses.address');
  971. $this->Form->input('Addresses.city');
  972. $this->Form->input('Addresses.phone');
  973. $result = $this->Form->fields;
  974. $expected = array(
  975. 'Addresses.id' => '123456', 'Addresses.title', 'Addresses.last_name',
  976. 'Addresses.city', 'Addresses.phone'
  977. );
  978. $this->assertEquals($expected, $result);
  979. $result = $this->Form->secure($expected);
  980. $hash = '2981c38990f3f6ba935e6561dc77277966fabd6d%3AAddresses.id';
  981. $expected = array(
  982. 'div' => array('style' => 'display:none;'),
  983. array('input' => array(
  984. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  985. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  986. )),
  987. array('input' => array(
  988. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  989. 'value' => 'address%7Cfirst_name', 'id' => 'preg:/TokenUnlocked\d+/'
  990. )),
  991. '/div'
  992. );
  993. $this->assertTags($result, $expected);
  994. }
  995. /**
  996. * test securing inputs with custom name attributes.
  997. *
  998. * @return void
  999. */
  1000. public function testFormSecureWithCustomNameAttribute() {
  1001. $this->Form->request->params['_Token']['key'] = 'testKey';
  1002. $this->Form->text('UserForm.published', array('name' => 'data[User][custom]'));
  1003. $this->assertEquals('User.custom', $this->Form->fields[0]);
  1004. $this->Form->text('UserForm.published', array('name' => 'data[User][custom][another][value]'));
  1005. $this->assertEquals('User.custom.another.value', $this->Form->fields[1]);
  1006. }
  1007. /**
  1008. * testFormSecuredInput method
  1009. *
  1010. * Test generation of entire secure form, assertions made on input() output.
  1011. *
  1012. * @return void
  1013. */
  1014. public function testFormSecuredInput() {
  1015. $this->Form->request['_Token'] = array('key' => 'testKey');
  1016. $result = $this->Form->create('Contact', array('url' => '/contacts/add'));
  1017. $encoding = strtolower(Configure::read('App.encoding'));
  1018. $expected = array(
  1019. 'form' => array('method' => 'post', 'action' => '/contacts/add', 'accept-charset' => $encoding, 'id' => 'ContactAddForm'),
  1020. 'div' => array('style' => 'display:none;'),
  1021. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  1022. array('input' => array(
  1023. 'type' => 'hidden', 'name' => 'data[_Token][key]',
  1024. 'value' => 'testKey', 'id' => 'preg:/Token\d+/'
  1025. )),
  1026. '/div'
  1027. );
  1028. $this->assertTags($result, $expected);
  1029. $result = $this->Form->input('UserForm.published', array('type' => 'text'));
  1030. $expected = array(
  1031. 'div' => array('class' => 'input text'),
  1032. 'label' => array('for' => 'UserFormPublished'),
  1033. 'Published',
  1034. '/label',
  1035. array('input' => array(
  1036. 'type' => 'text', 'name' => 'data[UserForm][published]',
  1037. 'id' => 'UserFormPublished'
  1038. )),
  1039. '/div'
  1040. );
  1041. $this->assertTags($result, $expected);
  1042. $result = $this->Form->input('UserForm.other', array('type' => 'text'));
  1043. $expected = array(
  1044. 'div' => array('class' => 'input text'),
  1045. 'label' => array('for' => 'UserFormOther'),
  1046. 'Other',
  1047. '/label',
  1048. array('input' => array(
  1049. 'type' => 'text', 'name' => 'data[UserForm][other]',
  1050. 'id' => 'UserFormOther'
  1051. )),
  1052. '/div'
  1053. );
  1054. $this->assertTags($result, $expected);
  1055. $result = $this->Form->hidden('UserForm.stuff');
  1056. $expected = array('input' => array(
  1057. 'type' => 'hidden', 'name' => 'data[UserForm][stuff]',
  1058. 'id' => 'UserFormStuff'
  1059. ));
  1060. $this->assertTags($result, $expected);
  1061. $result = $this->Form->hidden('UserForm.hidden', array('value' => '0'));
  1062. $expected = array('input' => array(
  1063. 'type' => 'hidden', 'name' => 'data[UserForm][hidden]',
  1064. 'value' => '0', 'id' => 'UserFormHidden'
  1065. ));
  1066. $this->assertTags($result, $expected);
  1067. $result = $this->Form->input('UserForm.something', array('type' => 'checkbox'));
  1068. $expected = array(
  1069. 'div' => array('class' => 'input checkbox'),
  1070. array('input' => array(
  1071. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  1072. 'value' => '0', 'id' => 'UserFormSomething_'
  1073. )),
  1074. array('input' => array(
  1075. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  1076. 'value' => '1', 'id' => 'UserFormSomething'
  1077. )),
  1078. 'label' => array('for' => 'UserFormSomething'),
  1079. 'Something',
  1080. '/label',
  1081. '/div'
  1082. );
  1083. $this->assertTags($result, $expected);
  1084. $result = $this->Form->fields;
  1085. $expected = array(
  1086. 'UserForm.published', 'UserForm.other', 'UserForm.stuff' => '',
  1087. 'UserForm.hidden' => '0', 'UserForm.something'
  1088. );
  1089. $this->assertEquals($expected, $result);
  1090. $hash = 'bd7c4a654e5361f9a433a43f488ff9a1065d0aaf%3AUserForm.hidden%7CUserForm.stuff';
  1091. $result = $this->Form->secure($this->Form->fields);
  1092. $expected = array(
  1093. 'div' => array('style' => 'display:none;'),
  1094. array('input' => array(
  1095. 'type' => 'hidden', 'name' => 'data[_Token][fields]',
  1096. 'value' => $hash, 'id' => 'preg:/TokenFields\d+/'
  1097. )),
  1098. array('input' => array(
  1099. 'type' => 'hidden', 'name' => 'data[_Token][unlocked]',
  1100. 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/'
  1101. )),
  1102. '/div'
  1103. );
  1104. $this->assertTags($result, $expected);
  1105. }
  1106. /**
  1107. * Tests that the correct keys are added to the field hash index
  1108. *
  1109. * @return void
  1110. */
  1111. public function testFormSecuredFileInput() {
  1112. $this->Form->request['_Token'] = array('key' => 'testKey');
  1113. $this->assertEquals(array(), $this->Form->fields);
  1114. $this->Form->file('Attachment.file');
  1115. $expected = array(
  1116. 'Attachment.file.name', 'Attachment.file.type', 'Attachment.file.tmp_name',
  1117. 'Attachment.file.error', 'Attachment.file.size'
  1118. );
  1119. $this->assertEquals($expected, $this->Form->fields);
  1120. }
  1121. /**
  1122. * test that multiple selects keys are added to field hash
  1123. *
  1124. * @return void
  1125. */
  1126. public function testFormSecuredMultipleSelect() {
  1127. $this->Form->request['_Token'] = array('key' => 'testKey');
  1128. $this->assertEquals(array(), $this->Form->fields);
  1129. $options = array('1' => 'one', '2' => 'two');
  1130. $this->Form->select('Model.select', $options);
  1131. $expected = array('Model.select');
  1132. $this->assertEquals($expected, $this->Form->fields);
  1133. $this->Form->fields = array();
  1134. $this->Form->select('Model.select', $options, array('multiple' => true));
  1135. $this->assertEquals($expected, $this->Form->fields);
  1136. }
  1137. /**
  1138. * testFormSecuredRadio method
  1139. *
  1140. * @return void
  1141. */
  1142. public function testFormSecuredRadio() {
  1143. $this->Form->request['_Token'] = array('key' => 'testKey');
  1144. $this->assertEquals(array(), $this->Form->fields);
  1145. $options = array('1' => 'option1', '2' => 'option2');
  1146. $this->Form->radio('Test.test', $options);
  1147. $expected = array('Test.test');
  1148. $this->assertEquals($expected, $this->Form->fields);
  1149. }
  1150. /**
  1151. * test that forms with disabled inputs + secured forms leave off the inputs from the form
  1152. * hashing.
  1153. *
  1154. * @return void
  1155. */
  1156. public function testFormSecuredAndDisabled() {
  1157. $this->Form->request['_Token'] = array('key' => 'testKey');
  1158. $this->Form->checkbox('Model.checkbox', array('disabled' => true));
  1159. $this->Form->text('Model.text', array('disabled' => true));
  1160. $this->Form->password('Model.text', array('disabled' => true));
  1161. $this->Form->textarea('Model.textarea', array('disabled' => true));
  1162. $this->Form->select('Model.select', array(1, 2), array('disabled' => true));
  1163. $this->Form->radio('Model.radio', array(1, 2), array('disabled' => array(1, 2)));
  1164. $this->Form->year('Model.year', null, null, array('disabled' => true));
  1165. $this->Form->month('Model.month', array('disabled' => true));
  1166. $this->Form->day('Model.day', array('disabled' => true));
  1167. $this->Form->hour('Model.hour', false, array('disabled' => true));
  1168. $this->Form->minute('Model.minute', array('disabled' => true));
  1169. $this->Form->meridian('Model.meridian', array('disabled' => true));
  1170. $expected = array(
  1171. 'Model.radio' => ''
  1172. );
  1173. $this->assertEquals($expected, $this->Form->fields);
  1174. }
  1175. /**
  1176. * testDisableSecurityUsingForm method
  1177. *
  1178. * @return void
  1179. */
  1180. public function testDisableSecurityUsingForm() {
  1181. $this->Form->request['_Token'] = array(
  1182. 'key' => 'testKey',
  1183. 'disabledFields' => array()
  1184. );
  1185. $this->Form->create();
  1186. $this->Form->hidden('Addresses.id', array('value' => '123456'));
  1187. $this->Form->input('Addresses.title');
  1188. $this->Form->input('Addresses.first_name', array('secure' => false));
  1189. $this->Form->input('Addresses.city', array('type' => 'textarea', 'secure' => false));
  1190. $this->Form->input('Addresses.zip', array(
  1191. 'type' => 'select', 'options' => array(1,2), 'secure' => false
  1192. ));
  1193. $result = $this->Form->fields;
  1194. $expected = array(
  1195. 'Addresses.id' => '123456', 'Addresses.title',
  1196. );
  1197. $this->assertEquals($expected, $result);
  1198. }
  1199. /**
  1200. * test disableField
  1201. *
  1202. * @return void
  1203. */
  1204. public function testUnlockFieldAddsToList() {
  1205. $this->Form->request['_Token'] = array(
  1206. 'key' => 'testKey',
  1207. 'unlockedFields' => array()
  1208. );
  1209. $this->Form->create('Contact');
  1210. $this->Form->unlockField('Contact.name');
  1211. $this->Form->text('Contact.name');
  1212. $this->assertEquals(array('Contact.name'), $this->Form->unlockField());
  1213. $this->assertEquals(array(), $this->Form->fields);
  1214. }
  1215. /**
  1216. * test unlockField removing from fields array.
  1217. *
  1218. * @return void
  1219. */
  1220. public function testUnlockFieldRemovingFromFields() {
  1221. $this->Form->request['_Token'] = array(
  1222. 'key' => 'testKey',
  1223. 'unlockedFields' => array()
  1224. );
  1225. $this->Form->create('Contact');
  1226. $this->Form->hidden('Contact.id', array('value' => 1));
  1227. $this->Form->text('Contact.name');
  1228. $this->assertEquals(1, $this->Form->fields['Contact.id'], 'Hidden input should be secured.');
  1229. $this->assertTrue(in_array('Contact.name', $this->Form->fields), 'Field should be secured.');
  1230. $this->Form->unlockField('Contact.name');
  1231. $this->Form->unlockField('Contact.id');
  1232. $this->assertEquals(array(), $this->Form->fields);
  1233. }
  1234. /**
  1235. * testTagIsInvalid method
  1236. *
  1237. * @return void
  1238. */
  1239. public function testTagIsInvalid() {
  1240. $Contact = ClassRegistry::getObject('Contact');
  1241. $Contact->validationErrors[0]['email'] = array('Please provide an email');
  1242. $this->Form->setEntity('Contact.0.email');
  1243. $result = $this->Form->tagIsInvalid();
  1244. $expected = array('Please provide an email');
  1245. $this->assertEquals($expected, $result);
  1246. $this->Form->setEntity('Contact.1.email');
  1247. $result = $this->Form->tagIsInvalid();
  1248. $expected = false;
  1249. $this->assertSame($expected, $result);
  1250. $this->Form->setEntity('Contact.0.name');
  1251. $result = $this->Form->tagIsInvalid();
  1252. $expected = false;
  1253. $this->assertSame($expected, $result);
  1254. }
  1255. /**
  1256. * Test validation errors.
  1257. *
  1258. * @return void
  1259. */
  1260. public function testPasswordValidation() {
  1261. $Contact = ClassRegistry::getObject('Contact');
  1262. $Contact->validationErrors['password'] = array('Please provide a password');
  1263. $result = $this->Form->input('Contact.password');
  1264. $expected = array(
  1265. 'div' => array('class' => 'input password error'),
  1266. 'label' => array('for' => 'ContactPassword'),
  1267. 'Password',
  1268. '/label',
  1269. 'input' => array(
  1270. 'type' => 'password', 'name' => 'data[Contact][password]',
  1271. 'id' => 'ContactPassword', 'class' => 'form-error'
  1272. ),
  1273. array('div' => array('class' => 'error-message')),
  1274. 'Please provide a password',
  1275. '/div',
  1276. '/div'
  1277. );
  1278. $this->assertTags($result, $expected);
  1279. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1280. $expected = array(
  1281. 'div' => array('class' => 'input password error'),
  1282. 'label' => array('for' => 'ContactPassword'),
  1283. 'Password',
  1284. '/label',
  1285. 'input' => array(
  1286. 'type' => 'password', 'name' => 'data[Contact][password]',
  1287. 'id' => 'ContactPassword', 'class' => 'form-error'
  1288. ),
  1289. '/div'
  1290. );
  1291. $this->assertTags($result, $expected);
  1292. }
  1293. /**
  1294. * Test validation errors, when validation message is an empty string.
  1295. *
  1296. * @access public
  1297. * @return void
  1298. */
  1299. public function testEmptyErrorValidation() {
  1300. $this->Form->validationErrors['Contact']['password'] = '';
  1301. $result = $this->Form->input('Contact.password');
  1302. $expected = array(
  1303. 'div' => array('class' => 'input password error'),
  1304. 'label' => array('for' => 'ContactPassword'),
  1305. 'Password',
  1306. '/label',
  1307. 'input' => array(
  1308. 'type' => 'password', 'name' => 'data[Contact][password]',
  1309. 'id' => 'ContactPassword', 'class' => 'form-error'
  1310. ),
  1311. array('div' => array('class' => 'error-message')),
  1312. array(),
  1313. '/div',
  1314. '/div'
  1315. );
  1316. $this->assertTags($result, $expected);
  1317. $result = $this->Form->input('Contact.password', array('errorMessage' => false));
  1318. $expected = array(
  1319. 'div' => array('class' => 'input password error'),
  1320. 'label' => array('for' => 'ContactPassword'),
  1321. 'Password',
  1322. '/label',
  1323. 'input' => array(
  1324. 'type' => 'password', 'name' => 'data[Contact][password]',
  1325. 'id' => 'ContactPassword', 'class' => 'form-error'
  1326. ),
  1327. '/div'
  1328. );
  1329. $this->assertTags($result, $expected);
  1330. }
  1331. /**
  1332. * Test validation errors, when calling input() overriding validation message by an empty string.
  1333. *
  1334. * @access public
  1335. * @return void
  1336. */
  1337. public function testEmptyInputErrorValidation() {
  1338. $this->Form->validationErrors['Contact']['password'] = 'Please provide a password';
  1339. $result = $this->Form->input('Contact.password', array('error' => ''));
  1340. $expected = array(
  1341. 'div' => array('class' => 'input password error'),
  1342. 'label' => array('for' => 'ContactPassword'),
  1343. 'Password',
  1344. '/label',
  1345. 'input' => array(
  1346. 'type' => 'password', 'name' => 'data[Contact][password]',
  1347. 'id' => 'ContactPassword', 'class' => 'form-error'
  1348. ),
  1349. array('div' => array('class' => 'error-message')),
  1350. array(),
  1351. '/div',
  1352. '/div'
  1353. );
  1354. $this->assertTags($result, $expected);
  1355. $result = $this->Form->input('Contact.password', array('error' => '', 'errorMessage' => false));
  1356. $expected = array(
  1357. 'div' => array('class' => 'input password error'),
  1358. 'label' => array('for' => 'ContactPassword'),
  1359. 'Password',
  1360. '/label',
  1361. 'input' => array(
  1362. 'type' => 'password', 'name' => 'data[Contact][password]',
  1363. 'id' => 'ContactPassword', 'class' => 'form-error'
  1364. ),
  1365. '/div'
  1366. );
  1367. $this->assertTags($result, $expected);
  1368. }
  1369. /**
  1370. * testFormValidationAssociated method
  1371. *
  1372. * test display of form errors in conjunction with model::validates.
  1373. *
  1374. * @return void
  1375. */
  1376. public function testFormValidationAssociated() {
  1377. $this->UserForm = ClassRegistry::getObject('UserForm');
  1378. $this->UserForm->OpenidUrl = ClassRegistry::getObject('OpenidUrl');
  1379. $data = array(
  1380. 'UserForm' => array('name' => 'user'),
  1381. 'OpenidUrl' => array('url' => 'http://www.cakephp.org')
  1382. );
  1383. $result = $this->UserForm->OpenidUrl->create($data);
  1384. $this->assertFalse(empty($result));
  1385. $this->assertFalse($this->UserForm->OpenidUrl->validates());
  1386. $result = $this->Form->create('UserForm', array('type' => 'post', 'action' => 'login'));
  1387. $encoding = strtolower(Configure::read('App.encoding'));
  1388. $expected = array(
  1389. 'form' => array(
  1390. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  1391. 'accept-charset' => $encoding
  1392. ),
  1393. 'div' => array('style' => 'display:none;'),
  1394. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1395. '/div'
  1396. );
  1397. $this->assertTags($result, $expected);
  1398. $result = $this->Form->error(
  1399. 'OpenidUrl.openid_not_registered', 'Error, not registered', array('wrap' => false)
  1400. );
  1401. $this->assertEquals('Error, not registered', $result);
  1402. unset($this->UserForm->OpenidUrl, $this->UserForm);
  1403. }
  1404. /**
  1405. * testFormValidationAssociatedFirstLevel method
  1406. *
  1407. * test form error display with associated model.
  1408. *
  1409. * @return void
  1410. */
  1411. public function testFormValidationAssociatedFirstLevel() {
  1412. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1413. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1414. $data = array(
  1415. 'ValidateUser' => array('name' => 'mariano'),
  1416. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias')
  1417. );
  1418. $result = $this->ValidateUser->create($data);
  1419. $this->assertFalse(empty($result));
  1420. $this->assertFalse($this->ValidateUser->validates());
  1421. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1422. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1423. $encoding = strtolower(Configure::read('App.encoding'));
  1424. $expected = array(
  1425. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1426. 'div' => array('style' => 'display:none;'),
  1427. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1428. '/div'
  1429. );
  1430. $this->assertTags($result, $expected);
  1431. $result = $this->Form->error(
  1432. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1433. );
  1434. $this->assertEquals('Invalid email', $result);
  1435. $result = $this->Form->error(
  1436. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1437. );
  1438. $this->assertEquals('Invalid name', $result);
  1439. $result = $this->Form->error(
  1440. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1441. );
  1442. $this->assertEquals('Invalid city', $result);
  1443. unset($this->ValidateUser->ValidateProfile);
  1444. unset($this->ValidateUser);
  1445. }
  1446. /**
  1447. * testFormValidationAssociatedSecondLevel method
  1448. *
  1449. * test form error display with associated model.
  1450. *
  1451. * @return void
  1452. */
  1453. public function testFormValidationAssociatedSecondLevel() {
  1454. $this->ValidateUser = ClassRegistry::getObject('ValidateUser');
  1455. $this->ValidateUser->ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  1456. $this->ValidateUser->ValidateProfile->ValidateItem = ClassRegistry::getObject('ValidateItem');
  1457. $data = array(
  1458. 'ValidateUser' => array('name' => 'mariano'),
  1459. 'ValidateProfile' => array('full_name' => 'Mariano Iglesias'),
  1460. 'ValidateItem' => array('name' => 'Item')
  1461. );
  1462. $result = $this->ValidateUser->create($data);
  1463. $this->assertFalse(empty($result));
  1464. $this->assertFalse($this->ValidateUser->validates());
  1465. $this->assertFalse($this->ValidateUser->ValidateProfile->validates());
  1466. $this->assertFalse($this->ValidateUser->ValidateProfile->ValidateItem->validates());
  1467. $result = $this->Form->create('ValidateUser', array('type' => 'post', 'action' => 'add'));
  1468. $encoding = strtolower(Configure::read('App.encoding'));
  1469. $expected = array(
  1470. 'form' => array('method' => 'post', 'action' => '/validate_users/add', 'id','accept-charset' => $encoding),
  1471. 'div' => array('style' => 'display:none;'),
  1472. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  1473. '/div'
  1474. );
  1475. $this->assertTags($result, $expected);
  1476. $result = $this->Form->error(
  1477. 'ValidateUser.email', 'Invalid email', array('wrap' => false)
  1478. );
  1479. $this->assertEquals('Invalid email', $result);
  1480. $result = $this->Form->error(
  1481. 'ValidateProfile.full_name', 'Invalid name', array('wrap' => false)
  1482. );
  1483. $this->assertEquals('Invalid name', $result);
  1484. $result = $this->Form->error(
  1485. 'ValidateProfile.city', 'Invalid city', array('wrap' => false)
  1486. );
  1487. $result = $this->Form->error(
  1488. 'ValidateItem.description', 'Invalid description', array('wrap' => false)
  1489. );
  1490. $this->assertEquals('Invalid description', $result);
  1491. unset($this->ValidateUser->ValidateProfile->ValidateItem);
  1492. unset($this->ValidateUser->ValidateProfile);
  1493. unset($this->ValidateUser);
  1494. }
  1495. /**
  1496. * testFormValidationMultiRecord method
  1497. *
  1498. * test form error display with multiple records.
  1499. *
  1500. * @return void
  1501. */
  1502. public function testFormValidationMultiRecord() {
  1503. $Contact = ClassRegistry::getObject('Contact');
  1504. $Contact->validationErrors[2] = array(
  1505. 'name' => array('This field cannot be left blank')
  1506. );
  1507. $result = $this->Form->input('Contact.2.name');
  1508. $expected = array(
  1509. 'div' => array('class' => 'input text error'),
  1510. 'label' => array('for' => 'Contact2Name'),
  1511. 'Name',
  1512. '/label',
  1513. 'input' => array(
  1514. 'type' => 'text', 'name' => 'data[Contact][2][name]', 'id' => 'Contact2Name',
  1515. 'class' => 'form-error', 'maxlength' => 255
  1516. ),
  1517. array('div' => array('class' => 'error-message')),
  1518. 'This field cannot be left blank',
  1519. '/div',
  1520. '/div'
  1521. );
  1522. $this->assertTags($result, $expected);
  1523. }
  1524. /**
  1525. * testMultipleInputValidation method
  1526. *
  1527. * test multiple record form validation error display.
  1528. *
  1529. * @return void
  1530. */
  1531. public function testMultipleInputValidation() {
  1532. $Address = ClassRegistry::init(array('class' => 'Address', 'table' => false, 'ds' => 'test'));
  1533. $Address->validationErrors[0] = array(
  1534. 'title' => array('This field cannot be empty'),
  1535. 'first_name' => array('This field cannot be empty')
  1536. );
  1537. $Address->validationErrors[1] = array(
  1538. 'last_name' => array('You must have a last name')
  1539. );
  1540. $this->Form->create();
  1541. $result = $this->Form->input('Address.0.title');
  1542. $expected = array(
  1543. 'div' => array('class'),
  1544. 'label' => array('for'),
  1545. 'preg:/[^<]+/',
  1546. '/label',
  1547. 'input' => array(
  1548. 'type' => 'text', 'name', 'id', 'class' => 'form-error'
  1549. ),
  1550. array('div' => array('class' => 'error-message')),
  1551. 'This field cannot be empty',
  1552. '/div',
  1553. '/div'
  1554. );
  1555. $this->assertTags($result, $expected);
  1556. $result = $this->Form->input('Address.0.first_name');
  1557. $expected = array(
  1558. 'div' => array('class'),
  1559. 'label' => array('for'),
  1560. 'preg:/[^<]+/',
  1561. '/label',
  1562. 'input' => array('type' => 'text', 'name', 'id', 'class' => 'form-error'),
  1563. array('div' => array('class' => 'error-message')),
  1564. 'This field cannot be empty',
  1565. '/div',
  1566. '/div'
  1567. );
  1568. $this->assertTags($result, $expected);
  1569. $result = $this->Form->input('Address.0.last_name');
  1570. $expected = array(
  1571. 'div' => array('class'),
  1572. 'label' => array('for'),
  1573. 'preg:/[^<]+/',
  1574. '/label',
  1575. 'input' => array('type' => 'text', 'name', 'id'),
  1576. '/div'
  1577. );
  1578. $this->assertTags($result, $expected);
  1579. $result = $this->Form->input('Address.1.last_name');
  1580. $expected = array(
  1581. 'div' => array('class'),
  1582. 'label' => array('for'),
  1583. 'preg:/[^<]+/',
  1584. '/label',
  1585. 'input' => array(
  1586. 'type' => 'text', 'name' => 'preg:/[^<]+/',
  1587. 'id' => 'preg:/[^<]+/', 'class' => 'form-error'
  1588. ),
  1589. array('div' => array('class' => 'error-message')),
  1590. 'You must have a last name',
  1591. '/div',
  1592. '/div'
  1593. );
  1594. $this->assertTags($result, $expected);
  1595. }
  1596. /**
  1597. * testInput method
  1598. *
  1599. * Test various incarnations of input().
  1600. *
  1601. * @return void
  1602. */
  1603. public function testInput() {
  1604. $result = $this->Form->input('ValidateUser.balance');
  1605. $expected = array(
  1606. 'div' => array('class'),
  1607. 'label' => array('for'),
  1608. 'Balance',
  1609. '/label',
  1610. 'input' => array('name', 'type' => 'number', 'maxlength' => 8, 'id'),
  1611. '/div',
  1612. );
  1613. $this->assertTags($result, $expected);
  1614. $result = $this->Form->input('Contact.email', array('id' => 'custom'));
  1615. $expected = array(
  1616. 'div' => array('class' => 'input text'),
  1617. 'label' => array('for' => 'custom'),
  1618. 'Email',
  1619. '/label',
  1620. array('input' => array(
  1621. 'type' => 'text', 'name' => 'data[Contact][email]',
  1622. 'id' => 'custom', 'maxlength' => 255
  1623. )),
  1624. '/div'
  1625. );
  1626. $this->assertTags($result, $expected);
  1627. $result = $this->Form->input('Contact.email', array('div' => array('class' => false)));
  1628. $expected = array(
  1629. '<div',
  1630. 'label' => array('for' => 'ContactEmail'),
  1631. 'Email',
  1632. '/label',
  1633. array('input' => array(
  1634. 'type' => 'text', 'name' => 'data[Contact][email]',
  1635. 'id' => 'ContactEmail', 'maxlength' => 255
  1636. )),
  1637. '/div'
  1638. );
  1639. $this->assertTags($result, $expected);
  1640. $result = $this->Form->hidden('Contact.idontexist');
  1641. $expected = array('input' => array(
  1642. 'type' => 'hidden', 'name' => 'data[Contact][idontexist]',
  1643. 'id' => 'ContactIdontexist'
  1644. ));
  1645. $this->assertTags($result, $expected);
  1646. $result = $this->Form->input('Contact.email', array('type' => 'text'));
  1647. $expected = array(
  1648. 'div' => array('class' => 'input text'),
  1649. 'label' => array('for' => 'ContactEmail'),
  1650. 'Email',
  1651. '/label',
  1652. array('input' => array(
  1653. 'type' => 'text', 'name' => 'data[Contact][email]',
  1654. 'id' => 'ContactEmail'
  1655. )),
  1656. '/div'
  1657. );
  1658. $this->assertTags($result, $expected);
  1659. $result = $this->Form->input('Contact.5.email', array('type' => 'text'));
  1660. $expected = array(
  1661. 'div' => array('class' => 'input text'),
  1662. 'label' => array('for' => 'Contact5Email'),
  1663. 'Email',
  1664. '/label',
  1665. array('input' => array(
  1666. 'type' => 'text', 'name' => 'data[Contact][5][email]',
  1667. 'id' => 'Contact5Email'
  1668. )),
  1669. '/div'
  1670. );
  1671. $this->assertTags($result, $expected);
  1672. $result = $this->Form->input('Contact.password');
  1673. $expected = array(
  1674. 'div' => array('class' => 'input password'),
  1675. 'label' => array('for' => 'ContactPassword'),
  1676. 'Password',
  1677. '/label',
  1678. array('input' => array(
  1679. 'type' => 'password', 'name' => 'data[Contact][password]',
  1680. 'id' => 'ContactPassword'
  1681. )),
  1682. '/div'
  1683. );
  1684. $this->assertTags($result, $expected);
  1685. $result = $this->Form->input('Contact.email', array(
  1686. 'type' => 'file', 'class' => 'textbox'
  1687. ));
  1688. $expected = array(
  1689. 'div' => array('class' => 'input file'),
  1690. 'label' => array('for' => 'ContactEmail'),
  1691. 'Email',
  1692. '/label',
  1693. array('input' => array(
  1694. 'type' => 'file', 'name' => 'data[Contact][email]', 'class' => 'textbox',
  1695. 'id' => 'ContactEmail'
  1696. )),
  1697. '/div'
  1698. );
  1699. $this->assertTags($result, $expected);
  1700. $this->Form->request->data = array('Contact' => array('phone' => 'Hello & World > weird chars'));
  1701. $result = $this->Form->input('Contact.phone');
  1702. $expected = array(
  1703. 'div' => array('class' => 'input text'),
  1704. 'label' => array('for' => 'ContactPhone'),
  1705. 'Phone',
  1706. '/label',
  1707. array('input' => array(
  1708. 'type' => 'text', 'name' => 'data[Contact][phone]',
  1709. 'value' => 'Hello &amp; World &gt; weird chars',
  1710. 'id' => 'ContactPhone', 'maxlength' => 255
  1711. )),
  1712. '/div'
  1713. );
  1714. $this->assertTags($result, $expected);
  1715. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  1716. $result = $this->Form->input('Model.0.OtherModel.field', array('id' => 'myId'));
  1717. $expected = array(
  1718. 'div' => array('class' => 'input text'),
  1719. 'label' => array('for' => 'myId'),
  1720. 'Field',
  1721. '/label',
  1722. 'input' => array(
  1723. 'type' => 'text', 'name' => 'data[Model][0][OtherModel][field]',
  1724. 'value' => 'My value', 'id' => 'myId'
  1725. ),
  1726. '/div'
  1727. );
  1728. $this->assertTags($result, $expected);
  1729. unset($this->Form->request->data);
  1730. $Contact = ClassRegistry::getObject('Contact');
  1731. $Contact->validationErrors['field'] = array('Badness!');
  1732. $result = $this->Form->input('Contact.field');
  1733. $expected = array(
  1734. 'div' => array('class' => 'input text error'),
  1735. 'label' => array('for' => 'ContactField'),
  1736. 'Field',
  1737. '/label',
  1738. 'input' => array(
  1739. 'type' => 'text', 'name' => 'data[Contact][field]',
  1740. 'id' => 'ContactField', 'class' => 'form-error'
  1741. ),
  1742. array('div' => array('class' => 'error-message')),
  1743. 'Badness!',
  1744. '/div',
  1745. '/div'
  1746. );
  1747. $this->assertTags($result, $expected);
  1748. $result = $this->Form->input('Contact.field', array(
  1749. 'div' => false, 'error' => array('attributes' => array('wrap' => 'span'))
  1750. ));
  1751. $expected = array(
  1752. 'label' => array('for' => 'ContactField'),
  1753. 'Field',
  1754. '/label',
  1755. 'input' => array(
  1756. 'type' => 'text', 'name' => 'data[Contact][field]',
  1757. 'id' => 'ContactField', 'class' => 'form-error'
  1758. ),
  1759. array('span' => array('class' => 'error-message')),
  1760. 'Badness!',
  1761. '/span'
  1762. );
  1763. $this->assertTags($result, $expected);
  1764. $result = $this->Form->input('Contact.field', array(
  1765. 'type' => 'text', 'error' => array('attributes' => array('class' => 'error'))
  1766. ));
  1767. $expected = array(
  1768. 'div' => array('class' => 'input text error'),
  1769. 'label' => array('for' => 'ContactField'),
  1770. 'Field',
  1771. '/label',
  1772. 'input' => array(
  1773. 'type' => 'text', 'name' => 'data[Contact][field]',
  1774. 'id' => 'ContactField', 'class' => 'form-error'
  1775. ),
  1776. array('div' => array('class' => 'error')),
  1777. 'Badness!',
  1778. '/div'
  1779. );
  1780. $this->assertTags($result, $expected);
  1781. $result = $this->Form->input('Contact.field', array(
  1782. 'div' => array('tag' => 'span'), 'error' => array('attributes' => array('wrap' => false))
  1783. ));
  1784. $expected = array(
  1785. 'span' => array('class' => 'input text error'),
  1786. 'label' => array('for' => 'ContactField'),
  1787. 'Field',
  1788. '/label',
  1789. 'input' => array(
  1790. 'type' => 'text', 'name' => 'data[Contact][field]',
  1791. 'id' => 'ContactField', 'class' => 'form-error'
  1792. ),
  1793. 'Badness!',
  1794. '/span'
  1795. );
  1796. $this->assertTags($result, $expected);
  1797. $result = $this->Form->input('Contact.field', array('after' => 'A message to you, Rudy'));
  1798. $expected = array(
  1799. 'div' => array('class' => 'input text error'),
  1800. 'label' => array('for' => 'ContactField'),
  1801. 'Field',
  1802. '/label',
  1803. 'input' => array(
  1804. 'type' => 'text', 'name' => 'data[Contact][field]',
  1805. 'id' => 'ContactField', 'class' => 'form-error'
  1806. ),
  1807. 'A message to you, Rudy',
  1808. array('div' => array('class' => 'error-message')),
  1809. 'Badness!',
  1810. '/div',
  1811. '/div'
  1812. );
  1813. $this->assertTags($result, $expected);
  1814. $this->Form->setEntity(null);
  1815. $this->Form->setEntity('Contact.field');
  1816. $result = $this->Form->input('Contact.field', array(
  1817. 'after' => 'A message to you, Rudy', 'error' => false
  1818. ));
  1819. $expected = array(
  1820. 'div' => array('class' => 'input text'),
  1821. 'label' => array('for' => 'ContactField'),
  1822. 'Field',
  1823. '/label',
  1824. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1825. 'A message to you, Rudy',
  1826. '/div'
  1827. );
  1828. $this->assertTags($result, $expected);
  1829. $result = $this->Form->input('Object.field', array('after' => 'A message to you, Rudy'));
  1830. $expected = array(
  1831. 'div' => array('class' => 'input text'),
  1832. 'label' => array('for' => 'ObjectField'),
  1833. 'Field',
  1834. '/label',
  1835. 'input' => array('type' => 'text', 'name' => 'data[Object][field]', 'id' => 'ObjectField'),
  1836. 'A message to you, Rudy',
  1837. '/div'
  1838. );
  1839. $this->assertTags($result, $expected);
  1840. $Contact->validationErrors['field'] = array('minLength');
  1841. $result = $this->Form->input('Contact.field', array(
  1842. 'error' => array(
  1843. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1844. 'maxLength' => 'login too large'
  1845. )
  1846. ));
  1847. $expected = array(
  1848. 'div' => array('class' => 'input text error'),
  1849. 'label' => array('for' => 'ContactField'),
  1850. 'Field',
  1851. '/label',
  1852. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1853. array('div' => array('class' => 'error-message')),
  1854. 'Le login doit contenir au moins 2 caractères',
  1855. '/div',
  1856. '/div'
  1857. );
  1858. $this->assertTags($result, $expected);
  1859. $Contact->validationErrors['field'] = array('maxLength');
  1860. $result = $this->Form->input('Contact.field', array(
  1861. 'error' => array(
  1862. 'attributes' => array('wrap' => 'span', 'rel' => 'fake'),
  1863. 'minLength' => 'Le login doit contenir au moins 2 caractères',
  1864. 'maxLength' => 'login too large',
  1865. )
  1866. ));
  1867. $expected = array(
  1868. 'div' => array('class' => 'input text error'),
  1869. 'label' => array('for' => 'ContactField'),
  1870. 'Field',
  1871. '/label',
  1872. 'input' => array('type' => 'text', 'name' => 'data[Contact][field]', 'id' => 'ContactField', 'class' => 'form-error'),
  1873. array('span' => array('class' => 'error-message', 'rel' => 'fake')),
  1874. 'login too large',
  1875. '/span',
  1876. '/div'
  1877. );
  1878. $this->assertTags($result, $expected);
  1879. }
  1880. /**
  1881. * Test that inputs with 0 can be created.
  1882. *
  1883. * @return void
  1884. */
  1885. public function testInputZero() {
  1886. $this->Form->create('User');
  1887. $result = $this->Form->input('0');
  1888. $expected = array(
  1889. 'div' => array('class' => 'input text'),
  1890. 'label' => array('for' => 'User0'), '/label',
  1891. 'input' => array('type' => 'text', 'name' => 'data[User][0]', 'id' => 'User0'),
  1892. '/div'
  1893. );
  1894. $this->assertTags($result, $expected);
  1895. }
  1896. /**
  1897. * test input() with checkbox creation
  1898. *
  1899. * @return void
  1900. */
  1901. public function testInputCheckbox() {
  1902. $result = $this->Form->input('User.active', array('label' => false, 'checked' => true));
  1903. $expected = array(
  1904. 'div' => array('class' => 'input checkbox'),
  1905. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1906. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1907. '/div'
  1908. );
  1909. $this->assertTags($result, $expected);
  1910. $result = $this->Form->input('User.active', array('label' => false, 'checked' => 1));
  1911. $expected = array(
  1912. 'div' => array('class' => 'input checkbox'),
  1913. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1914. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1915. '/div'
  1916. );
  1917. $this->assertTags($result, $expected);
  1918. $result = $this->Form->input('User.active', array('label' => false, 'checked' => '1'));
  1919. $expected = array(
  1920. 'div' => array('class' => 'input checkbox'),
  1921. 'input' => array('type' => 'hidden', 'name' => 'data[User][active]', 'value' => '0', 'id' => 'UserActive_'),
  1922. array('input' => array('type' => 'checkbox', 'name' => 'data[User][active]', 'value' => '1', 'id' => 'UserActive', 'checked' => 'checked')),
  1923. '/div'
  1924. );
  1925. $this->assertTags($result, $expected);
  1926. }
  1927. /**
  1928. * test form->input() with time types.
  1929. *
  1930. */
  1931. public function testInputTime() {
  1932. extract($this->dateRegex);
  1933. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1934. $result = explode(':', $result);
  1935. $this->assertRegExp('/option value="23"/', $result[0]);
  1936. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1937. $result = $this->Form->input('Contact.created', array('type' => 'time', 'timeFormat' => 24));
  1938. $result = explode(':', $result);
  1939. $this->assertRegExp('/option value="23"/', $result[0]);
  1940. $this->assertNotRegExp('/option value="24"/', $result[0]);
  1941. $result = $this->Form->input('Model.field', array(
  1942. 'type' => 'time', 'timeFormat' => 24, 'interval' => 15
  1943. ));
  1944. $result = explode(':', $result);
  1945. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1946. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1947. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1948. $result = $this->Form->input('Model.field', array(
  1949. 'type' => 'time', 'timeFormat' => 12, 'interval' => 15
  1950. ));
  1951. $result = explode(':', $result);
  1952. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1953. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1954. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1955. $result = $this->Form->input('prueba', array(
  1956. 'type' => 'time', 'timeFormat' => 24 , 'dateFormat' => 'DMY' , 'minYear' => 2008,
  1957. 'maxYear' => date('Y') + 1 ,'interval' => 15
  1958. ));
  1959. $result = explode(':', $result);
  1960. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  1961. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  1962. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  1963. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  1964. $result = $this->Form->input('Random.start_time', array(
  1965. 'type' => 'time',
  1966. 'selected' => '18:15'
  1967. ));
  1968. $this->assertContains('<option value="06" selected="selected">6</option>', $result);
  1969. $this->assertContains('<option value="15" selected="selected">15</option>', $result);
  1970. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1971. }
  1972. /**
  1973. * Test interval + selected near the hour roll over.
  1974. *
  1975. * @return void
  1976. */
  1977. public function testTimeSelectedWithInterval() {
  1978. $result = $this->Form->input('Model.start_time', array(
  1979. 'type' => 'time',
  1980. 'interval' => 15,
  1981. 'selected' => array('hour' => '3', 'min' => '57', 'meridian' => 'pm')
  1982. ));
  1983. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  1984. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  1985. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1986. $result = $this->Form->input('Model.start_time', array(
  1987. 'type' => 'time',
  1988. 'interval' => 15,
  1989. 'selected' => '2012-10-23 15:57:00'
  1990. ));
  1991. $this->assertContains('<option value="04" selected="selected">4</option>', $result);
  1992. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  1993. $this->assertContains('<option value="pm" selected="selected">pm</option>', $result);
  1994. $result = $this->Form->input('Model.start_time', array(
  1995. 'timeFormat' => 24,
  1996. 'type' => 'time',
  1997. 'interval' => 15,
  1998. 'selected' => '15:57'
  1999. ));
  2000. $this->assertContains('<option value="16" selected="selected">16</option>', $result);
  2001. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2002. $result = $this->Form->input('Model.start_time', array(
  2003. 'timeFormat' => 24,
  2004. 'type' => 'time',
  2005. 'interval' => 15,
  2006. 'selected' => '23:57'
  2007. ));
  2008. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2009. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2010. $result = $this->Form->input('Model.created', array(
  2011. 'timeFormat' => 24,
  2012. 'type' => 'datetime',
  2013. 'interval' => 15,
  2014. 'selected' => '2012-09-30 23:56'
  2015. ));
  2016. $this->assertContains('<option value="2012" selected="selected">2012</option>', $result);
  2017. $this->assertContains('<option value="10" selected="selected">October</option>', $result);
  2018. $this->assertContains('<option value="01" selected="selected">1</option>', $result);
  2019. $this->assertContains('<option value="00" selected="selected">0</option>', $result);
  2020. $this->assertContains('<option value="00" selected="selected">00</option>', $result);
  2021. }
  2022. /**
  2023. * test form->input() with datetime, date and time types
  2024. *
  2025. * @return void
  2026. */
  2027. public function testInputDatetime() {
  2028. extract($this->dateRegex);
  2029. $result = $this->Form->input('prueba', array(
  2030. 'type' => 'datetime', 'timeFormat' => 24, 'dateFormat' => 'DMY', 'minYear' => 2008,
  2031. 'maxYear' => date('Y') + 1, 'interval' => 15
  2032. ));
  2033. $result = explode(':', $result);
  2034. $this->assertNotRegExp('#<option value="12"[^>]*>12</option>#', $result[1]);
  2035. $this->assertNotRegExp('#<option value="50"[^>]*>50</option>#', $result[1]);
  2036. $this->assertRegExp('#<option value="15"[^>]*>15</option>#', $result[1]);
  2037. $this->assertRegExp('#<option value="30"[^>]*>30</option>#', $result[1]);
  2038. //related to ticket #5013
  2039. $result = $this->Form->input('Contact.date', array(
  2040. 'type' => 'date', 'class' => 'customClass', 'onChange' => 'function(){}'
  2041. ));
  2042. $this->assertRegExp('/class="customClass"/', $result);
  2043. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2044. $result = $this->Form->input('Contact.date', array(
  2045. 'type' => 'date', 'id' => 'customId', 'onChange' => 'function(){}'
  2046. ));
  2047. $this->assertRegExp('/id="customIdDay"/', $result);
  2048. $this->assertRegExp('/id="customIdMonth"/', $result);
  2049. $this->assertRegExp('/onChange="function\(\)\{\}"/', $result);
  2050. $result = $this->Form->input('Model.field', array(
  2051. 'type' => 'datetime', 'timeFormat' => 24, 'id' => 'customID'
  2052. ));
  2053. $this->assertRegExp('/id="customIDDay"/', $result);
  2054. $this->assertRegExp('/id="customIDHour"/', $result);
  2055. $result = explode('</select><select', $result);
  2056. $result = explode(':', $result[1]);
  2057. $this->assertRegExp('/option value="23"/', $result[0]);
  2058. $this->assertNotRegExp('/option value="24"/', $result[0]);
  2059. $result = $this->Form->input('Model.field', array(
  2060. 'type' => 'datetime', 'timeFormat' => 12
  2061. ));
  2062. $result = explode('</select><select', $result);
  2063. $result = explode(':', $result[1]);
  2064. $this->assertRegExp('/option value="12"/', $result[0]);
  2065. $this->assertNotRegExp('/option value="13"/', $result[0]);
  2066. $this->Form->request->data = array('Contact' => array('created' => null));
  2067. $result = $this->Form->input('Contact.created', array('empty' => 'Date Unknown'));
  2068. $expected = array(
  2069. 'div' => array('class' => 'input date'),
  2070. 'label' => array('for' => 'ContactCreatedMonth'),
  2071. 'Created',
  2072. '/label',
  2073. array('select' => array('name' => 'data[Contact][created][month]', 'id' => 'ContactCreatedMonth')),
  2074. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2075. $monthsRegex,
  2076. '/select', '-',
  2077. array('select' => array('name' => 'data[Contact][created][day]', 'id' => 'ContactCreatedDay')),
  2078. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2079. $daysRegex,
  2080. '/select', '-',
  2081. array('select' => array('name' => 'data[Contact][created][year]', 'id' => 'ContactCreatedYear')),
  2082. array('option' => array('value' => '')), 'Date Unknown', '/option',
  2083. $yearsRegex,
  2084. '/select',
  2085. '/div'
  2086. );
  2087. $this->assertTags($result, $expected);
  2088. $this->Form->request->data = array('Contact' => array('created' => null));
  2089. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'dateFormat' => 'NONE'));
  2090. $this->assertRegExp('/for\="ContactCreatedHour"/', $result);
  2091. $this->Form->request->data = array('Contact' => array('created' => null));
  2092. $result = $this->Form->input('Contact.created', array('type' => 'datetime', 'timeFormat' => 'NONE'));
  2093. $this->assertRegExp('/for\="ContactCreatedMonth"/', $result);
  2094. $result = $this->Form->input('Contact.created', array(
  2095. 'type' => 'date',
  2096. 'id' => array('day' => 'created-day', 'month' => 'created-month', 'year' => 'created-year'),
  2097. 'timeFormat' => 'NONE'
  2098. ));
  2099. $this->assertRegExp('/for\="created-month"/', $result);
  2100. }
  2101. /**
  2102. * Test generating checkboxes in a loop.
  2103. *
  2104. * @return void
  2105. */
  2106. public function testInputCheckboxesInLoop() {
  2107. for ($i = 1; $i < 5; $i++) {
  2108. $result = $this->Form->input("Contact.{$i}.email", array('type' => 'checkbox', 'value' => $i));
  2109. $expected = array(
  2110. 'div' => array('class' => 'input checkbox'),
  2111. 'input' => array('type' => 'hidden', 'name' => "data[Contact][{$i}][email]", 'value' => '0', 'id' => "Contact{$i}Email_"),
  2112. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][{$i}][email]", 'value' => $i, 'id' => "Contact{$i}Email")),
  2113. 'label' => array('for' => "Contact{$i}Email"),
  2114. 'Email',
  2115. '/label',
  2116. '/div'
  2117. );
  2118. $this->assertTags($result, $expected);
  2119. }
  2120. }
  2121. /**
  2122. * Test generating checkboxes with disabled elements.
  2123. *
  2124. * @return void
  2125. */
  2126. public function testInputCheckboxWithDisabledElements() {
  2127. $options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
  2128. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
  2129. $expected = array(
  2130. array('div' => array('class' => 'input select')),
  2131. array('label' => array('for' => "ContactMultiple")),
  2132. 'Multiple',
  2133. '/label',
  2134. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2135. array('div' => array('class' => 'checkbox')),
  2136. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
  2137. array('label' => array('for' => "ContactMultiple1")),
  2138. 'One',
  2139. '/label',
  2140. '/div',
  2141. array('div' => array('class' => 'checkbox')),
  2142. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2143. array('label' => array('for' => "ContactMultiple2")),
  2144. 'Two',
  2145. '/label',
  2146. '/div',
  2147. array('div' => array('class' => 'checkbox')),
  2148. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2149. array('label' => array('for' => "ContactMultiple3")),
  2150. 'Three',
  2151. '/label',
  2152. '/div',
  2153. '/div'
  2154. );
  2155. $this->assertTags($result, $expected);
  2156. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
  2157. $this->assertTags($result, $expected);
  2158. $disabled = array('2', 3);
  2159. $expected = array(
  2160. array('div' => array('class' => 'input select')),
  2161. array('label' => array('for' => "ContactMultiple")),
  2162. 'Multiple',
  2163. '/label',
  2164. array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
  2165. array('div' => array('class' => 'checkbox')),
  2166. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
  2167. array('label' => array('for' => "ContactMultiple1")),
  2168. 'One',
  2169. '/label',
  2170. '/div',
  2171. array('div' => array('class' => 'checkbox')),
  2172. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
  2173. array('label' => array('for' => "ContactMultiple2")),
  2174. 'Two',
  2175. '/label',
  2176. '/div',
  2177. array('div' => array('class' => 'checkbox')),
  2178. array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
  2179. array('label' => array('for' => "ContactMultiple3")),
  2180. 'Three',
  2181. '/label',
  2182. '/div',
  2183. '/div'
  2184. );
  2185. $result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
  2186. $this->assertTags($result, $expected);
  2187. }
  2188. /**
  2189. * test input name with leading integer, ensure attributes are generated correctly.
  2190. *
  2191. * @return void
  2192. */
  2193. public function testInputWithLeadingInteger() {
  2194. $result = $this->Form->text('0.Node.title');
  2195. $expected = array(
  2196. 'input' => array('name' => 'data[0][Node][title]', 'id' => '0NodeTitle', 'type' => 'text')
  2197. );
  2198. $this->assertTags($result, $expected);
  2199. }
  2200. /**
  2201. * test form->input() with select type inputs.
  2202. *
  2203. * @return void
  2204. */
  2205. public function testInputSelectType() {
  2206. $result = $this->Form->input('email', array(
  2207. 'options' => array('è' => 'Firést', 'é' => 'Secoènd'), 'empty' => true)
  2208. );
  2209. $expected = array(
  2210. 'div' => array('class' => 'input select'),
  2211. 'label' => array('for' => 'email'),
  2212. 'Email',
  2213. '/label',
  2214. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2215. array('option' => array('value' => '')),
  2216. '/option',
  2217. array('option' => array('value' => 'è')),
  2218. 'Firést',
  2219. '/option',
  2220. array('option' => array('value' => 'é')),
  2221. 'Secoènd',
  2222. '/option',
  2223. '/select',
  2224. '/div'
  2225. );
  2226. $this->assertTags($result, $expected);
  2227. $result = $this->Form->input('email', array(
  2228. 'options' => array('First', 'Second'), 'empty' => true)
  2229. );
  2230. $expected = array(
  2231. 'div' => array('class' => 'input select'),
  2232. 'label' => array('for' => 'email'),
  2233. 'Email',
  2234. '/label',
  2235. array('select' => array('name' => 'data[email]', 'id' => 'email')),
  2236. array('option' => array('value' => '')),
  2237. '/option',
  2238. array('option' => array('value' => '0')),
  2239. 'First',
  2240. '/option',
  2241. array('option' => array('value' => '1')),
  2242. 'Second',
  2243. '/option',
  2244. '/select',
  2245. '/div'
  2246. );
  2247. $this->assertTags($result, $expected);
  2248. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2249. $this->Form->request->data = array('Model' => array('user_id' => 'value'));
  2250. $result = $this->Form->input('Model.user_id', array('empty' => true));
  2251. $expected = array(
  2252. 'div' => array('class' => 'input select'),
  2253. 'label' => array('for' => 'ModelUserId'),
  2254. 'User',
  2255. '/label',
  2256. 'select' => array('name' => 'data[Model][user_id]', 'id' => 'ModelUserId'),
  2257. array('option' => array('value' => '')),
  2258. '/option',
  2259. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2260. 'good',
  2261. '/option',
  2262. array('option' => array('value' => 'other')),
  2263. 'bad',
  2264. '/option',
  2265. '/select',
  2266. '/div'
  2267. );
  2268. $this->assertTags($result, $expected);
  2269. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2270. $this->Form->request->data = array('Thing' => array('user_id' => null));
  2271. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2272. $expected = array(
  2273. 'div' => array('class' => 'input select'),
  2274. 'label' => array('for' => 'ThingUserId'),
  2275. 'User',
  2276. '/label',
  2277. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2278. array('option' => array('value' => '')),
  2279. 'Some Empty',
  2280. '/option',
  2281. array('option' => array('value' => 'value')),
  2282. 'good',
  2283. '/option',
  2284. array('option' => array('value' => 'other')),
  2285. 'bad',
  2286. '/option',
  2287. '/select',
  2288. '/div'
  2289. );
  2290. $this->assertTags($result, $expected);
  2291. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2292. $this->Form->request->data = array('Thing' => array('user_id' => 'value'));
  2293. $result = $this->Form->input('Thing.user_id', array('empty' => 'Some Empty'));
  2294. $expected = array(
  2295. 'div' => array('class' => 'input select'),
  2296. 'label' => array('for' => 'ThingUserId'),
  2297. 'User',
  2298. '/label',
  2299. 'select' => array('name' => 'data[Thing][user_id]', 'id' => 'ThingUserId'),
  2300. array('option' => array('value' => '')),
  2301. 'Some Empty',
  2302. '/option',
  2303. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2304. 'good',
  2305. '/option',
  2306. array('option' => array('value' => 'other')),
  2307. 'bad',
  2308. '/option',
  2309. '/select',
  2310. '/div'
  2311. );
  2312. $this->assertTags($result, $expected);
  2313. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2314. $this->Form->request->data = array('User' => array('User' => array('value')));
  2315. $result = $this->Form->input('User.User', array('empty' => true));
  2316. $expected = array(
  2317. 'div' => array('class' => 'input select'),
  2318. 'label' => array('for' => 'UserUser'),
  2319. 'User',
  2320. '/label',
  2321. 'input' => array('type' => 'hidden', 'name' => 'data[User][User]', 'value' => '', 'id' => 'UserUser_'),
  2322. 'select' => array('name' => 'data[User][User][]', 'id' => 'UserUser', 'multiple' => 'multiple'),
  2323. array('option' => array('value' => '')),
  2324. '/option',
  2325. array('option' => array('value' => 'value', 'selected' => 'selected')),
  2326. 'good',
  2327. '/option',
  2328. array('option' => array('value' => 'other')),
  2329. 'bad',
  2330. '/option',
  2331. '/select',
  2332. '/div'
  2333. );
  2334. $this->assertTags($result, $expected);
  2335. $this->Form->data = array();
  2336. $result = $this->Form->input('Publisher.id', array(
  2337. 'label' => 'Publisher',
  2338. 'type' => 'select',
  2339. 'multiple' => 'checkbox',
  2340. 'options' => array('Value 1' => 'Label 1', 'Value 2' => 'Label 2')
  2341. ));
  2342. $expected = array(
  2343. array('div' => array('class' => 'input select')),
  2344. array('label' => array('for' => 'PublisherId')),
  2345. 'Publisher',
  2346. '/label',
  2347. 'input' => array('type' => 'hidden', 'name' => 'data[Publisher][id]', 'value' => '', 'id' => 'PublisherId'),
  2348. array('div' => array('class' => 'checkbox')),
  2349. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 1', 'id' => 'PublisherIdValue1')),
  2350. array('label' => array('for' => 'PublisherIdValue1')),
  2351. 'Label 1',
  2352. '/label',
  2353. '/div',
  2354. array('div' => array('class' => 'checkbox')),
  2355. array('input' => array('type' => 'checkbox', 'name' => 'data[Publisher][id][]', 'value' => 'Value 2', 'id' => 'PublisherIdValue2')),
  2356. array('label' => array('for' => 'PublisherIdValue2')),
  2357. 'Label 2',
  2358. '/label',
  2359. '/div',
  2360. '/div'
  2361. );
  2362. $this->assertTags($result, $expected);
  2363. }
  2364. /**
  2365. * test that input() and a non standard primary key makes a hidden input by default.
  2366. *
  2367. * @return void
  2368. */
  2369. public function testInputWithNonStandardPrimaryKeyMakesHidden() {
  2370. $this->Form->create('User');
  2371. $this->Form->fieldset = array(
  2372. 'User' => array(
  2373. 'fields' => array(
  2374. 'model_id' => array('type' => 'integer')
  2375. ),
  2376. 'validates' => array(),
  2377. 'key' => 'model_id'
  2378. )
  2379. );
  2380. $result = $this->Form->input('model_id');
  2381. $expected = array(
  2382. 'input' => array('type' => 'hidden', 'name' => 'data[User][model_id]', 'id' => 'UserModelId'),
  2383. );
  2384. $this->assertTags($result, $expected);
  2385. }
  2386. /**
  2387. * test that overriding the magic select type widget is possible
  2388. *
  2389. * @return void
  2390. */
  2391. public function testInputOverridingMagicSelectType() {
  2392. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2393. $result = $this->Form->input('Model.user_id', array('type' => 'text'));
  2394. $expected = array(
  2395. 'div' => array('class' => 'input text'),
  2396. 'label' => array('for' => 'ModelUserId'), 'User', '/label',
  2397. 'input' => array('name' => 'data[Model][user_id]', 'type' => 'text', 'id' => 'ModelUserId'),
  2398. '/div'
  2399. );
  2400. $this->assertTags($result, $expected);
  2401. //Check that magic types still work for plural/singular vars
  2402. $this->View->viewVars['types'] = array('value' => 'good', 'other' => 'bad');
  2403. $result = $this->Form->input('Model.type');
  2404. $expected = array(
  2405. 'div' => array('class' => 'input select'),
  2406. 'label' => array('for' => 'ModelType'), 'Type', '/label',
  2407. 'select' => array('name' => 'data[Model][type]', 'id' => 'ModelType'),
  2408. array('option' => array('value' => 'value')), 'good', '/option',
  2409. array('option' => array('value' => 'other')), 'bad', '/option',
  2410. '/select',
  2411. '/div'
  2412. );
  2413. $this->assertTags($result, $expected);
  2414. }
  2415. /**
  2416. * Test that magic input() selects are created for type=number
  2417. *
  2418. * @return void
  2419. */
  2420. public function testInputMagicSelectForTypeNumber() {
  2421. $this->View->viewVars['balances'] = array(0 => 'nothing', 1 => 'some', 100 => 'a lot');
  2422. $this->Form->request->data = array('ValidateUser' => array('balance' => 1));
  2423. $result = $this->Form->input('ValidateUser.balance');
  2424. $expected = array(
  2425. 'div' => array('class' => 'input select'),
  2426. 'label' => array('for' => 'ValidateUserBalance'),
  2427. 'Balance',
  2428. '/label',
  2429. 'select' => array('name' => 'data[ValidateUser][balance]', 'id' => 'ValidateUserBalance'),
  2430. array('option' => array('value' => '0')),
  2431. 'nothing',
  2432. '/option',
  2433. array('option' => array('value' => '1', 'selected' => 'selected')),
  2434. 'some',
  2435. '/option',
  2436. array('option' => array('value' => '100')),
  2437. 'a lot',
  2438. '/option',
  2439. '/select',
  2440. '/div'
  2441. );
  2442. $this->assertTags($result, $expected);
  2443. }
  2444. /**
  2445. * Test that magic input() selects can easily be converted into radio types without error.
  2446. *
  2447. * @return void
  2448. */
  2449. public function testInputMagicSelectChangeToRadio() {
  2450. $this->View->viewVars['users'] = array('value' => 'good', 'other' => 'bad');
  2451. $result = $this->Form->input('Model.user_id', array('type' => 'radio'));
  2452. $this->assertRegExp('/input type="radio"/', $result);
  2453. }
  2454. /**
  2455. * fields with the same name as the model should work.
  2456. *
  2457. * @return void
  2458. */
  2459. public function testInputWithMatchingFieldAndModelName() {
  2460. $this->Form->create('User');
  2461. $this->Form->fieldset = array(
  2462. 'User' => array(
  2463. 'fields' => array(
  2464. 'User' => array('type' => 'text')
  2465. ),
  2466. 'validates' => array(),
  2467. 'key' => 'id'
  2468. )
  2469. );
  2470. $this->Form->request->data['User']['User'] = 'ABC, Inc.';
  2471. $result = $this->Form->input('User', array('type' => 'text'));
  2472. $expected = array(
  2473. 'div' => array('class' => 'input text'),
  2474. 'label' => array('for' => 'UserUser'), 'User', '/label',
  2475. 'input' => array('name' => 'data[User][User]', 'type' => 'text', 'id' => 'UserUser', 'value' => 'ABC, Inc.'),
  2476. '/div'
  2477. );
  2478. $this->assertTags($result, $expected);
  2479. }
  2480. /**
  2481. * testFormInputs method
  2482. *
  2483. * test correct results from form::inputs().
  2484. *
  2485. * @return void
  2486. */
  2487. public function testFormInputs() {
  2488. $this->Form->create('Contact');
  2489. $result = $this->Form->inputs('The Legend');
  2490. $expected = array(
  2491. '<fieldset',
  2492. '<legend',
  2493. 'The Legend',
  2494. '/legend',
  2495. '*/fieldset',
  2496. );
  2497. $this->assertTags($result, $expected);
  2498. $result = $this->Form->inputs(array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2499. $expected = array(
  2500. 'fieldset' => array('class' => 'classy-stuff'),
  2501. '<legend',
  2502. 'Field of Dreams',
  2503. '/legend',
  2504. '*/fieldset'
  2505. );
  2506. $this->assertTags($result, $expected);
  2507. $result = $this->Form->inputs(null, null, array('legend' => 'Field of Dreams', 'fieldset' => 'classy-stuff'));
  2508. $this->assertTags($result, $expected);
  2509. $result = $this->Form->inputs('Field of Dreams', null, array('fieldset' => 'classy-stuff'));
  2510. $this->assertTags($result, $expected);
  2511. $this->Form->create('Contact');
  2512. $this->Form->request['prefix'] = 'admin';
  2513. $this->Form->request['action'] = 'admin_edit';
  2514. $result = $this->Form->inputs();
  2515. $expected = array(
  2516. '<fieldset',
  2517. '<legend',
  2518. 'Edit Contact',
  2519. '/legend',
  2520. '*/fieldset',
  2521. );
  2522. $this->assertTags($result, $expected);
  2523. $this->Form->create('Contact');
  2524. $result = $this->Form->inputs(false);
  2525. $expected = array(
  2526. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2527. array('div' => array('class' => 'input text')),
  2528. '*/div',
  2529. array('div' => array('class' => 'input text')),
  2530. '*/div',
  2531. array('div' => array('class' => 'input text')),
  2532. '*/div',
  2533. array('div' => array('class' => 'input password')),
  2534. '*/div',
  2535. array('div' => array('class' => 'input date')),
  2536. '*/div',
  2537. array('div' => array('class' => 'input date')),
  2538. '*/div',
  2539. array('div' => array('class' => 'input datetime')),
  2540. '*/div',
  2541. array('div' => array('class' => 'input number')),
  2542. '*/div',
  2543. array('div' => array('class' => 'input select')),
  2544. '*/div',
  2545. );
  2546. $this->assertTags($result, $expected);
  2547. $this->Form->create('Contact');
  2548. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => false));
  2549. $expected = array(
  2550. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2551. array('div' => array('class' => 'input text')),
  2552. '*/div',
  2553. array('div' => array('class' => 'input text')),
  2554. '*/div',
  2555. array('div' => array('class' => 'input text')),
  2556. '*/div',
  2557. array('div' => array('class' => 'input password')),
  2558. '*/div',
  2559. array('div' => array('class' => 'input date')),
  2560. '*/div',
  2561. array('div' => array('class' => 'input date')),
  2562. '*/div',
  2563. array('div' => array('class' => 'input datetime')),
  2564. '*/div',
  2565. array('div' => array('class' => 'input number')),
  2566. '*/div',
  2567. array('div' => array('class' => 'input select')),
  2568. '*/div',
  2569. );
  2570. $this->assertTags($result, $expected);
  2571. $this->Form->create('Contact');
  2572. $result = $this->Form->inputs(null, null, array('fieldset' => false));
  2573. $this->assertTags($result, $expected);
  2574. $this->Form->create('Contact');
  2575. $result = $this->Form->inputs(array('fieldset' => true, 'legend' => false));
  2576. $expected = array(
  2577. 'fieldset' => array(),
  2578. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2579. array('div' => array('class' => 'input text')),
  2580. '*/div',
  2581. array('div' => array('class' => 'input text')),
  2582. '*/div',
  2583. array('div' => array('class' => 'input text')),
  2584. '*/div',
  2585. array('div' => array('class' => 'input password')),
  2586. '*/div',
  2587. array('div' => array('class' => 'input date')),
  2588. '*/div',
  2589. array('div' => array('class' => 'input date')),
  2590. '*/div',
  2591. array('div' => array('class' => 'input datetime')),
  2592. '*/div',
  2593. array('div' => array('class' => 'input number')),
  2594. '*/div',
  2595. array('div' => array('class' => 'input select')),
  2596. '*/div',
  2597. '/fieldset'
  2598. );
  2599. $this->assertTags($result, $expected);
  2600. $this->Form->create('Contact');
  2601. $result = $this->Form->inputs(array('fieldset' => false, 'legend' => 'Hello'));
  2602. $expected = array(
  2603. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2604. array('div' => array('class' => 'input text')),
  2605. '*/div',
  2606. array('div' => array('class' => 'input text')),
  2607. '*/div',
  2608. array('div' => array('class' => 'input text')),
  2609. '*/div',
  2610. array('div' => array('class' => 'input password')),
  2611. '*/div',
  2612. array('div' => array('class' => 'input date')),
  2613. '*/div',
  2614. array('div' => array('class' => 'input date')),
  2615. '*/div',
  2616. array('div' => array('class' => 'input datetime')),
  2617. '*/div',
  2618. array('div' => array('class' => 'input number')),
  2619. '*/div',
  2620. array('div' => array('class' => 'input select')),
  2621. '*/div',
  2622. );
  2623. $this->assertTags($result, $expected);
  2624. $this->Form->create('Contact');
  2625. $result = $this->Form->inputs(null, null, array('fieldset' => false, 'legend' => 'Hello'));
  2626. $this->assertTags($result, $expected);
  2627. $this->Form->create('Contact');
  2628. $result = $this->Form->inputs('Hello');
  2629. $expected = array(
  2630. 'fieldset' => array(),
  2631. 'legend' => array(),
  2632. 'Hello',
  2633. '/legend',
  2634. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2635. array('div' => array('class' => 'input text')),
  2636. '*/div',
  2637. array('div' => array('class' => 'input text')),
  2638. '*/div',
  2639. array('div' => array('class' => 'input text')),
  2640. '*/div',
  2641. array('div' => array('class' => 'input password')),
  2642. '*/div',
  2643. array('div' => array('class' => 'input date')),
  2644. '*/div',
  2645. array('div' => array('class' => 'input date')),
  2646. '*/div',
  2647. array('div' => array('class' => 'input datetime')),
  2648. '*/div',
  2649. array('div' => array('class' => 'input number')),
  2650. '*/div',
  2651. array('div' => array('class' => 'input select')),
  2652. '*/div',
  2653. '/fieldset'
  2654. );
  2655. $this->assertTags($result, $expected);
  2656. $this->Form->create('Contact');
  2657. $result = $this->Form->inputs(array('legend' => 'Hello'));
  2658. $expected = array(
  2659. 'fieldset' => array(),
  2660. 'legend' => array(),
  2661. 'Hello',
  2662. '/legend',
  2663. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][id]', 'id' => 'ContactId'),
  2664. array('div' => array('class' => 'input text')),
  2665. '*/div',
  2666. array('div' => array('class' => 'input text')),
  2667. '*/div',
  2668. array('div' => array('class' => 'input text')),
  2669. '*/div',
  2670. array('div' => array('class' => 'input password')),
  2671. '*/div',
  2672. array('div' => array('class' => 'input date')),
  2673. '*/div',
  2674. array('div' => array('class' => 'input date')),
  2675. '*/div',
  2676. array('div' => array('class' => 'input datetime')),
  2677. '*/div',
  2678. array('div' => array('class' => 'input number')),
  2679. '*/div',
  2680. array('div' => array('class' => 'input select')),
  2681. '*/div',
  2682. '/fieldset'
  2683. );
  2684. $this->assertTags($result, $expected);
  2685. $this->Form->create('Contact');
  2686. $result = $this->Form->inputs(null, null, array('legend' => 'Hello'));
  2687. $this->assertTags($result, $expected);
  2688. $this->Form->end();
  2689. $this->Form->create(false);
  2690. $expected = array(
  2691. 'fieldset' => array(),
  2692. array('div' => array('class' => 'input text')),
  2693. 'label' => array('for' => 'foo'),
  2694. 'Foo',
  2695. '/label',
  2696. 'input' => array('type' => 'text', 'name' => 'data[foo]', 'id' => 'foo'),
  2697. '*/div',
  2698. '/fieldset'
  2699. );
  2700. $result = $this->Form->inputs(
  2701. array('foo' => array('type' => 'text')),
  2702. array(),
  2703. array('legend' => false)
  2704. );
  2705. $this->assertTags($result, $expected);
  2706. }
  2707. /**
  2708. * testSelectAsCheckbox method
  2709. *
  2710. * test multi-select widget with checkbox formatting.
  2711. *
  2712. * @return void
  2713. */
  2714. public function testSelectAsCheckbox() {
  2715. $result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
  2716. $expected = array(
  2717. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2718. array('div' => array('class' => 'checkbox')),
  2719. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
  2720. array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
  2721. 'first',
  2722. '/label',
  2723. '/div',
  2724. array('div' => array('class' => 'checkbox')),
  2725. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
  2726. array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
  2727. 'second',
  2728. '/label',
  2729. '/div',
  2730. array('div' => array('class' => 'checkbox')),
  2731. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  2732. array('label' => array('for' => 'ModelMultiField2')),
  2733. 'third',
  2734. '/label',
  2735. '/div',
  2736. );
  2737. $this->assertTags($result, $expected);
  2738. $result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
  2739. $expected = array(
  2740. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  2741. array('div' => array('class' => 'checkbox')),
  2742. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField12')),
  2743. array('label' => array('for' => 'ModelMultiField12')),
  2744. 'half',
  2745. '/label',
  2746. '/div',
  2747. );
  2748. $this->assertTags($result, $expected);
  2749. }
  2750. /**
  2751. * testLabel method
  2752. *
  2753. * test label generation.
  2754. *
  2755. * @return void
  2756. */
  2757. public function testLabel() {
  2758. $this->Form->text('Person.name');
  2759. $result = $this->Form->label();
  2760. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2761. $this->Form->text('Person.name');
  2762. $result = $this->Form->label();
  2763. $this->assertTags($result, array('label' => array('for' => 'PersonName'), 'Name', '/label'));
  2764. $result = $this->Form->label('Person.first_name');
  2765. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'First Name', '/label'));
  2766. $result = $this->Form->label('Person.first_name', 'Your first name');
  2767. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), 'Your first name', '/label'));
  2768. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class'));
  2769. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class'), 'Your first name', '/label'));
  2770. $result = $this->Form->label('Person.first_name', 'Your first name', array('class' => 'my-class', 'id' => 'LabelID'));
  2771. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName', 'class' => 'my-class', 'id' => 'LabelID'), 'Your first name', '/label'));
  2772. $result = $this->Form->label('Person.first_name', '');
  2773. $this->assertTags($result, array('label' => array('for' => 'PersonFirstName'), '/label'));
  2774. $result = $this->Form->label('Person.2.name', '');
  2775. $this->assertTags($result, array('label' => array('for' => 'Person2Name'), '/label'));
  2776. }
  2777. /**
  2778. * testTextbox method
  2779. *
  2780. * test textbox element generation
  2781. *
  2782. * @return void
  2783. */
  2784. public function testTextbox() {
  2785. $result = $this->Form->text('Model.field');
  2786. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2787. $result = $this->Form->text('Model.field', array('type' => 'password'));
  2788. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Model][field]', 'id' => 'ModelField')));
  2789. $result = $this->Form->text('Model.field', array('id' => 'theID'));
  2790. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'id' => 'theID')));
  2791. $this->Form->request->data['Model']['text'] = 'test <strong>HTML</strong> values';
  2792. $result = $this->Form->text('Model.text');
  2793. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][text]', 'value' => 'test &lt;strong&gt;HTML&lt;/strong&gt; values', 'id' => 'ModelText')));
  2794. $Contact = ClassRegistry::getObject('Contact');
  2795. $Contact->validationErrors['text'] = array(true);
  2796. $this->Form->request->data['Contact']['text'] = 'test';
  2797. $result = $this->Form->text('Contact.text', array('id' => 'theID'));
  2798. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Contact][text]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2799. $this->Form->request->data['Model']['0']['OtherModel']['field'] = 'My value';
  2800. $result = $this->Form->text('Model.0.OtherModel.field', array('id' => 'myId'));
  2801. $expected = array(
  2802. 'input' => array('type' => 'text', 'name' => 'data[Model][0][OtherModel][field]', 'value' => 'My value', 'id' => 'myId')
  2803. );
  2804. $this->assertTags($result, $expected);
  2805. }
  2806. /**
  2807. * testDefaultValue method
  2808. *
  2809. * Test default value setting
  2810. *
  2811. * @return void
  2812. */
  2813. public function testDefaultValue() {
  2814. $this->Form->request->data['Model']['field'] = 'test';
  2815. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2816. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'test', 'id' => 'ModelField')));
  2817. unset($this->Form->request->data['Model']['field']);
  2818. $result = $this->Form->text('Model.field', array('default' => 'default value'));
  2819. $this->assertTags($result, array('input' => array('type' => 'text', 'name' => 'data[Model][field]', 'value' => 'default value', 'id' => 'ModelField')));
  2820. }
  2821. /**
  2822. * testCheckboxDefaultValue method
  2823. *
  2824. * Test default value setting on checkbox() method
  2825. *
  2826. * @return void
  2827. */
  2828. public function testCheckboxDefaultValue() {
  2829. $this->Form->request->data['Model']['field'] = false;
  2830. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2831. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2832. unset($this->Form->request->data['Model']['field']);
  2833. $result = $this->Form->checkbox('Model.field', array('default' => true, 'hiddenField' => false));
  2834. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2835. $this->Form->request->data['Model']['field'] = true;
  2836. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2837. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked')));
  2838. unset($this->Form->request->data['Model']['field']);
  2839. $result = $this->Form->checkbox('Model.field', array('default' => false, 'hiddenField' => false));
  2840. $this->assertTags($result, array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField')));
  2841. }
  2842. /**
  2843. * testError method
  2844. *
  2845. * Test field error generation
  2846. *
  2847. * @return void
  2848. */
  2849. public function testError() {
  2850. $Contact = ClassRegistry::getObject('Contact');
  2851. $Contact->validationErrors['field'] = array(1);
  2852. $result = $this->Form->error('Contact.field');
  2853. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field Field', '/div'));
  2854. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2855. $this->assertEquals('Error in field Field', $result);
  2856. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2857. $result = $this->Form->error('Contact.field', null, array('wrap' => false));
  2858. $this->assertEquals('This field contains invalid input', $result);
  2859. $Contact->validationErrors['field'] = array("This field contains invalid input");
  2860. $result = $this->Form->error('Contact.field', null, array('wrap' => 'span'));
  2861. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'This field contains invalid input', '/span'));
  2862. $result = $this->Form->error('Contact.field', 'There is an error fool!', array('wrap' => 'span'));
  2863. $this->assertTags($result, array('span' => array('class' => 'error-message'), 'There is an error fool!', '/span'));
  2864. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false));
  2865. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2866. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => true));
  2867. $this->assertEquals('&lt;strong&gt;Badness!&lt;/strong&gt;', $result);
  2868. $result = $this->Form->error('Contact.field', "<strong>Badness!</strong>", array('wrap' => false, 'escape' => false));
  2869. $this->assertEquals('<strong>Badness!</strong>', $result);
  2870. $Contact->validationErrors['field'] = array("email");
  2871. $result = $this->Form->error('Contact.field', array('attributes' => array('class' => 'field-error'), 'email' => 'No good!'));
  2872. $expected = array(
  2873. 'div' => array('class' => 'field-error'),
  2874. 'No good!',
  2875. '/div'
  2876. );
  2877. $this->assertTags($result, $expected);
  2878. $Contact->validationErrors['field'] = array('notEmpty', 'email', 'Something else');
  2879. $result = $this->Form->error('Contact.field', array(
  2880. 'notEmpty' => 'Cannot be empty',
  2881. 'email' => 'No good!'
  2882. ));
  2883. $expected = array(
  2884. 'div' => array('class' => 'error-message'),
  2885. 'ul' => array(),
  2886. '<li', 'Cannot be empty', '/li',
  2887. '<li', 'No good!', '/li',
  2888. '<li', 'Something else', '/li',
  2889. '/ul',
  2890. '/div'
  2891. );
  2892. $this->assertTags($result, $expected);
  2893. // Testing error messages list options
  2894. $Contact->validationErrors['field'] = array('notEmpty', 'email');
  2895. $result = $this->Form->error('Contact.field', null, array('listOptions' => 'ol'));
  2896. $expected = array(
  2897. 'div' => array('class' => 'error-message'),
  2898. 'ol' => array(),
  2899. '<li', 'notEmpty', '/li',
  2900. '<li', 'email', '/li',
  2901. '/ol',
  2902. '/div'
  2903. );
  2904. $this->assertTags($result, $expected);
  2905. $result = $this->Form->error('Contact.field', null, array('listOptions' => array('tag' => 'ol')));
  2906. $expected = array(
  2907. 'div' => array('class' => 'error-message'),
  2908. 'ol' => array(),
  2909. '<li', 'notEmpty', '/li',
  2910. '<li', 'email', '/li',
  2911. '/ol',
  2912. '/div'
  2913. );
  2914. $this->assertTags($result, $expected);
  2915. $result = $this->Form->error('Contact.field', null, array(
  2916. 'listOptions' => array(
  2917. 'class' => 'ul-class',
  2918. 'itemOptions' => array(
  2919. 'class' => 'li-class'
  2920. )
  2921. )
  2922. ));
  2923. $expected = array(
  2924. 'div' => array('class' => 'error-message'),
  2925. 'ul' => array('class' => 'ul-class'),
  2926. array('li' => array('class' => 'li-class')), 'notEmpty', '/li',
  2927. array('li' => array('class' => 'li-class')), 'email', '/li',
  2928. '/ul',
  2929. '/div'
  2930. );
  2931. $this->assertTags($result, $expected);
  2932. }
  2933. /**
  2934. * test error options when using form->input();
  2935. *
  2936. * @return void
  2937. */
  2938. public function testInputErrorEscape() {
  2939. $this->Form->create('ValidateProfile');
  2940. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  2941. $ValidateProfile->validationErrors['city'] = array('required<br>');
  2942. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => true))));
  2943. $this->assertRegExp('/required&lt;br&gt;/', $result);
  2944. $result = $this->Form->input('city',array('error' => array('attributes' => array('escape' => false))));
  2945. $this->assertRegExp('/required<br>/', $result);
  2946. }
  2947. /**
  2948. * testPassword method
  2949. *
  2950. * Test password element generation
  2951. *
  2952. * @return void
  2953. */
  2954. public function testPassword() {
  2955. $Contact = ClassRegistry::getObject('Contact');
  2956. $result = $this->Form->password('Contact.field');
  2957. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][field]', 'id' => 'ContactField')));
  2958. $Contact->validationErrors['passwd'] = 1;
  2959. $this->Form->request->data['Contact']['passwd'] = 'test';
  2960. $result = $this->Form->password('Contact.passwd', array('id' => 'theID'));
  2961. $this->assertTags($result, array('input' => array('type' => 'password', 'name' => 'data[Contact][passwd]', 'value' => 'test', 'id' => 'theID', 'class' => 'form-error')));
  2962. }
  2963. /**
  2964. * testRadio method
  2965. *
  2966. * Test radio element set generation
  2967. *
  2968. * @return void
  2969. */
  2970. public function testRadio() {
  2971. $result = $this->Form->radio('Model.field', array('option A'));
  2972. $expected = array(
  2973. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2974. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2975. 'label' => array('for' => 'ModelField0'),
  2976. 'option A',
  2977. '/label'
  2978. );
  2979. $this->assertTags($result, $expected);
  2980. $result = $this->Form->radio('Model.field', array('1/2' => 'half'));
  2981. $expected = array(
  2982. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2983. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1/2', 'id' => 'ModelField12')),
  2984. 'label' => array('for' => 'ModelField12'),
  2985. 'half',
  2986. '/label'
  2987. );
  2988. $this->assertTags($result, $expected);
  2989. $result = $this->Form->radio('Model.field', array('option A', 'option B'));
  2990. $expected = array(
  2991. 'fieldset' => array(),
  2992. 'legend' => array(),
  2993. 'Field',
  2994. '/legend',
  2995. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  2996. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  2997. array('label' => array('for' => 'ModelField0')),
  2998. 'option A',
  2999. '/label',
  3000. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3001. array('label' => array('for' => 'ModelField1')),
  3002. 'option B',
  3003. '/label',
  3004. '/fieldset'
  3005. );
  3006. $this->assertTags($result, $expected);
  3007. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('separator' => '<br/>'));
  3008. $expected = array(
  3009. 'fieldset' => array(),
  3010. 'legend' => array(),
  3011. 'Field',
  3012. '/legend',
  3013. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3014. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3015. array('label' => array('for' => 'ModelField0')),
  3016. 'option A',
  3017. '/label',
  3018. 'br' => array(),
  3019. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3020. array('label' => array('for' => 'ModelField1')),
  3021. 'option B',
  3022. '/label',
  3023. '/fieldset'
  3024. );
  3025. $this->assertTags($result, $expected);
  3026. $result = $this->Form->radio('Employee.gender', array('male' => 'Male', 'female' => 'Female'));
  3027. $expected = array(
  3028. 'fieldset' => array(),
  3029. 'legend' => array(),
  3030. 'Gender',
  3031. '/legend',
  3032. 'input' => array('type' => 'hidden', 'name' => 'data[Employee][gender]', 'value' => '', 'id' => 'EmployeeGender_'),
  3033. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'male', 'id' => 'EmployeeGenderMale')),
  3034. array('label' => array('for' => 'EmployeeGenderMale')),
  3035. 'Male',
  3036. '/label',
  3037. array('input' => array('type' => 'radio', 'name' => 'data[Employee][gender]', 'value' => 'female', 'id' => 'EmployeeGenderFemale')),
  3038. array('label' => array('for' => 'EmployeeGenderFemale')),
  3039. 'Female',
  3040. '/label',
  3041. '/fieldset',
  3042. );
  3043. $this->assertTags($result, $expected);
  3044. $result = $this->Form->radio('Officer.gender', array('male' => 'Male', 'female' => 'Female'));
  3045. $expected = array(
  3046. 'fieldset' => array(),
  3047. 'legend' => array(),
  3048. 'Gender',
  3049. '/legend',
  3050. 'input' => array('type' => 'hidden', 'name' => 'data[Officer][gender]', 'value' => '', 'id' => 'OfficerGender_'),
  3051. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'male', 'id' => 'OfficerGenderMale')),
  3052. array('label' => array('for' => 'OfficerGenderMale')),
  3053. 'Male',
  3054. '/label',
  3055. array('input' => array('type' => 'radio', 'name' => 'data[Officer][gender]', 'value' => 'female', 'id' => 'OfficerGenderFemale')),
  3056. array('label' => array('for' => 'OfficerGenderFemale')),
  3057. 'Female',
  3058. '/label',
  3059. '/fieldset',
  3060. );
  3061. $this->assertTags($result, $expected);
  3062. $result = $this->Form->radio('Contact.1.imrequired', array('option A'));
  3063. $expected = array(
  3064. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][1][imrequired]', 'value' => '', 'id' => 'Contact1Imrequired_'),
  3065. array('input' => array(
  3066. 'type' => 'radio',
  3067. 'name' => 'data[Contact][1][imrequired]',
  3068. 'value' => '0',
  3069. 'id' => 'Contact1Imrequired0',
  3070. 'required' => 'required'
  3071. )),
  3072. 'label' => array('for' => 'Contact1Imrequired0'),
  3073. 'option A',
  3074. '/label'
  3075. );
  3076. $this->assertTags($result, $expected);
  3077. $result = $this->Form->radio('Model.1.field', array('option A'));
  3078. $expected = array(
  3079. 'input' => array('type' => 'hidden', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field_'),
  3080. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3081. 'label' => array('for' => 'Model1Field0'),
  3082. 'option A',
  3083. '/label'
  3084. );
  3085. $this->assertTags($result, $expected);
  3086. $result = $this->Form->radio('Model.field', array('option A', 'option B'), array('name' => 'data[Model][custom]'));
  3087. $expected = array(
  3088. 'fieldset' => array(),
  3089. 'legend' => array(),
  3090. 'Field',
  3091. '/legend',
  3092. 'input' => array('type' => 'hidden', 'name' => 'data[Model][custom]', 'value' => '', 'id' => 'ModelField_'),
  3093. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '0', 'id' => 'ModelField0')),
  3094. array('label' => array('for' => 'ModelField0')),
  3095. 'option A',
  3096. '/label',
  3097. array('input' => array('type' => 'radio', 'name' => 'data[Model][custom]', 'value' => '1', 'id' => 'ModelField1')),
  3098. array('label' => array('for' => 'ModelField1')),
  3099. 'option B',
  3100. '/label',
  3101. '/fieldset'
  3102. );
  3103. $this->assertTags($result, $expected);
  3104. $result = $this->Form->radio(
  3105. 'Model.field',
  3106. array('option A', 'option B'),
  3107. array('between' => 'I am between')
  3108. );
  3109. $expected = array(
  3110. 'fieldset' => array(),
  3111. 'legend' => array(),
  3112. 'Field',
  3113. '/legend',
  3114. 'I am between',
  3115. 'input' => array(
  3116. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3117. 'value' => '', 'id' => 'ModelField_'
  3118. ),
  3119. array('input' => array(
  3120. 'type' => 'radio', 'name' => 'data[Model][field]',
  3121. 'value' => '0', 'id' => 'ModelField0'
  3122. )),
  3123. array('label' => array('for' => 'ModelField0')),
  3124. 'option A',
  3125. '/label',
  3126. array('input' => array(
  3127. 'type' => 'radio', 'name' => 'data[Model][field]',
  3128. 'value' => '1', 'id' => 'ModelField1'
  3129. )),
  3130. array('label' => array('for' => 'ModelField1')),
  3131. 'option B',
  3132. '/label',
  3133. '/fieldset'
  3134. );
  3135. $this->assertTags($result, $expected);
  3136. $result = $this->Form->radio(
  3137. 'Model.field',
  3138. array('option A', 'option B', 'option C'),
  3139. array('separator' => '--separator--', 'between' => array('between A', 'between B', 'between C'))
  3140. );
  3141. $expected = array(
  3142. 'fieldset' => array(),
  3143. 'legend' => array(),
  3144. 'Field',
  3145. '/legend',
  3146. 'input' => array(
  3147. 'type' => 'hidden', 'name' => 'data[Model][field]',
  3148. 'value' => '', 'id' => 'ModelField_'
  3149. ),
  3150. array('input' => array(
  3151. 'type' => 'radio', 'name' => 'data[Model][field]',
  3152. 'value' => '0', 'id' => 'ModelField0'
  3153. )),
  3154. array('label' => array('for' => 'ModelField0')),
  3155. 'option A',
  3156. '/label',
  3157. 'between A',
  3158. '--separator--',
  3159. array('input' => array(
  3160. 'type' => 'radio', 'name' => 'data[Model][field]',
  3161. 'value' => '1', 'id' => 'ModelField1'
  3162. )),
  3163. array('label' => array('for' => 'ModelField1')),
  3164. 'option B',
  3165. '/label',
  3166. 'between B',
  3167. '--separator--',
  3168. array('input' => array(
  3169. 'type' => 'radio', 'name' => 'data[Model][field]',
  3170. 'value' => '2', 'id' => 'ModelField2'
  3171. )),
  3172. array('label' => array('for' => 'ModelField2')),
  3173. 'option C',
  3174. '/label',
  3175. 'between C',
  3176. '/fieldset'
  3177. );
  3178. $this->assertTags($result, $expected);
  3179. }
  3180. /**
  3181. * Test that radios with a 0 value are selected under the correct conditions.
  3182. * Also ensure that values that are booleanish are handled correctly.
  3183. *
  3184. * @return void
  3185. */
  3186. public function testRadioOptionWithBooleanishValues() {
  3187. $expected = array(
  3188. 'fieldset' => array(),
  3189. 'legend' => array(),
  3190. 'Field',
  3191. '/legend',
  3192. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3193. array('label' => array('for' => 'ModelField1')),
  3194. 'Yes',
  3195. '/label',
  3196. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'checked' => 'checked')),
  3197. array('label' => array('for' => 'ModelField0')),
  3198. 'No',
  3199. '/label',
  3200. '/fieldset'
  3201. );
  3202. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '0'));
  3203. $this->assertTags($result, $expected);
  3204. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 0));
  3205. $this->assertTags($result, $expected);
  3206. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => false));
  3207. $this->assertTags($result, $expected);
  3208. $expected = array(
  3209. 'fieldset' => array(),
  3210. 'legend' => array(),
  3211. 'Field',
  3212. '/legend',
  3213. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '', 'id' => 'ModelField_'),
  3214. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3215. array('label' => array('for' => 'ModelField1')),
  3216. 'Yes',
  3217. '/label',
  3218. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3219. array('label' => array('for' => 'ModelField0')),
  3220. 'No',
  3221. '/label',
  3222. '/fieldset'
  3223. );
  3224. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => null));
  3225. $this->assertTags($result, $expected);
  3226. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => ''));
  3227. $this->assertTags($result, $expected);
  3228. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'));
  3229. $this->assertTags($result, $expected);
  3230. $expected = array(
  3231. 'fieldset' => array(),
  3232. 'legend' => array(),
  3233. 'Field',
  3234. '/legend',
  3235. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelField1')),
  3236. array('label' => array('for' => 'ModelField1')),
  3237. 'Yes',
  3238. '/label',
  3239. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0')),
  3240. array('label' => array('for' => 'ModelField0')),
  3241. 'No',
  3242. '/label',
  3243. '/fieldset'
  3244. );
  3245. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => 1));
  3246. $this->assertTags($result, $expected);
  3247. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => '1'));
  3248. $this->assertTags($result, $expected);
  3249. $result = $this->Form->radio('Model.field', array('1' => 'Yes', '0' => 'No'), array('value' => true));
  3250. $this->assertTags($result, $expected);
  3251. }
  3252. /**
  3253. * test disabled radio options
  3254. *
  3255. * @return void
  3256. */
  3257. public function testRadioDisabled() {
  3258. $result = $this->Form->radio(
  3259. 'Model.field',
  3260. array('option A', 'option B'),
  3261. array('disabled' => array('option A'), 'value' => '0')
  3262. );
  3263. $expected = array(
  3264. 'fieldset' => array(),
  3265. 'legend' => array(),
  3266. 'Field',
  3267. '/legend',
  3268. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3269. array('label' => array('for' => 'ModelField0')),
  3270. 'option A',
  3271. '/label',
  3272. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3273. array('label' => array('for' => 'ModelField1')),
  3274. 'option B',
  3275. '/label',
  3276. '/fieldset'
  3277. );
  3278. $this->assertTags($result, $expected);
  3279. $result = $this->Form->radio(
  3280. 'Model.field',
  3281. array('option A', 'option B'),
  3282. array('disabled' => true, 'value' => '0')
  3283. );
  3284. $expected = array(
  3285. 'fieldset' => array(),
  3286. 'legend' => array(),
  3287. 'Field',
  3288. '/legend',
  3289. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3290. array('label' => array('for' => 'ModelField0')),
  3291. 'option A',
  3292. '/label',
  3293. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3294. array('label' => array('for' => 'ModelField1')),
  3295. 'option B',
  3296. '/label',
  3297. '/fieldset'
  3298. );
  3299. $this->assertTags($result, $expected);
  3300. $result = $this->Form->radio(
  3301. 'Model.field',
  3302. array('option A', 'option B'),
  3303. array('disabled' => 'disabled', 'value' => '0')
  3304. );
  3305. $expected = array(
  3306. 'fieldset' => array(),
  3307. 'legend' => array(),
  3308. 'Field',
  3309. '/legend',
  3310. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField0', 'disabled' => 'disabled', 'checked' => 'checked')),
  3311. array('label' => array('for' => 'ModelField0')),
  3312. 'option A',
  3313. '/label',
  3314. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1', 'disabled' => 'disabled')),
  3315. array('label' => array('for' => 'ModelField1')),
  3316. 'option B',
  3317. '/label',
  3318. '/fieldset'
  3319. );
  3320. $this->assertTags($result, $expected);
  3321. $result = $this->Form->input('Model.field', array(
  3322. 'options' => array('1' => 'first', '2' => 'second'),
  3323. 'type' => 'radio',
  3324. 'before' => '--before--',
  3325. 'after' => '--after--',
  3326. 'separator' => '--separator--',
  3327. 'between' => array('--between first--', '--between second--')
  3328. ));
  3329. $expected = array(
  3330. 'div' => array('class' => 'input radio'),
  3331. '--before--',
  3332. 'fieldset' => array(),
  3333. 'legend' => array(),
  3334. 'Field',
  3335. '/legend',
  3336. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3337. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3338. array('label' => array('for' => 'ModelField1')),
  3339. 'first',
  3340. '/label',
  3341. '--between first--',
  3342. '--separator--',
  3343. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3344. array('label' => array('for' => 'ModelField2')),
  3345. 'second',
  3346. '/label',
  3347. '--between second--',
  3348. '/fieldset',
  3349. '--after--',
  3350. '/div'
  3351. );
  3352. $this->assertTags($result, $expected);
  3353. $result = $this->Form->input('Model.field', array(
  3354. 'options' => array('1' => 'first', '2' => 'second'),
  3355. 'type' => 'radio',
  3356. 'before' => '--before--',
  3357. 'after' => '--after--',
  3358. 'separator' => '--separator--',
  3359. 'between' => array('--between first--')
  3360. ));
  3361. $expected = array(
  3362. 'div' => array('class' => 'input radio'),
  3363. '--before--',
  3364. 'fieldset' => array(),
  3365. 'legend' => array(),
  3366. 'Field',
  3367. '/legend',
  3368. array('input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'id' => 'ModelField_', 'value' => '')),
  3369. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField1')),
  3370. array('label' => array('for' => 'ModelField1')),
  3371. 'first',
  3372. '/label',
  3373. '--between first--',
  3374. '--separator--',
  3375. array('input' => array('type' => 'radio', 'name' => 'data[Model][field]', 'value' => '2', 'id' => 'ModelField2')),
  3376. array('label' => array('for' => 'ModelField2')),
  3377. 'second',
  3378. '/label',
  3379. '/fieldset',
  3380. '--after--',
  3381. '/div'
  3382. );
  3383. }
  3384. /**
  3385. * test disabling the hidden input for radio buttons
  3386. *
  3387. * @return void
  3388. */
  3389. public function testRadioHiddenInputDisabling() {
  3390. $result = $this->Form->input('Model.1.field', array(
  3391. 'type' => 'radio',
  3392. 'options' => array('option A'),
  3393. 'hiddenField' => false
  3394. )
  3395. );
  3396. $expected = array(
  3397. 'div' => array('class' => 'input radio'),
  3398. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3399. 'label' => array('for' => 'Model1Field0'),
  3400. 'option A',
  3401. '/label',
  3402. '/div'
  3403. );
  3404. $this->assertTags($result, $expected);
  3405. $result = $this->Form->radio('Model.1.field', array('option A'), array('hiddenField' => false));
  3406. $expected = array(
  3407. 'input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0'),
  3408. 'label' => array('for' => 'Model1Field0'),
  3409. 'option A',
  3410. '/label'
  3411. );
  3412. $this->assertTags($result, $expected);
  3413. }
  3414. /**
  3415. * test adding an empty option for radio buttons
  3416. *
  3417. * @return void
  3418. */
  3419. public function testRadioAddEmptyOption() {
  3420. $result = $this->Form->input('Model.1.field', array(
  3421. 'type' => 'radio',
  3422. 'options' => array('option A'),
  3423. 'empty' => true,
  3424. 'hiddenField' => false
  3425. ));
  3426. $expected = array(
  3427. 'div' => array('class' => 'input radio'),
  3428. 'fieldset' => array(),
  3429. 'legend' => array(),
  3430. 'Field',
  3431. '/legend',
  3432. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3433. array('label' => array('for' => 'Model1Field')),
  3434. __('empty'),
  3435. '/label',
  3436. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3437. array('label' => array('for' => 'Model1Field0')),
  3438. 'option A',
  3439. '/label',
  3440. '/fieldset',
  3441. '/div'
  3442. );
  3443. $this->assertTags($result, $expected);
  3444. $result = $this->Form->input('Model.1.field', array(
  3445. 'type' => 'radio',
  3446. 'options' => array('option A'),
  3447. 'empty' => 'CustomEmptyLabel',
  3448. 'hiddenField' => false
  3449. ));
  3450. $expected = array(
  3451. 'div' => array('class' => 'input radio'),
  3452. 'fieldset' => array(),
  3453. 'legend' => array(),
  3454. 'Field',
  3455. '/legend',
  3456. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '', 'id' => 'Model1Field')),
  3457. array('label' => array('for' => 'Model1Field')),
  3458. 'CustomEmptyLabel',
  3459. '/label',
  3460. array('input' => array('type' => 'radio', 'name' => 'data[Model][1][field]', 'value' => '0', 'id' => 'Model1Field0')),
  3461. array('label' => array('for' => 'Model1Field0')),
  3462. 'option A',
  3463. '/label',
  3464. '/fieldset',
  3465. '/div'
  3466. );
  3467. $this->assertTags($result, $expected);
  3468. $result = $this->Form->input('Model.1.field', array(
  3469. 'type' => 'radio',
  3470. 'options' => array('option A'),
  3471. 'empty' => false,
  3472. 'hiddenField' => false
  3473. ));
  3474. $this->assertTextNotContains('"Model1Field"', $result);
  3475. }
  3476. /**
  3477. * testSelect method
  3478. *
  3479. * Test select element generation.
  3480. *
  3481. * @return void
  3482. */
  3483. public function testSelect() {
  3484. $result = $this->Form->select('Model.field', array());
  3485. $expected = array(
  3486. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3487. array('option' => array('value' => '')),
  3488. '/option',
  3489. '/select'
  3490. );
  3491. $this->assertTags($result, $expected);
  3492. $this->Form->request->data = array('Model' => array('field' => 'value'));
  3493. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3494. $expected = array(
  3495. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3496. array('option' => array('value' => '')),
  3497. '/option',
  3498. array('option' => array('value' => 'value', 'selected' => 'selected')),
  3499. 'good',
  3500. '/option',
  3501. array('option' => array('value' => 'other')),
  3502. 'bad',
  3503. '/option',
  3504. '/select'
  3505. );
  3506. $this->assertTags($result, $expected);
  3507. $this->Form->request->data = array();
  3508. $result = $this->Form->select('Model.field', array('value' => 'good', 'other' => 'bad'));
  3509. $expected = array(
  3510. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3511. array('option' => array('value' => '')),
  3512. '/option',
  3513. array('option' => array('value' => 'value')),
  3514. 'good',
  3515. '/option',
  3516. array('option' => array('value' => 'other')),
  3517. 'bad',
  3518. '/option',
  3519. '/select'
  3520. );
  3521. $this->assertTags($result, $expected);
  3522. $result = $this->Form->select(
  3523. 'Model.field', array('first' => 'first "html" <chars>', 'second' => 'value'),
  3524. array('empty' => false)
  3525. );
  3526. $expected = array(
  3527. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3528. array('option' => array('value' => 'first')),
  3529. 'first &quot;html&quot; &lt;chars&gt;',
  3530. '/option',
  3531. array('option' => array('value' => 'second')),
  3532. 'value',
  3533. '/option',
  3534. '/select'
  3535. );
  3536. $this->assertTags($result, $expected);
  3537. $result = $this->Form->select(
  3538. 'Model.field',
  3539. array('first' => 'first "html" <chars>', 'second' => 'value'),
  3540. array('escape' => false, 'empty' => false)
  3541. );
  3542. $expected = array(
  3543. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3544. array('option' => array('value' => 'first')),
  3545. 'first "html" <chars>',
  3546. '/option',
  3547. array('option' => array('value' => 'second')),
  3548. 'value',
  3549. '/option',
  3550. '/select'
  3551. );
  3552. $this->assertTags($result, $expected);
  3553. $options = array(
  3554. array('value' => 'first', 'name' => 'First'),
  3555. array('value' => 'first', 'name' => 'Another First'),
  3556. );
  3557. $result = $this->Form->select(
  3558. 'Model.field',
  3559. $options,
  3560. array('escape' => false, 'empty' => false)
  3561. );
  3562. $expected = array(
  3563. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3564. array('option' => array('value' => 'first')),
  3565. 'First',
  3566. '/option',
  3567. array('option' => array('value' => 'first')),
  3568. 'Another First',
  3569. '/option',
  3570. '/select'
  3571. );
  3572. $this->assertTags($result, $expected);
  3573. $this->Form->request->data = array('Model' => array('contact_id' => 228));
  3574. $result = $this->Form->select(
  3575. 'Model.contact_id',
  3576. array('228' => '228 value', '228-1' => '228-1 value', '228-2' => '228-2 value'),
  3577. array('escape' => false, 'empty' => 'pick something')
  3578. );
  3579. $expected = array(
  3580. 'select' => array('name' => 'data[Model][contact_id]', 'id' => 'ModelContactId'),
  3581. array('option' => array('value' => '')), 'pick something', '/option',
  3582. array('option' => array('value' => '228', 'selected' => 'selected')), '228 value', '/option',
  3583. array('option' => array('value' => '228-1')), '228-1 value', '/option',
  3584. array('option' => array('value' => '228-2')), '228-2 value', '/option',
  3585. '/select'
  3586. );
  3587. $this->assertTags($result, $expected);
  3588. $this->Form->request->data['Model']['field'] = 0;
  3589. $result = $this->Form->select('Model.field', array('0' => 'No', '1' => 'Yes'));
  3590. $expected = array(
  3591. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3592. array('option' => array('value' => '')), '/option',
  3593. array('option' => array('value' => '0', 'selected' => 'selected')), 'No', '/option',
  3594. array('option' => array('value' => '1')), 'Yes', '/option',
  3595. '/select'
  3596. );
  3597. $this->assertTags($result, $expected);
  3598. }
  3599. /**
  3600. * test that select() with optiongroups listens to the escape param.
  3601. *
  3602. * @return void
  3603. */
  3604. public function testSelectOptionGroupEscaping() {
  3605. $options = array(
  3606. '>< Key' => array(
  3607. 1 => 'One',
  3608. 2 => 'Two'
  3609. )
  3610. );
  3611. $result = $this->Form->select('Model.field', $options, array('empty' => false));
  3612. $expected = array(
  3613. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3614. 'optgroup' => array('label' => '&gt;&lt; Key'),
  3615. array('option' => array('value' => '1')), 'One', '/option',
  3616. array('option' => array('value' => '2')), 'Two', '/option',
  3617. '/optgroup',
  3618. '/select'
  3619. );
  3620. $this->assertTags($result, $expected);
  3621. $options = array(
  3622. '>< Key' => array(
  3623. 1 => 'One',
  3624. 2 => 'Two'
  3625. )
  3626. );
  3627. $result = $this->Form->select('Model.field', $options, array('empty' => false, 'escape' => false));
  3628. $expected = array(
  3629. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3630. 'optgroup' => array('label' => '>< Key'),
  3631. array('option' => array('value' => '1')), 'One', '/option',
  3632. array('option' => array('value' => '2')), 'Two', '/option',
  3633. '/optgroup',
  3634. '/select'
  3635. );
  3636. $this->assertTags($result, $expected);
  3637. }
  3638. /**
  3639. * Tests that FormHelper::select() allows null to be passed in the $attributes parameter
  3640. *
  3641. * @return void
  3642. */
  3643. public function testSelectWithNullAttributes() {
  3644. $result = $this->Form->select('Model.field', array('first', 'second'), array('empty' => false));
  3645. $expected = array(
  3646. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3647. array('option' => array('value' => '0')),
  3648. 'first',
  3649. '/option',
  3650. array('option' => array('value' => '1')),
  3651. 'second',
  3652. '/option',
  3653. '/select'
  3654. );
  3655. $this->assertTags($result, $expected);
  3656. }
  3657. /**
  3658. * testNestedSelect method
  3659. *
  3660. * test select element generation with optgroups
  3661. *
  3662. * @return void
  3663. */
  3664. public function testNestedSelect() {
  3665. $result = $this->Form->select(
  3666. 'Model.field',
  3667. array(1 => 'One', 2 => 'Two', 'Three' => array(
  3668. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  3669. )), array('empty' => false)
  3670. );
  3671. $expected = array(
  3672. 'select' => array('name' => 'data[Model][field]',
  3673. 'id' => 'ModelField'),
  3674. array('option' => array('value' => 1)),
  3675. 'One',
  3676. '/option',
  3677. array('option' => array('value' => 2)),
  3678. 'Two',
  3679. '/option',
  3680. array('optgroup' => array('label' => 'Three')),
  3681. array('option' => array('value' => 4)),
  3682. 'Four',
  3683. '/option',
  3684. array('option' => array('value' => 5)),
  3685. 'Five',
  3686. '/option',
  3687. '/optgroup',
  3688. '/select'
  3689. );
  3690. $this->assertTags($result, $expected);
  3691. $result = $this->Form->select(
  3692. 'Model.field',
  3693. array(1 => 'One', 2 => 'Two', 'Three' => array(3 => 'Three', 4 => 'Four')),
  3694. array('showParents' => true, 'empty' => false)
  3695. );
  3696. $expected = array(
  3697. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  3698. array('option' => array('value' => 1)),
  3699. 'One',
  3700. '/option',
  3701. array('option' => array('value' => 2)),
  3702. 'Two',
  3703. '/option',
  3704. array('optgroup' => array('label' => 'Three')),
  3705. array('option' => array('value' => 3)),
  3706. 'Three',
  3707. '/option',
  3708. array('option' => array('value' => 4)),
  3709. 'Four',
  3710. '/option',
  3711. '/optgroup',
  3712. '/select'
  3713. );
  3714. $this->assertTags($result, $expected);
  3715. }
  3716. /**
  3717. * testSelectMultiple method
  3718. *
  3719. * test generation of multiple select elements
  3720. *
  3721. * @return void
  3722. */
  3723. public function testSelectMultiple() {
  3724. $options = array('first', 'second', 'third');
  3725. $result = $this->Form->select(
  3726. 'Model.multi_field', $options, array('multiple' => true)
  3727. );
  3728. $expected = array(
  3729. 'input' => array(
  3730. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3731. ),
  3732. 'select' => array(
  3733. 'name' => 'data[Model][multi_field][]',
  3734. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3735. ),
  3736. array('option' => array('value' => '0')),
  3737. 'first',
  3738. '/option',
  3739. array('option' => array('value' => '1')),
  3740. 'second',
  3741. '/option',
  3742. array('option' => array('value' => '2')),
  3743. 'third',
  3744. '/option',
  3745. '/select'
  3746. );
  3747. $this->assertTags($result, $expected);
  3748. $result = $this->Form->select(
  3749. 'Model.multi_field', $options, array('multiple' => 'multiple')
  3750. );
  3751. $expected = array(
  3752. 'input' => array(
  3753. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3754. ),
  3755. 'select' => array(
  3756. 'name' => 'data[Model][multi_field][]',
  3757. 'id' => 'ModelMultiField', 'multiple' => 'multiple'
  3758. ),
  3759. array('option' => array('value' => '0')),
  3760. 'first',
  3761. '/option',
  3762. array('option' => array('value' => '1')),
  3763. 'second',
  3764. '/option',
  3765. array('option' => array('value' => '2')),
  3766. 'third',
  3767. '/option',
  3768. '/select'
  3769. );
  3770. $this->assertTags($result, $expected);
  3771. $result = $this->Form->select(
  3772. 'Model.multi_field', $options, array('multiple' => true, 'value' => array(0, 1))
  3773. );
  3774. $expected = array(
  3775. 'input' => array(
  3776. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField_'
  3777. ),
  3778. 'select' => array(
  3779. 'name' => 'data[Model][multi_field][]', 'id' => 'ModelMultiField',
  3780. 'multiple' => 'multiple'
  3781. ),
  3782. array('option' => array('value' => '0', 'selected' => 'selected')),
  3783. 'first',
  3784. '/option',
  3785. array('option' => array('value' => '1', 'selected' => 'selected')),
  3786. 'second',
  3787. '/option',
  3788. array('option' => array('value' => '2')),
  3789. 'third',
  3790. '/option',
  3791. '/select'
  3792. );
  3793. $this->assertTags($result, $expected);
  3794. $result = $this->Form->select(
  3795. 'Model.multi_field', $options, array('multiple' => false, 'value' => array(0, 1))
  3796. );
  3797. $expected = array(
  3798. 'select' => array(
  3799. 'name' => 'data[Model][multi_field]', 'id' => 'ModelMultiField'
  3800. ),
  3801. array('option' => array('value' => '0', 'selected' => 'selected')),
  3802. 'first',
  3803. '/option',
  3804. array('option' => array('value' => '1', 'selected' => 'selected')),
  3805. 'second',
  3806. '/option',
  3807. array('option' => array('value' => '2')),
  3808. 'third',
  3809. '/option',
  3810. '/select'
  3811. );
  3812. $this->assertTags($result, $expected);
  3813. }
  3814. /**
  3815. * test generation of habtm select boxes.
  3816. *
  3817. * @return void
  3818. */
  3819. public function testHabtmSelectBox() {
  3820. $this->View->viewVars['contactTags'] = array(
  3821. 1 => 'blue',
  3822. 2 => 'red',
  3823. 3 => 'green'
  3824. );
  3825. $this->Form->request->data = array(
  3826. 'Contact' => array(),
  3827. 'ContactTag' => array(
  3828. array(
  3829. 'id' => 1,
  3830. 'name' => 'blue'
  3831. ),
  3832. array(
  3833. 'id' => 3,
  3834. 'name' => 'green'
  3835. )
  3836. )
  3837. );
  3838. $this->Form->create('Contact');
  3839. $result = $this->Form->input('ContactTag', array('div' => false, 'label' => false));
  3840. $expected = array(
  3841. 'input' => array(
  3842. 'type' => 'hidden', 'name' => 'data[ContactTag][ContactTag]', 'value' => '', 'id' => 'ContactTagContactTag_'
  3843. ),
  3844. 'select' => array(
  3845. 'name' => 'data[ContactTag][ContactTag][]', 'id' => 'ContactTagContactTag',
  3846. 'multiple' => 'multiple'
  3847. ),
  3848. array('option' => array('value' => '1', 'selected' => 'selected')),
  3849. 'blue',
  3850. '/option',
  3851. array('option' => array('value' => '2')),
  3852. 'red',
  3853. '/option',
  3854. array('option' => array('value' => '3', 'selected' => 'selected')),
  3855. 'green',
  3856. '/option',
  3857. '/select'
  3858. );
  3859. $this->assertTags($result, $expected);
  3860. }
  3861. /**
  3862. * test generation of multi select elements in checkbox format
  3863. *
  3864. * @return void
  3865. */
  3866. public function testSelectMultipleCheckboxes() {
  3867. $result = $this->Form->select(
  3868. 'Model.multi_field',
  3869. array('first', 'second', 'third'),
  3870. array('multiple' => 'checkbox')
  3871. );
  3872. $expected = array(
  3873. 'input' => array(
  3874. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3875. ),
  3876. array('div' => array('class' => 'checkbox')),
  3877. array('input' => array(
  3878. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3879. 'value' => '0', 'id' => 'ModelMultiField0'
  3880. )),
  3881. array('label' => array('for' => 'ModelMultiField0')),
  3882. 'first',
  3883. '/label',
  3884. '/div',
  3885. array('div' => array('class' => 'checkbox')),
  3886. array('input' => array(
  3887. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3888. 'value' => '1', 'id' => 'ModelMultiField1'
  3889. )),
  3890. array('label' => array('for' => 'ModelMultiField1')),
  3891. 'second',
  3892. '/label',
  3893. '/div',
  3894. array('div' => array('class' => 'checkbox')),
  3895. array('input' => array(
  3896. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3897. 'value' => '2', 'id' => 'ModelMultiField2'
  3898. )),
  3899. array('label' => array('for' => 'ModelMultiField2')),
  3900. 'third',
  3901. '/label',
  3902. '/div'
  3903. );
  3904. $this->assertTags($result, $expected);
  3905. $result = $this->Form->select(
  3906. 'Model.multi_field',
  3907. array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  3908. array('multiple' => 'checkbox')
  3909. );
  3910. $expected = array(
  3911. 'input' => array(
  3912. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3913. ),
  3914. array('div' => array('class' => 'checkbox')),
  3915. array('input' => array(
  3916. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3917. 'value' => 'a', 'id' => 'ModelMultiFieldA'
  3918. )),
  3919. array('label' => array('for' => 'ModelMultiFieldA')),
  3920. 'first',
  3921. '/label',
  3922. '/div',
  3923. array('div' => array('class' => 'checkbox')),
  3924. array('input' => array(
  3925. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3926. 'value' => 'b', 'id' => 'ModelMultiFieldB'
  3927. )),
  3928. array('label' => array('for' => 'ModelMultiFieldB')),
  3929. 'second',
  3930. '/label',
  3931. '/div',
  3932. array('div' => array('class' => 'checkbox')),
  3933. array('input' => array(
  3934. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3935. 'value' => 'c', 'id' => 'ModelMultiFieldC'
  3936. )),
  3937. array('label' => array('for' => 'ModelMultiFieldC')),
  3938. 'third',
  3939. '/label',
  3940. '/div'
  3941. );
  3942. $this->assertTags($result, $expected);
  3943. $result = $this->Form->select(
  3944. 'Model.multi_field', array('1' => 'first'), array('multiple' => 'checkbox')
  3945. );
  3946. $expected = array(
  3947. 'input' => array(
  3948. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'
  3949. ),
  3950. array('div' => array('class' => 'checkbox')),
  3951. array('input' => array(
  3952. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  3953. 'value' => '1', 'id' => 'ModelMultiField1'
  3954. )),
  3955. array('label' => array('for' => 'ModelMultiField1')),
  3956. 'first',
  3957. '/label',
  3958. '/div'
  3959. );
  3960. $this->assertTags($result, $expected);
  3961. $this->Form->request->data = array('Model' => array('tags' => array(1)));
  3962. $result = $this->Form->select(
  3963. 'Model.tags', array('1' => 'first', 'Array' => 'Array'), array('multiple' => 'checkbox')
  3964. );
  3965. $expected = array(
  3966. 'input' => array(
  3967. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  3968. ),
  3969. array('div' => array('class' => 'checkbox')),
  3970. array('input' => array(
  3971. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3972. 'value' => '1', 'id' => 'ModelTags1', 'checked' => 'checked'
  3973. )),
  3974. array('label' => array('for' => 'ModelTags1', 'class' => 'selected')),
  3975. 'first',
  3976. '/label',
  3977. '/div',
  3978. array('div' => array('class' => 'checkbox')),
  3979. array('input' => array(
  3980. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  3981. 'value' => 'Array', 'id' => 'ModelTagsArray'
  3982. )),
  3983. array('label' => array('for' => 'ModelTagsArray')),
  3984. 'Array',
  3985. '/label',
  3986. '/div'
  3987. );
  3988. $this->assertTags($result, $expected);
  3989. }
  3990. /**
  3991. * test multiple checkboxes with div styles.
  3992. *
  3993. * @return void
  3994. */
  3995. public function testSelectMultipleCheckboxDiv() {
  3996. $result = $this->Form->select(
  3997. 'Model.tags',
  3998. array('first', 'second'),
  3999. array('multiple' => 'checkbox', 'class' => 'my-class')
  4000. );
  4001. $expected = array(
  4002. 'input' => array(
  4003. 'type' => 'hidden', 'name' => 'data[Model][tags]', 'value' => '', 'id' => 'ModelTags'
  4004. ),
  4005. array('div' => array('class' => 'my-class')),
  4006. array('input' => array(
  4007. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4008. 'value' => '0', 'id' => 'ModelTags0'
  4009. )),
  4010. array('label' => array('for' => 'ModelTags0')), 'first', '/label',
  4011. '/div',
  4012. array('div' => array('class' => 'my-class')),
  4013. array('input' => array(
  4014. 'type' => 'checkbox', 'name' => 'data[Model][tags][]',
  4015. 'value' => '1', 'id' => 'ModelTags1'
  4016. )),
  4017. array('label' => array('for' => 'ModelTags1')), 'second', '/label',
  4018. '/div'
  4019. );
  4020. $this->assertTags($result, $expected);
  4021. $result = $this->Form->input('Model.tags', array(
  4022. 'options' => array('first', 'second'),
  4023. 'multiple' => 'checkbox',
  4024. 'class' => 'my-class',
  4025. 'div' => false,
  4026. 'label' => false
  4027. ));
  4028. $this->assertTags($result, $expected);
  4029. $Contact = ClassRegistry::getObject('Contact');
  4030. $Contact->validationErrors['tags'] = 'Select atleast one option';
  4031. $result = $this->Form->input('Contact.tags', array(
  4032. 'options' => array('one'),
  4033. 'multiple' => 'checkbox',
  4034. 'label' => false,
  4035. 'div' => false
  4036. ));
  4037. $expected = array(
  4038. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4039. array('div' => array('class' => 'checkbox form-error')),
  4040. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4041. array('label' => array('for' => 'ContactTags0')),
  4042. 'one',
  4043. '/label',
  4044. '/div'
  4045. );
  4046. $this->assertTags($result, $expected);
  4047. $result = $this->Form->input('Contact.tags', array(
  4048. 'options' => array('one'),
  4049. 'multiple' => 'checkbox',
  4050. 'class' => 'mycheckbox',
  4051. 'label' => false,
  4052. 'div' => false
  4053. ));
  4054. $expected = array(
  4055. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][tags]', 'value' => '', 'id' => 'ContactTags'),
  4056. array('div' => array('class' => 'mycheckbox form-error')),
  4057. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][tags][]', 'value' => '0', 'id' => 'ContactTags0')),
  4058. array('label' => array('for' => 'ContactTags0')),
  4059. 'one',
  4060. '/label',
  4061. '/div'
  4062. );
  4063. $this->assertTags($result, $expected);
  4064. }
  4065. /**
  4066. * Checks the security hash array generated for multiple-input checkbox elements
  4067. *
  4068. * @return void
  4069. */
  4070. public function testSelectMultipleCheckboxSecurity() {
  4071. $this->Form->request['_Token'] = array('key' => 'testKey');
  4072. $this->assertEquals(array(), $this->Form->fields);
  4073. $result = $this->Form->select(
  4074. 'Model.multi_field', array('1' => 'first', '2' => 'second', '3' => 'third'),
  4075. array('multiple' => 'checkbox')
  4076. );
  4077. $this->assertEquals(array('Model.multi_field'), $this->Form->fields);
  4078. $result = $this->Form->secure($this->Form->fields);
  4079. $key = 'f7d573650a295b94e0938d32b323fde775e5f32b%3A';
  4080. $this->assertRegExp('/"' . $key . '"/', $result);
  4081. }
  4082. /**
  4083. * Multiple select elements should always be secured as they always participate
  4084. * in the POST data.
  4085. *
  4086. * @return void
  4087. */
  4088. public function testSelectMultipleSecureWithNoOptions() {
  4089. $this->Form->request['_Token'] = array('key' => 'testkey');
  4090. $this->assertEquals(array(), $this->Form->fields);
  4091. $this->Form->select(
  4092. 'Model.select',
  4093. array(),
  4094. array('multiple' => true)
  4095. );
  4096. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4097. }
  4098. /**
  4099. * When a select box has no options it should not be added to the fields list
  4100. * as it always fail post validation.
  4101. *
  4102. * @return void
  4103. */
  4104. public function testSelectNoSecureWithNoOptions() {
  4105. $this->Form->request['_Token'] = array('key' => 'testkey');
  4106. $this->assertEquals(array(), $this->Form->fields);
  4107. $this->Form->select(
  4108. 'Model.select',
  4109. array()
  4110. );
  4111. $this->assertEquals(array(), $this->Form->fields);
  4112. $this->Form->select(
  4113. 'Model.select',
  4114. array(),
  4115. array('empty' => true)
  4116. );
  4117. $this->assertEquals(array('Model.select'), $this->Form->fields);
  4118. }
  4119. /**
  4120. * testInputMultipleCheckboxes method
  4121. *
  4122. * test input() resulting in multi select elements being generated.
  4123. *
  4124. * @return void
  4125. */
  4126. public function testInputMultipleCheckboxes() {
  4127. $result = $this->Form->input('Model.multi_field', array(
  4128. 'options' => array('first', 'second', 'third'),
  4129. 'multiple' => 'checkbox'
  4130. ));
  4131. $expected = array(
  4132. array('div' => array('class' => 'input select')),
  4133. array('label' => array('for' => 'ModelMultiField')),
  4134. 'Multi Field',
  4135. '/label',
  4136. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4137. array('div' => array('class' => 'checkbox')),
  4138. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4139. array('label' => array('for' => 'ModelMultiField0')),
  4140. 'first',
  4141. '/label',
  4142. '/div',
  4143. array('div' => array('class' => 'checkbox')),
  4144. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4145. array('label' => array('for' => 'ModelMultiField1')),
  4146. 'second',
  4147. '/label',
  4148. '/div',
  4149. array('div' => array('class' => 'checkbox')),
  4150. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4151. array('label' => array('for' => 'ModelMultiField2')),
  4152. 'third',
  4153. '/label',
  4154. '/div',
  4155. '/div'
  4156. );
  4157. $this->assertTags($result, $expected);
  4158. $result = $this->Form->input('Model.multi_field', array(
  4159. 'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
  4160. 'multiple' => 'checkbox'
  4161. ));
  4162. $expected = array(
  4163. array('div' => array('class' => 'input select')),
  4164. array('label' => array('for' => 'ModelMultiField')),
  4165. 'Multi Field',
  4166. '/label',
  4167. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4168. array('div' => array('class' => 'checkbox')),
  4169. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'a', 'id' => 'ModelMultiFieldA')),
  4170. array('label' => array('for' => 'ModelMultiFieldA')),
  4171. 'first',
  4172. '/label',
  4173. '/div',
  4174. array('div' => array('class' => 'checkbox')),
  4175. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'b', 'id' => 'ModelMultiFieldB')),
  4176. array('label' => array('for' => 'ModelMultiFieldB')),
  4177. 'second',
  4178. '/label',
  4179. '/div',
  4180. array('div' => array('class' => 'checkbox')),
  4181. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => 'c', 'id' => 'ModelMultiFieldC')),
  4182. array('label' => array('for' => 'ModelMultiFieldC')),
  4183. 'third',
  4184. '/label',
  4185. '/div',
  4186. '/div'
  4187. );
  4188. $this->assertTags($result, $expected);
  4189. $result = $this->Form->input('Model.multi_field', array(
  4190. 'options' => array('1' => 'first'),
  4191. 'multiple' => 'checkbox',
  4192. 'label' => false,
  4193. 'div' => false
  4194. ));
  4195. $expected = array(
  4196. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4197. array('div' => array('class' => 'checkbox')),
  4198. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4199. array('label' => array('for' => 'ModelMultiField1')),
  4200. 'first',
  4201. '/label',
  4202. '/div'
  4203. );
  4204. $this->assertTags($result, $expected);
  4205. $result = $this->Form->input('Model.multi_field', array(
  4206. 'options' => array('2' => 'second'),
  4207. 'multiple' => 'checkbox',
  4208. 'label' => false,
  4209. 'div' => false
  4210. ));
  4211. $expected = array(
  4212. 'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
  4213. array('div' => array('class' => 'checkbox')),
  4214. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
  4215. array('label' => array('for' => 'ModelMultiField2')),
  4216. 'second',
  4217. '/label',
  4218. '/div'
  4219. );
  4220. $this->assertTags($result, $expected);
  4221. }
  4222. /**
  4223. * testSelectHiddenFieldOmission method
  4224. *
  4225. * test that select() with 'hiddenField' => false omits the hidden field
  4226. *
  4227. * @return void
  4228. */
  4229. public function testSelectHiddenFieldOmission() {
  4230. $result = $this->Form->select('Model.multi_field',
  4231. array('first', 'second'),
  4232. array('multiple' => 'checkbox', 'hiddenField' => false, 'value' => null)
  4233. );
  4234. $expected = array(
  4235. array('div' => array('class' => 'checkbox')),
  4236. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4237. array('label' => array('for' => 'ModelMultiField0')),
  4238. 'first',
  4239. '/label',
  4240. '/div',
  4241. array('div' => array('class' => 'checkbox')),
  4242. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4243. array('label' => array('for' => 'ModelMultiField1')),
  4244. 'second',
  4245. '/label',
  4246. '/div'
  4247. );
  4248. $this->assertTags($result, $expected);
  4249. $result = $this->Form->input('Model.multi_field', array(
  4250. 'options' => array('first', 'second'),
  4251. 'multiple' => 'checkbox',
  4252. 'hiddenField' => false
  4253. ));
  4254. $expected = array(
  4255. array('div' => array('class' => 'input select')),
  4256. array('label' => array('for' => 'ModelMultiField')),
  4257. 'Multi Field',
  4258. '/label',
  4259. array('div' => array('class' => 'checkbox')),
  4260. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
  4261. array('label' => array('for' => 'ModelMultiField0')),
  4262. 'first',
  4263. '/label',
  4264. '/div',
  4265. array('div' => array('class' => 'checkbox')),
  4266. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
  4267. array('label' => array('for' => 'ModelMultiField1')),
  4268. 'second',
  4269. '/label',
  4270. '/div',
  4271. '/div'
  4272. );
  4273. $this->assertTags($result, $expected);
  4274. }
  4275. /**
  4276. * test that select() with multiple = checkbox works with overriding name attribute.
  4277. *
  4278. * @return void
  4279. */
  4280. public function testSelectCheckboxMultipleOverrideName() {
  4281. $result = $this->Form->input('category', array(
  4282. 'type' => 'select',
  4283. 'multiple' => 'checkbox',
  4284. 'name' => 'data[fish]',
  4285. 'options' => array('1', '2'),
  4286. 'div' => false,
  4287. 'label' => false,
  4288. ));
  4289. $expected = array(
  4290. 'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
  4291. array('div' => array('class' => 'checkbox')),
  4292. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
  4293. array('label' => array('for' => 'Category0')), '1', '/label',
  4294. '/div',
  4295. array('div' => array('class' => 'checkbox')),
  4296. array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
  4297. array('label' => array('for' => 'Category1')), '2', '/label',
  4298. '/div'
  4299. );
  4300. $this->assertTags($result, $expected);
  4301. }
  4302. /**
  4303. * Test that 'id' overrides all the checkbox id's as well.
  4304. *
  4305. * @return void
  4306. */
  4307. public function testSelectCheckboxMultipleId() {
  4308. $result = $this->Form->select(
  4309. 'Model.multi_field',
  4310. array('first', 'second', 'third'),
  4311. array('multiple' => 'checkbox', 'id' => 'CustomId')
  4312. );
  4313. $expected = array(
  4314. 'input' => array(
  4315. 'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
  4316. ),
  4317. array('div' => array('class' => 'checkbox')),
  4318. array('input' => array(
  4319. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4320. 'value' => '0', 'id' => 'CustomId0'
  4321. )),
  4322. array('label' => array('for' => 'CustomId0')),
  4323. 'first',
  4324. '/label',
  4325. '/div',
  4326. array('div' => array('class' => 'checkbox')),
  4327. array('input' => array(
  4328. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4329. 'value' => '1', 'id' => 'CustomId1'
  4330. )),
  4331. array('label' => array('for' => 'CustomId1')),
  4332. 'second',
  4333. '/label',
  4334. '/div',
  4335. array('div' => array('class' => 'checkbox')),
  4336. array('input' => array(
  4337. 'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
  4338. 'value' => '2', 'id' => 'CustomId2'
  4339. )),
  4340. array('label' => array('for' => 'CustomId2')),
  4341. 'third',
  4342. '/label',
  4343. '/div'
  4344. );
  4345. $this->assertTags($result, $expected);
  4346. }
  4347. /**
  4348. * testCheckbox method
  4349. *
  4350. * Test generation of checkboxes
  4351. *
  4352. * @return void
  4353. */
  4354. public function testCheckbox() {
  4355. $result = $this->Form->checkbox('Model.field');
  4356. $expected = array(
  4357. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4358. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4359. );
  4360. $this->assertTags($result, $expected);
  4361. $result = $this->Form->checkbox('Model.field', array('id' => 'theID', 'value' => 'myvalue'));
  4362. $expected = array(
  4363. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'theID_'),
  4364. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => 'myvalue', 'id' => 'theID'))
  4365. );
  4366. $this->assertTags($result, $expected);
  4367. $Contact = ClassRegistry::getObject('Contact');
  4368. $Contact->validationErrors['field'] = 1;
  4369. $this->Form->request->data['Contact']['field'] = 'myvalue';
  4370. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID', 'value' => 'myvalue'));
  4371. $expected = array(
  4372. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4373. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'theID', 'checked' => 'checked', 'class' => 'form-error'))
  4374. );
  4375. $this->assertTags($result, $expected);
  4376. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4377. $expected = array(
  4378. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4379. array('input' => array('preg:/[^<]+/', 'value' => 'myvalue', 'id' => 'ContactField', 'checked' => 'checked', 'class' => 'form-error'))
  4380. );
  4381. $this->assertTags($result, $expected);
  4382. $this->Form->request->data['Contact']['field'] = '';
  4383. $result = $this->Form->checkbox('Contact.field', array('id' => 'theID'));
  4384. $expected = array(
  4385. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'theID_'),
  4386. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => '1', 'id' => 'theID', 'class' => 'form-error'))
  4387. );
  4388. $this->assertTags($result, $expected);
  4389. $Contact->validationErrors = array();
  4390. $result = $this->Form->checkbox('Contact.field', array('value' => 'myvalue'));
  4391. $expected = array(
  4392. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][field]', 'value' => '0', 'id' => 'ContactField_'),
  4393. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][field]', 'value' => 'myvalue', 'id' => 'ContactField'))
  4394. );
  4395. $this->assertTags($result, $expected);
  4396. $this->Form->request->data['Contact']['published'] = 1;
  4397. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4398. $expected = array(
  4399. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4400. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID', 'checked' => 'checked'))
  4401. );
  4402. $this->assertTags($result, $expected);
  4403. $this->Form->request->data['Contact']['published'] = 0;
  4404. $result = $this->Form->checkbox('Contact.published', array('id' => 'theID'));
  4405. $expected = array(
  4406. 'input' => array('type' => 'hidden', 'name' => 'data[Contact][published]', 'value' => '0', 'id' => 'theID_'),
  4407. array('input' => array('type' => 'checkbox', 'name' => 'data[Contact][published]', 'value' => '1', 'id' => 'theID'))
  4408. );
  4409. $this->assertTags($result, $expected);
  4410. $result = $this->Form->checkbox('Model.CustomField.1.value');
  4411. $expected = array(
  4412. 'input' => array('type' => 'hidden', 'name' => 'data[Model][CustomField][1][value]', 'value' => '0', 'id' => 'ModelCustomField1Value_'),
  4413. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][CustomField][1][value]', 'value' => '1', 'id' => 'ModelCustomField1Value'))
  4414. );
  4415. $this->assertTags($result, $expected);
  4416. $result = $this->Form->checkbox('CustomField.1.value');
  4417. $expected = array(
  4418. 'input' => array('type' => 'hidden', 'name' => 'data[CustomField][1][value]', 'value' => '0', 'id' => 'CustomField1Value_'),
  4419. array('input' => array('type' => 'checkbox', 'name' => 'data[CustomField][1][value]', 'value' => '1', 'id' => 'CustomField1Value'))
  4420. );
  4421. $this->assertTags($result, $expected);
  4422. }
  4423. /**
  4424. * test checkbox() with a custom name attribute
  4425. *
  4426. * @return void
  4427. */
  4428. public function testCheckboxCustomNameAttribute() {
  4429. $result = $this->Form->checkbox('Test.test', array('name' => 'myField'));
  4430. $expected = array(
  4431. 'input' => array('type' => 'hidden', 'name' => 'myField', 'value' => '0', 'id' => 'TestTest_'),
  4432. array('input' => array('type' => 'checkbox', 'name' => 'myField', 'value' => '1', 'id' => 'TestTest'))
  4433. );
  4434. $this->assertTags($result, $expected);
  4435. }
  4436. /**
  4437. * test the checked option for checkboxes.
  4438. *
  4439. * @return void
  4440. */
  4441. public function testCheckboxCheckedOption() {
  4442. $result = $this->Form->checkbox('Model.field', array('checked' => 'checked'));
  4443. $expected = array(
  4444. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4445. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4446. );
  4447. $this->assertTags($result, $expected);
  4448. $result = $this->Form->checkbox('Model.field', array('checked' => 1));
  4449. $expected = array(
  4450. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4451. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4452. );
  4453. $this->assertTags($result, $expected);
  4454. $result = $this->Form->checkbox('Model.field', array('checked' => true));
  4455. $expected = array(
  4456. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4457. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField', 'checked' => 'checked'))
  4458. );
  4459. $this->assertTags($result, $expected);
  4460. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4461. $expected = array(
  4462. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4463. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4464. );
  4465. $this->assertTags($result, $expected);
  4466. $this->Form->request->data['Model']['field'] = 1;
  4467. $result = $this->Form->checkbox('Model.field', array('checked' => false));
  4468. $expected = array(
  4469. 'input' => array('type' => 'hidden', 'name' => 'data[Model][field]', 'value' => '0', 'id' => 'ModelField_'),
  4470. array('input' => array('type' => 'checkbox', 'name' => 'data[Model][field]', 'value' => '1', 'id' => 'ModelField'))
  4471. );
  4472. $this->assertTags($result, $expected);
  4473. }
  4474. /**
  4475. * Test that disabled attribute works on both the checkbox and hidden input.
  4476. *
  4477. * @return void
  4478. */
  4479. public function testCheckboxDisabling() {
  4480. $result = $this->Form->checkbox('Account.show_name', array('disabled' => 'disabled'));
  4481. $expected = array(
  4482. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_', 'disabled' => 'disabled')),
  4483. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName', 'disabled' => 'disabled'))
  4484. );
  4485. $this->assertTags($result, $expected);
  4486. $result = $this->Form->checkbox('Account.show_name', array('disabled' => false));
  4487. $expected = array(
  4488. array('input' => array('type' => 'hidden', 'name' => 'data[Account][show_name]', 'value' => '0', 'id' => 'AccountShowName_')),
  4489. array('input' => array('type' => 'checkbox', 'name' => 'data[Account][show_name]', 'value' => '1', 'id' => 'AccountShowName'))
  4490. );
  4491. $this->assertTags($result, $expected);
  4492. }
  4493. /**
  4494. * Test that the hidden input for checkboxes can be omitted or set to a
  4495. * specific value.
  4496. *
  4497. * @return void
  4498. */
  4499. public function testCheckboxHiddenField() {
  4500. $result = $this->Form->input('UserForm.something', array(
  4501. 'type' => 'checkbox',
  4502. 'hiddenField' => false
  4503. ));
  4504. $expected = array(
  4505. 'div' => array('class' => 'input checkbox'),
  4506. array('input' => array(
  4507. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4508. 'value' => '1', 'id' => 'UserFormSomething'
  4509. )),
  4510. 'label' => array('for' => 'UserFormSomething'),
  4511. 'Something',
  4512. '/label',
  4513. '/div'
  4514. );
  4515. $this->assertTags($result, $expected);
  4516. $result = $this->Form->input('UserForm.something', array(
  4517. 'type' => 'checkbox',
  4518. 'value' => 'Y',
  4519. 'hiddenField' => 'N',
  4520. ));
  4521. $expected = array(
  4522. 'div' => array('class' => 'input checkbox'),
  4523. array('input' => array(
  4524. 'type' => 'hidden', 'name' => 'data[UserForm][something]',
  4525. 'value' => 'N', 'id' => 'UserFormSomething_'
  4526. )),
  4527. array('input' => array(
  4528. 'type' => 'checkbox', 'name' => 'data[UserForm][something]',
  4529. 'value' => 'Y', 'id' => 'UserFormSomething'
  4530. )),
  4531. 'label' => array('for' => 'UserFormSomething'),
  4532. 'Something',
  4533. '/label',
  4534. '/div'
  4535. );
  4536. $this->assertTags($result, $expected);
  4537. }
  4538. /**
  4539. * testDateTime method
  4540. *
  4541. * Test generation of date/time select elements
  4542. *
  4543. * @return void
  4544. */
  4545. public function testDateTime() {
  4546. extract($this->dateRegex);
  4547. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4548. $now = strtotime('now');
  4549. $expected = array(
  4550. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4551. $daysRegex,
  4552. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4553. date('j', $now),
  4554. '/option',
  4555. '*/select',
  4556. '-',
  4557. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4558. $monthsRegex,
  4559. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4560. date('F', $now),
  4561. '/option',
  4562. '*/select',
  4563. '-',
  4564. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4565. $yearsRegex,
  4566. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4567. date('Y', $now),
  4568. '/option',
  4569. '*/select',
  4570. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4571. $hoursRegex,
  4572. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4573. date('g', $now),
  4574. '/option',
  4575. '*/select',
  4576. ':',
  4577. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4578. $minutesRegex,
  4579. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4580. date('i', $now),
  4581. '/option',
  4582. '*/select',
  4583. ' ',
  4584. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4585. $meridianRegex,
  4586. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4587. date('a', $now),
  4588. '/option',
  4589. '*/select'
  4590. );
  4591. $this->assertTags($result, $expected);
  4592. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4593. $expected = array(
  4594. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4595. $daysRegex,
  4596. array('option' => array('value' => '')),
  4597. '/option',
  4598. '*/select',
  4599. '-',
  4600. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4601. $monthsRegex,
  4602. array('option' => array('value' => '')),
  4603. '/option',
  4604. '*/select',
  4605. '-',
  4606. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4607. $yearsRegex,
  4608. array('option' => array('value' => '')),
  4609. '/option',
  4610. '*/select',
  4611. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4612. $hoursRegex,
  4613. array('option' => array('value' => '')),
  4614. '/option',
  4615. '*/select',
  4616. ':',
  4617. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4618. $minutesRegex,
  4619. array('option' => array('value' => '')),
  4620. '/option',
  4621. '*/select',
  4622. ' ',
  4623. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4624. $meridianRegex,
  4625. array('option' => array('value' => '')),
  4626. '/option',
  4627. '*/select'
  4628. );
  4629. $this->assertTags($result, $expected);
  4630. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4631. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => false));
  4632. $this->assertTags($result, $expected);
  4633. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4634. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('value' => ''));
  4635. $this->assertTags($result, $expected);
  4636. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4637. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('interval' => 5, 'value' => ''));
  4638. $expected = array(
  4639. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4640. $daysRegex,
  4641. array('option' => array('value' => '')),
  4642. '/option',
  4643. '*/select',
  4644. '-',
  4645. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4646. $monthsRegex,
  4647. array('option' => array('value' => '')),
  4648. '/option',
  4649. '*/select',
  4650. '-',
  4651. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4652. $yearsRegex,
  4653. array('option' => array('value' => '')),
  4654. '/option',
  4655. '*/select',
  4656. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4657. $hoursRegex,
  4658. array('option' => array('value' => '')),
  4659. '/option',
  4660. '*/select',
  4661. ':',
  4662. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4663. $minutesRegex,
  4664. array('option' => array('value' => '')),
  4665. '/option',
  4666. array('option' => array('value' => '00')),
  4667. '00',
  4668. '/option',
  4669. array('option' => array('value' => '05')),
  4670. '05',
  4671. '/option',
  4672. array('option' => array('value' => '10')),
  4673. '10',
  4674. '/option',
  4675. '*/select',
  4676. ' ',
  4677. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4678. $meridianRegex,
  4679. array('option' => array('value' => '')),
  4680. '/option',
  4681. '*/select'
  4682. );
  4683. $this->assertTags($result, $expected);
  4684. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4685. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4686. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('minuteInterval' => 5, 'value' => ''));
  4687. $this->Form->request->data['Contact']['data'] = null;
  4688. $result = $this->Form->dateTime('Contact.date', 'DMY', '12');
  4689. $expected = array(
  4690. array('select' => array('name' => 'data[Contact][date][day]', 'id' => 'ContactDateDay')),
  4691. $daysRegex,
  4692. array('option' => array('value' => '')),
  4693. '/option',
  4694. '*/select',
  4695. '-',
  4696. array('select' => array('name' => 'data[Contact][date][month]', 'id' => 'ContactDateMonth')),
  4697. $monthsRegex,
  4698. array('option' => array('value' => '')),
  4699. '/option',
  4700. '*/select',
  4701. '-',
  4702. array('select' => array('name' => 'data[Contact][date][year]', 'id' => 'ContactDateYear')),
  4703. $yearsRegex,
  4704. array('option' => array('value' => '')),
  4705. '/option',
  4706. '*/select',
  4707. array('select' => array('name' => 'data[Contact][date][hour]', 'id' => 'ContactDateHour')),
  4708. $hoursRegex,
  4709. array('option' => array('value' => '')),
  4710. '/option',
  4711. '*/select',
  4712. ':',
  4713. array('select' => array('name' => 'data[Contact][date][min]', 'id' => 'ContactDateMin')),
  4714. $minutesRegex,
  4715. array('option' => array('value' => '')),
  4716. '/option',
  4717. '*/select',
  4718. ' ',
  4719. array('select' => array('name' => 'data[Contact][date][meridian]', 'id' => 'ContactDateMeridian')),
  4720. $meridianRegex,
  4721. array('option' => array('value' => '')),
  4722. '/option',
  4723. '*/select'
  4724. );
  4725. $this->assertTags($result, $expected);
  4726. $this->assertNotRegExp('/<option[^<>]+value=""[^<>]+selected="selected"[^>]*>/', $result);
  4727. $this->Form->request->data['Model']['field'] = date('Y') . '-01-01 00:00:00';
  4728. $now = strtotime($this->Form->data['Model']['field']);
  4729. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('empty' => false));
  4730. $expected = array(
  4731. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  4732. $daysRegex,
  4733. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4734. date('j', $now),
  4735. '/option',
  4736. '*/select',
  4737. '-',
  4738. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  4739. $monthsRegex,
  4740. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4741. date('F', $now),
  4742. '/option',
  4743. '*/select',
  4744. '-',
  4745. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  4746. $yearsRegex,
  4747. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4748. date('Y', $now),
  4749. '/option',
  4750. '*/select',
  4751. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  4752. $hoursRegex,
  4753. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4754. date('g', $now),
  4755. '/option',
  4756. '*/select',
  4757. ':',
  4758. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  4759. $minutesRegex,
  4760. array('option' => array('value' => date('i', $now), 'selected' => 'selected')),
  4761. date('i', $now),
  4762. '/option',
  4763. '*/select',
  4764. ' ',
  4765. array('select' => array('name' => 'data[Model][field][meridian]', 'id' => 'ModelFieldMeridian')),
  4766. $meridianRegex,
  4767. array('option' => array('value' => date('a', $now), 'selected' => 'selected')),
  4768. date('a', $now),
  4769. '/option',
  4770. '*/select'
  4771. );
  4772. $this->assertTags($result, $expected);
  4773. $selected = strtotime('2008-10-26 12:33:00');
  4774. $result = $this->Form->dateTime('Model.field', 'DMY', '12', array('value' => $selected));
  4775. $this->assertRegExp('/<option[^<>]+value="2008"[^<>]+selected="selected"[^>]*>2008<\/option>/', $result);
  4776. $this->assertRegExp('/<option[^<>]+value="10"[^<>]+selected="selected"[^>]*>October<\/option>/', $result);
  4777. $this->assertRegExp('/<option[^<>]+value="26"[^<>]+selected="selected"[^>]*>26<\/option>/', $result);
  4778. $this->assertRegExp('/<option[^<>]+value="12"[^<>]+selected="selected"[^>]*>12<\/option>/', $result);
  4779. $this->assertRegExp('/<option[^<>]+value="33"[^<>]+selected="selected"[^>]*>33<\/option>/', $result);
  4780. $this->assertRegExp('/<option[^<>]+value="pm"[^<>]+selected="selected"[^>]*>pm<\/option>/', $result);
  4781. $this->Form->create('Contact');
  4782. $result = $this->Form->input('published');
  4783. $now = strtotime('now');
  4784. $expected = array(
  4785. 'div' => array('class' => 'input date'),
  4786. 'label' => array('for' => 'ContactPublishedMonth'),
  4787. 'Published',
  4788. '/label',
  4789. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4790. $monthsRegex,
  4791. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4792. date('F', $now),
  4793. '/option',
  4794. '*/select',
  4795. '-',
  4796. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4797. $daysRegex,
  4798. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4799. date('j', $now),
  4800. '/option',
  4801. '*/select',
  4802. '-',
  4803. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4804. $yearsRegex,
  4805. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4806. date('Y', $now),
  4807. '/option',
  4808. '*/select',
  4809. '/div'
  4810. );
  4811. $this->assertTags($result, $expected);
  4812. $result = $this->Form->input('published2', array('type' => 'date'));
  4813. $now = strtotime('now');
  4814. $expected = array(
  4815. 'div' => array('class' => 'input date'),
  4816. 'label' => array('for' => 'ContactPublished2Month'),
  4817. 'Published2',
  4818. '/label',
  4819. array('select' => array('name' => 'data[Contact][published2][month]', 'id' => 'ContactPublished2Month')),
  4820. $monthsRegex,
  4821. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4822. date('F', $now),
  4823. '/option',
  4824. '*/select',
  4825. '-',
  4826. array('select' => array('name' => 'data[Contact][published2][day]', 'id' => 'ContactPublished2Day')),
  4827. $daysRegex,
  4828. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4829. date('j', $now),
  4830. '/option',
  4831. '*/select',
  4832. '-',
  4833. array('select' => array('name' => 'data[Contact][published2][year]', 'id' => 'ContactPublished2Year')),
  4834. $yearsRegex,
  4835. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4836. date('Y', $now),
  4837. '/option',
  4838. '*/select',
  4839. '/div'
  4840. );
  4841. $this->assertTags($result, $expected);
  4842. $this->Form->create('Contact');
  4843. $result = $this->Form->input('published', array('monthNames' => false));
  4844. $now = strtotime('now');
  4845. $expected = array(
  4846. 'div' => array('class' => 'input date'),
  4847. 'label' => array('for' => 'ContactPublishedMonth'),
  4848. 'Published',
  4849. '/label',
  4850. array('select' => array('name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth')),
  4851. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4852. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  4853. date('m', $now),
  4854. '/option',
  4855. '*/select',
  4856. '-',
  4857. array('select' => array('name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay')),
  4858. $daysRegex,
  4859. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  4860. date('j', $now),
  4861. '/option',
  4862. '*/select',
  4863. '-',
  4864. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  4865. $yearsRegex,
  4866. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  4867. date('Y', $now),
  4868. '/option',
  4869. '*/select',
  4870. '/div'
  4871. );
  4872. $this->assertTags($result, $expected);
  4873. $result = $this->Form->input('published', array('type' => 'time'));
  4874. $now = strtotime('now');
  4875. $expected = array(
  4876. 'div' => array('class' => 'input time'),
  4877. 'label' => array('for' => 'ContactPublishedHour'),
  4878. 'Published',
  4879. '/label',
  4880. array('select' => array('name' => 'data[Contact][published][hour]', 'id' => 'ContactPublishedHour')),
  4881. 'preg:/(?:<option value="([\d])+">[\d]+<\/option>[\r\n]*)*/',
  4882. array('option' => array('value' => date('h', $now), 'selected' => 'selected')),
  4883. date('g', $now),
  4884. '/option',
  4885. '*/select',
  4886. ':',
  4887. );
  4888. $this->assertTags($result, $expected);
  4889. $result = $this->Form->input('published', array(
  4890. 'timeFormat' => 24,
  4891. 'interval' => 5,
  4892. 'selected' => strtotime('2009-09-03 13:37:00'),
  4893. 'type' => 'datetime'
  4894. ));
  4895. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4896. $this->assertRegExp('/<option[^<>]+value="09"[^<>]+selected="selected"[^>]*>September<\/option>/', $result);
  4897. $this->assertRegExp('/<option[^<>]+value="03"[^<>]+selected="selected"[^>]*>3<\/option>/', $result);
  4898. $this->assertRegExp('/<option[^<>]+value="13"[^<>]+selected="selected"[^>]*>13<\/option>/', $result);
  4899. $this->assertRegExp('/<option[^<>]+value="35"[^<>]+selected="selected"[^>]*>35<\/option>/', $result);
  4900. $this->assertNoErrors();
  4901. $this->Form->request->data['Contact'] = array(
  4902. 'date' => array(
  4903. 'day' => '',
  4904. 'month' => '',
  4905. 'year' => '',
  4906. 'hour' => '',
  4907. 'min' => '',
  4908. 'meridian' => ''
  4909. )
  4910. );
  4911. $result = $this->Form->dateTime('Contact.date', 'DMY', '12', array('empty' => false));
  4912. }
  4913. /**
  4914. * test that datetime() and default values work.
  4915. *
  4916. * @return void
  4917. */
  4918. public function testDatetimeWithDefault() {
  4919. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => '2009-06-01 11:15:30'));
  4920. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4921. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4922. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4923. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array(
  4924. 'default' => '2009-06-01 11:15:30'
  4925. ));
  4926. $this->assertRegExp('/<option[^<>]+value="2009"[^<>]+selected="selected"[^>]*>2009<\/option>/', $result);
  4927. $this->assertRegExp('/<option[^<>]+value="01"[^<>]+selected="selected"[^>]*>1<\/option>/', $result);
  4928. $this->assertRegExp('/<option[^<>]+value="06"[^<>]+selected="selected"[^>]*>June<\/option>/', $result);
  4929. }
  4930. /**
  4931. * test that bogus non-date time data doesn't cause errors.
  4932. *
  4933. * @return void
  4934. */
  4935. public function testDateTimeWithBogusData() {
  4936. $result = $this->Form->dateTime('Contact.updated', 'DMY', '12', array('value' => 'CURRENT_TIMESTAMP'));
  4937. $this->assertNotRegExp('/selected="selected">\d/', $result);
  4938. }
  4939. /**
  4940. * testDateTimeEmptyAsArray
  4941. *
  4942. * @return void
  4943. */
  4944. public function testDateTimeEmptyAsArray() {
  4945. $result = $this->Form->dateTime('Contact.date',
  4946. 'DMY',
  4947. '12',
  4948. array(
  4949. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
  4950. 'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
  4951. )
  4952. )
  4953. );
  4954. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4955. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4956. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4957. $this->assertRegExp('/<option value="">HOUR<\/option>/', $result);
  4958. $this->assertRegExp('/<option value="">MINUTE<\/option>/', $result);
  4959. $this->assertNotRegExp('/<option value=""><\/option>/', $result);
  4960. $result = $this->Form->dateTime('Contact.date',
  4961. 'DMY',
  4962. '12',
  4963. array(
  4964. 'empty' => array('day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR')
  4965. )
  4966. );
  4967. $this->assertRegExp('/<option value="">DAY<\/option>/', $result);
  4968. $this->assertRegExp('/<option value="">MONTH<\/option>/', $result);
  4969. $this->assertRegExp('/<option value="">YEAR<\/option>/', $result);
  4970. $this->assertRegExp('/<select[^<>]+id="ContactDateHour">\s<option value=""><\/option>/', $result);
  4971. $this->assertRegExp('/<select[^<>]+id="ContactDateMin">\s<option value=""><\/option>/', $result);
  4972. $this->assertRegExp('/<select[^<>]+id="ContactDateMeridian">\s<option value=""><\/option>/', $result);
  4973. }
  4974. /**
  4975. * testFormDateTimeMulti method
  4976. *
  4977. * test multiple datetime element generation
  4978. *
  4979. * @return void
  4980. */
  4981. public function testFormDateTimeMulti() {
  4982. extract($this->dateRegex);
  4983. $result = $this->Form->dateTime('Contact.1.updated');
  4984. $expected = array(
  4985. array('select' => array('name' => 'data[Contact][1][updated][day]', 'id' => 'Contact1UpdatedDay')),
  4986. $daysRegex,
  4987. array('option' => array('value' => '')),
  4988. '/option',
  4989. '*/select',
  4990. '-',
  4991. array('select' => array('name' => 'data[Contact][1][updated][month]', 'id' => 'Contact1UpdatedMonth')),
  4992. $monthsRegex,
  4993. array('option' => array('value' => '')),
  4994. '/option',
  4995. '*/select',
  4996. '-',
  4997. array('select' => array('name' => 'data[Contact][1][updated][year]', 'id' => 'Contact1UpdatedYear')),
  4998. $yearsRegex,
  4999. array('option' => array('value' => '')),
  5000. '/option',
  5001. '*/select',
  5002. array('select' => array('name' => 'data[Contact][1][updated][hour]', 'id' => 'Contact1UpdatedHour')),
  5003. $hoursRegex,
  5004. array('option' => array('value' => '')),
  5005. '/option',
  5006. '*/select',
  5007. ':',
  5008. array('select' => array('name' => 'data[Contact][1][updated][min]', 'id' => 'Contact1UpdatedMin')),
  5009. $minutesRegex,
  5010. array('option' => array('value' => '')),
  5011. '/option',
  5012. '*/select',
  5013. ' ',
  5014. array('select' => array('name' => 'data[Contact][1][updated][meridian]', 'id' => 'Contact1UpdatedMeridian')),
  5015. $meridianRegex,
  5016. array('option' => array('value' => '')),
  5017. '/option',
  5018. '*/select'
  5019. );
  5020. $this->assertTags($result, $expected);
  5021. $result = $this->Form->dateTime('Contact.2.updated');
  5022. $expected = array(
  5023. array('select' => array('name' => 'data[Contact][2][updated][day]', 'id' => 'Contact2UpdatedDay')),
  5024. $daysRegex,
  5025. array('option' => array('value' => '')),
  5026. '/option',
  5027. '*/select',
  5028. '-',
  5029. array('select' => array('name' => 'data[Contact][2][updated][month]', 'id' => 'Contact2UpdatedMonth')),
  5030. $monthsRegex,
  5031. array('option' => array('value' => '')),
  5032. '/option',
  5033. '*/select',
  5034. '-',
  5035. array('select' => array('name' => 'data[Contact][2][updated][year]', 'id' => 'Contact2UpdatedYear')),
  5036. $yearsRegex,
  5037. array('option' => array('value' => '')),
  5038. '/option',
  5039. '*/select',
  5040. array('select' => array('name' => 'data[Contact][2][updated][hour]', 'id' => 'Contact2UpdatedHour')),
  5041. $hoursRegex,
  5042. array('option' => array('value' => '')),
  5043. '/option',
  5044. '*/select',
  5045. ':',
  5046. array('select' => array('name' => 'data[Contact][2][updated][min]', 'id' => 'Contact2UpdatedMin')),
  5047. $minutesRegex,
  5048. array('option' => array('value' => '')),
  5049. '/option',
  5050. '*/select',
  5051. ' ',
  5052. array('select' => array('name' => 'data[Contact][2][updated][meridian]', 'id' => 'Contact2UpdatedMeridian')),
  5053. $meridianRegex,
  5054. array('option' => array('value' => '')),
  5055. '/option',
  5056. '*/select'
  5057. );
  5058. $this->assertTags($result, $expected);
  5059. }
  5060. /**
  5061. * When changing the date format, the label should always focus the first select box when
  5062. * clicked.
  5063. *
  5064. * @return void
  5065. */
  5066. public function testDateTimeLabelIdMatchesFirstInput() {
  5067. $result = $this->Form->input('Model.date', array('type' => 'date'));
  5068. $this->assertContains('label for="ModelDateMonth"', $result);
  5069. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'DMY'));
  5070. $this->assertContains('label for="ModelDateDay"', $result);
  5071. $result = $this->Form->input('Model.date', array('type' => 'date', 'dateFormat' => 'YMD'));
  5072. $this->assertContains('label for="ModelDateYear"', $result);
  5073. }
  5074. /**
  5075. * testMonth method
  5076. *
  5077. * @return void
  5078. */
  5079. public function testMonth() {
  5080. $result = $this->Form->month('Model.field');
  5081. $expected = array(
  5082. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5083. array('option' => array('value' => '')),
  5084. '/option',
  5085. array('option' => array('value' => '01')),
  5086. date('F', strtotime('2008-01-01 00:00:00')),
  5087. '/option',
  5088. array('option' => array('value' => '02')),
  5089. date('F', strtotime('2008-02-01 00:00:00')),
  5090. '/option',
  5091. '*/select',
  5092. );
  5093. $this->assertTags($result, $expected);
  5094. $result = $this->Form->month('Model.field', array('empty' => true));
  5095. $expected = array(
  5096. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5097. array('option' => array('value' => '')),
  5098. '/option',
  5099. array('option' => array('value' => '01')),
  5100. date('F', strtotime('2008-01-01 00:00:00')),
  5101. '/option',
  5102. array('option' => array('value' => '02')),
  5103. date('F', strtotime('2008-02-01 00:00:00')),
  5104. '/option',
  5105. '*/select',
  5106. );
  5107. $this->assertTags($result, $expected);
  5108. $result = $this->Form->month('Model.field', array('monthNames' => false));
  5109. $expected = array(
  5110. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5111. array('option' => array('value' => '')),
  5112. '/option',
  5113. array('option' => array('value' => '01')),
  5114. '01',
  5115. '/option',
  5116. array('option' => array('value' => '02')),
  5117. '02',
  5118. '/option',
  5119. '*/select',
  5120. );
  5121. $this->assertTags($result, $expected);
  5122. $monthNames = array(
  5123. '01' => 'Jan', '02' => 'Feb', '03' => 'Mar', '04' => 'Apr', '05' => 'May', '06' => 'Jun',
  5124. '07' => 'Jul', '08' => 'Aug', '09' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  5125. $result = $this->Form->month('Model.field', array('monthNames' => $monthNames));
  5126. $expected = array(
  5127. array('select' => array('name' => 'data[Model][field][month]', 'id' => 'ModelFieldMonth')),
  5128. array('option' => array('value' => '')),
  5129. '/option',
  5130. array('option' => array('value' => '01')),
  5131. 'Jan',
  5132. '/option',
  5133. array('option' => array('value' => '02')),
  5134. 'Feb',
  5135. '/option',
  5136. '*/select',
  5137. );
  5138. $this->assertTags($result, $expected);
  5139. }
  5140. /**
  5141. * testDay method
  5142. *
  5143. * @return void
  5144. */
  5145. public function testDay() {
  5146. extract($this->dateRegex);
  5147. $result = $this->Form->day('Model.field', array('value' => false));
  5148. $expected = array(
  5149. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5150. array('option' => array('value' => '')),
  5151. '/option',
  5152. array('option' => array('value' => '01')),
  5153. '1',
  5154. '/option',
  5155. array('option' => array('value' => '02')),
  5156. '2',
  5157. '/option',
  5158. $daysRegex,
  5159. '/select',
  5160. );
  5161. $this->assertTags($result, $expected);
  5162. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5163. $result = $this->Form->day('Model.field');
  5164. $expected = array(
  5165. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5166. array('option' => array('value' => '')),
  5167. '/option',
  5168. array('option' => array('value' => '01')),
  5169. '1',
  5170. '/option',
  5171. array('option' => array('value' => '02')),
  5172. '2',
  5173. '/option',
  5174. $daysRegex,
  5175. array('option' => array('value' => '10', 'selected' => 'selected')),
  5176. '10',
  5177. '/option',
  5178. $daysRegex,
  5179. '/select',
  5180. );
  5181. $this->assertTags($result, $expected);
  5182. $this->Form->request->data['Model']['field'] = '';
  5183. $result = $this->Form->day('Model.field', array('value' => '10'));
  5184. $expected = array(
  5185. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5186. array('option' => array('value' => '')),
  5187. '/option',
  5188. array('option' => array('value' => '01')),
  5189. '1',
  5190. '/option',
  5191. array('option' => array('value' => '02')),
  5192. '2',
  5193. '/option',
  5194. $daysRegex,
  5195. array('option' => array('value' => '10', 'selected' => 'selected')),
  5196. '10',
  5197. '/option',
  5198. $daysRegex,
  5199. '/select',
  5200. );
  5201. $this->assertTags($result, $expected);
  5202. $this->Form->request->data['Model']['field'] = '2006-10-10 23:12:32';
  5203. $result = $this->Form->day('Model.field', array('value' => true));
  5204. $expected = array(
  5205. array('select' => array('name' => 'data[Model][field][day]', 'id' => 'ModelFieldDay')),
  5206. array('option' => array('value' => '')),
  5207. '/option',
  5208. array('option' => array('value' => '01')),
  5209. '1',
  5210. '/option',
  5211. array('option' => array('value' => '02')),
  5212. '2',
  5213. '/option',
  5214. $daysRegex,
  5215. array('option' => array('value' => '10', 'selected' => 'selected')),
  5216. '10',
  5217. '/option',
  5218. $daysRegex,
  5219. '/select',
  5220. );
  5221. $this->assertTags($result, $expected);
  5222. }
  5223. /**
  5224. * testMinute method
  5225. *
  5226. * @return void
  5227. */
  5228. public function testMinute() {
  5229. extract($this->dateRegex);
  5230. $result = $this->Form->minute('Model.field');
  5231. $expected = array(
  5232. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5233. array('option' => array('value' => '')),
  5234. '/option',
  5235. array('option' => array('value' => '00')),
  5236. '00',
  5237. '/option',
  5238. array('option' => array('value' => '01')),
  5239. '01',
  5240. '/option',
  5241. array('option' => array('value' => '02')),
  5242. '02',
  5243. '/option',
  5244. $minutesRegex,
  5245. '/select',
  5246. );
  5247. $this->assertTags($result, $expected);
  5248. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5249. $result = $this->Form->minute('Model.field');
  5250. $expected = array(
  5251. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5252. array('option' => array('value' => '')),
  5253. '/option',
  5254. array('option' => array('value' => '00')),
  5255. '00',
  5256. '/option',
  5257. array('option' => array('value' => '01')),
  5258. '01',
  5259. '/option',
  5260. array('option' => array('value' => '02')),
  5261. '02',
  5262. '/option',
  5263. $minutesRegex,
  5264. array('option' => array('value' => '12', 'selected' => 'selected')),
  5265. '12',
  5266. '/option',
  5267. $minutesRegex,
  5268. '/select',
  5269. );
  5270. $this->assertTags($result, $expected);
  5271. $this->Form->request->data['Model']['field'] = '';
  5272. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5273. $expected = array(
  5274. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5275. array('option' => array('value' => '')),
  5276. '/option',
  5277. array('option' => array('value' => '00')),
  5278. '00',
  5279. '/option',
  5280. array('option' => array('value' => '05')),
  5281. '05',
  5282. '/option',
  5283. array('option' => array('value' => '10')),
  5284. '10',
  5285. '/option',
  5286. $minutesRegex,
  5287. '/select',
  5288. );
  5289. $this->assertTags($result, $expected);
  5290. $this->Form->request->data['Model']['field'] = '2006-10-10 00:10:32';
  5291. $result = $this->Form->minute('Model.field', array('interval' => 5));
  5292. $expected = array(
  5293. array('select' => array('name' => 'data[Model][field][min]', 'id' => 'ModelFieldMin')),
  5294. array('option' => array('value' => '')),
  5295. '/option',
  5296. array('option' => array('value' => '00')),
  5297. '00',
  5298. '/option',
  5299. array('option' => array('value' => '05')),
  5300. '05',
  5301. '/option',
  5302. array('option' => array('value' => '10', 'selected' => 'selected')),
  5303. '10',
  5304. '/option',
  5305. $minutesRegex,
  5306. '/select',
  5307. );
  5308. $this->assertTags($result, $expected);
  5309. }
  5310. /**
  5311. * testHour method
  5312. *
  5313. * @return void
  5314. */
  5315. public function testHour() {
  5316. extract($this->dateRegex);
  5317. $result = $this->Form->hour('Model.field', false);
  5318. $expected = array(
  5319. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5320. array('option' => array('value' => '')),
  5321. '/option',
  5322. array('option' => array('value' => '01')),
  5323. '1',
  5324. '/option',
  5325. array('option' => array('value' => '02')),
  5326. '2',
  5327. '/option',
  5328. $hoursRegex,
  5329. '/select',
  5330. );
  5331. $this->assertTags($result, $expected);
  5332. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5333. $result = $this->Form->hour('Model.field', false);
  5334. $expected = array(
  5335. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5336. array('option' => array('value' => '')),
  5337. '/option',
  5338. array('option' => array('value' => '01')),
  5339. '1',
  5340. '/option',
  5341. array('option' => array('value' => '02')),
  5342. '2',
  5343. '/option',
  5344. $hoursRegex,
  5345. array('option' => array('value' => '12', 'selected' => 'selected')),
  5346. '12',
  5347. '/option',
  5348. '/select',
  5349. );
  5350. $this->assertTags($result, $expected);
  5351. $this->Form->request->data['Model']['field'] = '';
  5352. $result = $this->Form->hour('Model.field', true, array('value' => '23'));
  5353. $this->assertContains('<option value="23" selected="selected">23</option>', $result);
  5354. $result = $this->Form->hour('Model.field', false, array('value' => '23'));
  5355. $this->assertContains('<option value="11" selected="selected">11</option>', $result);
  5356. $this->Form->request->data['Model']['field'] = '2006-10-10 00:12:32';
  5357. $result = $this->Form->hour('Model.field', true);
  5358. $expected = array(
  5359. array('select' => array('name' => 'data[Model][field][hour]', 'id' => 'ModelFieldHour')),
  5360. array('option' => array('value' => '')),
  5361. '/option',
  5362. array('option' => array('value' => '00', 'selected' => 'selected')),
  5363. '0',
  5364. '/option',
  5365. array('option' => array('value' => '01')),
  5366. '1',
  5367. '/option',
  5368. array('option' => array('value' => '02')),
  5369. '2',
  5370. '/option',
  5371. $hoursRegex,
  5372. '/select',
  5373. );
  5374. $this->assertTags($result, $expected);
  5375. unset($this->Form->request->data['Model']['field']);
  5376. $result = $this->Form->hour('Model.field', true, array('value' => 'now'));
  5377. $thisHour = date('H');
  5378. $optValue = date('G');
  5379. $this->assertRegExp('/<option value="' . $thisHour . '" selected="selected">' . $optValue . '<\/option>/', $result);
  5380. }
  5381. /**
  5382. * testYear method
  5383. *
  5384. * @return void
  5385. */
  5386. public function testYear() {
  5387. $result = $this->Form->year('Model.field', 2006, 2007);
  5388. $expected = array(
  5389. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5390. array('option' => array('value' => '')),
  5391. '/option',
  5392. array('option' => array('value' => '2007')),
  5393. '2007',
  5394. '/option',
  5395. array('option' => array('value' => '2006')),
  5396. '2006',
  5397. '/option',
  5398. '/select',
  5399. );
  5400. $this->assertTags($result, $expected);
  5401. $result = $this->Form->year('Model.field', 2006, 2007, array('orderYear' => 'asc'));
  5402. $expected = array(
  5403. array('select' => array('name' => 'data[Model][field][year]', 'id' => 'ModelFieldYear')),
  5404. array('option' => array('value' => '')),
  5405. '/option',
  5406. array('option' => array('value' => '2006')),
  5407. '2006',
  5408. '/option',
  5409. array('option' => array('value' => '2007')),
  5410. '2007',
  5411. '/option',
  5412. '/select',
  5413. );
  5414. $this->assertTags($result, $expected);
  5415. $this->request->data['Contact']['published'] = '';
  5416. $result = $this->Form->year('Contact.published', 2006, 2007, array('class' => 'year'));
  5417. $expected = array(
  5418. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear', 'class' => 'year')),
  5419. array('option' => array('value' => '')),
  5420. '/option',
  5421. array('option' => array('value' => '2007')),
  5422. '2007',
  5423. '/option',
  5424. array('option' => array('value' => '2006')),
  5425. '2006',
  5426. '/option',
  5427. '/select',
  5428. );
  5429. $this->assertTags($result, $expected);
  5430. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5431. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false));
  5432. $expected = array(
  5433. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5434. array('option' => array('value' => '2007')),
  5435. '2007',
  5436. '/option',
  5437. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5438. '2006',
  5439. '/option',
  5440. '/select',
  5441. );
  5442. $this->assertTags($result, $expected);
  5443. $this->Form->request->data['Contact']['published'] = '';
  5444. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => false));
  5445. $expected = array(
  5446. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5447. array('option' => array('value' => '')),
  5448. '/option',
  5449. array('option' => array('value' => '2007')),
  5450. '2007',
  5451. '/option',
  5452. array('option' => array('value' => '2006')),
  5453. '2006',
  5454. '/option',
  5455. '/select',
  5456. );
  5457. $this->assertTags($result, $expected);
  5458. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5459. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => false));
  5460. $expected = array(
  5461. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5462. array('option' => array('value' => '2007')),
  5463. '2007',
  5464. '/option',
  5465. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5466. '2006',
  5467. '/option',
  5468. '/select',
  5469. );
  5470. $this->assertTags($result, $expected);
  5471. $this->Form->request->data['Contact']['published'] = '';
  5472. $result = $this->Form->year('Contact.published', 2006, 2007, array('value' => 2007));
  5473. $expected = array(
  5474. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5475. array('option' => array('value' => '')),
  5476. '/option',
  5477. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5478. '2007',
  5479. '/option',
  5480. array('option' => array('value' => '2006')),
  5481. '2006',
  5482. '/option',
  5483. '/select',
  5484. );
  5485. $this->assertTags($result, $expected);
  5486. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5487. $result = $this->Form->year('Contact.published', 2006, 2007, array('empty' => false, 'value' => 2007));
  5488. $expected = array(
  5489. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5490. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5491. '2007',
  5492. '/option',
  5493. array('option' => array('value' => '2006')),
  5494. '2006',
  5495. '/option',
  5496. '/select',
  5497. );
  5498. $this->assertTags($result, $expected);
  5499. $this->Form->request->data['Contact']['published'] = '';
  5500. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false, 'value' => 2007));
  5501. $expected = array(
  5502. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5503. array('option' => array('value' => '2008')),
  5504. '2008',
  5505. '/option',
  5506. array('option' => array('value' => '2007', 'selected' => 'selected')),
  5507. '2007',
  5508. '/option',
  5509. array('option' => array('value' => '2006')),
  5510. '2006',
  5511. '/option',
  5512. '/select',
  5513. );
  5514. $this->assertTags($result, $expected);
  5515. $this->Form->request->data['Contact']['published'] = '2006-10-10';
  5516. $result = $this->Form->year('Contact.published', 2006, 2008, array('empty' => false));
  5517. $expected = array(
  5518. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5519. array('option' => array('value' => '2008')),
  5520. '2008',
  5521. '/option',
  5522. array('option' => array('value' => '2007')),
  5523. '2007',
  5524. '/option',
  5525. array('option' => array('value' => '2006', 'selected' => 'selected')),
  5526. '2006',
  5527. '/option',
  5528. '/select',
  5529. );
  5530. $this->assertTags($result, $expected);
  5531. $this->Form->request->data = array();
  5532. $this->Form->create('Contact');
  5533. $result = $this->Form->year('published', 2006, 2008, array('empty' => false));
  5534. $expected = array(
  5535. array('select' => array('name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear')),
  5536. array('option' => array('value' => '2008')),
  5537. '2008',
  5538. '/option',
  5539. array('option' => array('value' => '2007')),
  5540. '2007',
  5541. '/option',
  5542. array('option' => array('value' => '2006')),
  5543. '2006',
  5544. '/option',
  5545. '/select',
  5546. );
  5547. $this->assertTags($result, $expected);
  5548. $result = $this->Form->year('published', array(), array(), array('empty' => false));
  5549. $this->assertContains('data[Contact][published][year]', $result);
  5550. }
  5551. /**
  5552. * testTextArea method
  5553. *
  5554. * @return void
  5555. */
  5556. public function testTextArea() {
  5557. $this->Form->request->data = array('Model' => array('field' => 'some test data'));
  5558. $result = $this->Form->textarea('Model.field');
  5559. $expected = array(
  5560. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5561. 'some test data',
  5562. '/textarea',
  5563. );
  5564. $this->assertTags($result, $expected);
  5565. $result = $this->Form->textarea('Model.tmp');
  5566. $expected = array(
  5567. 'textarea' => array('name' => 'data[Model][tmp]', 'id' => 'ModelTmp'),
  5568. '/textarea',
  5569. );
  5570. $this->assertTags($result, $expected);
  5571. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5572. $result = $this->Form->textarea('Model.field');
  5573. $expected = array(
  5574. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5575. htmlentities('some <strong>test</strong> data with <a href="#">HTML</a> chars'),
  5576. '/textarea',
  5577. );
  5578. $this->assertTags($result, $expected);
  5579. $this->Form->request->data = array('Model' => array('field' => 'some <strong>test</strong> data with <a href="#">HTML</a> chars'));
  5580. $result = $this->Form->textarea('Model.field', array('escape' => false));
  5581. $expected = array(
  5582. 'textarea' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  5583. 'some <strong>test</strong> data with <a href="#">HTML</a> chars',
  5584. '/textarea',
  5585. );
  5586. $this->assertTags($result, $expected);
  5587. $this->Form->request->data['Model']['0']['OtherModel']['field'] = null;
  5588. $result = $this->Form->textarea('Model.0.OtherModel.field');
  5589. $expected = array(
  5590. 'textarea' => array('name' => 'data[Model][0][OtherModel][field]', 'id' => 'Model0OtherModelField'),
  5591. '/textarea'
  5592. );
  5593. $this->assertTags($result, $expected);
  5594. }
  5595. /**
  5596. * testTextAreaWithStupidCharacters method
  5597. *
  5598. * test text area with non-ascii characters
  5599. *
  5600. * @return void
  5601. */
  5602. public function testTextAreaWithStupidCharacters() {
  5603. $this->loadFixtures('Post');
  5604. $result = $this->Form->input('Post.content', array(
  5605. 'label' => 'Current Text', 'value' => "GREAT®", 'rows' => '15', 'cols' => '75'
  5606. ));
  5607. $expected = array(
  5608. 'div' => array('class' => 'input textarea'),
  5609. 'label' => array('for' => 'PostContent'),
  5610. 'Current Text',
  5611. '/label',
  5612. 'textarea' => array('name' => 'data[Post][content]', 'id' => 'PostContent', 'rows' => '15', 'cols' => '75'),
  5613. 'GREAT®',
  5614. '/textarea',
  5615. '/div'
  5616. );
  5617. $this->assertTags($result, $expected);
  5618. }
  5619. /**
  5620. * testHiddenField method
  5621. *
  5622. * @return void
  5623. */
  5624. public function testHiddenField() {
  5625. $Contact = ClassRegistry::getObject('Contact');
  5626. $Contact->validationErrors['field'] = 1;
  5627. $this->Form->request->data['Contact']['field'] = 'test';
  5628. $result = $this->Form->hidden('Contact.field', array('id' => 'theID'));
  5629. $this->assertTags($result, array(
  5630. 'input' => array('type' => 'hidden', 'class' => 'form-error', 'name' => 'data[Contact][field]', 'id' => 'theID', 'value' => 'test'))
  5631. );
  5632. }
  5633. /**
  5634. * testFileUploadField method
  5635. *
  5636. * @return void
  5637. */
  5638. public function testFileUploadField() {
  5639. $result = $this->Form->file('Model.upload');
  5640. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5641. $this->Form->request->data['Model.upload'] = array("name" => "", "type" => "", "tmp_name" => "", "error" => 4, "size" => 0);
  5642. $result = $this->Form->input('Model.upload', array('type' => 'file'));
  5643. $expected = array(
  5644. 'div' => array('class' => 'input file'),
  5645. 'label' => array('for' => 'ModelUpload'),
  5646. 'Upload',
  5647. '/label',
  5648. 'input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload'),
  5649. '/div'
  5650. );
  5651. $this->assertTags($result, $expected);
  5652. $this->Form->request->data['Model']['upload'] = 'no data should be set in value';
  5653. $result = $this->Form->file('Model.upload');
  5654. $this->assertTags($result, array('input' => array('type' => 'file', 'name' => 'data[Model][upload]', 'id' => 'ModelUpload')));
  5655. }
  5656. /**
  5657. * test File upload input on a model not used in create();
  5658. *
  5659. * @return void
  5660. */
  5661. public function testFileUploadOnOtherModel() {
  5662. $this->Form->create('ValidateUser', array('type' => 'file'));
  5663. $result = $this->Form->file('ValidateProfile.city');
  5664. $expected = array(
  5665. 'input' => array('type' => 'file', 'name' => 'data[ValidateProfile][city]', 'id' => 'ValidateProfileCity')
  5666. );
  5667. $this->assertTags($result, $expected);
  5668. }
  5669. /**
  5670. * testButton method
  5671. *
  5672. * @return void
  5673. */
  5674. public function testButton() {
  5675. $result = $this->Form->button('Hi');
  5676. $this->assertTags($result, array('button' => array('type' => 'submit'), 'Hi', '/button'));
  5677. $result = $this->Form->button('Clear Form >', array('type' => 'reset'));
  5678. $this->assertTags($result, array('button' => array('type' => 'reset'), 'Clear Form >', '/button'));
  5679. $result = $this->Form->button('Clear Form >', array('type' => 'reset', 'id' => 'clearForm'));
  5680. $this->assertTags($result, array('button' => array('type' => 'reset', 'id' => 'clearForm'), 'Clear Form >', '/button'));
  5681. $result = $this->Form->button('<Clear Form>', array('type' => 'reset', 'escape' => true));
  5682. $this->assertTags($result, array('button' => array('type' => 'reset'), '&lt;Clear Form&gt;', '/button'));
  5683. $result = $this->Form->button('No type', array('type' => false));
  5684. $this->assertTags($result, array('button' => array(), 'No type', '/button'));
  5685. $result = $this->Form->button('Upload Text', array('onClick' => "$('#postAddForm').ajaxSubmit({target: '#postTextUpload', url: '/posts/text'});return false;'", 'escape' => false));
  5686. $this->assertNotRegExp('/\&039/', $result);
  5687. }
  5688. /**
  5689. * Test that button() makes unlocked fields by default.
  5690. *
  5691. * @return void
  5692. */
  5693. public function testButtonUnlockedByDefault() {
  5694. $this->Form->request->params['_Token']['key'] = 'secured';
  5695. $this->Form->button('Save', array('name' => 'save'));
  5696. $this->Form->button('Clear');
  5697. $result = $this->Form->unlockField();
  5698. $this->assertEquals(array('save'), $result);
  5699. }
  5700. /**
  5701. * testPostButton method
  5702. *
  5703. * @return void
  5704. */
  5705. public function testPostButton() {
  5706. $result = $this->Form->postButton('Hi', '/controller/action');
  5707. $this->assertTags($result, array(
  5708. 'form' => array('method' => 'post', 'action' => '/controller/action', 'accept-charset' => 'utf-8'),
  5709. 'div' => array('style' => 'display:none;'),
  5710. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5711. '/div',
  5712. 'button' => array('type' => 'submit'),
  5713. 'Hi',
  5714. '/button',
  5715. '/form'
  5716. ));
  5717. $result = $this->Form->postButton('Send', '/', array('data' => array('extra' => 'value')));
  5718. $this->assertTrue(strpos($result, '<input type="hidden" name="data[extra]" value="value"/>') !== false);
  5719. }
  5720. /**
  5721. * Test that postButton adds _Token fields.
  5722. *
  5723. * @return void
  5724. */
  5725. public function testSecurePostButton() {
  5726. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5727. $result = $this->Form->postButton('Delete', '/posts/delete/1');
  5728. $expected = array(
  5729. 'form' => array(
  5730. 'method' => 'post', 'action' => '/posts/delete/1', 'accept-charset' => 'utf-8',
  5731. ),
  5732. array('div' => array('style' => 'display:none;')),
  5733. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5734. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5735. '/div',
  5736. 'button' => array('type' => 'submit'),
  5737. 'Delete',
  5738. '/button',
  5739. array('div' => array('style' => 'display:none;')),
  5740. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5741. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5742. '/div',
  5743. '/form',
  5744. );
  5745. $this->assertTags($result, $expected);
  5746. }
  5747. /**
  5748. * testPostLink method
  5749. *
  5750. * @return void
  5751. */
  5752. public function testPostLink() {
  5753. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5754. $this->assertTags($result, array(
  5755. 'form' => array(
  5756. 'method' => 'post', 'action' => '/posts/delete/1',
  5757. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5758. ),
  5759. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5760. '/form',
  5761. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5762. 'Delete',
  5763. '/a'
  5764. ));
  5765. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('method' => 'delete'));
  5766. $this->assertTags($result, array(
  5767. 'form' => array(
  5768. 'method' => 'post', 'action' => '/posts/delete/1',
  5769. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5770. ),
  5771. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'DELETE'),
  5772. '/form',
  5773. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5774. 'Delete',
  5775. '/a'
  5776. ));
  5777. $result = $this->Form->postLink('Delete', '/posts/delete/1', array(), 'Confirm?');
  5778. $this->assertTags($result, array(
  5779. 'form' => array(
  5780. 'method' => 'post', 'action' => '/posts/delete/1',
  5781. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5782. ),
  5783. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5784. '/form',
  5785. 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\(&#039;Confirm\?&#039;\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'),
  5786. 'Delete',
  5787. '/a'
  5788. ));
  5789. $result = $this->Form->postLink('Delete', '/posts/delete', array('data' => array('id' => 1)));
  5790. $this->assertContains('<input type="hidden" name="data[id]" value="1"/>', $result);
  5791. $result = $this->Form->postLink('Delete', '/posts/delete/1', array('target' => '_blank'));
  5792. $this->assertTags($result, array(
  5793. 'form' => array(
  5794. 'method' => 'post', 'target' => '_blank', 'action' => '/posts/delete/1',
  5795. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5796. ),
  5797. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  5798. '/form',
  5799. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5800. 'Delete',
  5801. '/a'
  5802. ));
  5803. }
  5804. /**
  5805. * Test that postLink adds _Token fields.
  5806. *
  5807. * @return void
  5808. */
  5809. public function testSecurePostLink() {
  5810. $this->Form->request->params['_Token'] = array('key' => 'testkey');
  5811. $result = $this->Form->postLink('Delete', '/posts/delete/1');
  5812. $expected = array(
  5813. 'form' => array(
  5814. 'method' => 'post', 'action' => '/posts/delete/1',
  5815. 'name' => 'preg:/post_\w+/', 'id' => 'preg:/post_\w+/', 'style' => 'display:none;'
  5816. ),
  5817. array('input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST')),
  5818. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][key]', 'value' => 'testkey', 'id' => 'preg:/Token\d+/')),
  5819. 'div' => array('style' => 'display:none;'),
  5820. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][fields]', 'value' => 'preg:/[\w\d%]+/', 'id' => 'preg:/TokenFields\d+/')),
  5821. array('input' => array('type' => 'hidden', 'name' => 'data[_Token][unlocked]', 'value' => '', 'id' => 'preg:/TokenUnlocked\d+/')),
  5822. '/div',
  5823. '/form',
  5824. 'a' => array('href' => '#', 'onclick' => 'preg:/document\.post_\w+\.submit\(\); event\.returnValue = false; return false;/'),
  5825. 'Delete',
  5826. '/a'
  5827. );
  5828. $this->assertTags($result, $expected);
  5829. }
  5830. /**
  5831. * testSubmitButton method
  5832. *
  5833. * @return void
  5834. */
  5835. public function testSubmitButton() {
  5836. $result = $this->Form->submit('');
  5837. $expected = array(
  5838. 'div' => array('class' => 'submit'),
  5839. 'input' => array('type' => 'submit', 'value' => ''),
  5840. '/div'
  5841. );
  5842. $this->assertTags($result, $expected);
  5843. $result = $this->Form->submit('Test Submit');
  5844. $expected = array(
  5845. 'div' => array('class' => 'submit'),
  5846. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5847. '/div'
  5848. );
  5849. $this->assertTags($result, $expected);
  5850. $result = $this->Form->submit('Test Submit', array('div' => array('tag' => 'span')));
  5851. $expected = array(
  5852. 'span' => array('class' => 'submit'),
  5853. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5854. '/span'
  5855. );
  5856. $this->assertTags($result, $expected);
  5857. $result = $this->Form->submit('Test Submit', array('class' => 'save', 'div' => false));
  5858. $expected = array('input' => array('type' => 'submit', 'value' => 'Test Submit', 'class' => 'save'));
  5859. $this->assertTags($result, $expected);
  5860. $result = $this->Form->submit('Test Submit', array('div' => array('id' => 'SaveButton')));
  5861. $expected = array(
  5862. 'div' => array('class' => 'submit', 'id' => 'SaveButton'),
  5863. 'input' => array('type' => 'submit', 'value' => 'Test Submit'),
  5864. '/div'
  5865. );
  5866. $this->assertTags($result, $expected);
  5867. $result = $this->Form->submit('Next >');
  5868. $expected = array(
  5869. 'div' => array('class' => 'submit'),
  5870. 'input' => array('type' => 'submit', 'value' => 'Next &gt;'),
  5871. '/div'
  5872. );
  5873. $this->assertTags($result, $expected);
  5874. $result = $this->Form->submit('Next >', array('escape' => false));
  5875. $expected = array(
  5876. 'div' => array('class' => 'submit'),
  5877. 'input' => array('type' => 'submit', 'value' => 'Next >'),
  5878. '/div'
  5879. );
  5880. $this->assertTags($result, $expected);
  5881. $result = $this->Form->submit('Reset!', array('type' => 'reset'));
  5882. $expected = array(
  5883. 'div' => array('class' => 'submit'),
  5884. 'input' => array('type' => 'reset', 'value' => 'Reset!'),
  5885. '/div'
  5886. );
  5887. $this->assertTags($result, $expected);
  5888. $before = '--before--';
  5889. $after = '--after--';
  5890. $result = $this->Form->submit('Test', array('before' => $before));
  5891. $expected = array(
  5892. 'div' => array('class' => 'submit'),
  5893. '--before--',
  5894. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5895. '/div'
  5896. );
  5897. $this->assertTags($result, $expected);
  5898. $result = $this->Form->submit('Test', array('after' => $after));
  5899. $expected = array(
  5900. 'div' => array('class' => 'submit'),
  5901. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5902. '--after--',
  5903. '/div'
  5904. );
  5905. $this->assertTags($result, $expected);
  5906. $result = $this->Form->submit('Test', array('before' => $before, 'after' => $after));
  5907. $expected = array(
  5908. 'div' => array('class' => 'submit'),
  5909. '--before--',
  5910. 'input' => array('type' => 'submit', 'value' => 'Test'),
  5911. '--after--',
  5912. '/div'
  5913. );
  5914. $this->assertTags($result, $expected);
  5915. }
  5916. /**
  5917. * test image submit types.
  5918. *
  5919. * @return void
  5920. */
  5921. public function testSubmitImage() {
  5922. $result = $this->Form->submit('http://example.com/cake.power.gif');
  5923. $expected = array(
  5924. 'div' => array('class' => 'submit'),
  5925. 'input' => array('type' => 'image', 'src' => 'http://example.com/cake.power.gif'),
  5926. '/div'
  5927. );
  5928. $this->assertTags($result, $expected);
  5929. $result = $this->Form->submit('/relative/cake.power.gif');
  5930. $expected = array(
  5931. 'div' => array('class' => 'submit'),
  5932. 'input' => array('type' => 'image', 'src' => 'relative/cake.power.gif'),
  5933. '/div'
  5934. );
  5935. $this->assertTags($result, $expected);
  5936. $result = $this->Form->submit('cake.power.gif');
  5937. $expected = array(
  5938. 'div' => array('class' => 'submit'),
  5939. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5940. '/div'
  5941. );
  5942. $this->assertTags($result, $expected);
  5943. $result = $this->Form->submit('Not.an.image');
  5944. $expected = array(
  5945. 'div' => array('class' => 'submit'),
  5946. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5947. '/div'
  5948. );
  5949. $this->assertTags($result, $expected);
  5950. $after = '--after--';
  5951. $before = '--before--';
  5952. $result = $this->Form->submit('cake.power.gif', array('after' => $after));
  5953. $expected = array(
  5954. 'div' => array('class' => 'submit'),
  5955. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5956. '--after--',
  5957. '/div'
  5958. );
  5959. $this->assertTags($result, $expected);
  5960. $result = $this->Form->submit('cake.power.gif', array('before' => $before));
  5961. $expected = array(
  5962. 'div' => array('class' => 'submit'),
  5963. '--before--',
  5964. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5965. '/div'
  5966. );
  5967. $this->assertTags($result, $expected);
  5968. $result = $this->Form->submit('cake.power.gif', array('before' => $before, 'after' => $after));
  5969. $expected = array(
  5970. 'div' => array('class' => 'submit'),
  5971. '--before--',
  5972. 'input' => array('type' => 'image', 'src' => 'img/cake.power.gif'),
  5973. '--after--',
  5974. '/div'
  5975. );
  5976. $this->assertTags($result, $expected);
  5977. $result = $this->Form->submit('Not.an.image', array('before' => $before, 'after' => $after));
  5978. $expected = array(
  5979. 'div' => array('class' => 'submit'),
  5980. '--before--',
  5981. 'input' => array('type' => 'submit', 'value' => 'Not.an.image'),
  5982. '--after--',
  5983. '/div'
  5984. );
  5985. $this->assertTags($result, $expected);
  5986. }
  5987. /**
  5988. * Submit buttons should be unlocked by default as there could be multiples, and only one will
  5989. * be submitted at a time.
  5990. *
  5991. * @return void
  5992. */
  5993. public function testSubmitUnlockedByDefault() {
  5994. $this->Form->request->params['_Token']['key'] = 'secured';
  5995. $this->Form->submit('Go go');
  5996. $this->Form->submit('Save', array('name' => 'save'));
  5997. $result = $this->Form->unlockField();
  5998. $this->assertEquals(array('save'), $result, 'Only submits with name attributes should be unlocked.');
  5999. }
  6000. /**
  6001. * Test submit image with timestamps.
  6002. *
  6003. * @return void
  6004. */
  6005. public function testSubmitImageTimestamp() {
  6006. Configure::write('Asset.timestamp', 'force');
  6007. $result = $this->Form->submit('cake.power.gif');
  6008. $expected = array(
  6009. 'div' => array('class' => 'submit'),
  6010. 'input' => array('type' => 'image', 'src' => 'preg:/img\/cake\.power\.gif\?\d*/'),
  6011. '/div'
  6012. );
  6013. $this->assertTags($result, $expected);
  6014. }
  6015. /**
  6016. * test the create() method
  6017. *
  6018. * @return void
  6019. */
  6020. public function testCreate() {
  6021. $result = $this->Form->create('Contact');
  6022. $encoding = strtolower(Configure::read('App.encoding'));
  6023. $expected = array(
  6024. 'form' => array(
  6025. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6026. 'accept-charset' => $encoding
  6027. ),
  6028. 'div' => array('style' => 'preg:/display\s*\:\s*none;\s*/'),
  6029. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6030. '/div'
  6031. );
  6032. $this->assertTags($result, $expected);
  6033. $result = $this->Form->create('Contact', array('type' => 'GET'));
  6034. $expected = array('form' => array(
  6035. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6036. 'accept-charset' => $encoding
  6037. ));
  6038. $this->assertTags($result, $expected);
  6039. $result = $this->Form->create('Contact', array('type' => 'get'));
  6040. $expected = array('form' => array(
  6041. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6042. 'accept-charset' => $encoding
  6043. ));
  6044. $this->assertTags($result, $expected);
  6045. $result = $this->Form->create('Contact', array('type' => 'put'));
  6046. $expected = array(
  6047. 'form' => array(
  6048. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6049. 'accept-charset' => $encoding
  6050. ),
  6051. 'div' => array('style' => 'display:none;'),
  6052. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6053. '/div'
  6054. );
  6055. $this->assertTags($result, $expected);
  6056. $result = $this->Form->create('Contact', array('type' => 'file'));
  6057. $expected = array(
  6058. 'form' => array(
  6059. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6060. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6061. ),
  6062. 'div' => array('style' => 'display:none;'),
  6063. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6064. '/div'
  6065. );
  6066. $this->assertTags($result, $expected);
  6067. $this->Form->request->data['Contact']['id'] = 1;
  6068. $this->Form->request->here = '/contacts/edit/1';
  6069. $this->Form->request['action'] = 'edit';
  6070. $result = $this->Form->create('Contact');
  6071. $expected = array(
  6072. 'form' => array(
  6073. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6074. 'accept-charset' => $encoding
  6075. ),
  6076. 'div' => array('style' => 'display:none;'),
  6077. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6078. '/div'
  6079. );
  6080. $this->assertTags($result, $expected);
  6081. $this->Form->request->data['Contact']['id'] = 1;
  6082. $this->Form->request->here = '/contacts/edit/1';
  6083. $this->Form->request['action'] = 'edit';
  6084. $result = $this->Form->create('Contact', array('type' => 'file'));
  6085. $expected = array(
  6086. 'form' => array(
  6087. 'id' => 'ContactEditForm', 'method' => 'post', 'action' => '/contacts/edit/1',
  6088. 'accept-charset' => $encoding, 'enctype' => 'multipart/form-data'
  6089. ),
  6090. 'div' => array('style' => 'display:none;'),
  6091. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6092. '/div'
  6093. );
  6094. $this->assertTags($result, $expected);
  6095. $this->Form->request->data['ContactNonStandardPk']['pk'] = 1;
  6096. $result = $this->Form->create('ContactNonStandardPk', array('url' => array('action' => 'edit')));
  6097. $expected = array(
  6098. 'form' => array(
  6099. 'id' => 'ContactNonStandardPkEditForm', 'method' => 'post',
  6100. 'action' => '/contact_non_standard_pks/edit/1','accept-charset' => $encoding
  6101. ),
  6102. 'div' => array('style' => 'display:none;'),
  6103. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6104. '/div'
  6105. );
  6106. $this->assertTags($result, $expected);
  6107. $result = $this->Form->create('Contact', array('id' => 'TestId'));
  6108. $expected = array(
  6109. 'form' => array(
  6110. 'id' => 'TestId', 'method' => 'post', 'action' => '/contacts/edit/1',
  6111. 'accept-charset' => $encoding
  6112. ),
  6113. 'div' => array('style' => 'display:none;'),
  6114. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'PUT'),
  6115. '/div'
  6116. );
  6117. $this->assertTags($result, $expected);
  6118. $this->Form->request['action'] = 'add';
  6119. $result = $this->Form->create('User', array('url' => array('action' => 'login')));
  6120. $expected = array(
  6121. 'form' => array(
  6122. 'id' => 'UserAddForm', 'method' => 'post', 'action' => '/users/login',
  6123. 'accept-charset' => $encoding
  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. $result = $this->Form->create('User', array('action' => 'login'));
  6131. $expected = array(
  6132. 'form' => array(
  6133. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/users/login',
  6134. 'accept-charset' => $encoding
  6135. ),
  6136. 'div' => array('style' => 'display:none;'),
  6137. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6138. '/div'
  6139. );
  6140. $this->assertTags($result, $expected);
  6141. $result = $this->Form->create('User', array('url' => '/users/login'));
  6142. $expected = array(
  6143. 'form' => array('method' => 'post', 'action' => '/users/login', 'accept-charset' => $encoding, 'id' => 'UserAddForm'),
  6144. 'div' => array('style' => 'display:none;'),
  6145. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6146. '/div'
  6147. );
  6148. $this->assertTags($result, $expected);
  6149. $this->Form->request['controller'] = 'pages';
  6150. $result = $this->Form->create('User', array('action' => 'signup'));
  6151. $expected = array(
  6152. 'form' => array(
  6153. 'id' => 'UserSignupForm', 'method' => 'post', 'action' => '/users/signup',
  6154. 'accept-charset' => $encoding
  6155. ),
  6156. 'div' => array('style' => 'display:none;'),
  6157. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6158. '/div'
  6159. );
  6160. $this->assertTags($result, $expected);
  6161. $this->Form->request->data = array();
  6162. $this->Form->request['controller'] = 'contacts';
  6163. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6164. $result = $this->Form->create(array('url' => array('action' => 'index', 'param')));
  6165. $expected = array(
  6166. 'form' => array(
  6167. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/index/param',
  6168. 'accept-charset' => 'utf-8'
  6169. ),
  6170. 'div' => array('style' => 'display:none;'),
  6171. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6172. '/div'
  6173. );
  6174. $this->assertTags($result, $expected);
  6175. }
  6176. /**
  6177. * Test the onsubmit option for create()
  6178. *
  6179. * @return void
  6180. */
  6181. public function testCreateOnSubmit() {
  6182. $this->Form->request->data = array();
  6183. $this->Form->request['controller'] = 'contacts';
  6184. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6185. $result = $this->Form->create(array('url' => array('action' => 'index', 'param'), 'default' => false));
  6186. $expected = array(
  6187. 'form' => array(
  6188. 'id' => 'ContactAddForm', 'method' => 'post', 'onsubmit' => 'event.returnValue = false; return false;', 'action' => '/contacts/index/param',
  6189. 'accept-charset' => 'utf-8'
  6190. ),
  6191. 'div' => array('style' => 'display:none;'),
  6192. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6193. '/div'
  6194. );
  6195. $this->assertTags($result, $expected);
  6196. $this->Form->request->data = array();
  6197. $this->Form->request['controller'] = 'contacts';
  6198. $this->Form->request['models'] = array('Contact' => array('plugin' => null, 'className' => 'Contact'));
  6199. $result = $this->Form->create(array(
  6200. 'url' => array('action' => 'index', 'param'),
  6201. 'default' => false,
  6202. 'onsubmit' => 'someFunction();'
  6203. ));
  6204. $expected = array(
  6205. 'form' => array(
  6206. 'id' => 'ContactAddForm', 'method' => 'post',
  6207. 'onsubmit' => 'someFunction();event.returnValue = false; return false;',
  6208. 'action' => '/contacts/index/param',
  6209. 'accept-charset' => 'utf-8'
  6210. ),
  6211. 'div' => array('style' => 'display:none;'),
  6212. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6213. '/div'
  6214. );
  6215. $this->assertTags($result, $expected);
  6216. }
  6217. /**
  6218. * test create() with automatic url generation
  6219. *
  6220. * @return void
  6221. */
  6222. public function testCreateAutoUrl() {
  6223. Router::setRequestInfo(array(array(), array('base' => '/base_url')));
  6224. $this->Form->request->here = '/base_url/contacts/add/Contact:1';
  6225. $this->Form->request->base = '/base_url';
  6226. $result = $this->Form->create('Contact');
  6227. $expected = array(
  6228. 'form' => array(
  6229. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/base_url/contacts/add/Contact:1',
  6230. 'accept-charset' => 'utf-8'
  6231. ),
  6232. 'div' => array('style' => 'display:none;'),
  6233. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6234. '/div'
  6235. );
  6236. $this->assertTags($result, $expected);
  6237. $this->Form->request['action'] = 'delete';
  6238. $this->Form->request->here = '/base_url/contacts/delete/10/User:42';
  6239. $this->Form->request->base = '/base_url';
  6240. $result = $this->Form->create('Contact');
  6241. $expected = array(
  6242. 'form' => array(
  6243. 'id' => 'ContactDeleteForm', 'method' => 'post', 'action' => '/base_url/contacts/delete/10/User:42',
  6244. 'accept-charset' => 'utf-8'
  6245. ),
  6246. 'div' => array('style' => 'display:none;'),
  6247. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6248. '/div'
  6249. );
  6250. $this->assertTags($result, $expected);
  6251. }
  6252. /**
  6253. * test create() with a custom route
  6254. *
  6255. * @return void
  6256. */
  6257. public function testCreateCustomRoute() {
  6258. Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
  6259. $encoding = strtolower(Configure::read('App.encoding'));
  6260. $result = $this->Form->create('User', array('action' => 'login'));
  6261. $expected = array(
  6262. 'form' => array(
  6263. 'id' => 'UserLoginForm', 'method' => 'post', 'action' => '/login',
  6264. 'accept-charset' => $encoding
  6265. ),
  6266. 'div' => array('style' => 'display:none;'),
  6267. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6268. '/div'
  6269. );
  6270. $this->assertTags($result, $expected);
  6271. }
  6272. /**
  6273. * test that inputDefaults are stored and used.
  6274. *
  6275. * @return void
  6276. */
  6277. public function testCreateWithInputDefaults() {
  6278. $this->Form->create('User', array(
  6279. 'inputDefaults' => array(
  6280. 'div' => false,
  6281. 'label' => false,
  6282. 'error' => array('attributes' => array('wrap' => 'small', 'class' => 'error')),
  6283. 'format' => array('before', 'label', 'between', 'input', 'after', 'error')
  6284. )
  6285. ));
  6286. $result = $this->Form->input('username');
  6287. $expected = array(
  6288. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername')
  6289. );
  6290. $this->assertTags($result, $expected);
  6291. $result = $this->Form->input('username', array('div' => true, 'label' => 'username'));
  6292. $expected = array(
  6293. 'div' => array('class' => 'input text'),
  6294. 'label' => array('for' => 'UserUsername'), 'username', '/label',
  6295. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6296. '/div'
  6297. );
  6298. $this->assertTags($result, $expected);
  6299. $result = $this->Form->input('username', array('label' => 'Username', 'format' => array('input', 'label')));
  6300. $expected = array(
  6301. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6302. 'label' => array('for' => 'UserUsername'), 'Username', '/label',
  6303. );
  6304. $this->assertTags($result, $expected);
  6305. $this->Form->create('User', array(
  6306. 'inputDefaults' => array(
  6307. 'div' => false,
  6308. 'label' => array('class' => 'nice', 'for' => 'changed'),
  6309. )
  6310. ));
  6311. $result = $this->Form->input('username', array('div' => true));
  6312. $expected = array(
  6313. 'div' => array('class' => 'input text'),
  6314. 'label' => array('for' => 'changed', 'class' => 'nice'), 'Username', '/label',
  6315. 'input' => array('type' => 'text', 'name' => 'data[User][username]', 'id' => 'UserUsername'),
  6316. '/div'
  6317. );
  6318. $this->assertTags($result, $expected);
  6319. }
  6320. /**
  6321. * test automatic accept-charset overriding
  6322. *
  6323. * @return void
  6324. */
  6325. public function testCreateWithAcceptCharset() {
  6326. $result = $this->Form->create('UserForm', array(
  6327. 'type' => 'post', 'action' => 'login','encoding' => 'iso-8859-1'
  6328. )
  6329. );
  6330. $expected = array(
  6331. 'form' => array(
  6332. 'method' => 'post', 'action' => '/user_forms/login', 'id' => 'UserFormLoginForm',
  6333. 'accept-charset' => 'iso-8859-1'
  6334. ),
  6335. 'div' => array('style' => 'display:none;'),
  6336. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6337. '/div'
  6338. );
  6339. $this->assertTags($result, $expected);
  6340. }
  6341. /**
  6342. * Test base form url when url param is passed with multiple parameters (&)
  6343. *
  6344. */
  6345. public function testCreateQuerystringrequest() {
  6346. $encoding = strtolower(Configure::read('App.encoding'));
  6347. $result = $this->Form->create('Contact', array(
  6348. 'type' => 'post',
  6349. 'escape' => false,
  6350. 'url' => array(
  6351. 'controller' => 'controller',
  6352. 'action' => 'action',
  6353. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6354. )
  6355. ));
  6356. $expected = array(
  6357. 'form' => array(
  6358. 'id' => 'ContactAddForm',
  6359. 'method' => 'post',
  6360. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6361. 'accept-charset' => $encoding
  6362. ),
  6363. 'div' => array('style' => 'display:none;'),
  6364. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6365. '/div'
  6366. );
  6367. $this->assertTags($result, $expected);
  6368. $result = $this->Form->create('Contact', array(
  6369. 'type' => 'post',
  6370. 'url' => array(
  6371. 'controller' => 'controller',
  6372. 'action' => 'action',
  6373. '?' => array('param1' => 'value1', 'param2' => 'value2')
  6374. )
  6375. ));
  6376. $expected = array(
  6377. 'form' => array(
  6378. 'id' => 'ContactAddForm',
  6379. 'method' => 'post',
  6380. 'action' => '/controller/action?param1=value1&amp;param2=value2',
  6381. 'accept-charset' => $encoding
  6382. ),
  6383. 'div' => array('style' => 'display:none;'),
  6384. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6385. '/div'
  6386. );
  6387. $this->assertTags($result, $expected);
  6388. }
  6389. /**
  6390. * test that create() doesn't cause errors by multiple id's being in the primary key
  6391. * as could happen with multiple select or checkboxes.
  6392. *
  6393. * @return void
  6394. */
  6395. public function testCreateWithMultipleIdInData() {
  6396. $encoding = strtolower(Configure::read('App.encoding'));
  6397. $this->Form->request->data['Contact']['id'] = array(1, 2);
  6398. $result = $this->Form->create('Contact');
  6399. $expected = array(
  6400. 'form' => array(
  6401. 'id' => 'ContactAddForm',
  6402. 'method' => 'post',
  6403. 'action' => '/contacts/add',
  6404. 'accept-charset' => $encoding
  6405. ),
  6406. 'div' => array('style' => 'display:none;'),
  6407. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6408. '/div'
  6409. );
  6410. $this->assertTags($result, $expected);
  6411. }
  6412. /**
  6413. * test that create() doesn't add in extra passed params.
  6414. *
  6415. * @return void
  6416. */
  6417. public function testCreatePassedArgs() {
  6418. $encoding = strtolower(Configure::read('App.encoding'));
  6419. $this->Form->request->data['Contact']['id'] = 1;
  6420. $result = $this->Form->create('Contact', array(
  6421. 'type' => 'post',
  6422. 'escape' => false,
  6423. 'url' => array(
  6424. 'action' => 'edit',
  6425. 'myparam'
  6426. )
  6427. ));
  6428. $expected = array(
  6429. 'form' => array(
  6430. 'id' => 'ContactAddForm',
  6431. 'method' => 'post',
  6432. 'action' => '/contacts/edit/myparam',
  6433. 'accept-charset' => $encoding
  6434. ),
  6435. 'div' => array('style' => 'display:none;'),
  6436. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6437. '/div'
  6438. );
  6439. $this->assertTags($result, $expected);
  6440. }
  6441. /**
  6442. * test creating a get form, and get form inputs.
  6443. *
  6444. * @return void
  6445. */
  6446. public function testGetFormCreate() {
  6447. $encoding = strtolower(Configure::read('App.encoding'));
  6448. $result = $this->Form->create('Contact', array('type' => 'get'));
  6449. $this->assertTags($result, array('form' => array(
  6450. 'id' => 'ContactAddForm', 'method' => 'get', 'action' => '/contacts/add',
  6451. 'accept-charset' => $encoding
  6452. )));
  6453. $result = $this->Form->text('Contact.name');
  6454. $this->assertTags($result, array('input' => array(
  6455. 'name' => 'name', 'type' => 'text', 'id' => 'ContactName',
  6456. )));
  6457. $result = $this->Form->password('password');
  6458. $this->assertTags($result, array('input' => array(
  6459. 'name' => 'password', 'type' => 'password', 'id' => 'ContactPassword'
  6460. )));
  6461. $this->assertNotRegExp('/<input[^<>]+[^id|name|type|value]=[^<>]*>$/', $result);
  6462. $result = $this->Form->text('user_form');
  6463. $this->assertTags($result, array('input' => array(
  6464. 'name' => 'user_form', 'type' => 'text', 'id' => 'ContactUserForm'
  6465. )));
  6466. }
  6467. /**
  6468. * test get form, and inputs when the model param is false
  6469. *
  6470. * @return void
  6471. */
  6472. public function testGetFormWithFalseModel() {
  6473. $encoding = strtolower(Configure::read('App.encoding'));
  6474. $this->Form->request['controller'] = 'contact_test';
  6475. $result = $this->Form->create(false, array('type' => 'get', 'url' => array('controller' => 'contact_test')));
  6476. $expected = array('form' => array(
  6477. 'id' => 'addForm', 'method' => 'get', 'action' => '/contact_test/add',
  6478. 'accept-charset' => $encoding
  6479. ));
  6480. $this->assertTags($result, $expected);
  6481. $result = $this->Form->text('reason');
  6482. $expected = array(
  6483. 'input' => array('type' => 'text', 'name' => 'reason', 'id' => 'reason')
  6484. );
  6485. $this->assertTags($result, $expected);
  6486. }
  6487. /**
  6488. * test that datetime() works with GET style forms.
  6489. *
  6490. * @return void
  6491. */
  6492. public function testDateTimeWithGetForms() {
  6493. extract($this->dateRegex);
  6494. $this->Form->create('Contact', array('type' => 'get'));
  6495. $result = $this->Form->datetime('created');
  6496. $this->assertRegExp('/name="created\[year\]"/', $result, 'year name attribute is wrong.');
  6497. $this->assertRegExp('/name="created\[month\]"/', $result, 'month name attribute is wrong.');
  6498. $this->assertRegExp('/name="created\[day\]"/', $result, 'day name attribute is wrong.');
  6499. $this->assertRegExp('/name="created\[hour\]"/', $result, 'hour name attribute is wrong.');
  6500. $this->assertRegExp('/name="created\[min\]"/', $result, 'min name attribute is wrong.');
  6501. $this->assertRegExp('/name="created\[meridian\]"/', $result, 'meridian name attribute is wrong.');
  6502. }
  6503. /**
  6504. * testEditFormWithData method
  6505. *
  6506. * test auto populating form elements from submitted data.
  6507. *
  6508. * @return void
  6509. */
  6510. public function testEditFormWithData() {
  6511. $this->Form->request->data = array('Person' => array(
  6512. 'id' => 1,
  6513. 'first_name' => 'Nate',
  6514. 'last_name' => 'Abele',
  6515. 'email' => 'nate@example.com'
  6516. ));
  6517. $this->Form->request->addParams(array(
  6518. 'models' => array('Person'),
  6519. 'controller' => 'people',
  6520. 'action' => 'add'
  6521. ));
  6522. $options = array(1 => 'Nate', 2 => 'Garrett', 3 => 'Larry');
  6523. $this->Form->create();
  6524. $result = $this->Form->select('People.People', $options, array('multiple' => true));
  6525. $expected = array(
  6526. 'input' => array('type' => 'hidden', 'name' => 'data[People][People]', 'value' => '', 'id' => 'PeoplePeople_'),
  6527. 'select' => array(
  6528. 'name' => 'data[People][People][]', 'multiple' => 'multiple', 'id' => 'PeoplePeople'
  6529. ),
  6530. array('option' => array('value' => 1)), 'Nate', '/option',
  6531. array('option' => array('value' => 2)), 'Garrett', '/option',
  6532. array('option' => array('value' => 3)), 'Larry', '/option',
  6533. '/select'
  6534. );
  6535. $this->assertTags($result, $expected);
  6536. }
  6537. /**
  6538. * Test that required fields are created for various types of validation.
  6539. *
  6540. * @return void
  6541. */
  6542. public function testFormInputRequiredDetection() {
  6543. $this->Form->create('Contact');
  6544. $result = $this->Form->input('Contact.non_existing');
  6545. $expected = array(
  6546. 'div' => array('class' => 'input text'),
  6547. 'label' => array('for' => 'ContactNonExisting'),
  6548. 'Non Existing',
  6549. '/label',
  6550. 'input' => array(
  6551. 'type' => 'text', 'name' => 'data[Contact][non_existing]',
  6552. 'id' => 'ContactNonExisting'
  6553. ),
  6554. '/div'
  6555. );
  6556. $this->assertTags($result, $expected);
  6557. $result = $this->Form->input('Contact.imrequired');
  6558. $expected = array(
  6559. 'div' => array('class' => 'input text required'),
  6560. 'label' => array('for' => 'ContactImrequired'),
  6561. 'Imrequired',
  6562. '/label',
  6563. 'input' => array(
  6564. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  6565. 'id' => 'ContactImrequired',
  6566. 'required' => 'required'
  6567. ),
  6568. '/div'
  6569. );
  6570. $this->assertTags($result, $expected);
  6571. $result = $this->Form->input('Contact.imalsorequired');
  6572. $expected = array(
  6573. 'div' => array('class' => 'input text required'),
  6574. 'label' => array('for' => 'ContactImalsorequired'),
  6575. 'Imalsorequired',
  6576. '/label',
  6577. 'input' => array(
  6578. 'type' => 'text', 'name' => 'data[Contact][imalsorequired]',
  6579. 'id' => 'ContactImalsorequired',
  6580. 'required' => 'required'
  6581. ),
  6582. '/div'
  6583. );
  6584. $this->assertTags($result, $expected);
  6585. $result = $this->Form->input('Contact.imrequiredtoo');
  6586. $expected = array(
  6587. 'div' => array('class' => 'input text required'),
  6588. 'label' => array('for' => 'ContactImrequiredtoo'),
  6589. 'Imrequiredtoo',
  6590. '/label',
  6591. 'input' => array(
  6592. 'type' => 'text', 'name' => 'data[Contact][imrequiredtoo]',
  6593. 'id' => 'ContactImrequiredtoo',
  6594. 'required' => 'required'
  6595. ),
  6596. '/div'
  6597. );
  6598. $this->assertTags($result, $expected);
  6599. $result = $this->Form->input('Contact.required_one');
  6600. $expected = array(
  6601. 'div' => array('class' => 'input text required'),
  6602. 'label' => array('for' => 'ContactRequiredOne'),
  6603. 'Required One',
  6604. '/label',
  6605. 'input' => array(
  6606. 'type' => 'text', 'name' => 'data[Contact][required_one]',
  6607. 'id' => 'ContactRequiredOne',
  6608. 'required' => 'required'
  6609. ),
  6610. '/div'
  6611. );
  6612. $this->assertTags($result, $expected);
  6613. $result = $this->Form->input('Contact.string_required');
  6614. $expected = array(
  6615. 'div' => array('class' => 'input text required'),
  6616. 'label' => array('for' => 'ContactStringRequired'),
  6617. 'String Required',
  6618. '/label',
  6619. 'input' => array(
  6620. 'type' => 'text', 'name' => 'data[Contact][string_required]',
  6621. 'id' => 'ContactStringRequired',
  6622. 'required' => 'required'
  6623. ),
  6624. '/div'
  6625. );
  6626. $this->assertTags($result, $expected);
  6627. $result = $this->Form->input('Contact.imnotrequired');
  6628. $expected = array(
  6629. 'div' => array('class' => 'input text'),
  6630. 'label' => array('for' => 'ContactImnotrequired'),
  6631. 'Imnotrequired',
  6632. '/label',
  6633. 'input' => array(
  6634. 'type' => 'text', 'name' => 'data[Contact][imnotrequired]',
  6635. 'id' => 'ContactImnotrequired'
  6636. ),
  6637. '/div'
  6638. );
  6639. $this->assertTags($result, $expected);
  6640. $result = $this->Form->input('Contact.imalsonotrequired');
  6641. $expected = array(
  6642. 'div' => array('class' => 'input text'),
  6643. 'label' => array('for' => 'ContactImalsonotrequired'),
  6644. 'Imalsonotrequired',
  6645. '/label',
  6646. 'input' => array(
  6647. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired]',
  6648. 'id' => 'ContactImalsonotrequired'
  6649. ),
  6650. '/div'
  6651. );
  6652. $this->assertTags($result, $expected);
  6653. $result = $this->Form->input('Contact.imalsonotrequired2');
  6654. $expected = array(
  6655. 'div' => array('class' => 'input text'),
  6656. 'label' => array('for' => 'ContactImalsonotrequired2'),
  6657. 'Imalsonotrequired2',
  6658. '/label',
  6659. 'input' => array(
  6660. 'type' => 'text', 'name' => 'data[Contact][imalsonotrequired2]',
  6661. 'id' => 'ContactImalsonotrequired2'
  6662. ),
  6663. '/div'
  6664. );
  6665. $this->assertTags($result, $expected);
  6666. $result = $this->Form->input('Contact.imnotrequiredeither');
  6667. $expected = array(
  6668. 'div' => array('class' => 'input text'),
  6669. 'label' => array('for' => 'ContactImnotrequiredeither'),
  6670. 'Imnotrequiredeither',
  6671. '/label',
  6672. 'input' => array(
  6673. 'type' => 'text', 'name' => 'data[Contact][imnotrequiredeither]',
  6674. 'id' => 'ContactImnotrequiredeither'
  6675. ),
  6676. '/div'
  6677. );
  6678. $this->assertTags($result, $expected);
  6679. $result = $this->Form->input('Contact.iamrequiredalways');
  6680. $expected = array(
  6681. 'div' => array('class' => 'input text required'),
  6682. 'label' => array('for' => 'ContactIamrequiredalways'),
  6683. 'Iamrequiredalways',
  6684. '/label',
  6685. 'input' => array(
  6686. 'type' => 'text', 'name' => 'data[Contact][iamrequiredalways]',
  6687. 'id' => 'ContactIamrequiredalways',
  6688. 'required' => 'required'
  6689. ),
  6690. '/div'
  6691. );
  6692. $this->assertTags($result, $expected);
  6693. $result = $this->Form->input('Contact.boolean_field', array('type' => 'checkbox'));
  6694. $expected = array(
  6695. 'div' => array('class' => 'input checkbox required'),
  6696. array('input' => array(
  6697. 'type' => 'hidden',
  6698. 'name' => 'data[Contact][boolean_field]',
  6699. 'id' => 'ContactBooleanField_',
  6700. 'value' => '0'
  6701. )),
  6702. array('input' => array(
  6703. 'type' => 'checkbox',
  6704. 'name' => 'data[Contact][boolean_field]',
  6705. 'value' => '1',
  6706. 'id' => 'ContactBooleanField'
  6707. )),
  6708. 'label' => array('for' => 'ContactBooleanField'),
  6709. 'Boolean Field',
  6710. '/label',
  6711. '/div'
  6712. );
  6713. $this->assertTags($result, $expected);
  6714. }
  6715. /**
  6716. * testFormMagicInput method
  6717. *
  6718. * @return void
  6719. */
  6720. public function testFormMagicInput() {
  6721. $encoding = strtolower(Configure::read('App.encoding'));
  6722. $result = $this->Form->create('Contact');
  6723. $expected = array(
  6724. 'form' => array(
  6725. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6726. 'accept-charset' => $encoding
  6727. ),
  6728. 'div' => array('style' => 'display:none;'),
  6729. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6730. '/div'
  6731. );
  6732. $this->assertTags($result, $expected);
  6733. $result = $this->Form->input('name');
  6734. $expected = array(
  6735. 'div' => array('class' => 'input text'),
  6736. 'label' => array('for' => 'ContactName'),
  6737. 'Name',
  6738. '/label',
  6739. 'input' => array(
  6740. 'type' => 'text', 'name' => 'data[Contact][name]',
  6741. 'id' => 'ContactName', 'maxlength' => '255'
  6742. ),
  6743. '/div'
  6744. );
  6745. $this->assertTags($result, $expected);
  6746. $result = $this->Form->input('non_existing_field_in_contact_model');
  6747. $expected = array(
  6748. 'div' => array('class' => 'input text'),
  6749. 'label' => array('for' => 'ContactNonExistingFieldInContactModel'),
  6750. 'Non Existing Field In Contact Model',
  6751. '/label',
  6752. 'input' => array(
  6753. 'type' => 'text', 'name' => 'data[Contact][non_existing_field_in_contact_model]',
  6754. 'id' => 'ContactNonExistingFieldInContactModel'
  6755. ),
  6756. '/div'
  6757. );
  6758. $this->assertTags($result, $expected);
  6759. $result = $this->Form->input('Address.street');
  6760. $expected = array(
  6761. 'div' => array('class' => 'input text'),
  6762. 'label' => array('for' => 'AddressStreet'),
  6763. 'Street',
  6764. '/label',
  6765. 'input' => array(
  6766. 'type' => 'text', 'name' => 'data[Address][street]',
  6767. 'id' => 'AddressStreet'
  6768. ),
  6769. '/div'
  6770. );
  6771. $this->assertTags($result, $expected);
  6772. $result = $this->Form->input('Address.non_existing_field_in_model');
  6773. $expected = array(
  6774. 'div' => array('class' => 'input text'),
  6775. 'label' => array('for' => 'AddressNonExistingFieldInModel'),
  6776. 'Non Existing Field In Model',
  6777. '/label',
  6778. 'input' => array(
  6779. 'type' => 'text', 'name' => 'data[Address][non_existing_field_in_model]',
  6780. 'id' => 'AddressNonExistingFieldInModel'
  6781. ),
  6782. '/div'
  6783. );
  6784. $this->assertTags($result, $expected);
  6785. $result = $this->Form->input('name', array('div' => false));
  6786. $expected = array(
  6787. 'label' => array('for' => 'ContactName'),
  6788. 'Name',
  6789. '/label',
  6790. 'input' => array(
  6791. 'type' => 'text', 'name' => 'data[Contact][name]',
  6792. 'id' => 'ContactName', 'maxlength' => '255'
  6793. )
  6794. );
  6795. $this->assertTags($result, $expected);
  6796. extract($this->dateRegex);
  6797. $now = strtotime('now');
  6798. $result = $this->Form->input('Contact.published', array('div' => false));
  6799. $expected = array(
  6800. 'label' => array('for' => 'ContactPublishedMonth'),
  6801. 'Published',
  6802. '/label',
  6803. array('select' => array(
  6804. 'name' => 'data[Contact][published][month]', 'id' => 'ContactPublishedMonth'
  6805. )),
  6806. $monthsRegex,
  6807. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6808. date('F', $now),
  6809. '/option',
  6810. '*/select',
  6811. '-',
  6812. array('select' => array(
  6813. 'name' => 'data[Contact][published][day]', 'id' => 'ContactPublishedDay'
  6814. )),
  6815. $daysRegex,
  6816. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6817. date('j', $now),
  6818. '/option',
  6819. '*/select',
  6820. '-',
  6821. array('select' => array(
  6822. 'name' => 'data[Contact][published][year]', 'id' => 'ContactPublishedYear'
  6823. )),
  6824. $yearsRegex,
  6825. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6826. date('Y', $now),
  6827. '*/select'
  6828. );
  6829. $this->assertTags($result, $expected);
  6830. $result = $this->Form->input('Contact.updated', array('div' => false));
  6831. $expected = array(
  6832. 'label' => array('for' => 'ContactUpdatedMonth'),
  6833. 'Updated',
  6834. '/label',
  6835. array('select' => array(
  6836. 'name' => 'data[Contact][updated][month]', 'id' => 'ContactUpdatedMonth'
  6837. )),
  6838. $monthsRegex,
  6839. array('option' => array('value' => date('m', $now), 'selected' => 'selected')),
  6840. date('F', $now),
  6841. '/option',
  6842. '*/select',
  6843. '-',
  6844. array('select' => array(
  6845. 'name' => 'data[Contact][updated][day]', 'id' => 'ContactUpdatedDay'
  6846. )),
  6847. $daysRegex,
  6848. array('option' => array('value' => date('d', $now), 'selected' => 'selected')),
  6849. date('j', $now),
  6850. '/option',
  6851. '*/select',
  6852. '-',
  6853. array('select' => array(
  6854. 'name' => 'data[Contact][updated][year]', 'id' => 'ContactUpdatedYear'
  6855. )),
  6856. $yearsRegex,
  6857. array('option' => array('value' => date('Y', $now), 'selected' => 'selected')),
  6858. date('Y', $now),
  6859. '*/select'
  6860. );
  6861. $this->assertTags($result, $expected);
  6862. $result = $this->Form->input('UserForm.stuff');
  6863. $expected = array(
  6864. 'div' => array('class' => 'input text'),
  6865. 'label' => array('for' => 'UserFormStuff'),
  6866. 'Stuff',
  6867. '/label',
  6868. 'input' => array(
  6869. 'type' => 'text', 'name' => 'data[UserForm][stuff]',
  6870. 'id' => 'UserFormStuff', 'maxlength' => 10
  6871. ),
  6872. '/div'
  6873. );
  6874. $this->assertTags($result, $expected);
  6875. }
  6876. /**
  6877. * testForMagicInputNonExistingNorValidated method
  6878. *
  6879. * @return void
  6880. */
  6881. public function testForMagicInputNonExistingNorValidated() {
  6882. $encoding = strtolower(Configure::read('App.encoding'));
  6883. $result = $this->Form->create('Contact');
  6884. $expected = array(
  6885. 'form' => array(
  6886. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6887. 'accept-charset' => $encoding
  6888. ),
  6889. 'div' => array('style' => 'display:none;'),
  6890. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6891. '/div'
  6892. );
  6893. $this->assertTags($result, $expected);
  6894. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6895. $expected = array(
  6896. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6897. 'Non Existing Nor Validated',
  6898. '/label',
  6899. 'input' => array(
  6900. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6901. 'id' => 'ContactNonExistingNorValidated'
  6902. )
  6903. );
  6904. $this->assertTags($result, $expected);
  6905. $result = $this->Form->input('Contact.non_existing_nor_validated', array(
  6906. 'div' => false, 'value' => 'my value'
  6907. ));
  6908. $expected = array(
  6909. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6910. 'Non Existing Nor Validated',
  6911. '/label',
  6912. 'input' => array(
  6913. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6914. 'value' => 'my value', 'id' => 'ContactNonExistingNorValidated'
  6915. )
  6916. );
  6917. $this->assertTags($result, $expected);
  6918. $this->Form->request->data = array(
  6919. 'Contact' => array('non_existing_nor_validated' => 'CakePHP magic'
  6920. ));
  6921. $result = $this->Form->input('Contact.non_existing_nor_validated', array('div' => false));
  6922. $expected = array(
  6923. 'label' => array('for' => 'ContactNonExistingNorValidated'),
  6924. 'Non Existing Nor Validated',
  6925. '/label',
  6926. 'input' => array(
  6927. 'type' => 'text', 'name' => 'data[Contact][non_existing_nor_validated]',
  6928. 'value' => 'CakePHP magic', 'id' => 'ContactNonExistingNorValidated'
  6929. )
  6930. );
  6931. $this->assertTags($result, $expected);
  6932. }
  6933. /**
  6934. * testFormMagicInputLabel method
  6935. *
  6936. * @return void
  6937. */
  6938. public function testFormMagicInputLabel() {
  6939. $encoding = strtolower(Configure::read('App.encoding'));
  6940. $result = $this->Form->create('Contact');
  6941. $expected = array(
  6942. 'form' => array(
  6943. 'id' => 'ContactAddForm', 'method' => 'post', 'action' => '/contacts/add',
  6944. 'accept-charset' => $encoding
  6945. ),
  6946. 'div' => array('style' => 'display:none;'),
  6947. 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'),
  6948. '/div'
  6949. );
  6950. $this->assertTags($result, $expected);
  6951. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => false));
  6952. $this->assertTags($result, array('input' => array(
  6953. 'name' => 'data[Contact][name]', 'type' => 'text',
  6954. 'id' => 'ContactName', 'maxlength' => '255')
  6955. ));
  6956. $result = $this->Form->input('Contact.name', array('div' => false, 'label' => 'My label'));
  6957. $expected = array(
  6958. 'label' => array('for' => 'ContactName'),
  6959. 'My label',
  6960. '/label',
  6961. 'input' => array(
  6962. 'type' => 'text', 'name' => 'data[Contact][name]',
  6963. 'id' => 'ContactName', 'maxlength' => '255'
  6964. )
  6965. );
  6966. $this->assertTags($result, $expected);
  6967. $result = $this->Form->input('Contact.name', array(
  6968. 'div' => false, 'label' => array('class' => 'mandatory')
  6969. ));
  6970. $expected = array(
  6971. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6972. 'Name',
  6973. '/label',
  6974. 'input' => array(
  6975. 'type' => 'text', 'name' => 'data[Contact][name]',
  6976. 'id' => 'ContactName', 'maxlength' => '255'
  6977. )
  6978. );
  6979. $this->assertTags($result, $expected);
  6980. $result = $this->Form->input('Contact.name', array(
  6981. 'div' => false, 'label' => array('class' => 'mandatory', 'text' => 'My label')
  6982. ));
  6983. $expected = array(
  6984. 'label' => array('for' => 'ContactName', 'class' => 'mandatory'),
  6985. 'My label',
  6986. '/label',
  6987. 'input' => array(
  6988. 'type' => 'text', 'name' => 'data[Contact][name]',
  6989. 'id' => 'ContactName', 'maxlength' => '255'
  6990. )
  6991. );
  6992. $this->assertTags($result, $expected);
  6993. $result = $this->Form->input('Contact.name', array(
  6994. 'div' => false, 'id' => 'my_id', 'label' => array('for' => 'my_id')
  6995. ));
  6996. $expected = array(
  6997. 'label' => array('for' => 'my_id'),
  6998. 'Name',
  6999. '/label',
  7000. 'input' => array(
  7001. 'type' => 'text', 'name' => 'data[Contact][name]',
  7002. 'id' => 'my_id', 'maxlength' => '255'
  7003. )
  7004. );
  7005. $this->assertTags($result, $expected);
  7006. $result = $this->Form->input('1.id');
  7007. $this->assertTags($result, array('input' => array(
  7008. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7009. 'id' => 'Contact1Id'
  7010. )));
  7011. $result = $this->Form->input("1.name");
  7012. $expected = array(
  7013. 'div' => array('class' => 'input text'),
  7014. 'label' => array('for' => 'Contact1Name'),
  7015. 'Name',
  7016. '/label',
  7017. 'input' => array(
  7018. 'type' => 'text', 'name' => 'data[Contact][1][name]',
  7019. 'id' => 'Contact1Name', 'maxlength' => '255'
  7020. ),
  7021. '/div'
  7022. );
  7023. $this->assertTags($result, $expected);
  7024. $result = $this->Form->input('Contact.1.id');
  7025. $this->assertTags($result, array(
  7026. 'input' => array(
  7027. 'type' => 'hidden', 'name' => 'data[Contact][1][id]',
  7028. 'id' => 'Contact1Id'
  7029. )
  7030. ));
  7031. $result = $this->Form->input("Model.1.name");
  7032. $expected = array(
  7033. 'div' => array('class' => 'input text'),
  7034. 'label' => array('for' => 'Model1Name'),
  7035. 'Name',
  7036. '/label',
  7037. 'input' => array(
  7038. 'type' => 'text', 'name' => 'data[Model][1][name]',
  7039. 'id' => 'Model1Name'
  7040. ),
  7041. '/div'
  7042. );
  7043. $this->assertTags($result, $expected);
  7044. }
  7045. /**
  7046. * testFormEnd method
  7047. *
  7048. * @return void
  7049. */
  7050. public function testFormEnd() {
  7051. $this->assertEquals('</form>', $this->Form->end());
  7052. $result = $this->Form->end('');
  7053. $expected = array(
  7054. 'div' => array('class' => 'submit'),
  7055. 'input' => array('type' => 'submit', 'value' => ''),
  7056. '/div',
  7057. '/form'
  7058. );
  7059. $this->assertTags($result, $expected);
  7060. $result = $this->Form->end(array('label' => ''));
  7061. $expected = array(
  7062. 'div' => array('class' => 'submit'),
  7063. 'input' => array('type' => 'submit', 'value' => ''),
  7064. '/div',
  7065. '/form'
  7066. );
  7067. $this->assertTags($result, $expected);
  7068. $result = $this->Form->end('save');
  7069. $expected = array(
  7070. 'div' => array('class' => 'submit'),
  7071. 'input' => array('type' => 'submit', 'value' => 'save'),
  7072. '/div',
  7073. '/form'
  7074. );
  7075. $this->assertTags($result, $expected);
  7076. $result = $this->Form->end(array('label' => 'save'));
  7077. $expected = array(
  7078. 'div' => array('class' => 'submit'),
  7079. 'input' => array('type' => 'submit', 'value' => 'save'),
  7080. '/div',
  7081. '/form'
  7082. );
  7083. $this->assertTags($result, $expected);
  7084. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever'));
  7085. $expected = array(
  7086. 'div' => array('class' => 'submit'),
  7087. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7088. '/div',
  7089. '/form'
  7090. );
  7091. $this->assertTags($result, $expected);
  7092. $result = $this->Form->end(array('name' => 'Whatever'));
  7093. $expected = array(
  7094. 'div' => array('class' => 'submit'),
  7095. 'input' => array('type' => 'submit', 'value' => 'Submit', 'name' => 'Whatever'),
  7096. '/div',
  7097. '/form'
  7098. );
  7099. $this->assertTags($result, $expected);
  7100. $result = $this->Form->end(array('label' => 'save', 'name' => 'Whatever', 'div' => 'good'));
  7101. $expected = array(
  7102. 'div' => array('class' => 'good'),
  7103. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7104. '/div',
  7105. '/form'
  7106. );
  7107. $this->assertTags($result, $expected);
  7108. $result = $this->Form->end(array(
  7109. 'label' => 'save', 'name' => 'Whatever', 'div' => array('class' => 'good')
  7110. ));
  7111. $expected = array(
  7112. 'div' => array('class' => 'good'),
  7113. 'input' => array('type' => 'submit', 'value' => 'save', 'name' => 'Whatever'),
  7114. '/div',
  7115. '/form'
  7116. );
  7117. $this->assertTags($result, $expected);
  7118. }
  7119. /**
  7120. * testMultipleFormWithIdFields method
  7121. *
  7122. * @return void
  7123. */
  7124. public function testMultipleFormWithIdFields() {
  7125. $this->Form->create('UserForm');
  7126. $result = $this->Form->input('id');
  7127. $this->assertTags($result, array('input' => array(
  7128. 'type' => 'hidden', 'name' => 'data[UserForm][id]', 'id' => 'UserFormId'
  7129. )));
  7130. $result = $this->Form->input('ValidateItem.id');
  7131. $this->assertTags($result, array('input' => array(
  7132. 'type' => 'hidden', 'name' => 'data[ValidateItem][id]',
  7133. 'id' => 'ValidateItemId'
  7134. )));
  7135. $result = $this->Form->input('ValidateUser.id');
  7136. $this->assertTags($result, array('input' => array(
  7137. 'type' => 'hidden', 'name' => 'data[ValidateUser][id]',
  7138. 'id' => 'ValidateUserId'
  7139. )));
  7140. }
  7141. /**
  7142. * testDbLessModel method
  7143. *
  7144. * @return void
  7145. */
  7146. public function testDbLessModel() {
  7147. $this->Form->create('TestMail');
  7148. $result = $this->Form->input('name');
  7149. $expected = array(
  7150. 'div' => array('class' => 'input text'),
  7151. 'label' => array('for' => 'TestMailName'),
  7152. 'Name',
  7153. '/label',
  7154. 'input' => array(
  7155. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7156. 'id' => 'TestMailName'
  7157. ),
  7158. '/div'
  7159. );
  7160. $this->assertTags($result, $expected);
  7161. ClassRegistry::init('TestMail');
  7162. $this->Form->create('TestMail');
  7163. $result = $this->Form->input('name');
  7164. $expected = array(
  7165. 'div' => array('class' => 'input text'),
  7166. 'label' => array('for' => 'TestMailName'),
  7167. 'Name',
  7168. '/label',
  7169. 'input' => array(
  7170. 'name' => 'data[TestMail][name]', 'type' => 'text',
  7171. 'id' => 'TestMailName'
  7172. ),
  7173. '/div'
  7174. );
  7175. $this->assertTags($result, $expected);
  7176. }
  7177. /**
  7178. * testBrokenness method
  7179. *
  7180. * @return void
  7181. */
  7182. public function testBrokenness() {
  7183. /*
  7184. * #4 This test has two parents and four children. By default (as of r7117) both
  7185. * parents are show but the first parent is missing a child. This is the inconsistency
  7186. * in the default behaviour - one parent has all children, the other does not - dependent
  7187. * on the data values.
  7188. */
  7189. $result = $this->Form->select('Model.field', array(
  7190. 'Fred' => array(
  7191. 'freds_son_1' => 'Fred',
  7192. 'freds_son_2' => 'Freddie'
  7193. ),
  7194. 'Bert' => array(
  7195. 'berts_son_1' => 'Albert',
  7196. 'berts_son_2' => 'Bertie')
  7197. ),
  7198. array('showParents' => true, 'empty' => false)
  7199. );
  7200. $expected = array(
  7201. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7202. array('optgroup' => array('label' => 'Fred')),
  7203. array('option' => array('value' => 'freds_son_1')),
  7204. 'Fred',
  7205. '/option',
  7206. array('option' => array('value' => 'freds_son_2')),
  7207. 'Freddie',
  7208. '/option',
  7209. '/optgroup',
  7210. array('optgroup' => array('label' => 'Bert')),
  7211. array('option' => array('value' => 'berts_son_1')),
  7212. 'Albert',
  7213. '/option',
  7214. array('option' => array('value' => 'berts_son_2')),
  7215. 'Bertie',
  7216. '/option',
  7217. '/optgroup',
  7218. '/select'
  7219. );
  7220. $this->assertTags($result, $expected);
  7221. /*
  7222. * #2 This is structurally identical to the test above (#1) - only the parent name has
  7223. * changed, so we should expect the same select list data, just with a different name
  7224. * for the parent. As of #7117, this test fails because option 3 => 'Three' disappears.
  7225. * This is where data corruption can occur, because when a select value is missing from
  7226. * a list a form will substitute the first value in the list - without the user knowing.
  7227. * If the optgroup name 'Parent' (above) is updated to 'Three' (below), this should not
  7228. * affect the availability of 3 => 'Three' as a valid option.
  7229. */
  7230. $options = array(1 => 'One', 2 => 'Two', 'Three' => array(
  7231. 3 => 'Three', 4 => 'Four', 5 => 'Five'
  7232. ));
  7233. $result = $this->Form->select(
  7234. 'Model.field', $options, array('showParents' => true, 'empty' => false)
  7235. );
  7236. $expected = array(
  7237. 'select' => array('name' => 'data[Model][field]', 'id' => 'ModelField'),
  7238. array('option' => array('value' => 1)),
  7239. 'One',
  7240. '/option',
  7241. array('option' => array('value' => 2)),
  7242. 'Two',
  7243. '/option',
  7244. array('optgroup' => array('label' => 'Three')),
  7245. array('option' => array('value' => 3)),
  7246. 'Three',
  7247. '/option',
  7248. array('option' => array('value' => 4)),
  7249. 'Four',
  7250. '/option',
  7251. array('option' => array('value' => 5)),
  7252. 'Five',
  7253. '/option',
  7254. '/optgroup',
  7255. '/select'
  7256. );
  7257. $this->assertTags($result, $expected);
  7258. }
  7259. /**
  7260. * Test the generation of fields for a multi record form.
  7261. *
  7262. * @return void
  7263. */
  7264. public function testMultiRecordForm() {
  7265. $this->Form->create('ValidateProfile');
  7266. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['name'] = 'Value';
  7267. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.name');
  7268. $expected = array(
  7269. 'div' => array('class' => 'input textarea'),
  7270. 'label' => array('for' => 'ValidateProfile1ValidateItem2Name'),
  7271. 'Name',
  7272. '/label',
  7273. 'textarea' => array(
  7274. 'id' => 'ValidateProfile1ValidateItem2Name',
  7275. 'name' => 'data[ValidateProfile][1][ValidateItem][2][name]',
  7276. 'cols' => 30,
  7277. 'rows' => 6
  7278. ),
  7279. 'Value',
  7280. '/textarea',
  7281. '/div'
  7282. );
  7283. $this->assertTags($result, $expected);
  7284. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.created',array('empty' => true));
  7285. $expected = array(
  7286. 'div' => array('class' => 'input date'),
  7287. 'label' => array('for' => 'ValidateProfile1ValidateItem2CreatedMonth'),
  7288. 'Created',
  7289. '/label',
  7290. array('select' => array(
  7291. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][month]',
  7292. 'id' => 'ValidateProfile1ValidateItem2CreatedMonth'
  7293. )
  7294. ),
  7295. array('option' => array('value' => '')), '/option',
  7296. $this->dateRegex['monthsRegex'],
  7297. '/select', '-',
  7298. array('select' => array(
  7299. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][day]',
  7300. 'id' => 'ValidateProfile1ValidateItem2CreatedDay'
  7301. )
  7302. ),
  7303. array('option' => array('value' => '')), '/option',
  7304. $this->dateRegex['daysRegex'],
  7305. '/select', '-',
  7306. array('select' => array(
  7307. 'name' => 'data[ValidateProfile][1][ValidateItem][2][created][year]',
  7308. 'id' => 'ValidateProfile1ValidateItem2CreatedYear'
  7309. )
  7310. ),
  7311. array('option' => array('value' => '')), '/option',
  7312. $this->dateRegex['yearsRegex'],
  7313. '/select',
  7314. '/div'
  7315. );
  7316. $this->assertTags($result, $expected);
  7317. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7318. $ValidateProfile->validationErrors[1]['ValidateItem'][2]['profile_id'] = 'Error';
  7319. $this->Form->request->data['ValidateProfile'][1]['ValidateItem'][2]['profile_id'] = '1';
  7320. $result = $this->Form->input('ValidateProfile.1.ValidateItem.2.profile_id');
  7321. $expected = array(
  7322. 'div' => array('class' => 'input select error'),
  7323. 'label' => array('for' => 'ValidateProfile1ValidateItem2ProfileId'),
  7324. 'Profile',
  7325. '/label',
  7326. 'select' => array(
  7327. 'name' => 'data[ValidateProfile][1][ValidateItem][2][profile_id]',
  7328. 'id' => 'ValidateProfile1ValidateItem2ProfileId',
  7329. 'class' => 'form-error'
  7330. ),
  7331. '/select',
  7332. array('div' => array('class' => 'error-message')),
  7333. 'Error',
  7334. '/div',
  7335. '/div'
  7336. );
  7337. $this->assertTags($result, $expected);
  7338. }
  7339. /**
  7340. * test the correct display of multi-record form validation errors.
  7341. *
  7342. * @return void
  7343. */
  7344. public function testMultiRecordFormValidationErrors() {
  7345. $this->Form->create('ValidateProfile');
  7346. $ValidateProfile = ClassRegistry::getObject('ValidateProfile');
  7347. $ValidateProfile->validationErrors[2]['ValidateItem'][1]['name'] = array('Error in field name');
  7348. $result = $this->Form->error('ValidateProfile.2.ValidateItem.1.name');
  7349. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field name', '/div'));
  7350. $ValidateProfile->validationErrors[2]['city'] = array('Error in field city');
  7351. $result = $this->Form->error('ValidateProfile.2.city');
  7352. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7353. $result = $this->Form->error('2.city');
  7354. $this->assertTags($result, array('div' => array('class' => 'error-message'), 'Error in field city', '/div'));
  7355. }
  7356. /**
  7357. * tests the ability to change the order of the form input placeholder "input", "label", "before", "between", "after", "error"
  7358. *
  7359. * @return void
  7360. */
  7361. public function testInputTemplate() {
  7362. $result = $this->Form->input('Contact.email', array(
  7363. 'type' => 'text', 'format' => array('input')
  7364. ));
  7365. $expected = array(
  7366. 'div' => array('class' => 'input text'),
  7367. 'input' => array(
  7368. 'type' => 'text', 'name' => 'data[Contact][email]',
  7369. 'id' => 'ContactEmail'
  7370. ),
  7371. '/div'
  7372. );
  7373. $this->assertTags($result, $expected);
  7374. $result = $this->Form->input('Contact.email', array(
  7375. 'type' => 'text', 'format' => array('input', 'label'),
  7376. 'label' => '<em>Email (required)</em>'
  7377. ));
  7378. $expected = array(
  7379. 'div' => array('class' => 'input text'),
  7380. array('input' => array(
  7381. 'type' => 'text', 'name' => 'data[Contact][email]',
  7382. 'id' => 'ContactEmail'
  7383. )),
  7384. 'label' => array('for' => 'ContactEmail'),
  7385. 'em' => array(),
  7386. 'Email (required)',
  7387. '/em',
  7388. '/label',
  7389. '/div'
  7390. );
  7391. $this->assertTags($result, $expected);
  7392. $result = $this->Form->input('Contact.email', array(
  7393. 'type' => 'text', 'format' => array('input', 'between', 'label', 'after'),
  7394. 'between' => '<div>Something in the middle</div>',
  7395. 'after' => '<span>Some text at the end</span>'
  7396. ));
  7397. $expected = array(
  7398. 'div' => array('class' => 'input text'),
  7399. array('input' => array(
  7400. 'type' => 'text', 'name' => 'data[Contact][email]',
  7401. 'id' => 'ContactEmail'
  7402. )),
  7403. array('div' => array()),
  7404. 'Something in the middle',
  7405. '/div',
  7406. 'label' => array('for' => 'ContactEmail'),
  7407. 'Email',
  7408. '/label',
  7409. 'span' => array(),
  7410. 'Some text at the end',
  7411. '/span',
  7412. '/div'
  7413. );
  7414. $this->assertTags($result, $expected);
  7415. $result = $this->Form->input('Contact.method', array(
  7416. 'type' => 'radio',
  7417. 'options' => array('email' => 'Email', 'pigeon' => 'Pigeon'),
  7418. 'between' => 'I am between',
  7419. ));
  7420. $expected = array(
  7421. 'div' => array('class' => 'input radio'),
  7422. 'fieldset' => array(),
  7423. 'legend' => array(),
  7424. 'Method',
  7425. '/legend',
  7426. 'I am between',
  7427. 'input' => array(
  7428. 'type' => 'hidden', 'name' => 'data[Contact][method]',
  7429. 'value' => '', 'id' => 'ContactMethod_'
  7430. ),
  7431. array('input' => array(
  7432. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7433. 'value' => 'email', 'id' => 'ContactMethodEmail'
  7434. )),
  7435. array('label' => array('for' => 'ContactMethodEmail')),
  7436. 'Email',
  7437. '/label',
  7438. array('input' => array(
  7439. 'type' => 'radio', 'name' => 'data[Contact][method]',
  7440. 'value' => 'pigeon', 'id' => 'ContactMethodPigeon'
  7441. )),
  7442. array('label' => array('for' => 'ContactMethodPigeon')),
  7443. 'Pigeon',
  7444. '/label',
  7445. '/fieldset',
  7446. '/div',
  7447. );
  7448. $this->assertTags($result, $expected);
  7449. }
  7450. /**
  7451. * test that some html5 inputs + FormHelper::__call() work
  7452. *
  7453. * @return void
  7454. */
  7455. public function testHtml5Inputs() {
  7456. $result = $this->Form->email('User.email');
  7457. $expected = array(
  7458. 'input' => array('type' => 'email', 'name' => 'data[User][email]', 'id' => 'UserEmail')
  7459. );
  7460. $this->assertTags($result, $expected);
  7461. $result = $this->Form->search('User.query');
  7462. $expected = array(
  7463. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery')
  7464. );
  7465. $this->assertTags($result, $expected);
  7466. $result = $this->Form->search('User.query', array('value' => 'test'));
  7467. $expected = array(
  7468. 'input' => array('type' => 'search', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7469. );
  7470. $this->assertTags($result, $expected);
  7471. $result = $this->Form->search('User.query', array('type' => 'text', 'value' => 'test'));
  7472. $expected = array(
  7473. 'input' => array('type' => 'text', 'name' => 'data[User][query]', 'id' => 'UserQuery', 'value' => 'test')
  7474. );
  7475. $this->assertTags($result, $expected);
  7476. $result = $this->Form->input('User.website', array('type' => 'url', 'value' => 'http://domain.tld', 'div' => false, 'label' => false));
  7477. $expected = array(
  7478. 'input' => array('type' => 'url', 'name' => 'data[User][website]', 'id' => 'UserWebsite', 'value' => 'http://domain.tld')
  7479. );
  7480. $this->assertTags($result, $expected);
  7481. }
  7482. /**
  7483. *
  7484. * @expectedException CakeException
  7485. * @return void
  7486. */
  7487. public function testHtml5InputException() {
  7488. $this->Form->email();
  7489. }
  7490. /**
  7491. * Tests that a model can be loaded from the model names passed in the request object
  7492. *
  7493. * @return void
  7494. */
  7495. public function testIntrospectModelFromRequest() {
  7496. $this->loadFixtures('Post');
  7497. App::build(array(
  7498. 'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS)
  7499. ));
  7500. CakePlugin::load('TestPlugin');
  7501. $this->Form->request['models'] = array('TestPluginPost' => array('plugin' => 'TestPlugin', 'className' => 'TestPluginPost'));
  7502. $this->assertFalse(ClassRegistry::isKeySet('TestPluginPost'));
  7503. $this->Form->create('TestPluginPost');
  7504. $this->assertTrue(ClassRegistry::isKeySet('TestPluginPost'));
  7505. $this->assertInstanceOf('TestPluginPost', ClassRegistry::getObject('TestPluginPost'));
  7506. CakePlugin::unload();
  7507. App::build();
  7508. }
  7509. /**
  7510. * Tests that it is possible to set the validation errors directly in the helper for a field
  7511. *
  7512. * @return void
  7513. */
  7514. public function testCustomValidationErrors() {
  7515. $this->Form->validationErrors['Thing']['field'] = 'Badness!';
  7516. $result = $this->Form->error('Thing.field', null, array('wrap' => false));
  7517. $this->assertEquals('Badness!', $result);
  7518. }
  7519. /**
  7520. * Tests that the 'on' key validates as expected on create
  7521. *
  7522. * @return void
  7523. */
  7524. public function testRequiredOnCreate() {
  7525. $this->Form->create('Contact');
  7526. $result = $this->Form->input('Contact.imrequiredonupdate');
  7527. $expected = array(
  7528. 'div' => array('class' => 'input text'),
  7529. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7530. 'Imrequiredonupdate',
  7531. '/label',
  7532. 'input' => array(
  7533. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7534. 'id' => 'ContactImrequiredonupdate'
  7535. ),
  7536. '/div'
  7537. );
  7538. $this->assertTags($result, $expected);
  7539. $result = $this->Form->input('Contact.imrequiredoncreate');
  7540. $expected = array(
  7541. 'div' => array('class' => 'input text required'),
  7542. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7543. 'Imrequiredoncreate',
  7544. '/label',
  7545. 'input' => array(
  7546. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7547. 'id' => 'ContactImrequiredoncreate',
  7548. 'required' => 'required'
  7549. ),
  7550. '/div'
  7551. );
  7552. $this->assertTags($result, $expected);
  7553. $result = $this->Form->input('Contact.imrequiredonboth');
  7554. $expected = array(
  7555. 'div' => array('class' => 'input text required'),
  7556. 'label' => array('for' => 'ContactImrequiredonboth'),
  7557. 'Imrequiredonboth',
  7558. '/label',
  7559. 'input' => array(
  7560. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7561. 'id' => 'ContactImrequiredonboth',
  7562. 'required' => 'required'
  7563. ),
  7564. '/div'
  7565. );
  7566. $this->assertTags($result, $expected);
  7567. $this->Form->inputDefaults(array('required' => false));
  7568. $result = $this->Form->input('Contact.imrequired');
  7569. $expected = array(
  7570. 'div' => array('class' => 'input text required'),
  7571. 'label' => array('for' => 'ContactImrequired'),
  7572. 'Imrequired',
  7573. '/label',
  7574. 'input' => array(
  7575. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7576. 'id' => 'ContactImrequired'
  7577. ),
  7578. '/div'
  7579. );
  7580. $this->assertTags($result, $expected);
  7581. $result = $this->Form->input('Contact.imrequired', array('required' => false));
  7582. $this->assertTags($result, $expected);
  7583. }
  7584. /**
  7585. * Tests that the 'on' key validates as expected on update
  7586. *
  7587. * @return void
  7588. */
  7589. public function testRequiredOnUpdate() {
  7590. $this->Form->request->data['Contact']['id'] = 1;
  7591. $this->Form->create('Contact');
  7592. $result = $this->Form->input('Contact.imrequiredonupdate');
  7593. $expected = array(
  7594. 'div' => array('class' => 'input text required'),
  7595. 'label' => array('for' => 'ContactImrequiredonupdate'),
  7596. 'Imrequiredonupdate',
  7597. '/label',
  7598. 'input' => array(
  7599. 'type' => 'text', 'name' => 'data[Contact][imrequiredonupdate]',
  7600. 'id' => 'ContactImrequiredonupdate',
  7601. 'required' => 'required'
  7602. ),
  7603. '/div'
  7604. );
  7605. $this->assertTags($result, $expected);
  7606. $result = $this->Form->input('Contact.imrequiredoncreate');
  7607. $expected = array(
  7608. 'div' => array('class' => 'input text'),
  7609. 'label' => array('for' => 'ContactImrequiredoncreate'),
  7610. 'Imrequiredoncreate',
  7611. '/label',
  7612. 'input' => array(
  7613. 'type' => 'text', 'name' => 'data[Contact][imrequiredoncreate]',
  7614. 'id' => 'ContactImrequiredoncreate'
  7615. ),
  7616. '/div'
  7617. );
  7618. $this->assertTags($result, $expected);
  7619. $result = $this->Form->input('Contact.imrequiredonboth');
  7620. $expected = array(
  7621. 'div' => array('class' => 'input text required'),
  7622. 'label' => array('for' => 'ContactImrequiredonboth'),
  7623. 'Imrequiredonboth',
  7624. '/label',
  7625. 'input' => array(
  7626. 'type' => 'text', 'name' => 'data[Contact][imrequiredonboth]',
  7627. 'id' => 'ContactImrequiredonboth',
  7628. 'required' => 'required'
  7629. ),
  7630. '/div'
  7631. );
  7632. $this->assertTags($result, $expected);
  7633. $result = $this->Form->input('Contact.imrequired');
  7634. $expected = array(
  7635. 'div' => array('class' => 'input text required'),
  7636. 'label' => array('for' => 'ContactImrequired'),
  7637. 'Imrequired',
  7638. '/label',
  7639. 'input' => array(
  7640. 'type' => 'text', 'name' => 'data[Contact][imrequired]',
  7641. 'id' => 'ContactImrequired',
  7642. 'required' => 'required'
  7643. ),
  7644. '/div'
  7645. );
  7646. $this->assertTags($result, $expected);
  7647. }
  7648. /**
  7649. * Test inputDefaults setter and getter
  7650. *
  7651. * @return void
  7652. */
  7653. public function testInputDefaults() {
  7654. $this->Form->create('Contact');
  7655. $this->Form->inputDefaults(array(
  7656. 'label' => false,
  7657. 'div' => array(
  7658. 'style' => 'color: #000;'
  7659. )
  7660. ));
  7661. $result = $this->Form->input('Contact.field1');
  7662. $expected = array(
  7663. 'div' => array('class' => 'input text', 'style' => 'color: #000;'),
  7664. 'input' => array(
  7665. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7666. 'id' => 'ContactField1'
  7667. ),
  7668. '/div'
  7669. );
  7670. $this->assertTags($result, $expected);
  7671. $this->Form->inputDefaults(array(
  7672. 'div' => false,
  7673. 'label' => 'Label',
  7674. ));
  7675. $result = $this->Form->input('Contact.field1');
  7676. $expected = array(
  7677. 'label' => array('for' => 'ContactField1'),
  7678. 'Label',
  7679. '/label',
  7680. 'input' => array(
  7681. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7682. 'id' => 'ContactField1'
  7683. ),
  7684. );
  7685. $this->assertTags($result, $expected);
  7686. $this->Form->inputDefaults(array(
  7687. 'label' => false,
  7688. ), true);
  7689. $result = $this->Form->input('Contact.field1');
  7690. $expected = array(
  7691. 'input' => array(
  7692. 'type' => 'text', 'name' => 'data[Contact][field1]',
  7693. 'id' => 'ContactField1'
  7694. ),
  7695. );
  7696. $this->assertTags($result, $expected);
  7697. $result = $this->Form->inputDefaults();
  7698. $expected = array(
  7699. 'div' => false,
  7700. 'label' => false,
  7701. );
  7702. $this->assertEqual($result, $expected);
  7703. }
  7704. }